Skip to content

Catchphrase

Raymond Chen edited this page Aug 1, 2024 · 5 revisions

TIP102 Unit 1 Session 1 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 5 mins
  • 🛠️ Topics: Functions, Strings, Conditionals

1: U-nderstand

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 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
Input: "Pooh"
Expected Output: Oh bother!

Input: "Tigger"
Expected Output: TTFN: Ta-ta for now!

EDGE CASE
Input: "Piglet"
Expected Output: Sorry! I don't know Piglet's catchphrase!

Input: "" (empty string)
Expected Output: Sorry! I don't know 's catchphrase!

2: M-atch

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.

This problem falls under: Function Definition and Conditionals.

3: 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. Define the function `print_catchphrase(character)`.
2. Use conditional statements to check the value of `character`.
3. Print the corresponding catchphrase if the character matches one from the table.
4. If the character does not match any in the table, print a default message.

⚠️ Common Mistakes

  • Incorrectly formatting the strings (ensure they match exactly).
  • Forgetting to handle characters not listed in the table.

4: I-mplement

Implement the code to solve the algorithm.

def print_catchphrase(character):
    if character == "Pooh":
        print("Oh bother!")
    elif character == "Tigger":
        print("TTFN: Ta-ta for now!")
    elif character == "Eeyore":
        print("Thanks for noticing me.")
    elif character == "Christopher Robin":
        print("Silly old bear.")
    else:
        print(f"Sorry! I don't know {character}'s catchphrase!")

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

Call the function with the provided examples:

print_catchphrase("Pooh")
print_catchphrase("Tigger")
print_catchphrase("Eeyore")
print_catchphrase("Christopher Robin")
print_catchphrase("Piglet")
print_catchphrase("")

Expected outputs:

Oh bother!
TTFN: Ta-ta for now!
Thanks for noticing me.
Silly old bear.
Sorry! I don't know Piglet's catchphrase!
Sorry! I don't know 's catchphrase!

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

  • Time Complexity: O(1) since it only involves a fixed number of conditionals.
  • Space Complexity: O(1) as no additional data structures are used.
Clone this wiki locally