-
Notifications
You must be signed in to change notification settings - Fork 247
Bouncy, Flouncy, Trouncy, Pouncy
JCSU Unit 4 Problem Set 1 (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10 mins
- 🛠️ Topics: Iteration, Conditionals, String Matching
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?
- What is the goal of the problem?
- Compute the final value of
tigger
after applying a sequence of operations.
- Compute the final value of
- Are there constraints on input?
- Operations are always one of the four valid strings: "bouncy", "flouncy", "trouncy", or "pouncy".
HAPPY CASE Input: operations = ["trouncy", "flouncy", "flouncy"] Output: 2 Explanation: Start with tigger = 1. Subtract 1 for "trouncy", add 1 for each "flouncy" twice. Final value = 2.
EDGE CASE Input: operations = [] Output: 1 Explanation: With no operations, the initial value of tigger remains unchanged.
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 sequential update problems, we want to consider the following approaches:
- Iteration and Conditionals: Loop through each operation and use conditionals to update the variable.
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Iterate through the list of operations. If the operation is "bouncy" or "flouncy", increment tigger
. If the operation is "trouncy" or "pouncy", decrement tigger
.
- Initialize the variable
tigger
to 1. - Iterate through the list of
operations
:- If the operation is "bouncy" or "flouncy", increment
tigger
by 1. - If the operation is "trouncy" or "pouncy", decrement
tigger
by 1.
- If the operation is "bouncy" or "flouncy", increment
- Return the final value of
tigger
.
Implement the code to solve the algorithm.
def bouncy_flouncy_trouncy_pouncy(operations):
tigger = 1 # Initialize tigger with a value of 1
for operation in operations: # Iterate through the list of operations
if operation in ["bouncy", "flouncy"]: # Both increment tigger
tigger += 1
elif operation in ["trouncy", "pouncy"]: # Both decrement tigger
tigger -= 1
return tigger # Return the final value of tigger
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
- Input: operations = ["trouncy", "flouncy", "flouncy"]
- Expected Output: 2
- Observed Output: 2
Example 2:
- Input: operations = ["bouncy", "bouncy", "flouncy"]
- Expected Output: 4
- Observed Output: 4
Example 3:
- Input: operations = []
- Expected Output: 1
- Observed Output: 1
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the length of the operations list.
- Time Complexity: O(n) because we iterate through the list once.
-
Space Complexity: O(1) because we use a single variable to store the value of
tigger
.