0
0
Swiftprogramming~10 mins

Range operators (... and ..<) 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 a closed range from 1 to 5.

Swift
let range = 1 [1] 5
print(Array(range))
Drag options to blanks, or click blank then click option'
A..<
B...
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using ..< excludes the last number.
Using - or + is not valid for ranges.
2fill in blank
medium

Complete the code to create a half-open range from 1 up to but not including 5.

Swift
let range = 1 [1] 5
print(Array(range))
Drag options to blanks, or click blank then click option'
A..<
B...
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using ... includes the last number.
Using + or - is not valid for ranges.
3fill in blank
hard

Fix the error in the code to print numbers from 1 to 4 using a half-open range.

Swift
for i in 1 [1] 5 {
    print(i)
}
Drag options to blanks, or click blank then click option'
A...
B+
C..<
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using ... includes 5, which is not desired here.
Using + or - is invalid syntax.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths for words longer than 3 characters.

Swift
let words = ["apple", "bat", "carrot", "dog"]
let lengths = Dictionary(uniqueKeysWithValues: words.filter { word in [2] }.map { word in (word, [1]) } )
Drag options to blanks, or click blank then click option'
Aword.count
Bword.count > 3
Cword.count < 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of word.count for length.
Filtering words with length less than 3.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words and their lengths for words longer than 3 characters.

Swift
let words = ["apple", "bat", "carrot", "dog"]
let lengths = Dictionary(uniqueKeysWithValues: words.filter { word in [3] }.map { word in ([1], [2]) } )
Drag options to blanks, or click blank then click option'
Aword.uppercased()
Bword.count
Cword.count > 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of word.uppercased() for keys.
Not filtering words by length.