-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodecademyModules.swift
More file actions
677 lines (493 loc) · 13.2 KB
/
Copy pathcodecademyModules.swift
File metadata and controls
677 lines (493 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// codecademy conditionals module
var episode = 3
var villain: String
// switch cases
switch episode {
case 1...3:
villain = "Emperor Palpatine"
case 4...6:
villain = "Darth Vader"
case 7...9:
villain = "Kylo Ren"
default:
villain = ""
}
print(villain)
// switch statement: where clause
var wholeNumber = Int.random(in: 1...10000000)
switch wholeNumber {
case let x where x % 2 == 0:
print("\(wholeNumber) is Composite")
case let x where x % 3 == 0:
print("\(wholeNumber) is Composite")
default:
print("\(wholeNumber) is Prime")
}
// Ternary Conditional Operator
var windy = true
if windy {
print("Sails up")
} else {
print("Motor on")
}
// ternary version ⛵
windy ? print("Sails up") : print("Motor on")
// logical AND operator
var midnight = true
var date = "January 1, 2020"
if midnight && date == "January 1, 2020" {
print("It's the start of a new decade!")
}
// logical OR operator
var carInMotion = false
var insideACar = true
if carInMotion || insideACar {print("Safety first! Buckle up.")}
// combining logical operators
var correctPassword = false
var lessThanThreeTries = true
var accessThroughTouchID = true
var unlock: Bool
if correctPassword && lessThanThreeTries || accessThroughTouchID {
unlock = true
} else {
unlock = false
}
print(unlock)
// order of execution
let bool1 = (true || false) || false || false
let bool2 = !true || (false && false || true)
let bool3 = !(false && true) && (false || false)
print(bool1) // should be true
print(bool2) // should be true
print(bool3) // should be false
// prime number checker with for and while loops
var n: Int = -15454
if n < 2 {
print("you can't test under 2, it's the smallest prime'")
n += (2 - n)
}
while n <= 100 {
var isPrime = true
for i in stride(from:2, to: n - 1, by: 1) {
if n % i == 0 {
isPrime = false
}
}
isPrime ? print("\(n) is prime.") : print("\(n) is not prime.")
n += 1
}
// .append() method
var resolutions = ["play more music 🎸",
"read more books 📚",
"drink more water 💧"]
print(resolutions)
resolutions.append("something")
print(resolutions)
resolutions += ["other thing", "and another one"]
print(resolutions)
// .insert() and .remove() Methods
var dna = ["ATG", "ACG", "GAA", "TAT"]
dna.insert("GTG", at: 3)
dna.remove(at: 0)
print(dna)
// write a swift program to find the sum of even numbers and the product of odd numbers in an array.
var list = [2, 4, 3, 6, 1, 9]
var sum = 0
var prod = 1
for x in list {
if x % 2 == 0 {
sum += x
} else {
prod *= x
}
}
print("Sum of even numbers is \(sum)")
print("Product of odd numbers is \(prod)")
// checking for elements in sets
var coffeeFlavors: Set = ["Caramel", "Mocha", "Pumpkin Spice", "Vanilla", "Blueberry"]
if coffeeFlavors.contains("Blueberry") {
print("One blueberry coffee coming right up.")
} else {
print("We do not serve that coffee flavor here.")
}
// iterating through a set
var thingsToPack: Set = ["Bathing Suit", "Clothes", "Sunglasses", "Sunscreen", "Favorite Book", "Phone Charger"]
var suitcase = Set<String>()
for item in thingsToPack {
suitcase.insert(item)
}
print(suitcase)
// set operations: .intersection()
var swim: Set = ["Turtles", "Ducks", "Puffins", "Shark"]
var fly: Set = ["Humming birds", "Bats", "Ducks", "Puffins"]
var swimAndFly = swim.intersection(fly)
print(swimAndFly)
// set operations: .union()
var consonants: Set = ["B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z"]
var vowels: Set = ["A", "E", "I", "O", "U"]
var alphabet = consonants.union(vowels)
print(alphabet)
// set operations: .symmetricDifference()
var oscarWinners: Set = ["Heath Ledger", "Rita Moreno", "Audrey Hepburn", "John Legend"]
var emmyWinners: Set = ["Peter Dinklage", "John Legend", "Audrey Hepburn", "Rita Moreno"]
var difference = oscarWinners.symmetricDifference(emmyWinners)
print(difference)
// set operations: .subtracting()
var foodEmojis: Set = ["🥕", "🍇", "🌶️", "🍒", "🍎", "🥦"]
var fruitEmojis: Set = ["🍇", "🍎", "🍒"]
var veggieEmojis = foodEmojis.subtracting(fruitEmojis)
print(veggieEmojis)
// Creating an Empty Dictionary
var emptyLiteral: [String: Int] = [:]
var emptyInitializer = [String: Bool]()
// Creating a Dictionary Literal
var roleModels: [String: String] = [
"Mr. Rogers": "Fred McFeely Rogers",
"The Crocodile Hunter": "Stephen Robert Irwin",
"Bill Nye the Science Guy": "William Sanford Nye"
]
print(roleModels)
// Type Inference
var movieYears = [
"Finding Nemo": 2003,
"Toy Story": 1995
]
print(movieYears)
// Adding Elements
var teaSteepingTemperature = [
"Black": 212,
"Oolong": 185,
"White": 185,
]
teaSteepingTemperature["Green"] = 185
teaSteepingTemperature["Rooibos"] = 212
print(teaSteepingTemperature)
// updating elements
var abbreviations = [
"LOL": "Laboring Over Latkes",
"LMK": "Let Me Know",
"BRB": "Bringing Radishes Back",
"GJOYC": "Great Job On Your Code"
]
abbreviations["LOL"] = "Laugh Out Loud"
abbreviations.updateValue("Be Right Back", forKey: "BRB")
print(abbreviations)
// Removing Elements
ar rainbowHex = [
"red": "#ff0000",
"pink": "#ffc0cb",
"yellow": "#ffff00",
"maroon": "#800000",
"green": "#00ff00",
"blue": "#0000ff",
"violet": "#ee82ee"
]
rainbowHex["maroon"] = nil
rainbowHex.removeValue(forKey: "pink")
rainbowHex.removeAll()
print(rainbowHex)
// Inspecting a Dictionary
var numberOfSides = [
"triangle": 3,
"square": 4,
"rectangle": 4,
"decagon": 10,
"triacontagon": 30
]
if numberOfSides.isEmpty {
print("This dictionary has no elements in it.")
} else {
print(numberOfSides.count)
}
// Accessing Values
var flowerNames = [
"Lily": "Lilium",
"Sunflower": "Helianthus",
"Orchid": "Orchidaceae",
"Daffodil": "Narcissus"
]
var sunflowerScientific = flowerNames["Sunflower"]!
print(sunflowerScientific)
if let lilyScientific = flowerNames["Lily"] {
print("A lily is referred to as a \(lilyScientific) in the science community.")
}
// Iterating through a Dictionary
var mythology = [
"Zeus": "Jupiter",
"Athena": "Minerva",
"Poseidon": "Neptune",
"Demeter": "Ceres"]
for (greekName, romanName) in mythology {
print("\(greekName) is also known as \(romanName)")
}
// Using .keys and .values
var lemonadeStand = [
"April": 8.50,
"May": 12.75,
"June": 22.50,
"July": 38.25,
"August": 32.50,
"September": 11.50
]
var total: Double = 0.0
// Add your code below 🍋
for monthlyProfit in lemonadeStand.values {
total += monthlyProfit
}
print("Total profits are \(total)")
// Defining a Function w/Void -> does not return any value.
func directionsToTimesSq() -> Void {
print("Walk 4 mins to 34th St Herald Square train station.")
print("Take the Northbound N, Q, R, or W train 1 stop.")
print("Get off the Times Square 42nd Street stop.")
print("Take lots of pictures! 📸")
}
// Multiple Parameters
func timeToDestination(distance: Int, speed: Int) -> Int {
let time = distance / speed
return time
}
print(timeToDestination(distance: 6836, speed: 560))
// Argument Labels
var friendsList = [String]()
func addFriend(named friendName: String) {
friendsList.append(friendName)
}
addFriend(named: "Alice")
addFriend(named: "Bob")
addFriend(named: "Cindy")
print(friendsList)
// Omitting Argument Labels
let adults = 2
let students = 15
func museumEntry(_ numAdults: Int, _ numStudents: Int) -> Int {
let studentTicket = 14
let adultTicket = 25
let total = (studentTicket * numStudents) + (adultTicket * numAdults)
return total
}
print(museumEntry(adults, students))
// Returning Multiple Values
func favoriteCuisine() -> (name: String, dish: String) {
return ("Italian", "pizza")
}
let cuisine = favoriteCuisine()
print("My favorite \(cuisine.name) dish is \(cuisine.dish)")
// Default Parameters
func bookingTicket(passengerName: String = "Adam", seatClass: String = "Economy", timeOfDeparture: Int) -> String {
return "\(passengerName), your seat class is \(seatClass), and you will be departing at \(timeOfDeparture)"
}
print(bookingTicket(timeOfDeparture: 9))
print(bookingTicket(seatClass: "Business", timeOfDeparture: 9))
// Variadic Parameters
func avgSongLength(times: Int...) -> Int {
var total = 0
for time in times {
total += time
}
return total / times.count
}
print(avgSongLength(times: 183, 176, 180, 176, 184, 179, 181, 180, 172, 178))
// In-Out Parameters
var currentGeneratorState = "Off"
func generators(powerOutage: Bool, state: inout String) {
if powerOutage == true {
state = "On"
}
}
generators(powerOutage: true, state: ¤tGeneratorState)
print(currentGeneratorState)
// -- structures --
// The Init Method
struct Book
{
var title: String
var pages: Int
init (title: String, pages: Int)
{
self.title = title
self.pages = pages
}
}
var theHobbit = Book(title: "The Hobbit", pages: 300)
// Structure Methods
struct Band {
var genre: String
var members: Int
var isActive: Bool
init(genre: String, members: Int, isActive: Bool) {
self.genre = genre
self.members = members
self.isActive = isActive
}
func pumpUpCrowd() -> String
{
if self.isActive
{
return "Are you ready to ROCK?"
} else {
return "..."
}
}
}
var fooFighters = Band(genre: "rock", members: 6, isActive: true)
print(fooFighters.pumpUpCrowd())
// Mutating Methods
struct Band {
var genre: String
var members: Int
var isActive: Bool
init(genre: String, members: Int, isActive: Bool) {
self.genre = genre
self.members = members
self.isActive = isActive
}
func pumpUpCrowd() -> String {
if self.isActive {
return "Are you ready to ROCK?"
} else {
return "..."
}
}
mutating func changeGenre(newGenre: String) -> String
{
self.genre = newGenre
return self.genre
}
}
var journey = Band(genre: "jazz", members: 5, isActive: true)
var bandsNewGenre = journey.changeGenre(newGenre: "rock")
print(bandsNewGenre)
// New Type
struct Band {
var genre: String
var members: Int
var isActive: Bool
init(genre: String, members: Int, isActive: Bool) {
self.genre = genre
self.members = members
self.isActive = isActive
}
func pumpUpCrowd() -> String {
if self.isActive {
return "Are you ready to ROCK?"
} else {
return "..."
}
}
mutating func changeGenre(newGenre: String) -> String
{
self.genre = newGenre
return self.genre
}
}
var journey = Band(genre: "rock", members: 5, isActive: true)
print(type(of: journey))
var bts: Band = Band(genre: "kpop", members: 7, isActive: true)
// Structures are Value Types
struct Finch
{
var lengthInCm: Double
var nestLocation: String
init(lengthInCm: Double, nestLocation: String)
{
self.lengthInCm = lengthInCm
self.nestLocation = nestLocation
}
}
var groundFinch = Finch(lengthInCm: 15.0, nestLocation: "Bush")
var cactusFinch = groundFinch
cactusFinch.nestLocation = "Cactus"
print(cactusFinch.nestLocation)
print(groundFinch.nestLocation)
// classes
class Restaurant
{
var name = ""
var type = [""]
var rating = 0.0
var delivery = false
}
var bobsBurgers = Restaurant()
bobsBurgers.name = "Bob's Burgers"
bobsBurgers.type = ["Burgers", "Fast Food"]
bobsBurgers.rating = 3.5
bobsBurgers.delivery = true
print(bobsBurgers.name)
print(bobsBurgers.type)
print(bobsBurgers.rating)
print(bobsBurgers.delivery)
// The Init Method
class Restaurant2
{
var name = ""
var type = [""]
var rating = 0.0
var delivery = false
init(name: String, type: [String], rating: Double, delivery: Bool)
{
self.name = name
self.type = type
self.rating = rating
self.delivery = delivery
}
}
var laRatatouille = Restaurant2(name: "La Ratatouille", type: ["French"], rating: 4.7, delivery: false)
print(laRatatouille.name)
print(laRatatouille.type)
print(laRatatouille.rating)
print(laRatatouille.delivery)
// Inheritence and Overriding Methods
class Order
{
var items = [""]
var subtotal = 0.0
var tip = 0.0
var total = 0.0
func printReceipt()
{
print("Items: \(items)")
print("Subtotal: $\(subtotal)")
print("Tip: $\(tip)")
print("Total: $\(total)")
}
}
class DeliveryOrder: Order
{
var deliveryFee = 2.0
override func printReceipt()
{
print("Items: \(items)")
print("Subtotal: $\(subtotal)")
print("Tip: $\(tip)")
print("Delivery: $\(deliveryFee)")
print("Total: $\(total)")
}
}
var order2 = DeliveryOrder()
order2.items = ["Ramen", "Diet Coke"]
order2.subtotal = 14.69
order2.tip = 2.00
order2.deliveryFee = 3.00
order2.total = 19.69
order2.printReceipt()
// Classes are Reference Types (unlike value type-structures)
class Order
{
var items = [""]
var subtotal = 0.0
var tip = 0.0
var total = 0.0
init(items: [String], subtotal: Double, tip: Double, total: Double)
{
self.items = items
self.subtotal = subtotal
self.tip = tip
self.total = total
}
}
var order1 = Order(items: ["Chili Fries", "Lemonade"], subtotal: 8.75, tip: 2.0, total: 12.75)
var order8 = order1
order8.total = 0.0
print(order1.total) //prints 0.0
print(order8.total) //also prints 0.0