-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathContents.swift
58 lines (40 loc) · 1023 Bytes
/
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
import Foundation
/*
Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit#heading=h.za36ai6n5fth
*/
//Question 1
func fib(firstNumber: Int, secondNumber: Int, n: Int) -> Int {
var previousNumber1 = firstNumber
var previousNumber2 = secondNumber
var nth = 0
for _ in 0..<n - 2 {
nth = previousNumber1 + previousNumber2
previousNumber1 = previousNumber2
previousNumber2 = nth
}
return nth
}
//print(fib(1, secondNumber: 1, n: 5))
//Question 2
var stepNum = 0
func tryStep() -> Int {
let stepCount = Int(arc4random_uniform(3)) - 1
stepNum += stepCount;
switch(stepCount) {
case -1: print("Ouch \(stepNum)")
case 1: print("Yay \(stepNum)")
default: print("Beep \(stepNum)")
}
return stepCount
}
func stepUp() {
let didTryStep = tryStep()
if didTryStep == 0 {
stepUp()
} else if didTryStep == -1 {
stepUp()
stepUp()
}
}
stepUp()
//Question 3