Nick Walter Challenges (Part 3)
Soooo before going back to Angela's course, I want to go through the rest of these Nick Walter challenges. They've been good fun and very simple, yet encouraging that I CAN apply what I know.
Start time - 13:39
Switch Statements
OK, looking ahead, I'm not familiar with the terms multiple matches and interval matching. Good - something to learn and look up! Let's look within Nick's course first....
Right I got that - multiple matches I have seen before - it's when there are a few matches in one line which can return the same string.
Interval matching is what I suspected it was - using the 1...5 range or 1..<10 etc.
Right let's go!
Start time - 13:39
Switch Statements
// 1) Make a switch for a double constant with at least 3 cases and a defauly
// 2) Make a new swtich that has multiple matches
// 3) Make a switch that uses interval matching
OK, looking ahead, I'm not familiar with the terms multiple matches and interval matching. Good - something to learn and look up! Let's look within Nick's course first....
Right I got that - multiple matches I have seen before - it's when there are a few matches in one line which can return the same string.
Interval matching is what I suspected it was - using the 1...5 range or 1..<10 etc.
Right let's go!
//1)
let number = 7.5
switch number {
case 7.3:
print("\(number) is too low!")
case 7.4:
print("\(number) is nearly there!")
case 7.5:
print("\(number) is the maximum score! Well done!")
default:
print("That is an impossible score!")
}
//2)
let dogName = "Bob"
switch dogName {
case "Clint", "Pudding", "Funkhouser", "Fred", "Toby":
print("\(dogName) is one of Cath's beagles!")
default:
print("\(dogName) is not in the gang!")
}
//3)
var driverPoints: Int = 75
switch driverPoints {
case 0...30:
print("Disaster!")
case 31...60:
print("Not a great point's haul")
case 61...90:
print("Not a bad return")
case 91...150:
print("A fair few points scored!")
case 151...200:
print("Lots of points!")
case 201..<350:
print("Wow - champion surely!")
default:
print("That's an invalid score")
}
All good fun and no problems! Let's just check the solutions....
Yep perfect!
Paused at 14:02 (23 minutes so far)
Arrays
Restart at 14:09
I've tried using screenshot again but not sure whether the above will save! Basically saving time to type out all of this...
Ok for the challenge, all of those look fine. Let's go!
//1)
var numberArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
//2)
numberArray += [11]
//3)
let numberArrayCount = numberArray.count
//4)
numberArray[3]
//5)
numberArray.remove(at: 10)
//6)
numberArray.contains(8)
All good now let's see the solution!
I could also have done the append (//2) as numberArray.append(11).
Removing the last would have been easier as numberArray.removeLast()
Dictionaries
//1)
var dict = [1.0: true, 2.0: false, 3.0: true]
//2)
dict[1.0] = false
//3)
dict[4.0] = false
//4)
dict.removeValue(forKey: 2.0)
//5)
print(dict.count)
//6)
dict.removeAll()
Not TOTALLY sure of the syntax, especially as Xcode has stopped printing and running again...
So let us check the solution!
Nailed them! Interestingly, the removeAll wasn't shown - I'm assuming that's because Swift 2.0 (what Nick Walter had at the time of this video) didn't have that feature! Using dict = [ : ] is another way of doing it.
One more challenge! I'm going to skip sets as I've never seen those used in practice. So onto Loops instead!
Loops
I hope that the updates to Swift since then will still make this work...of course they will! Let's go!
//1
var x = "🐹"
for x in 1...100 {
print(x)
}
//2
var intArray = [1, 3, 5, 7, 9]
for number in intArray {
number *= 2
print(number)
}
//3
var radioheadAlbums = [1: "Pablo Honey", 2: "The Bends", 3: "OK Computer", 4: "Kid A", 5: "Amnesiac", 6: "Hail to the Thief", 7: "In Rainbows", 8: "The King of Limbs", 9: "A Moon Shaped Pool"]
for (albumNumber, albumName) in radioheadAlbums {
print ("Album number: \(albumNumber) is called \(albumName)")
}
//4
var newNumber = 1
while newNumber <= 10_000 {
print(newNumber)
newNumber *= newNumber
}
An error has come up but it could just my Xcode being silly. Let's check the solutions!
OK so first challenge, here was his solution, which I did initially, but not the _ instead of x...
for _ in 1...100 {
print("🐹")
}
Makes sense! But I think mine does still work (hard to tell with Xcode... let's use XCNW as 'Xcode not working'!)
for number in intArray {
print(number * 2)
}
It showed up an error with me doing number *= 2 as it is a let....still not sure about that.
Finish time - 14:52 (50 minutes; 1 hour 13 minutes total)
OK that will do for now, I will be back tomorrow for more challenges! Next time it will be functions, optionals, classes and structs! For optionals I think I'm actually going to watch each video!
A really good effort today, considering my XCNW and the fact that I went straight into these challenges without revising anything. Everything was pretty much spot on and each challenge got trickier. This is definitely good practice!
Comments
Post a Comment