Vegetable Harvest - codepath/compsci_guides GitHub Wiki
TIP102 Unit 1 Session 1 Advanced (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10 mins
- 🛠️ Topics: 2D Arrays, Matrix traversal
U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
-
Q: What is the input to the function?
- A: The input is a 2D matrix
vegetable_patch
where each element represents a spot in the garden.
- A: The input is a 2D matrix
-
Q: What is the expected output of the function?
- A: The function should return an integer representing the total number of carrots (
'c'
) that are ready to harvest in the vegetable patch.
- A: The function should return an integer representing the total number of carrots (
-
Q: How are the carrots represented in the matrix?
- A: Carrots that are ready to harvest are represented by the character
'c'
.
- A: Carrots that are ready to harvest are represented by the character
-
Q: What should the function return if there are no carrots ready to harvest?
- A: The function should return
0
if there are no carrots ('c'
) in the matrix.
- A: The function should return
-
Q: Can the matrix be empty or have rows of varying lengths?
- A: The problem assumes the matrix is not empty and that all rows have the same number of columns (i.e., the matrix is well-formed).
-
The function
print_catchphrase()
should take a single parameter, character, and print the corresponding catchphrase based on the given character. If the character does not match any in the table, it should print a default message.
HAPPY CASE
Example 1:
Input:
[
['x', 'c', 'x'],
['x', 'x', 'x'],
['x', 'c', 'c'],
['c', 'c', 'c']
]
Output: 6
Explanation: vegetable_patch[0][1], vegetable_patch[2][1], vegetable_patch[2][2], vegetable_patch[3][0], vegetable_patch[3][1], and vegetable_patch[3][2] all have value 'c'
Example 2:
Input:
[
['x', 'x', 'x'],
['x', 'x', 'x'],
['x', 'x', 'x'],
['x', 'x', 'x']
]
Output: 0
Explanation: There are no 'c' in the vegetable patch.
EDGE CASE
If the matrix is empty, the function should return 0.
Example: []
P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the 2D matrix vegetable_patch
to count the number of carrots (represented by the character 'c'
). Use a nested loop to iterate over each row and each column in the matrix, checking each element for the presence of a carrot.
1) Initialize a counter `carrot_count` to keep track of the number of carrots.
2) Get the number of rows (`n`) in the matrix and the number of columns (`m`) in each row.
3) Iterate over each row in the matrix using a loop variable `row`.
4) For each row, iterate over each column using a loop variable `col`.
5) Check if the element at position `[row][col]` is `'c'`.
6) If it is, increment the `carrot_count`.
7) After all rows and columns have been checked, return the value of `carrot_count`.
⚠️ Common Mistakes
- Incorrectly indexing the matrix, leading to out-of-bounds errors.
- Forgetting to check all elements in the matrix, missing potential carrots.
- Confusing row and column indices, leading to incorrect checks.
⚠️ Common Mistakes
- Incorrectly formatting the strings (ensure they match exactly).
- Forgetting to handle characters not listed in the table.
I-mplement
Implement the code to solve the algorithm.
def harvest(vegetable_patch):
# Initialize the carrot counter
carrot_count = 0
# Get the number of rows (n) and columns (m)
n = len(vegetable_patch)
m = len(vegetable_patch[0])
# Traverse the 2D matrix
for row in range(n):
for col in range(m):
# Check if the current element is 'c'
if vegetable_patch[row][col] == 'c':
# Increment the carrot counter
carrot_count += 1
# Return the total number of carrots
return carrot_count