From 42982296d458058ad1d837d95f2d0469d5962892 Mon Sep 17 00:00:00 2001 From: Nelson Gonzalez Date: Wed, 15 Jan 2020 21:38:15 -0500 Subject: [PATCH 1/9] Completed step 1 --- NOCList.playground/Contents.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index 71c2654..a494614 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -29,7 +29,17 @@ This message will self destruct in 5 seconds. */ //: ## Step 1 //: Create constants for each of the above agents and store all their information in a tuple. - +let ethanHunt = (coverName: "Ethan Hunt", realName: "Tom Cruise", accessLevel: 8, compromised: false) +let jimPhelps = (coverName: "Jim Phelps", realName: "Jon Voight", accessLevel: 9, compromised: true) +let clairePhelps = (coverName: "Claire Phelps", realName: "Emmanuelle Beart", accessLevel: 5, compromised: false) +let eugene = (coverName: "Eugene Kittridge", realName: "Henry Czerny", accessLevel: 10, compromised: true) +let franz = (coverName: "Franz Krieger", realName: "Jean Reno", accessLevel: 4, compromised: false) +let lutherStickell = (coverName: "Luther Stickell", realName: "Ving Rhames", accessLevel: 4, compromised: false) +let sarahDavies = (coverName: "Sarah Davies", realName: "Kristin Scott Thomas", accessLevel: 5, compromised: true) +let max = (coverName: "Max RotGrab", realName: "Vanessa Redgrave", accessLevel: 4, compromised: false) +let hannahWilliams = (coverName: "Hannah Williams", realName: "Ingeborga Dapkūnaitė", accessLevel: 5, compromised: true) +let jackHermon = (coverName: "Jack Harmon", realName: "Emilio Estevez", accessLevel: 6, compromised: true) +let frankBarnes = (coverName: "Frank Barnes", realName: "Dale Dye", accessLevel: 9, compromised: false) //: ## Step 2 From 085a9a9974c9bf4bd90ea27f32c343391d3eddc2 Mon Sep 17 00:00:00 2001 From: Nelson Gonzalez Date: Wed, 15 Jan 2020 21:39:09 -0500 Subject: [PATCH 2/9] Completed step 2 --- NOCList.playground/Contents.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index a494614..b620a26 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -44,7 +44,7 @@ let frankBarnes = (coverName: "Frank Barnes", realName: "Dale Dye", accessLevel: //: ## Step 2 //: Place the above constants inside an array. Declare this array as a constant as well. - +let agentsArray = [ethanHunt, jimPhelps, clairePhelps, eugene, franz, lutherStickell, sarahDavies, max, hannahWilliams, jackHermon, frankBarnes] //: ## Step 3 From cd16c7a8246f295c88214159a0b6904b1b9cace3 Mon Sep 17 00:00:00 2001 From: Nelson Gonzalez Date: Wed, 15 Jan 2020 21:39:49 -0500 Subject: [PATCH 3/9] Completed step 3 --- NOCList.playground/Contents.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index b620a26..7c7d4bd 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -49,6 +49,18 @@ let agentsArray = [ethanHunt, jimPhelps, clairePhelps, eugene, franz, lutherStic //: ## Step 3 //: Create a function that calculates the total number of compromised agents. Inside the function, iterate over the array of agents to determine which ones are compromised. Return the total count. +func totalNumberOfCompromisedAgents() -> Int { + var compromisedCount = 0 + for agent in agentsArray { + if agent.compromised { + compromisedCount += 1 + } + } + + return compromisedCount +} + +print(totalNumberOfCompromisedAgents()) From e2c495a80b8a2c7cafb8ca34475a11190f23229b Mon Sep 17 00:00:00 2001 From: Nelson Gonzalez Date: Wed, 15 Jan 2020 21:40:38 -0500 Subject: [PATCH 4/9] Completed step 4 --- NOCList.playground/Contents.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index 7c7d4bd..34a2f7a 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -66,7 +66,7 @@ print(totalNumberOfCompromisedAgents()) //: ## Step 4 //: Call the above function to find the total number of compromised agents and then print a sentence that says "# agents have been compromised!" using string interpolation. - +print("\(totalNumberOfCompromisedAgents()) agents have been compromised!") //: ## Step 5 From a46035c8c77e84b130acdc1f0613a09fd87ac717 Mon Sep 17 00:00:00 2001 From: Nelson Gonzalez Date: Wed, 15 Jan 2020 21:41:39 -0500 Subject: [PATCH 5/9] Completed step 5 --- NOCList.playground/Contents.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index 34a2f7a..4e347df 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -71,7 +71,19 @@ print("\(totalNumberOfCompromisedAgents()) agents have been compromised!") //: ## Step 5 //: Create a function called "findCleanAgents" that both prints the cover names of all uncompromised agents, as well as returns an array of agents that are uncompromised. +func findCleanAgents() -> [(coverName: String, realName: String, accessLevel: Int, compromised: Bool)] { + var uncompromisedAgents = [(coverName: String, realName: String, accessLevel: Int, compromised: Bool)]() + for agent in agentsArray { + if agent.compromised == false { + print(agent.coverName) + uncompromisedAgents.append(agent) + } + } + print(uncompromisedAgents.count) + return uncompromisedAgents +} +findCleanAgents() //: ## Step 6 From ad59690f7079b5ca68f68024312c8b980a636da8 Mon Sep 17 00:00:00 2001 From: Nelson Gonzalez Date: Wed, 15 Jan 2020 21:42:27 -0500 Subject: [PATCH 6/9] Completed step 6 --- NOCList.playground/Contents.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index 4e347df..63ab57c 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -88,7 +88,7 @@ findCleanAgents() //: ## Step 6 //: Call the above function to find the total number of clean agents and print a message that says "# clean agents out of # total agents." Use the total number of agents in the array from step 2 as the second number in the string. - +print("\(findCleanAgents().count) clean agents our of \(agentsArray.count) total agents") //: ## Step 7 From bc9a9adf123415ed07d52512eb93235fc0df74be Mon Sep 17 00:00:00 2001 From: Nelson Gonzalez Date: Wed, 15 Jan 2020 21:43:07 -0500 Subject: [PATCH 7/9] Completed step 7 --- NOCList.playground/Contents.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index 63ab57c..b6e730c 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -94,7 +94,18 @@ print("\(findCleanAgents().count) clean agents our of \(agentsArray.count) total //: ## Step 7 //: Create a function called "findHighRisk" that prints out the real names and access levels of agents with level 8 or higher. If one of these agents is also currently compromised, add `**WARNING** **COMPROMISED**` to the end of the string that includes their name and access level. //: - Example: `Jon Voight, level: 9 **WARNING** **COMPROMISED**` - +func findHighRisk() { + for agent in agentsArray { + if agent.accessLevel >= 8 { + switch agent.compromised { + case true: + print("\(agent.realName), \(agent.accessLevel) **WARNING** **COMPROMISED**") + case false: + print("\(agent.realName), \(agent.accessLevel)") + } + } + } +} //: ## Step 8 From 59c71ed0b893bcad358ac43388d6e969e05cbe3c Mon Sep 17 00:00:00 2001 From: Nelson Gonzalez Date: Wed, 15 Jan 2020 21:44:17 -0500 Subject: [PATCH 8/9] Completed step 8-10 --- NOCList.playground/Contents.swift | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index b6e730c..b2b8b12 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -111,17 +111,37 @@ func findHighRisk() { //: ## Step 8 //: Call the above function and check the output in the console to ensure it is functioning properly. - +findHighRisk() //: ## Step 9 //: Create a function that finds totals for low, mid, and high level agents. Low level agents are 4 or lower, mid are 5-7, and high level agents are 8 or above. Iterate over each agent and use a `switch` statement to determine their level group. At the end of the function, print a statement like the following: "# low level agents, # mid level agents, and # high level agents" +func findTotalsForAgentsLevel() { + var lowLevelAgents = 0 + var midLevelAgents = 0 + var highLevelAgents = 0 + + for agent in agentsArray { + switch agent.accessLevel { + case 0...4: + lowLevelAgents += 1 + case 5...7: + midLevelAgents += 1 + case let x where x >= 8: + highLevelAgents += 1 + default: + break + } + } + + print("\(lowLevelAgents) low level agents, \(midLevelAgents) mid level agents, and \(highLevelAgents) high level agents") +} //: ## Step 10 //: Call the above function and check its output in the console. - +findTotalsForAgentsLevel() //: ## Step 11 (Optional) //: Create and call a function that prints the cover names and access levels of all agents, but the list should be sorted by access level, in ascending order. From df5c715f39761e28c4430d7fc65746057e2405d7 Mon Sep 17 00:00:00 2001 From: Nelson Gonzalez Date: Wed, 15 Jan 2020 21:45:23 -0500 Subject: [PATCH 9/9] Completed step 11 --- NOCList.playground/Contents.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index b2b8b12..c92dbc3 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -145,5 +145,14 @@ findTotalsForAgentsLevel() //: ## Step 11 (Optional) //: Create and call a function that prints the cover names and access levels of all agents, but the list should be sorted by access level, in ascending order. +func coverNameAndAccessLevelsInAscendingOrder() { + + let newArray = agentsArray.sorted { $0.accessLevel < $1.accessLevel } + for agent in newArray { + + print(agent.coverName, agent.accessLevel) + } +} +coverNameAndAccessLevelsInAscendingOrder()