TIP102 Unit 7 Session 1 Advanced (Click for link to problem statements)
Given a string expression
representing arbitrarily nested ternary expressions, evaluate the expression, and return its result as a string.
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
condition ? true_choice : false_choice
, and they group right-to-left.HAPPY CASE
Input: "T?2:3"
Output: "2"
Explanation: If True, then result is 2; otherwise result is 3.
Input: "F?1:T?4:5"
Output: "4"
Explanation: The conditional expressions group right-to-left, and the result is "4".
Input: "T?T?F:5:3"
Output: "F"
Explanation: The conditional expressions group right-to-left, and the result is "F".
EDGE CASE
Input: "F?9:T?F:0"
Output: "0"
Explanation: The conditional expressions group right-to-left, and the result is "0".
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Evaluating Ternary Expressions, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Recursive Approach:
1) Define a helper function that takes an index `i` and returns the evaluated result of the expression starting from that index, along with the updated index.
2) If the current character at index `i` is not `T`, `F`, or `?`, it must be a digit or a boolean result; return it.
3) If the current character is a condition (either `T` or `F`), skip the next character (`?`) and recursively evaluate the true expression.
4) Skip the `:` character and recursively evaluate the false expression.
5) Return the result based on whether the condition is `T` or `F`.
6) Start the recursive evaluation from the first character of the expression.
⚠️ Common Mistakes
?
and :
characters correctly, leading to incorrect parsing.Implement the code to solve the algorithm.
def evaluate_ternary_expression_recursive(expression):
def helper(i):
# Base case: return a digit or boolean value if it's just that
if i >= len(expression) or expression[i] not in 'TF?':
return expression[i], i
# Current character should be a condition (either 'T' or 'F')
condition = expression[i]
i += 2 # Skip over '?' after the condition
# Recursively evaluate the true_expression
true_result, i = helper(i)
# Skip over the ':' separating true and false expressions
i += 1
# Recursively evaluate the false_expression
false_result, i = helper(i)
# Return the appropriate result based on the condition
if condition == 'T':
return true_result, i
else:
return false_result, i
# Start the recursive evaluation from the first character
result, _ = helper(0)
return result
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
evaluate_ternary_expression_recursive
function with the input "T?2:3"
. The function should return "2"
after evaluating the ternary expression."F?1:T?4:5"
. The function should return "4"
after correctly grouping and evaluating the expression right-to-left.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N)
, where N
is the length of the string. The function processes each character exactly once.O(N)
, due to the recursion stack. The depth of recursion is proportional to the nesting level of the ternary expressions.