Created
March 15, 2018 00:53
-
-
Save marsikeda/6a7924344c457e13146e303dc5003c60 to your computer and use it in GitHub Desktop.
Midterm Fundamentals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myArray = [1, 2, 4, 6, 7] | |
func sumArray (array: [Int]) -> Int { | |
return array.reduce(0, +) | |
} | |
sumArray(array: myArray) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let q10answer = "No, it would not change because structs (and enums) are value types and create individual, separate instances." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let someCapitals = ["Alabama" : "Montgomery", | |
"Alaska" : "Juneau", | |
"Arizona" : "Phoenix", | |
"Arkansas" : "Little Rock", | |
"California" : "Sacramento", | |
"Colorado" : "Denver"] | |
func arkansasCap (capitals: [String : String]) -> String { | |
guard let arkCapCity = capitals["Arkansas"] else { | |
return "No capital found." | |
} | |
return arkCapCity | |
} | |
arkansasCap(capitals: someCapitals) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func denverState (capitals: [String: String]) -> String { | |
var state = "state not in dictionary" | |
for (key, value) in capitals where value == "Denver" { | |
state = key | |
return state | |
} | |
return state | |
} | |
denverState(capitals: someCapitals) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Ship: String { | |
case battle | |
case friend | |
} | |
print("Hi, \(Ship.friend), let's do \(Ship.battle).") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func battleShip (incoming: Ship) -> String { | |
switch incoming { | |
case Ship.battle: | |
return "DANGER" | |
case Ship.friend: | |
return "SAFE" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func intify (inputElement: String) -> Int? { | |
if let newElement = Int(inputElement) { | |
return newElement | |
} | |
return nil | |
} | |
func smoosh (inputArray: [String], closure: (String) -> Int?) -> [Int?] { | |
var newArray: [Int?] = [] | |
for element in inputArray { | |
let newElement = closure(element) | |
newArray.append(newElement) | |
} | |
return newArray | |
} | |
smoosh(inputArray: numb3rs, closure: intify) | |
//this doesn't work but is as far as I got. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var input = ["canoeing", "hiking", "be", "camping", "drive", "drink", "shampooing"] | |
//Sample output: ["canoeing", "hiking", "camping", "shampooing"] | |
func verbing (input: [String]) -> [String] { | |
var answer: [String] = [] | |
for word in input { | |
if word.contains("ing") { | |
answer.append(word) | |
} | |
} | |
return answer | |
} | |
verbing(input: input) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//a) | |
class C4QTA { | |
let name: String | |
let id: Int | |
let githubAccountName: String | |
let favoriteColor: String | |
init(name: String, id: Int, githubAccountName: String, favoriteColor: String) { | |
self.name = name | |
self.id = id | |
self.githubAccountName = githubAccountName | |
self.favoriteColor = favoriteColor | |
} | |
} | |
//b) | |
let ta1 = C4QTA(name: "Karen", id: 3217, githubAccountName: "karen-fuentes", favoriteColor: "green") | |
let ta2 = C4QTA(name: "Vic", id: 3236, githubAccountName: "vic-zhong", favoriteColor: "purple") | |
let ta3 = C4QTA(name: "Gabe", id: 3214, githubAccountName: "GabrielCodex", favoriteColor: "blue") | |
let ta4 = C4QTA(name: "Marcel", id: 3222, githubAccountName: "Marcel1234", favoriteColor: "red") | |
let ta5 = C4QTA(name: "Tom", id: 3233, githubAccountName: "seymotom", favoriteColor: "yellow") | |
var c4QTAs = [ta1, ta2, ta3, ta4, ta5] | |
var work = [ta1] | |
//c) | |
var sortedTAs: [C4QTA] = [] | |
sortedTAs = c4QTAs.sorted(by: { $0.name < $1.name}) | |
print(sortedTAs) | |
//d | |
var idSortedTAs = c4QTAs.sorted(by: { $0.id < $1.id}) | |
print(idSortedTAs) | |
//e | |
var blueArray: [C4QTA] = [] | |
for element in c4QTAs { | |
blueArray.append(element) { | |
for index in 1...blueArray.count | |
blueArray[index].favoriteColor = "blue" | |
} | |
} | |
print(blueArray) | |
//this was giving me errors for parts c-e |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let q9answer = "Because classes are reference types and instances are not separate. Instead they are all shared." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment