Skip to content

Instantly share code, notes, and snippets.

@profh
Last active November 2, 2015 19:29
Show Gist options
  • Save profh/a6e7e604faa536634d4e to your computer and use it in GitHub Desktop.
Save profh/a6e7e604faa536634d4e to your computer and use it in GitHub Desktop.
67442_Midterm_Playground (Contents)
//: Possible solutions to parts 1 & 2 of the 67-442 midterm (2015)
import UIKit
// PART 1: Closures
var characterFirstAppearance = ["obi-wan": "ANH",
"yoda": "ESB",
"luke": "ANH",
"windu": "TPM",
"anakin": "TPM",
"qui-gon": "TPM",
"plo-koon": "TPM",
"vader": "ANH",
"sidious": "ROJ",
"tyranus": "AOC",
"maul": "TPM",
"leia": "ANH",
"han": "ANH",
"chewbacca": "ANH",
"ackbar": "ROJ",
"antilles": "ANH"]
var movies = ["TPM": "The Phantom Menace", "AOC": "Attack of the Clones", "ROS": "Revenge of the Sith", "ANH": "A New Hope", "ESB": "Empire Strikes Back", "ROJ": "Return of the Jedi"]
var rebels = ["leia", "luke", "han", "chewbacca", "ackbar", "antilles"]
var sith = ["vader", "ventress", "sidious", "tyranus", "plagueis", "maul"]
var jedi = ["obi-wan", "yoda", "luke", "windu", "anakin", "qui-gon", "sifo-dyas", "ahsoka", "plo-koon"]
struct Character {
var name: String
var firstAppeared: String
init(name: String, firstAppeared: String){
self.name = name
self.firstAppeared = firstAppeared
}
}
var arrayCharacters = map(characterFirstAppearance){ Character(name: $0, firstAppeared: $1) }
arrayCharacters
var jediRebels = jedi.filter{contains(rebels, $0)}
//var jediRebels = jedi.filter{rebels.contains($0)}
jediRebels
//var sithNotAppearing = sith.filter{!contains(arrayCharacters.map{$0.name}, $0)}
var sithNotAppearing = sith.filter{!contains(Array(characterFirstAppearance.keys), $0)}
sithNotAppearing
var sithList = ", ".join(sithNotAppearing)
sithList
var firstInANH = arrayCharacters.filter({$0.firstAppeared == "ANH"}).map{$0.name}
firstInANH.sort(<)
// -----------------
// PART 2: Optionals
var capitol: String?
var ship: String? = "Millennium Falcon"
var planets: [String]? = ["Dagobah", "Endor", "Naboo", "Alderaan", "Tatooine"]
var s1 = "The Galatic Empire is on the planet \(capitol)"
var s2 = "Han Solo's ship is called \(ship)"
var s3 = "Yoda lived on \(planets!.first)"
//var s3 = "Yoda lived on \(planets!.first!)"
@profh
Copy link
Author

profh commented Nov 2, 2015

These contents can be added to an Xcode playground. Note that first appearance is order in which movies were produced, not the story's chronological order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment