-
Notifications
You must be signed in to change notification settings - Fork 266
Bouncy Flouncy Trouncy Pouncy
Raymond Chen edited this page Aug 1, 2024
·
7 revisions
TIP102 Unit 1 Session 1 Advanced (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 5 mins
- 🛠️ Topics: Lists, Iteration, Conditionals
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?
- The function
final_value_after_operations()
should take a list of strings operations and return the final value of the variable tigger after performing all the operations. The initial value of tigger is 1.
HAPPY CASE
Input: operations = ["trouncy", "flouncy", "flouncy"]
Expected Output: 2
Explanation: The operations are performed as follows:
Initially, tigger = 1.
trouncy: tigger is decremented by 1, tigger = 0.
flouncy: tigger is incremented by 1, tigger = 1.
flouncy: tigger is incremented by 1, tigger = 2.
Input: operations = ["bouncy", "bouncy", "flouncy"]
Expected Output: 4
Explanation: The operations are performed as follows:
Initially, tigger = 1.
bouncy: tigger is incremented by 1, tigger = 2.
bouncy: tigger is incremented by 1, tigger = 3.
flouncy: tigger is incremented by 1, tigger = 4.
EDGE CASE
Input: operations = ["trouncy"]
Expected Output: 0
Explanation: The only operation decrements tigger to 0.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Initialize tigger to 1, then iterate through the list of operations to modify tigger based on the operation type.
1. Define the function `final_value_after_operations(operations)`.
2. Initialize the variable `tigger` to 1.
3. Iterate through the list of `operations`.
4. For each operation:
- If it is "bouncy" or "flouncy", increment `tigger` by 1.
- If it is "trouncy" or "pouncy", decrement `tigger` by 1.
5. Return the final value of `tigger`.
- Forgetting to account for all operation types.
- Not initializing tigger correctly.
Implement the code to solve the algorithm.
def final_value_after_operations(operations):
# Initialize tigger to 1
tigger = 1
# Iterate through the list of operations
for operation in operations:
if operation == "bouncy" or operation == "flouncy":
tigger += 1
elif operation == "trouncy" or operation == "pouncy":
tigger -= 1
# Return the final value of tigger
return tigger