Set Character - codepath/compsci_guides GitHub Wiki

Unit 5 Session 1 (Click for link to problem statements)

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

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 15-20 mins
  • 🛠️ Topics: Classes, Object-Oriented Programming, Setter Methods, Data Validation

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?
  • What should the set_catchphrase method do?

    • Validate the input new_catchphrase and update the catchphrase attribute if valid, otherwise print an error message.
  • What constitutes a valid catchphrase?

    • It must be less than 20 characters long and contain only alphabetic characters and whitespace.
HAPPY CASE
Input: 
bones = Villager("Bones", "Dog", "Lazy", "yip yip")
bones.set_catchphrase("soar high")
print(bones.greet_player("Samia"))

Output: 
Catchphrase updated
"Bones: Hey there, Samia! How's it going, soar high?"

Explanation: 
The `catchphrase` attribute of `bones` is successfully updated to `"soar high"`, and the `greet_player` method returns the expected greeting.

EDGE CASE
Input: 
bones = Villager("Bones", "Dog", "Lazy", "yip yip")
bones.set_catchphrase("#?!")
print(bones.greet_player("Samia"))

Output: 
Invalid catchphrase
"Bones: Hey there, Samia! How's it going, yip yip?"

Explanation: 
The `catchphrase` attribute of `bones` is not updated because the input is invalid, and the `greet_player` method reflects the original 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.

For Object-Oriented Programming problems, we want to consider the following approaches:

  • Implement setter methods to encapsulate data validation.
  • Ensure the methods correctly reflect validated attribute updates.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Implement the set_catchphrase method to validate and update the catchphrase attribute.

1) Define the `set_catchphrase` method in the `Villager` class.
2) Implement validation to check if `new_catchphrase` is less than 20 characters long.
3) Check if all characters in `new_catchphrase` are alphabetic or whitespace.
4) If valid, update the `catchphrase` attribute and print "Catchphrase updated".
5) If invalid, print "Invalid catchphrase".

⚠️ Common Mistakes

  • Forgetting to validate all characters in the new_catchphrase.
  • Not checking the length of the new_catchphrase correctly.
  • Incorrectly referencing or updating the attribute.

4: I-mplement

Implement the code to solve the algorithm.

class Villager:
    def __init__(self, name, species, personality, catchphrase):
        self.name = name
        self.species = species
        self.personality = personality
        self.catchphrase = catchphrase
        self.furniture = []

    def set_catchphrase(self, new_catchphrase):
        if len(new_catchphrase) < 20:
            for c in new_catchphrase:
                if not (c.isalpha() or c.isspace()):
                    print("Invalid catchphrase")
                    return
            self.catchphrase = new_catchphrase
            print("Catchphrase updated")
        else:
            print("Invalid catchphrase")

5: R-eview

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

  • Instantiate the Villager object bones.
  • Validate different catchphrases and check the output messages.
  • Ensure the greet_player method reflects the updated catchphrase if valid.

6: E-valuate

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

Assume N represents the number of characters in the new_catchphrase.

  • Time Complexity: O(N) because we need to check each character in the new_catchphrase.
  • Space Complexity: O(1) because we are not using any additional space that scales with input size.