Start learning free

LESSON 73 OF 292 · STRINGS - CHALLENGES

Burrrrp in Swift: String(repeating:count:)

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

    SWIFT · STRINGS - CHALLENGES

    Burrrrp in Swift, explained

    Problem overview

    The function burrrrp receives a number n from 1 to 20 and returns the sound of a burp with n letters r: "bu", then n letters r, then "p". The result is all lowercase.

    For n = 4 the answer is "burrrrp", the burp in the lesson's title. For n = 1 it is "burp", the shortest burp. For n = 7 it is "burrrrrrrp".

    Approach: fixed ends, a repeating middle

    Look at the three answers. They all start with bu and end with p, and only the number of r letters between them changes. So the answer has three pieces: a fixed start, a middle that repeats n times, and a fixed end. Swift's String(repeating:count:) builds the middle: String(repeating: "r", count: 3) is "rrr".

    Step by step

    1. Write the fixed start as the string "bu".
    2. Build the middle with String(repeating: "r", count: n).
    3. Write the fixed end as "p".
    4. Join the three pieces with + and return the result.

    The solution in Swift

    class Solution {
        func burrrrp(_ n: Int) -> String {
            return "bu" + String(repeating: "r", count: n) + "p"
        }
    }
    

    Details worth knowing:

    • The labels are part of the call. You write repeating: and count: every time. Leaving them out is a compile error.
    • The first argument can be a string of any length. String(repeating: "ab", count: 3) is "ababab". Here the piece is a single letter.
    • The argument of the function. The parameter is declared as _ n: Int, with the underscore, so a caller writes burrrrp(4) without a label.
    • A count of zero is allowed. String(repeating: "r", count: 0) is an empty string, so the same code with n = 0 would return "bup". The lesson's range starts at 1, but the code does not break outside it on the low side. A negative count, however, stops the program at run time.
    • Why not a loop. A for loop that appends "r" to a var would also work, but the initializer is shorter and the result needs no mutable variable.

    The same result with a loop

    Before learning the initializer, you could build the middle by hand. Start with an empty var string and append "r" once per turn of a for loop:

    let n = 3
    var rs = ""
    for _ in 0..<n {
        rs += "r"
    }
    print("bu" + rs + "p") // burrrp
    

    This prints burrrp. The underscore in for _ in 0..<n says the loop counter isn't used. The loop does the same work as String(repeating: "r", count: n) in more lines and with a variable that can change; when a single call says what you want, prefer it.

    A quick length check

    A useful way to test the result without reading every letter is to count characters. The fixed pieces "bu" and "p" add 3 characters, and the middle adds n, so the answer always has n + 3 characters. For n = 4 that is 7, the length of "burrrrp", and for n = 20 it is 23. If your result is one character too long or too short, look for a stray space or a missing letter in the fixed pieces.

    Edge cases and common mistakes

    • The shortest burp. n = 1 gives "burp": bu, one r and p.
    • The longest burp. n = 20 gives bu, twenty letters r, then p.
    • Counting the wrong letters. The count is for the r letters only. The name burrrrp has four of them in the first example, so n = 4 is right, not 5.
    • Repeating the whole word. String(repeating: "burp", count: n) repeats everything instead of just the r.
    • Capital letters. The answer is lowercase everywhere.

    Testing

    • n = 4 gives "burrrrp".
    • n = 1 gives "burp".
    • n = 7 gives "burrrrrrrp".
    • n = 2 gives "burrp".
    • n = 20 gives "burrrrrrrrrrrrrrrrrrrrp".
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free