0
0
Swiftprogramming~10 mins

Collection slicing and indices 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 get a slice of the array from index 1 to 3.

Swift
let numbers = [10, 20, 30, 40, 50]
let slice = numbers[[1]]
Drag options to blanks, or click blank then click option'
A1...3
B0...2
C3...5
D2...4
Attempts:
3 left
💡 Hint
Common Mistakes
Using a range that starts at 0 instead of 1.
Using an open range operator which excludes the last index.
2fill in blank
medium

Complete the code to get the first three elements of the array using prefix.

Swift
let fruits = ["apple", "banana", "cherry", "date"]
let firstThree = fruits.[1](3)
Drag options to blanks, or click blank then click option'
Asuffix
BdropLast
CdropFirst
Dprefix
Attempts:
3 left
💡 Hint
Common Mistakes
Using suffix instead of prefix.
Using dropFirst which removes elements instead of selecting.
3fill in blank
hard

Fix the error in the code to correctly get a slice from index 2 to the end.

Swift
let letters = ["a", "b", "c", "d", "e"]
let slice = letters[[1]...]
Drag options to blanks, or click blank then click option'
A2
B3
Cletters.startIndex
Dletters.endIndex
Attempts:
3 left
💡 Hint
Common Mistakes
Using endIndex which is one past the last valid index.
Using startIndex which gets the whole array.
4fill in blank
hard

Fill both blanks to create a slice of the array from index 1 up to but not including index 4.

Swift
let colors = ["red", "green", "blue", "yellow", "purple"]
let slice = colors[[1]..<[2]]
Drag options to blanks, or click blank then click option'
A1
B3
C4
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a closed range which includes index 4.
Using wrong start or end indices.
5fill in blank
hard

Fill all three blanks to create a dictionary with keys as uppercase letters and values as their index, only for letters with index greater than 1.

Swift
let letters = ["a", "b", "c", "d", "e"]
let dict = [[1]: [2] for (index, letter) in letters.enumerated() if index [3] 1]
Drag options to blanks, or click blank then click option'
Aletter.uppercased()
Bindex
C>
Dletter
Attempts:
3 left
💡 Hint
Common Mistakes
Using the letter itself instead of uppercase for keys.
Using wrong comparison operator in the condition.