Skip to content

NaNaNa Batman!

LeWiz24 edited this page Aug 27, 2024 · 3 revisions

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

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 5 mins
  • 🛠️ Topics: String Manipulation, For Loops, Print Statements

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: What should the function print if x is 0?

    • A: The function should print "batman!" since no "na" should be repeated.
  • Q: How does the function handle the repetition of "na"?

    • A: The function repeats the string "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!"

P-lan

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

  • Forgetting to print the final result or including an extra space.

I-mplement

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)
Clone this wiki locally