-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathContents.swift
65 lines (48 loc) · 1.03 KB
/
Contents.swift
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
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
/*
Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit#heading=h.za36ai6n5fth
*/
//Question 1
//Appendix A: Recursive Fibonacci from class (non-memoized)
//func fib(n: Int) -> Int {
// print("X")
// if (n == 0 || n == 1) {
// return 1
// }
// return fib(n - 1) + fib(n - 2)
//}
//fib(5)
//Iterative
func fib(n: Int) -> Int {
var a = 1
var b = 1
for i in 0..<n {
let
}
return fib(n - 1) + fib(n - 2)
}
//Question 2
var stepNum = 0
func tryStep() -> Bool {
let success = Int(arc4random_uniform(2)) > 0
if (success) {
stepNum++
print("Yay! \(stepNum)")
} else {
stepNum--;
print("Ow! \(stepNum)")
}
return success
}
func stepUp() {
if tryStep() {
// We’re done!
return
}
// Now we’re two steps below where we want to be :-(
stepUp()
stepUp()
}
//Question 3