Problem overview
The method burrrrp receives a number n from 1 to 20 and returns the sound of a burp with n letters r: "bu", then n times r, then "p", all lower case.
For n = 4 the result is "burrrrp". For n = 1 it is "burp", the shortest burp. For n = 7 it is "burrrrrrrp".
Approach
Only the middle part changes with n. The start, "bu", and the end, "p", are always the same. The middle is one letter repeated, and in Python "r" * n repeats a string n times. Join the three pieces with +.
You can think of the answer as a template with one blank. The template is bu, a blank, and p, and the blank is filled with exactly n letters r. Python has no special function for this. The repetition operator already does the job, and it works for any non-negative whole number.
The * operator means repetition when one side is a string and ordinary multiplication when both sides are numbers, so "r" * 4 is "rrrr" while 2 * 4 is 8. Python decides from the types of the two operands.
Step by step
- Start with the fixed beginning,
"bu". - Add the repeated letters:
"r" * n. Multiplication binds tighter than+, so it is done before the pieces are joined. - Add the fixed ending,
"p", and return the result.
The solution in Python
class Solution:
def burrrrp(self, n: int) -> str:
return "bu" + "r" * n + "p"
Some details worth knowing:
- Operator precedence matters here.
"bu" + "r" * n + "p"means"bu" + ("r" * n) + "p". If you wrote("bu" + "r") * n + "p", you would repeat"bur"instead. - Each
+creates a new string, because Python strings are immutable. For three small pieces this is not a concern. - The method is named
burrrrp, exactly as in the editor, with four r's.
The burp grows with n
Only the number of r's changes as n grows:
| n | result |
|---|---|
| 1 | "burp" |
| 2 | "burrp" |
| 3 | "burrrp" |
| 4 | "burrrrp" |
| 5 | "burrrrrp" |
The length of the result is always n + 3: the three fixed characters b, u and p plus n letters r.
The same idea with a named variable
You can build the middle part first and then wrap it. With n = 7 the code below prints rrrrrrr, then burrrrrrrp, then 10 10:
n = 7
r = "r" * n
print(r)
print("bu" + r + "p")
print(len("bu" + r + "p"), n + 3)
The last line compares the length of the result with n + 3, and they agree. A length check like that is a quick way to catch a missing or extra letter when your output looks almost right.
Since the string is built by joining, the order of the pieces decides the order of the letters. Putting "p" first would give a very different sound, so read the title and the examples to see which piece comes where: bu first, the r's in the middle, p last.
Edge cases and common mistakes
- The smallest burp.
n = 1gives"burp", with a single r. - Counting the r in "bu" or "p". Neither contains an r. The letters between them come only from the repetition.
- Passing a string for
n."r" * "4"raises aTypeError. The number must be anint. - Parenthesising the wrong part, as in
("bu" + "r") * n + "p", which forn = 4gives"burburburburp".
Testing
n = 4gives"burrrrp".n = 1gives"burp".n = 7gives"burrrrrrrp".n = 20gives"burrrrrrrrrrrrrrrrrrrrp".
