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
- Write the fixed start as the string
"bu". - Build the middle with
String(repeating: "r", count: n). - Write the fixed end as
"p". - 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:andcount: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 writesburrrrp(4)without a label. - A count of zero is allowed.
String(repeating: "r", count: 0)is an empty string, so the same code withn = 0would 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
forloop that appends"r"to avarwould 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 = 1gives"burp":bu, onerandp. - The longest burp.
n = 20givesbu, twenty lettersr, thenp. - Counting the wrong letters. The count is for the
rletters only. The nameburrrrphas four of them in the first example, son = 4is 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 = 4gives"burrrrp".n = 1gives"burp".n = 7gives"burrrrrrrp".n = 2gives"burrp".n = 20gives"burrrrrrrrrrrrrrrrrrrrp".
