Skip to content

Commit

Permalink
Update Checker.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Chin-may02 authored Oct 24, 2024
1 parent ef79537 commit 4341615
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Algorithms_and_Data_Structures/TimeComplexity_analyzer/Checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@
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)

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"
Expand Down

0 comments on commit 4341615

Please sign in to comment.