Skip to content

Commit 6bd5fd2

Browse files
committed
Day 16 commit
1 parent 074819f commit 6bd5fd2

File tree

15 files changed

+184
-0
lines changed

15 files changed

+184
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Cocoa
2+
3+
// Your challenge is this:
4+
// write a function that accepts an optional array of integers,
5+
// and returns one randomly.
6+
// If the array is missing
7+
// or empty, return a random number in the range 1 through 100.
8+
//
9+
// If that sounds easy, it’s because I haven’t explained the catch yet:
10+
// I want you to write your function in a single line of code.
11+
// No, that doesn’t mean you should just write lots of code then remove all the line breaks – you should be able to write this whole thing in one line of code.
12+
13+
14+
func getRandomInt(_ array: [Int]?) -> Int {
15+
array?.randomElement() ?? getRandomInt(Array(1...100))
16+
}
17+
18+
print(getRandomInt(nil))
19+
print(getRandomInt([]))
20+
print(getRandomInt([1, 2, 3]))
21+
22+
23+
func getRandomInt2(_ array: [Int?]?) -> Int {
24+
if let array = array {
25+
if let elem = array.randomElement() {
26+
// .randomElement returns an optional. If the array is empty, it's nil.
27+
// But even if the array is not empty, the element may be nil, because that is a possible value for elements of the array.
28+
// Therefore, we need to unwrap the element again.
29+
if let elem = elem {
30+
return elem
31+
}
32+
}
33+
}
34+
return getRandomInt(Array(1...100))
35+
}
36+
37+
print(getRandomInt2(nil))
38+
print(getRandomInt2([]))
39+
print(getRandomInt2([nil, nil]))
40+
print(getRandomInt2([1, 2, 3]))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

checkpoint-9.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>day-12 (Playground).xcscheme</key>
8+
<dict>
9+
<key>isShown</key>
10+
<false/>
11+
<key>orderHint</key>
12+
<integer>0</integer>
13+
</dict>
14+
</dict>
15+
</dict>
16+
</plist>

day-14.playground/Contents.swift

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import Cocoa
2+
3+
let opposites = [
4+
"Mario": "Wario",
5+
"Luigi": "Waluigi"
6+
]
7+
8+
let peachOpposite = opposites["Peach"]
9+
10+
print(type(of: peachOpposite))
11+
12+
13+
func square(number: Int){
14+
number * number
15+
}
16+
17+
let number: Int? = nil
18+
19+
// This does not work, because the number is optional.
20+
//print(square(number: number))
21+
22+
if let unwrappedNumber = number {
23+
print("The number is something")
24+
print(square(number: unwrappedNumber))
25+
} else {
26+
print("The number is nothing")
27+
}
28+
29+
30+
// What happens if I just use if?
31+
if number == nil {
32+
print("Nothing")
33+
}
34+
35+
36+
var myVar: Int? = 3
37+
38+
if let unwrapped = myVar {
39+
print("Run if myVar has a value inside")
40+
}
41+
42+
enum ValueError: Error {
43+
case noValue
44+
}
45+
46+
guard let unwrappedWithGuard = myVar else {
47+
print("Run if myVar doesn't have a value inside")
48+
throw ValueError.noValue
49+
}
50+
51+
// Because we used guard to unwrap, the constant is available here.
52+
print(unwrappedWithGuard)
53+
54+
55+
let captains = [
56+
"Enterprise": "Picard",
57+
"Voyager": "Janeway",
58+
"Defiant": "Sisko"
59+
]
60+
61+
let new = captains["Serenity"] ?? "N/A"
62+
print(new)
63+
64+
65+
let names = ["Arya", "Bran", "Robb", "Sansa"]
66+
67+
let chosen = names.randomElement()?.uppercased()
68+
if let chosen = chosen {
69+
print("Next in line: \(chosen)")
70+
}
71+
72+
73+
enum UserError: Error {
74+
case badID
75+
case networkFailed
76+
}
77+
78+
func getUser(id: Int) throws -> String {
79+
throw UserError.networkFailed
80+
}
81+
82+
if let user = try? getUser(id: 1) {
83+
print("We got the user: \(user)")
84+
}
85+
86+
let user = (try? getUser(id: 1)) ?? "Anonymous"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

day-14.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)