Problem overview
The method burrrrp receives a number n and returns the sound of a burp with n letters r: "bu", then n times r, then "p". With n = 4 it returns "burrrrp", with n = 1 it returns "burp", and with n = 7 it returns "burrrrrrrp". The lesson keeps n between 1 and 20, and the result is all lowercase.
Approach
Only one part of the answer changes with the input: the run of r letters. The start, "bu", and the end, "p", are always the same. So the method is three pieces joined together, with the middle piece made by repeating the string "r" n times.
In Scala, a String times an Int repeats the string: "r" * 4 is "rrrr". That means no loop is needed.
This is a useful habit for any string-building task: write down the result for two or three inputs, circle what stays the same, and see which part depends on the input. Here the answers for 1 and 4 differ only in the number of r letters, so the fixed parts become literals and the changing part becomes a repetition driven by n.
Step by step
- Start with the fixed text
"bu". - Add the repeated letters with
"r" * n. - Add the ending
"p", and return the joined string.
The solution in Scala
class Solution {
def burrrrp(n: Int): String = {
return "bu" + "r" * n + "p"
}
}
A few Scala details:
- Operator order.
*binds tighter than+, so"r" * nis computed first and then joined between"bu"and"p". No parentheses are needed. If you want to be explicit, you can write"bu" + ("r" * n) + "p", which means the same. return. The lesson writesreturnexplicitly. Scala also lets a method end with the expression itself, so"bu" + "r" * n + "p"alone, as the last line, returns the same string.- The type of
n.nis anInt, which is what*on a string expects. A negative count would give an empty repetition, but the lesson never passes one. - Double quotes.
"r"is aString. A single-quoted'r'is aChar, and'r' * nis a number: the code ofr(114) timesn.
Tracing an example
For n = 4, "r" * 4 is "rrrr". The pieces "bu", "rrrr" and "p" are joined into "burrrrp". For n = 1, "r" * 1 is just "r", and the result is "burp", the shortest burp. For n = 7, the result is "burrrrrrrp".
Compared with a loop
The same string can be built with a loop that adds one "r" at a time:
var middle = ""
for (i <- 1 to n) middle = middle + "r"
"bu" + middle + "p"
For n = 4 this gives "burrrrp" too, and for n = 20 the result has 23 characters. It needs a var, a loop and the range 1 to n, which includes both ends. The * version says the same thing in one expression, and there is no counter to get wrong.
Edge cases and common mistakes
- Counting the other letters. The number
ncounts only therletters. The total length of the result isn + 3. - Repeating the whole word.
("bu" + "r" + "p") * nrepeats the entire burp, giving"burpburpburpburp"forn = 4. - Using a
Char. ACharrepeated with*becomes a number, not text, so the method would not return a string. - Zero repetitions.
"r" * 0is the empty string, son = 0would give"bup". The lesson's constraints start at 1.
Testing
4gives"burrrrp".1gives"burp".7gives"burrrrrrrp".20, the largest value, gives"burrrrrrrrrrrrrrrrrrrrp", which is 23 characters long.
