Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Chin-may02 authored Oct 24, 2024
1 parent ec65669 commit d7b69bb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py
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 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.

0 comments on commit d7b69bb

Please sign in to comment.