MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / evaluate_recursive

Function evaluate_recursive

data_structures/stacks/prefix_evaluation.py:58–83  ·  view source on GitHub ↗

Alternative recursive implementation >>> evaluate_recursive(['2']) 2 >>> expression = ['+', '*', '2', '3', '/', '8', '4'] >>> evaluate_recursive(expression) 8.0 >>> expression [] >>> evaluate_recursive(['+', '9', '*', '2', '6']) 21 >>> evaluate_recursive

(expression: list[str])

Source from the content-addressed store, hash-verified

56
57
58def evaluate_recursive(expression: list[str]):
59 """
60 Alternative recursive implementation
61
62 >>> evaluate_recursive(['2'])
63 2
64 >>> expression = ['+', '*', '2', '3', '/', '8', '4']
65 >>> evaluate_recursive(expression)
66 8.0
67 >>> expression
68 []
69 >>> evaluate_recursive(['+', '9', '*', '2', '6'])
70 21
71 >>> evaluate_recursive(['/', '*', '10', '2', '+', '4', '1'])
72 4.0
73 """
74
75 op = expression.pop(0)
76 if is_operand(op):
77 return int(op)
78
79 operation = operators[op]
80
81 a = evaluate_recursive(expression)
82 b = evaluate_recursive(expression)
83 return operation(a, b)
84
85
86# Driver code

Callers

nothing calls this directly

Calls 2

is_operandFunction · 0.85
popMethod · 0.45

Tested by

no test coverage detected