From 4cb178cd1ea977e38a15eac6688f8dc64a0a271d Mon Sep 17 00:00:00 2001 From: aedwardg Date: Sat, 31 Oct 2020 23:09:18 -0700 Subject: [PATCH] Add challenge solutions Added solutions for the 3 optional challenges at the end of the project. Also included example of how to use a for-loop to solve challenge 1. I felt this was important because most Codecademy learners -- even those who know other languages or have completed the entire Learn Swift course -- would have trouble using a for-loop for Challenge #1 due to the nature of Swift's sets. Sets in Swift are value types and cannot be subscripted by their index the way arrays can. This presents unique issues when trying to loop through a set of sets to perform manipulations on those sets. --- 5-arrays-and-sets/school-roster/Roster.swift | 58 ++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/5-arrays-and-sets/school-roster/Roster.swift b/5-arrays-and-sets/school-roster/Roster.swift index 16c1377..529e12e 100644 --- a/5-arrays-and-sets/school-roster/Roster.swift +++ b/5-arrays-and-sets/school-roster/Roster.swift @@ -61,3 +61,61 @@ for aClass in classSet { } print ("There are \(sevenPlus) classes with seven or more students.") + + +// Task Group: Challenge #1 + +// Use `.remove()` to delete Skyla from any classes they are currently enrolled in. +spanish101.remove("Skyla") +artHistory.remove("Skyla") +computerScience.remove("Skyla") + +// Create updated set of class rosters +var newClassSet: Set = [spanish101, german101, englishLiterature, computerScience, artHistory, advancedCalculus] + +print("New Class Rosters:") +for aClass in newClassSet { + print(aClass) +} + + + +// Task Group: Challenge #2 + +// Create a set called `fieldTrip` that combines these two sets using `.union()` +var fieldTrip = Set(computerScience.union(advancedCalculus)) + +print("Field Trip Group:") +print(fieldTrip) + + + +// Task Group: Challenge #3 + +// Use `.subtracting()` to remove any students in `fieldTrip` who are also in `german101` +fieldTrip = fieldTrip.subtracting(german101) +print("Updated Field Trip Group:") +print(fieldTrip) + + + +// Alternate for-loop solution for Challenge #1 +// Note: using this will not remove Skyla from the individual class sets (e.g., `computerScience`) +// Uncomment the code block below to try it out: +/* +var updatedClassSet = Set>() +for var innerSet in classSet { + if innerSet.contains("Skyla") { + innerSet.remove("Skyla") + updatedClassSet.insert(innerSet) + } else { + // If "Skyla" is not in the innerSet, insert to updatedClassSet as-is + updatedClassSet.insert(innerSet) + } +} + +print("New Class Rosters:") +for aClass in updatedClassSet { + print(aClass) +} +*/ \ No newline at end of file