forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec65669
commit d7b69bb
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}") |
19 changes: 19 additions & 0 deletions
19
Algorithms_and_Data_Structures/TimeComplexity_analyzer/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |