Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to combine two arrays using zip and print the pairs.
Swift
let numbers = [1, 2, 3] let words = ["one", "two", "three"] for pair in [1](numbers, words) { print(pair) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'combine' or 'merge' which are not Swift functions for this purpose.
✗ Incorrect
The zip function combines two sequences into pairs.
2fill in blank
mediumComplete the code to print the first elements of each pair from zipped arrays.
Swift
let a = [10, 20, 30] let b = ["A", "B", "C"] for ([1], _) in zip(a, b) { print([1]) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'first' which is a property, not a variable name here.
✗ Incorrect
The variable 'x' is used to capture the first element in the tuple.
3fill in blank
hardFix the error in the code to correctly zip and print pairs.
Swift
let nums = [1, 2, 3] let strs = ["one", "two", "three"] for pair in zip(nums, [1]) { print(pair) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'strings' or 'strs()' which are undefined or incorrect syntax.
✗ Incorrect
The variable 'strs' holds the second array to zip with 'nums'.
4fill in blank
hardFill both blanks to create a dictionary from zipped arrays where keys are numbers and values are words.
Swift
let keys = [1, 2, 3] let values = ["one", "two", "three"] let dict = Dictionary(uniqueKeysWithValues: [1](keys, [2]))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'enumerated' which is not for pairing two sequences.
✗ Incorrect
Use zip to pair keys and values arrays to create the dictionary.
5fill in blank
hardFill all three blanks to filter zipped pairs where the number is greater than 1 and print the word.
Swift
let nums = [1, 2, 3] let words = ["one", "two", "three"] for ([1], [2]) in zip(nums, words) where [1] > 1 { print([2]) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names or wrong variables in the condition.
✗ Incorrect
Use 'num' and 'word' as variable names; filter where num > 1 and print word.