@@ -23,7 +23,7 @@ let widthLabel = label + String(width)
23
23
// do you get?
24
24
//
25
25
// ANSWER:
26
- // Error: "Binary operator '+' cannot be applied to operands of type 'String'
26
+ // Error: "Binary operator '+' cannot be applied to operands of type 'String'
27
27
// and 'Int'"
28
28
29
29
let apples = 3
@@ -41,9 +41,9 @@ let expStr = "Sum of PI and e is \(math_pi + math_e)."
41
41
let expGreeting = " Hello, \( author) ! "
42
42
43
43
let quotation = """
44
- I said " I have \( apples) apples. "
45
- And then I said " I have \( apples + oranges) pieces of fruit. "
46
- """
44
+ I said " I have \( apples) apples. "
45
+ And then I said " I have \( apples + oranges) pieces of fruit. "
46
+ """
47
47
48
48
var shoppingList = [ " catfish " , " water " , " tulips " ]
49
49
shoppingList [ 1 ] = " bottle of water "
@@ -83,7 +83,7 @@ var optionalName: String? = "John Appleseed"
83
83
var greeting = " Hello! "
84
84
if let name = optionalName {
85
85
greeting = " Hello, \( name) "
86
- } else { // For the EXPERIMENT.
86
+ } else { // For the EXPERIMENT.
87
87
greeting = " Hello, Modnar! "
88
88
}
89
89
print ( greeting)
@@ -164,7 +164,7 @@ func greet(person: String, day: String) -> String {
164
164
greet ( person: " Bob " , day: " Tuesday " )
165
165
166
166
// EXPEIMENT:
167
- // Remove the `day` parameter. Add a parameter to include today's lunch
167
+ // Remove the `day` parameter. Add a parameter to include today's lunch
168
168
// special in the greeting.
169
169
func greet( person: String , lunch: String ) -> String {
170
170
return " Hello \( person) , today's lunch is \( lunch) ! "
@@ -179,7 +179,7 @@ func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
179
179
var min = scores [ 0 ]
180
180
var max = scores [ 0 ]
181
181
var sum = 0
182
-
182
+
183
183
for score in scores {
184
184
if score > max {
185
185
max = score
@@ -188,7 +188,7 @@ func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
188
188
}
189
189
sum += score
190
190
}
191
-
191
+
192
192
return ( min, max, sum)
193
193
}
194
194
let statistics = calculateStatistics ( scores: [ 5 , 3 , 100 , 3 , 9 ] )
@@ -253,7 +253,7 @@ class Shape {
253
253
return " A shape with \( numberOfSides) sides. "
254
254
}
255
255
// EXPERIMENT:
256
- // Add a constant property with `let`, and add another method that takes an
256
+ // Add a constant property with `let`, and add another method that takes an
257
257
// argument.
258
258
let constVal = 0
259
259
func printMessage( message: String ) -> String {
@@ -268,29 +268,29 @@ var shapeDescription = shape.simpleDescription()
268
268
class NamedShape {
269
269
var numberOfSides = 0
270
270
var name : String
271
-
271
+
272
272
init ( name: String ) {
273
273
self . name = name
274
274
}
275
-
275
+
276
276
func simpleDescription( ) -> String {
277
277
return " A shape with \( numberOfSides) sides. "
278
278
}
279
279
}
280
280
281
281
class Square : NamedShape {
282
282
var sideLength : Double
283
-
283
+
284
284
init ( sideLength: Double , name: String ) {
285
285
self . sideLength = sideLength
286
286
super. init ( name: name)
287
287
numberOfSides = 4
288
288
}
289
-
289
+
290
290
func area( ) -> Double {
291
291
return sideLength * sideLength
292
292
}
293
-
293
+
294
294
override func simpleDescription( ) -> String {
295
295
return " A square with sides of length \( sideLength) . "
296
296
}
@@ -301,13 +301,13 @@ test.simpleDescription()
301
301
302
302
class EquilateraTriangle : NamedShape {
303
303
var sideLength : Double = 0.0
304
-
304
+
305
305
init ( sideLength: Double , name: String ) {
306
306
self . sideLength = sideLength
307
307
super. init ( name: name)
308
308
numberOfSides = 3
309
309
}
310
-
310
+
311
311
var perimeter : Double {
312
312
get {
313
313
return 3.0 * sideLength
@@ -316,7 +316,7 @@ class EquilateraTriangle: NamedShape {
316
316
sideLength = newValue / 3.0
317
317
}
318
318
}
319
-
319
+
320
320
override func simpleDescription( ) -> String {
321
321
return " An equilateral triangle with sides of length \( sideLength) . "
322
322
}
@@ -328,16 +328,16 @@ class EquilateraTriangle: NamedShape {
328
328
// `simpleDescription()` method on the `Circle` class.
329
329
class Circle : NamedShape {
330
330
var radius : Double = 0.0
331
-
331
+
332
332
init ( radius: Double , name: String ) {
333
333
self . radius = radius
334
334
super. init ( name: name)
335
335
}
336
-
336
+
337
337
func area( ) -> Double {
338
338
return 3.14 * radius * radius
339
339
}
340
-
340
+
341
341
override func simpleDescription( ) -> String {
342
342
return " A circle with radius \( radius) . "
343
343
}
@@ -378,7 +378,7 @@ enum Rank: Int {
378
378
case ace = 1
379
379
case two, three, four, five, six, seven, eight, nine, ten
380
380
case jack, queen, king
381
-
381
+
382
382
func simpleDescription( ) -> String {
383
383
switch self {
384
384
case . ace:
@@ -411,7 +411,7 @@ if let convertedRank = Rank(rawValue: 3) {
411
411
412
412
enum Suit {
413
413
case spades, hearts, diamonds, clubs
414
-
414
+
415
415
func simpleDescription( ) -> String {
416
416
switch self {
417
417
case . spades:
@@ -424,7 +424,7 @@ enum Suit {
424
424
return " clubs "
425
425
}
426
426
}
427
-
427
+
428
428
// EXPERIMENT
429
429
// Add a `color()` method to `Suit` that returns "black" for spades and clubs,
430
430
// and returns "red" for hearts and diamonds.
591
591
}
592
592
593
593
// EXPERIMENT:
594
- // Add code to throw an error inside the `do` block. What kind of error do you
595
- // need to throw so that the error is handled by the first `catch` block? What
594
+ // Add code to throw an error inside the `do` block. What kind of error do you
595
+ // need to throw so that the error is handled by the first `catch` block? What
596
596
// about the second and third blocks?
597
597
//
598
598
// ANSWER:
@@ -615,7 +615,7 @@ func fridgeContains(_ food: String) -> Bool {
615
615
defer {
616
616
fridgeIsOpen = false
617
617
}
618
-
618
+
619
619
let result = fridgeContent. contains ( food)
620
620
return result
621
621
}
@@ -641,8 +641,7 @@ var possibleInteger: OptionalValue<Int> = .none
641
641
possibleInteger = . some( 100 )
642
642
643
643
func anyCommonElements< T: Sequence , U: Sequence > ( _ lhs: T , _ rhs: U ) -> Bool
644
- where T. Element: Equatable , T. Element == U . Element
645
- {
644
+ where T. Element: Equatable , T. Element == U . Element {
646
645
for lhsItem in lhs {
647
646
for rhsItem in rhs {
648
647
if lhsItem == rhsItem {
@@ -658,8 +657,7 @@ anyCommonElements([1, 2, 3], [3])
658
657
// Modify the anyCommonElements(_:_:) function to make a function that returns an
659
658
// array of the elements that any two sequences have in common.
660
659
func commonElements< T: Sequence , U: Sequence > ( _ lhs: T , _ rhs: U ) -> [ T . Element ]
661
- where T. Element: Equatable , T. Element == U . Element
662
- {
660
+ where T. Element: Equatable , T. Element == U . Element {
663
661
var ans = [ T . Element] ( )
664
662
for lhsItem in lhs {
665
663
for rhsItem in rhs {
0 commit comments