0
0
Swiftprogramming~10 mins

Set creation and operations 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 create an empty set of integers.

Swift
var numbers: Set<Int> = [1]
Drag options to blanks, or click blank then click option'
A[]
B{}
CArray()
DSet()
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] creates an empty array, not a set.
Using curly braces {} is invalid syntax for sets in Swift.
2fill in blank
medium

Complete the code to create a set with the elements 1, 2, and 3.

Swift
let digits: Set<Int> = [1]
Drag options to blanks, or click blank then click option'
ASet(1, 2, 3)
BSet([1, 2, 3])
C[1, 2, 3]
D(1, 2, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to pass multiple arguments directly to Set initializer.
Using array literal without Set initializer.
3fill in blank
hard

Fix the error in the code to check if two sets have any common elements.

Swift
let setA: Set<Int> = Set([1, 2, 3])
let setB: Set<Int> = Set([3, 4, 5])
let hasCommon = !setA.[1](setB).isEmpty
Drag options to blanks, or click blank then click option'
Aintersection
Bunion
CsymmetricDifference
Dsubtracting
Attempts:
3 left
💡 Hint
Common Mistakes
Using union returns all elements from both sets, not just common ones.
Using symmetricDifference returns elements in either set but not both.
4fill in blank
hard

Fill both blanks to create a set of even numbers from 1 to 10.

Swift
let numbers = Set(1...10)
let evenNumbers = numbers.filter { $0 [1] 2 [2] 0 }
Drag options to blanks, or click blank then click option'
A%
B==
C!=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes odd numbers to be selected.
Using '>' does not check for evenness.
5fill in blank
hard

Fill all three blanks to create a dictionary from a set of words with their lengths, but only include words longer than 3 characters.

Swift
let words: Set<String> = Set(["apple", "cat", "banana", "dog"])
let wordLengths = [[1]: [2] for [3] in words if [2] > 3]
Drag options to blanks, or click blank then click option'
Aword
Bword.count
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect variable names.
Not filtering words by length.