diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..a0d831a Binary files /dev/null and b/.DS_Store differ diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index 13cc14e..3c5aa8b 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -13,10 +13,26 @@ Link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZ 1) - - -2) - +func fib(n: Int) -> Int { +print("X") +if (n == 0 || n == 1) { +return 1 +} +return fib(n - 1) + fib(n - 2) +} + +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 +} 3) diff --git a/HWFrom1-24(Recursion).playground/Contents.swift b/HWFrom1-24(Recursion).playground/Contents.swift index 1c44504..d33593d 100644 --- a/HWFrom1-24(Recursion).playground/Contents.swift +++ b/HWFrom1-24(Recursion).playground/Contents.swift @@ -1,3 +1,4 @@ +import Foundation /* @@ -11,15 +12,44 @@ Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3l //Question 1 - - - - +func fib(n: Int) -> Int { + + var a = 1 + var b = 1 + + for _ in 0.. 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() { + + switch tryStep() { + case 1: + return + case -1: + stepUp() + stepUp() + default: + stepUp() + } +} + +//Question 3 diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift index bcf8eda..ad6a470 100644 --- a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift @@ -1,16 +1,230 @@ + +import Foundation //: Playground - noun: a place where people can play //Answer the questions in the homework below //https://docs.google.com/document/d/1KlK3PmRMybmHS5db_AP11uHXIcbIeM5vHuuAZOV3xME/edit# //1) +/* +bubble sort Time: O(n^2), Space: O(1) +insertion sort Time: O(n^2), Space: O(1) +selction sort Time: O(n^2), Space: O(1) +merge sort Time: O(n log n), Space: O(n) +quick sort Time: O(n log n), Space: O(log n) +*/ //2) +// Smaller space complexity + //3) +func median(inout array: [Int], first: Int, last: Int) { + + if last - first > 4 { + + var medianIndex: Int + var candidates: [Int] = [array.first!, array[(last+first) / 2], array.last!] + + if (candidates[0] >= candidates[1] && candidates[0] <= candidates[2]) || + (candidates[0] >= candidates[2] && candidates[0] <= candidates[1]) { + + medianIndex = first + + } else if (candidates[1] >= candidates[2] && candidates[1] <= candidates[0]) || + (candidates[1] >= candidates[0] && candidates[1] <= candidates[2]) { + + medianIndex = array.count / 2 + + } else { + + medianIndex = last + } + + if medianIndex != first { + + swap(&array[first], &array[medianIndex]) + } + } +} + +func partition(inout array: [Int], first: Int, last: Int) -> Int { + + let pivotIndex = first + var rightIndex = last + var leftIndex = pivotIndex + 1 + + while leftIndex <= rightIndex { + + if array[leftIndex] > array[pivotIndex] { + + if array[rightIndex] < array[pivotIndex] { + + swap(&array[rightIndex], &array[leftIndex]) + + } else { + + rightIndex-- + } + + } else { + + leftIndex++ + } + } + + if pivotIndex != rightIndex { + + swap(&array[pivotIndex], &array[rightIndex]) + } + + return rightIndex +} + +func quickSort(inout array: [Int], first: Int, last: Int) { + + if last - first <= 0 { + return + } + median(&array, first: first, last: last) + let splitPoint = partition(&array, first: first, last: last) + quickSort(&array, first: 0, last: splitPoint - 1) + quickSort(&array, first: splitPoint + 1, last: last) +} + + //4) +func generateArray(size: Int, valueUpperBound: Int) -> [Int] { + + var array = Array(count: size, repeatedValue: 0) + + for i in 0.. { + + var items: [T] + + init() { + items = [T]() + } + + + mutating func push(element: T) { + + items.append(element) + } + + mutating func pop() -> T? { + + if items.count > 0 { + + return items.removeLast() + } + return nil + } + + func peek() -> T? { + + return items.last + } + + func size() -> Int { + + return items.count + } +} + +struct Bracket { + + var orientation: String + var name: String + + init(symbol: String) { + + switch symbol { + case "(": + orientation = "open" + name = "parenthesis" + case ")": + orientation = "close" + name = "parenthesis" + case "[": + orientation = "open" + name = "bracket" + case "]": + orientation = "close" + name = "bracket" + case "{": + orientation = "open" + name = "brace" + case "}": + orientation = "close" + name = "brace" + default: + orientation = "invalid" + name = "invalid" + } + } +} + + +func isBalanced(array: [String]) -> Bool { + if array.count % 2 != 0 { return false } + + var stack = Stack() + + for i in 0.. Int { + + var sumArr = 0 + var sumList = 0 + + for i in 1...input { + + sumArr += i + } + + for i in list { + + sumList += i + } + + return sumArr - sumList + +} -3) +//2) Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not -4) +func isDuplicate(numArray: [Int]) -> Bool { + let set = Set(numArray) + + if set.count == numArray.count { + return false + } + return true +} + +let arrTest = [2, 3, 4, 5, 6, 7] +isDuplicate(arrTest) -*/ +//3) Given two lists, find the smallest value that exists in both lists. +//L1 = [1,2,5,9] +//L2 = [9, 20 , 5] +var firstList = [1, 2, 5, 9] +var secondList = [9, 20, 5] + +func smallestValue(theFirstList: [Int], theSecondList: [Int]) -> Int? { + let list1 = Set(theFirstList) + let list2 = Set(theSecondList) + + return list1.intersect(list2).minElement() + +} + +smallestValue(firstList, theSecondList: secondList) + + +//4) Check to see if an integer is a palindrome don’t use casting + +func Palindrome(var num: Int) -> Bool { + let originalNum = num + var finalNum = 0 + while(num > 0) { + finalNum *= 10 + finalNum += num % 10 + num /= 10 + } + return finalNum == originalNum +} diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..5ce8c79 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -1,9 +1,3 @@ -//: Playground - noun: a place where people can play - -import UIKit - -var str = "Hello, playground" - /* Use the link here to get the questions. Then code your solutions below. If it does not require code, just write your answer in comments. @@ -12,19 +6,69 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 1) + a) 229mn + 19n + 10 picoseconds + O(m * n) + b) O(n^2) + c) O(n^4) 2) + a) O(n^3) + b) O(n^2) + c) O(n^2) + d) O(nLog(n)) -3) +3) + a) Binary Search Tree + b) Array + c) Tree -4) -5) +4) +*/ +func factorial(num: Int) -> Int { + if num == 0 { + return 1 + } else { + return num * factorial(num - 1) + } +} + +factorial(5) + +//5) +func multiplyByAddition(let numA: Int, let numB: Int) -> Int{ + + var sum = 0 + + for _ in 0...numB { + + sum += numA + } + + return sum +} + +//O(n) +//6) + +func russianPeasantMult(var numA: Int, var numB: Int) -> Int { + var product: Int = 0 + + while numA > 1{ + numA = numA / 1 + numB = numB * 2 + + if numA % 2 != 0 { + product += numB + } + + } + return product +} + +//7) -6) -7) -*/ diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..a508a0b 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -11,19 +11,49 @@ Question 1: https://www.hackerrank.com/challenges/minimum-draws Copy and paste your code: +func draw(let n:Int) -> Int +{ +return n + 1 +} + What is the big O runtime of your code?: +O(1) Question 2: https://www.hackerrank.com/challenges/handshake - Copy and paste your code: +func handshakes(let num: Int) -> Int +{ + +return num * (num-1) / 2 +} What is the big O runtime of your code?: +O(1) Question 3: https://www.hackerrank.com/challenges/connecting-towns Copy and paste your code: -What is the big O runtime of your code?: +let T = Int(readLine(stripNewline: true)!)! + +for i in (0..