Start learning free

LESSON 74 OF 285 · STRINGS - CHALLENGES

Burrrrp in Go: Building a String with strings.Repeat

Practice Read the full explanation

Given a number n, return the sound of a burp with n letters r: "bu", then n letters r, then "p".

 

Example 1:

Input: n = 4
Output: "burrrrp"
Explanation: "bu", then 4 letters r, then "p". That's the burp in this lesson's title.

Example 2:

Input: n = 1
Output: "burp"
Explanation: The shortest burp has a single r.

Example 3:

Input: n = 7
Output: "burrrrrrrp"
Explanation: "bu", then 7 letters r, then "p".

 

Constraints:

  • n is a whole number from 1 to 20.
  • The result is all lowercase.
STEP 1 OF 1
    Ctrl + Enter to check

    GO · STRINGS - CHALLENGES

    Burrrrp in Go, explained

    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

    1. Start with the fixed beginning, the literal "bu".
    2. Add the r's with strings.Repeat("r", n).
    3. Add the fixed ending, the literal "p".
    4. 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 what strings.Repeat expects 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 strings package. In a file of your own, add import "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". With n between 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. n counts the letters r, not the whole word. For n = 4 the word has 4 r's, and its length is 7, counting b, u and p.
    • Repeating the whole word. strings.Repeat("bu", n) or repeating "burp" would also repeat the beginning and the ending.
    • The smallest burp. With n = 1 the middle is a single r, 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 = 4 gives "burrrrp".
    • n = 1 gives "burp".
    • n = 7 gives "burrrrrrrp".
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free