Problem overview
The function burrrrp receives a whole number n from 1 to 20 and returns the sound of a burp with n letters r: the text "bu", then n times the letter r, then "p". The result is all lower case.
With n = 4 the result is "burrrrp". With n = 1 it is "burp", the shortest burp. With n = 7 it is "burrrrrrrp".
Approach
Only one part of the answer changes with n: the run of r letters. The start, "bu", and the end, "p", are always the same. So the answer is three pieces joined together, and the middle one is the letter "r" repeated n times.
You could build the middle with a loop that appends one r per turn. The standard library does that in one call: strings.Repeat(text, n) returns text written n times in a row. For strings.Repeat("ab", 3) the result is "ababab".
This pattern, fixed text around a part that depends on an input, comes up often. Keeping the three pieces separate in the code makes it easy to see which part is which, and to change one without touching the others.
Step by step
- Start with the fixed beginning, the literal
"bu". - Add the r's with
strings.Repeat("r", n). - Add the fixed ending, the literal
"p". - Return the three pieces joined with
+.
The solution in Go
func burrrrp(n int) string {
return "bu" + strings.Repeat("r", n) + "p"
}
Running it on the three examples gives these results:
n = 4 -> "burrrrp"
n = 1 -> "burp"
n = 7 -> "burrrrrrrp"
Details to keep in mind:
- Double quotes make strings.
"r"is a string, which is whatstrings.Repeatexpects as its first argument. A single-quoted'r'is a rune, a number, and the call would not compile. +joins strings. The expression"bu" + strings.Repeat("r", n) + "p"builds a new string out of three; none of the pieces is changed.- The
stringspackage. In a file of your own, addimport "strings"at the top. In the lesson's editor, the import is already there. - The count must not be negative.
strings.Repeat("r", -1)stops the program with "strings: negative Repeat count". Withnbetween 1 and 20 that can't happen in this lesson.
The same thing with a loop
It helps to see what strings.Repeat saves you. A loop that adds one r per turn gives the same results:
func burrrrpLoop(n int) string {
result := "bu"
for i := 0; i < n; i++ {
result += "r"
}
return result + "p"
}
This version also returns "burrrrp" for 4 and "burp" for 1. It is longer, and there is more room for a slip in the loop condition, so the lesson uses strings.Repeat. For the largest input, n = 20, the result is 23 characters long: 20 r's plus b, u and p.
Edge cases and common mistakes
- Off by one.
ncounts the lettersr, not the whole word. Forn = 4the word has 4 r's, and its length is 7, countingb,uandp. - Repeating the whole word.
strings.Repeat("bu", n)or repeating"burp"would also repeat the beginning and the ending. - The smallest burp. With
n = 1the middle is a singler, giving"burp". Nothing needs a special case. - Upper case. The result is all lower case, so write the literals in lower case and don't call a case function.
Testing
Try the shortest burp and two longer ones:
n = 4gives"burrrrp".n = 1gives"burp".n = 7gives"burrrrrrrp".
