Greeting - codepath/compsci_guides GitHub Wiki
TIP102 Unit 1 Session 1 Standard (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 5 mins
- 🛠️ Topics: Functions, Strings, Parameters
Understand what the interviewer is asking for by using test cases and questions about the problem.
-
Q: What should the function
greeting()
do?- A: The function should take a single string parameter
name
and print a specific greeting message incorporating the givenname
.
- A: The function should take a single string parameter
-
Q: What should the output look like?
- A: The output should be a single line of text:
"Welcome to The Hundred Acre Wood <name>! My name is Christopher Robin."
- A: The output should be a single line of text:
-
Q: Are there any constraints on the input?
- A: The input will always be a string, representing the name of the person being greeted.
-
The function
greeting()
should take a single parameter, name, and print a specific greeting message that includes the provided name.
HAPPY CASE
Input: "Michael"
Expected Output: Welcome to The Hundred Acre Wood Michael! My name is Christopher Robin.
Input: "Winnie the Pooh"
Expected Output: Welcome to The Hundred Acre Wood Winnie the Pooh! My name is Christopher Robin.
EDGE CASE
Input: " (empty string)
Expected Output: Welcome to The Hundred Acre Wood ! My name is Christopher Robin.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Define a function that takes a parameter and prints a formatted string using that parameter.
1. Define the function `greeting(name)`.
2. Use the `print()` function to display the string "Welcome to The Hundred Acre Wood {name}! My name is Christopher Robin." using the provided name.
- Incorrectly formatting the string (ensure it matches exactly).
- Forgetting to pass the parameter to the function.
Implement the code to solve the algorithm.
def greeting(name):
print(f"Welcome to The Hundred Acre Wood {name}! My name is Christopher Robin.")