From 4341615ecc8b17def31c4a5109af1c4d811f54c5 Mon Sep 17 00:00:00 2001 From: Vuppu Chinmay Date: Thu, 24 Oct 2024 18:31:15 +0530 Subject: [PATCH] Update Checker.py --- .../TimeComplexity_analyzer/Checker.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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"