From d7b69bbe62092c57e17cdac54337beb955ffb406 Mon Sep 17 00:00:00 2001 From: Vuppu Chinmay Date: Thu, 24 Oct 2024 18:29:14 +0530 Subject: [PATCH 1/3] Add files via upload --- .../TimeComplexity_analyzer/Checker.py | 53 +++++++++++++++++++ .../TimeComplexity_analyzer/README.md | 19 +++++++ 2 files changed, 72 insertions(+) create mode 100644 Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py create mode 100644 Algorithms_and_Data_Structures/TimeComplexity_analyzer/README.md 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..7014c989 --- /dev/null +++ b/Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py @@ -0,0 +1,53 @@ +import ast + +class TimeComplexityAnalyzer(ast.NodeVisitor): + def __init__(self): + self.complexity = 0 + + def visit_For(self, node): + self.complexity += 1 + self.generic_visit(node) + + def visit_While(self, node): + self.complexity += 1 + self.generic_visit(node) + + def visit_FunctionDef(self, node): + self.generic_visit(node) + + def visit_Call(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 From ef79537d429670850c9ab3e005faf8144e448047 Mon Sep 17 00:00:00 2001 From: Chin-may02 Date: Thu, 24 Oct 2024 12:59:58 +0000 Subject: [PATCH 2/3] updating Project-Structure.md --- Project-Structure.md | 2 ++ 1 file changed, 2 insertions(+) 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) From 4341615ecc8b17def31c4a5109af1c4d811f54c5 Mon Sep 17 00:00:00 2001 From: Vuppu Chinmay Date: Thu, 24 Oct 2024 18:31:15 +0530 Subject: [PATCH 3/3] Update Checker.py --- .../TimeComplexity_analyzer/Checker.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py b/Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py index 7014c989..f0cc4df8 100644 --- a/Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py +++ b/Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py @@ -3,14 +3,19 @@ class TimeComplexityAnalyzer(ast.NodeVisitor): def __init__(self): self.complexity = 0 + self.loop_depth = 0 def visit_For(self, node): - self.complexity += 1 + self.loop_depth += 1 + self.complexity += 2 ** self.loop_depth self.generic_visit(node) + self.loop_depth -= 1 def visit_While(self, node): - self.complexity += 1 + 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) @@ -18,6 +23,9 @@ def visit_FunctionDef(self, 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"