TIP102 Unit 1 Session 1 Standard (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
x
is 0?
"batman!"
since no "na"
should be repeated.Q: How does the function handle the repetition of "na"
?
"na"
x
times and then appends " batman!"
to it.The function nanana_batman()
should take an integer x and print the string "nanana batman!" where "na" is repeated x times.
HAPPY CASE
Input: 6
Output: "nananananana batman!"
EDGE CASE
Input: 0
Output: "batman!"
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Define the function to iterate x times and build the string.
1. Define the function `nanana_batman(x)`.
2. Initialize an empty string `na_string` to accumulate the "na" strings.
3. Use a for loop to repeat "na" `x` times:
a. Concatenate "na" to `na_string`.
4. Concatenate " batman!" to `na_string`.
5. Print the final result.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def nanana_batman(x):
# Initialize an empty string to accumulate the "na"s
na_string = "
# Use a for loop to repeat "na" x times
for _ in range(x):
na_string += "na"
# Concatenate " batman!" to the repeated "na" string
result = na_string + " batman!"
# Print the result
print(result)