From 1892b7e4e603a4ff37a534d95e42c5072022bf50 Mon Sep 17 00:00:00 2001 From: Sammy Alvarado Date: Mon, 8 Mar 2021 16:15:07 -0500 Subject: [PATCH 1/7] Step 1 created 11 constants of the listed agents. --- NOCList.playground/Contents.swift | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index 71c2654..d10d213 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -29,9 +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 agent1 = (coverName: "Ethan Hunt", realName: "Tom Cruise", accessLevel: 8, compromised: false) +let agent2 = (coverName: "Jim Phelps", realName: "Jon Voight", accessLevel: 9, compromised: true) +let agent3 = (coverName: "Claire Phelps", realName: "Emmanuelle Beart", accessLevel: 5, compromised: false) +let agent4 = (coverName: "Eugene Kittridge", realName: "Henry Czerny", accessLevel: 10, compromised: true) +let agent5 = (coverName: "Franz Krieger", realName: "Jean Reno", accessLevel: 4, compromised: false) +let agent6 = (coverName: "Luther Stickell", realName: "Ving Rhames", accessLevel: 4, compromised: false) +let agent7 = (coverName: "Sarah Davies", realName: "Kristin Scott Thomas", accessLevel: 5, compromised: true) +let agent8 = (coverName: "Max RotGrab", realName: "Vanessa Redgrave", accessLevel: 4, compromised: false) +let agent9 = (coverName: "Hannah Williams", realName: "Ingeborga Dapkūnaitė", accessLevel: 5, compromised: true) +let agent10 = (coverName: "Jack Harmon", realName: "Emilio Estevez", accessLevel: 6, compromised: true) +let agent11 = (coverName: "Frank Barnes", realName: "Dale Dye", accessLevel: 9, compromised: false) //: ## Step 2 //: Place the above constants inside an array. Declare this array as a constant as well. From 1e4b46b2daef6d438ce3c8e9e940aa5fa507d78a Mon Sep 17 00:00:00 2001 From: Sammy Alvarado Date: Mon, 8 Mar 2021 16:17:12 -0500 Subject: [PATCH 2/7] Step 2 created an array of agents from the agent tuples. --- NOCList.playground/Contents.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index d10d213..ba20994 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -42,9 +42,7 @@ let agent10 = (coverName: "Jack Harmon", realName: "Emilio Estevez", accessLevel let agent11 = (coverName: "Frank Barnes", realName: "Dale Dye", accessLevel: 9, compromised: false) //: ## Step 2 //: Place the above constants inside an array. Declare this array as a constant as well. - - - +let agents = [agent1, agent2, agent3, agent4, agent5, agent6, agent7, agent8, agent9, agent10, agent11] //: ## 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. From 7c9994a95b97a95c662c56daea06a7dd5c048dba Mon Sep 17 00:00:00 2001 From: Sammy Alvarado Date: Mon, 8 Mar 2021 16:22:40 -0500 Subject: [PATCH 3/7] Steps 3 & 4 Created a function that iterates over the array of agents to determine which ones are compromised and return the count. --- NOCList.playground/Contents.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index ba20994..c254b89 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -45,14 +45,18 @@ let agent11 = (coverName: "Frank Barnes", realName: "Dale Dye", accessLevel: 9, let agents = [agent1, agent2, agent3, agent4, agent5, agent6, agent7, agent8, agent9, agent10, agent11] //: ## 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 compromisedAgents() -> Int { + var count = 0 + for agent in agents { + if agent.compromised != false { + count += 1 + } + } + return count +} //: ## 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("\(compromisedAgents()) 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. From bef83b15aa4e58debdea3ab877d706140e6e1c12 Mon Sep 17 00:00:00 2001 From: Sammy Alvarado Date: Mon, 8 Mar 2021 16:44:41 -0500 Subject: [PATCH 4/7] Steps 5 & 6 Created a function to find a clean agent that also prints out the cover name. --- NOCList.playground/Contents.swift | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index c254b89..fc82cd6 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -59,13 +59,20 @@ func compromisedAgents() -> Int { print("\(compromisedAgents()) 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() -> Int { + var cleanAgents = 0 + for agent in agents { + if agent.compromised != true { + cleanAgents += 1 + print("The cleaned agents are \(agent.coverName)") + } + } + return cleanAgents +} //: ## 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. - - +findCleanAgents() +print("\(findCleanAgents()) clean agents out of \(agents.count) total agents.") //: ## 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. From 88c89e6a0359f6ae7450adb75e4d4aec3066e524 Mon Sep 17 00:00:00 2001 From: Sammy Alvarado Date: Mon, 8 Mar 2021 16:46:18 -0500 Subject: [PATCH 5/7] steps 7 & 8 created a function that allow me to print let 8 or higher agents and shows who has been compromised. --- NOCList.playground/Contents.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index fc82cd6..19e06bf 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -77,14 +77,18 @@ print("\(findCleanAgents()) clean agents out of \(agents.count) total agents.") //: ## 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 agents { + if agent.accessLevel >= 8 && agent.compromised != true { + print("\(agent.realName), level: \(agent.accessLevel)") + } else if agent.accessLevel >= 8 && agent.compromised != false { + print("\(agent.realName), level: \(agent.accessLevel) **WARNING** **COMPROMISED**") + } + } +} //: ## 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" From 2e7dfd1d09cdc07359902f9f9045f5e046d410ee Mon Sep 17 00:00:00 2001 From: Sammy Alvarado Date: Mon, 8 Mar 2021 16:48:45 -0500 Subject: [PATCH 6/7] Steps 9 & 10 Created a function that has a switch statement that iterates over each agent to print the low, mid, and hight level agents. --- NOCList.playground/Contents.swift | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index 19e06bf..6ae337a 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -91,14 +91,28 @@ func findHighRisk() { 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 lowMidHighLevelAgents() { + var lowLevelAgent = 0 + var midLevelAgent = 0 + var highLevelAgent = 0 + + for agent in agents { + switch agent.accessLevel { + case ...4: + lowLevelAgent += 1 + case 5...7: + midLevelAgent += 1 + case 8...: + highLevelAgent += 1 + default: + print("There are no agents with in these levels") + } + } + print("\(lowLevelAgent) low level agents, \(midLevelAgent) mid level agents, and \(highLevelAgent) high level agents") +} //: ## Step 10 //: Call the above function and check its output in the console. - - - +lowMidHighLevelAgents() //: ## 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 dcea9b90c0bb231ff447c0f6028120083b982de5 Mon Sep 17 00:00:00 2001 From: Sammy Alvarado Date: Mon, 8 Mar 2021 16:50:28 -0500 Subject: [PATCH 7/7] Step 11 Created the final function to prints out the cover name an access levels of all agents that is an a ascending order. --- NOCList.playground/Contents.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/NOCList.playground/Contents.swift b/NOCList.playground/Contents.swift index 6ae337a..e1482d8 100644 --- a/NOCList.playground/Contents.swift +++ b/NOCList.playground/Contents.swift @@ -115,5 +115,13 @@ func lowMidHighLevelAgents() { lowMidHighLevelAgents() //: ## 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 agentCoverName() { + + let agentList = agents.sorted(by: { $0.accessLevel < $1.accessLevel }) + + for agent in agentList { + print("\(agent.realName), access level: \(agent.accessLevel)") + } +} - +agentCoverName()