diff --git a/Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py b/Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py new file mode 100644 index 00000000..f0cc4df8 --- /dev/null +++ b/Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py @@ -0,0 +1,61 @@ +import ast + +class TimeComplexityAnalyzer(ast.NodeVisitor): + def __init__(self): + self.complexity = 0 + self.loop_depth = 0 + + def visit_For(self, node): + self.loop_depth += 1 + self.complexity += 2 ** self.loop_depth + self.generic_visit(node) + self.loop_depth -= 1 + + def visit_While(self, node): + self.loop_depth += 1 + self.complexity += 2 ** self.loop_depth + self.generic_visit(node) + self.loop_depth -= 1 + + def visit_FunctionDef(self, node): + self.generic_visit(node) + + def visit_Call(self, node): + self.generic_visit(node) + + def visit_If(self, node): + self.generic_visit(node) + + def get_complexity(self): + if self.complexity == 0: + return "O(1) - Constant Time" + elif self.complexity == 1: + return "O(log n) - Logarithmic Time" + elif self.complexity == 2: + return "O(n) - Linear Time" + elif self.complexity == 3: + return "O(n log n) - Linearithmic Time" + elif self.complexity == 4: + return "O(n^2) - Quadratic Time" + elif self.complexity == 5: + return "O(n^3) - Cubic Time" + elif self.complexity >= 6: + return f"O(n^{self.complexity}) - Polynomial Time" + return "O(2^n) - Exponential Time" + +def analyze_code(code): + try: + tree = ast.parse(code) + analyzer = TimeComplexityAnalyzer() + analyzer.visit(tree) + return analyzer.get_complexity() + except SyntaxError as e: + return f"Syntax Error: {e.msg} at line {e.lineno}, column {e.offset}" + except Exception as e: + return f"An unexpected error occurred: {str(e)}" + +if __name__ == "__main__": + print("Welcome to the Time Complexity Analyzer!") + user_code = input("Please enter a piece of Python code:\n") + complexity = analyze_code(user_code) + print(f"Estimated time complexity: {complexity}") diff --git a/Algorithms_and_Data_Structures/TimeComplexity_analyzer/README.md b/Algorithms_and_Data_Structures/TimeComplexity_analyzer/README.md new file mode 100644 index 00000000..f367ac31 --- /dev/null +++ b/Algorithms_and_Data_Structures/TimeComplexity_analyzer/README.md @@ -0,0 +1,19 @@ +# Time Complexity Analyzer + +## Overview +The Time Complexity Analyzer is a Python script designed to analyze the time complexity of user-provided Python code. By parsing the code and evaluating its structure, the program estimates the time complexity and provides a corresponding order of growth. This tool is particularly useful for developers and students looking to understand the efficiency of their algorithms. +## What Have I Done +In this project, I developed a program that leverages Python's Abstract Syntax Tree (AST) module to parse code input from the user. The program identifies loops and function definitions to estimate the time complexity based on common patterns. It provides clear feedback, including error handling for syntax errors, enhancing the user experience. + +## What the Program Does +- Accepts a piece of Python code as input from the user. +- Parses the code using the AST module. +- Analyzes the structure of the code to identify loops and function calls. +- Estimates the time complexity and provides an order of growth (e.g., O(1), O(n), O(n^2)). +- Outputs detailed error messages in case of syntax issues. + +## Libraries Used +- **ast**: A built-in Python library for parsing Python source code into its Abstract Syntax Tree representation. + +## Conclusion +The Time Complexity Analyzer provides a straightforward and user-friendly way to estimate the efficiency of Python code. With its ability to handle various types of growth patterns and robust error handling, it serves as a valuable tool for anyone looking to improve their understanding of algorithmic efficiency. Future enhancements could include support for more complex constructs and deeper semantic analysis of code. \ No newline at end of file diff --git a/Project-Structure.md b/Project-Structure.md index 037e2b23..9a82dde9 100644 --- a/Project-Structure.md +++ b/Project-Structure.md @@ -130,6 +130,8 @@ * [Prefixtoinfix](Algorithms_and_Data_Structures/Stack/PrefixToInfix.py) * [Prefixtopostfix](Algorithms_and_Data_Structures/Stack/PrefixToPostfix.py) * [Stack](Algorithms_and_Data_Structures/Stack/stack.py) + * Timecomplexity Analyzer + * [Checker](Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py) * Trees * [Menu Driven Code For Avl Tree](Algorithms_and_Data_Structures/Trees/Menu_Driven_Code_for_Avl_Tree.py) * [Menu Driven Code For Binary Search Tree](Algorithms_and_Data_Structures/Trees/Menu_Driven_Code_for_Binary_Search_Tree.py)