Complete the code to create a new array with each number doubled using map.
let numbers = [1, 2, 3, 4] let doubled = numbers.[1] { $0 * 2 }
The map function applies the given closure to each element and returns a new array with the results.
Complete the code to filter out numbers less than 3.
let numbers = [1, 2, 3, 4, 5] let filtered = numbers.[1] { $0 >= 3 }
The filter function returns a new array containing only elements that satisfy the condition.
Fix the error in the code to sum all numbers using reduce.
let numbers = [1, 2, 3, 4] let sum = numbers.reduce([1]) { $0 + $1 }
The initial value for summing numbers should be 0, so the sum starts from zero.
Fill both blanks to create a dictionary with words as keys and their lengths as values, filtering words longer than 3 characters.
let words = ["apple", "bat", "car", "door"] let lengths = Dictionary(uniqueKeysWithValues: words.filter { $0.count [1] 3 }.map { [2] in ([2], [2].count) })
We filter words with length greater than 3, then map each word to a tuple of (word, length).
Fill all three blanks to create a dictionary with uppercase words as keys, their counts as values, filtering words with count greater than 1.
let words = ["apple", "bat", "apple", "car", "bat", "bat"] let counts = words.reduce(into: [:]) { counts, [1] in counts[[2]] = (counts[[3]] ?? 0) + 1 } let filteredCounts = counts.filter { $0.value > 1 }
We count occurrences of uppercase words. The key in the dictionary is the uppercase word, so we use word.uppercased() for keys and lookups.