Write code that counts how deeply nested a formula is. Here are the rules for wh
ID: 3706744 • Letter: W
Question
Write code that counts how deeply nested a formula is.
Here are the rules for what "depth of nesting" means: (1) A variable is a formula of depth 0. (2) If f is a formula of depth d, then NOT (f) is a formula of depth d+1. (3) If f1 is a formula of depth d1 and f2 is a formula of depth d2, then (f1) OR (f2) is a formula of depth max(d1, d2)+1. (4) If f1 is a formula of depth d1 and f2 is a formula of depth d2, then (f1) AND (f2) is a formula of depth max(d1, d2)+1.
1 def NOT(f): 2 return ["not", f] 3 def AND(f1, f2): 4 return [f1, "and", f2] 5 def OR(f1, f2): 6 return [f1, "or", f2] 7 # DO NOT CHANGE THE CODE ABOVE THIS LINE 9 # WRITE YOUR CODE BELOW. 10 def depth(f): # do not change this line 11.1 return 0 # this is a placeholder change it to actual codeExplanation / Answer
def depth(f): if type(f) is not list: return 0 else: max_depth = 0 for item in f: d = depth(item) if d > max_depth: max_depth = d return max_depth+1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.