Complete the code to create a closed range from 1 to 5.
let range = 1 [1] 5 print(Array(range))
The ... operator creates a closed range including both start and end values.
Complete the code to create a half-open range from 1 up to but not including 5.
let range = 1 [1] 5 print(Array(range))
The ..< operator creates a half-open range that excludes the upper bound.
Fix the error in the code to print numbers from 1 to 4 using a half-open range.
for i in 1 [1] 5 { print(i) }
Using ..< creates a range from 1 up to but not including 5, so it prints 1 to 4.
Fill both blanks to create a dictionary with word lengths for words longer than 3 characters.
let words = ["apple", "bat", "carrot", "dog"] let lengths = Dictionary(uniqueKeysWithValues: words.filter { word in [2] }.map { word in (word, [1]) } )
We use word.count to get the length and filter words with length greater than 3.
Fill all three blanks to create a dictionary of uppercase words and their lengths for words longer than 3 characters.
let words = ["apple", "bat", "carrot", "dog"] let lengths = Dictionary(uniqueKeysWithValues: words.filter { word in [3] }.map { word in ([1], [2]) } )
The keys are uppercase words, values are their lengths, filtered by length greater than 3.