Skip to content

Vegetable Harvest

LeWiz24 edited this page Aug 20, 2024 · 6 revisions

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.
  • 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.
  • Q: How are the carrots represented in the matrix?

    • A: Carrots that are ready to harvest are represented by the character 'c'.
  • 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.
  • 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: Define a function that uses conditionals to match the input character to a predefined set of catchphrases, printing the appropriate message.

1. Initialize a counter for carrots.
2. Iterate over each row in the matrix.
3. Iterate over each element in the row.
4. Check if the element is 'c'.
5. Increment the counter if the element is 'c'.
6. Return the counter

⚠️ 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 i in range(n):
        for j in range(m):
            # Check if the current element is 'c'
            if vegetable_patch[i][j] == 'c':
                # Increment the carrot counter
                carrot_count += 1
    
    # Return the total number of carrots
    return carrot_count
Clone this wiki locally