0
0
Swiftprogramming~10 mins

Zip for combining sequences in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Amerge
Bcombine
Czip
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'combine' or 'merge' which are not Swift functions for this purpose.
2fill in blank
medium

Complete 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'
Ax
Bfirst
Celement
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'first' which is a property, not a variable name here.
3fill in blank
hard

Fix 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'
Astrs
Bstrings
Cstr
Dstrs()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'strings' or 'strs()' which are undefined or incorrect syntax.
4fill in blank
hard

Fill 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'
Azip
Bvalues
Ckeys
Denumerated
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'enumerated' which is not for pairing two sequences.
5fill in blank
hard

Fill 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'
Anum
Bword
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names or wrong variables in the condition.