Last active
November 2, 2015 19:29
-
-
Save profh/a6e7e604faa536634d4e to your computer and use it in GitHub Desktop.
67442_Midterm_Playground (Contents)
This file contains 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
//: 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!)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.