From 4f5df48cd63c3a64114febfe749e3d4e0cc826f3 Mon Sep 17 00:00:00 2001 From: Bereket Ghebremedhin Date: Thu, 28 Jan 2016 19:00:47 -0500 Subject: [PATCH] did first two --- .../Contents.swift | 140 +++++++++++++++++- 1 file changed, 139 insertions(+), 1 deletion(-) diff --git a/HWFrom1-24(Recursion).playground/Contents.swift b/HWFrom1-24(Recursion).playground/Contents.swift index 1c44504..b859005 100644 --- a/HWFrom1-24(Recursion).playground/Contents.swift +++ b/HWFrom1-24(Recursion).playground/Contents.swift @@ -8,11 +8,149 @@ Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3l */ +//Question 1 + + + +import Foundation + + + +/* + + + + + +Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit#heading=h.za36ai6n5fth + + + + + + + +*/ + + + + + + //Question 1 +var count = 0; + +var firstNumber = 0 + +var secondNumber = 1 + + + + + + + + + + + +func fib(n: Int) -> Int { + + + + while count <= n { + + + + let temp = secondNumber + + secondNumber = secondNumber + firstNumber + + firstNumber = temp + + + + } + + + + 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() { + + if tryStep() == 1 { + + // We’re done! + + return + + } + + else if tryStep() == 0 { + + + + stepUp() + + } + + + + + + // Now we’re two steps below where we want to be :-( + + stepUp() + + stepUp() + +} + + + + + + + + + +//Question 2 + + + + + +//Question 3 @@ -22,4 +160,4 @@ Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3l -//Question 3 \ No newline at end of file +//Question 3