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"