diff --git a/MoreSwiftBasics.playground/Pages/Arrays.xcplaygroundpage/Contents.swift b/MoreSwiftBasics.playground/Pages/Arrays.xcplaygroundpage/Contents.swift index c398900..af7ab62 100644 --- a/MoreSwiftBasics.playground/Pages/Arrays.xcplaygroundpage/Contents.swift +++ b/MoreSwiftBasics.playground/Pages/Arrays.xcplaygroundpage/Contents.swift @@ -125,24 +125,33 @@ var animalArray: [String] = ["Lion", "Zebra", "Elephant", "Turtle"] var optionalArray: [Int]? // [2] Is there a logical error with accessing index `4` in a 4-element array? -print("4th element of animalArray: \(animalArray[4])") +print("4th element of animalArray: \(animalArray[3])") // [3] The operation below is quite dangerous. How can we make it safer? -let unwrappedArray = optionalArray! -print(unwrappedArray) +if let unwrappedArray = optionalArray { + print(unwrappedArray) +} else { + print("optionalArray is nil") +} // [4] Initialize optionalArray /* BEGIN CODE */ - +optionalArray = [] /* END CODE */ // [5, 6, 7] Add elements to optionalArray /* BEGIN CODE */ - +optionalArray?.append(10) +optionalArray?.append(contentsOf: [6, 7, 8, 9]) +optionalArray?.insert(55, at: 2) /* END CODE */ // [8] Print the 5th element of optionalArray /* BEGIN CODE */ - +if let optionalArray = optionalArray, optionalArray.count >= 5 { + print("5th element of optionalArray: \(optionalArray[4])") +} else { + print("optionalArray doesn't have a 5th element") +} /* END CODE */ diff --git a/MoreSwiftBasics.playground/Pages/FunctionsClosures.xcplaygroundpage/Contents.swift b/MoreSwiftBasics.playground/Pages/FunctionsClosures.xcplaygroundpage/Contents.swift index ef9424a..e10f057 100644 --- a/MoreSwiftBasics.playground/Pages/FunctionsClosures.xcplaygroundpage/Contents.swift +++ b/MoreSwiftBasics.playground/Pages/FunctionsClosures.xcplaygroundpage/Contents.swift @@ -242,7 +242,7 @@ print("Alphabetical: \(cities)\n") // [1] Use the .sort(by:) function to sort the array from shortest name to // longest. Check the docs for reference material if needed /* BEGIN CODE */ - +cities.sort(by: { $0.count < $1.count }) /* END CODE */ print("Length (.sort): \(cities)") @@ -264,10 +264,21 @@ for i in 0 to (arr_length - 1): */ func selectionSort(_ input: [String]) -> [String] { var arr: [String] = input - /* BEGIN CODE */ - - /* END CODE */ + for i in 0.. = ["Zebra"] +if !mySet.contains("Zebra") { + print("--> FAILURE: mySet does not contain Zebra") +} else if !mySet.isSubset(of: animalSet) { + print("--> FAILURE: mySet is not a subset of animalSet") +} else { + print("SUCCESS: mySet contains Zebra and is a subset of animalSet") +} /* END CODE */ - +var optionalSet: Set? // [3] Insert "Giraffe" into optionalSet. /* BEGIN CODE */ - +optionalSet?.insert("Giraffe") /* END CODE */ // [4] Check if optionalSet contains "Giraffe" /* BEGIN CODE */ - +if let set = optionalSet, set.contains("Giraffe") { + print("optionalSet contains Giraffe") +} else { + print("optionalSet does not contain Giraffe") +} /* END CODE */ // <-- [RUN HERE] optionalSet should NOT contain Giraffe at this point // [5] Initialize optionalSet with "Cow", "Elephant" // (can be done in one line) /* BEGIN CODE */ - +optionalSet = ["Cow", "Elephant"] /* END CODE */ // [6] Insert "Giraffe" into optionalSet again /* BEGIN CODE */ - +optionalSet?.insert("Giraffe") /* END CODE */ // [7] Check if optionalSet contains "Giraffe" /* BEGIN CODE */ - +if let set = optionalSet, set.contains("Giraffe") { + print("optionalSet contains Giraffe") +} else { + print("optionalSet does not contain Giraffe") +} +print("optionalSet size: \(optionalSet?.count ?? 0)") /* END CODE */ // <-- [RUN HERE] optionalSet SHOULD contain Giraffe at this point @@ -179,7 +195,9 @@ print("optionalSet size: \(optionalSet?.count)") // [8] Insert "Giraffe" into optionalSet again /* BEGIN CODE */ +optionalSet?.insert("Giraffe") +print("optionalSet size: \(optionalSet?.count ?? 0)") /* END CODE */ print("optionalSet size: \(optionalSet?.count)")