Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a time complexity checker #829

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py
Original file line number Diff line number Diff line change
@@ -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}")
19 changes: 19 additions & 0 deletions Algorithms_and_Data_Structures/TimeComplexity_analyzer/README.md
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.
2 changes: 2 additions & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading