From 91ac96464d5d521fd337b626601e89ec6aa61d21 Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 14 Jan 2016 17:04:03 -0500 Subject: [PATCH 1/6] In progress --- .../Contents.swift | 41 ++++++++++++++++--- .../Contents.swift | 29 +++++++------ 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..2469d4d 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -10,14 +10,45 @@ Use the link here to get the questions. Then code your solutions below. If it https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/edit +*/ -1) +//1) Given an integer N, there is a list of size N-1 that is missing one number from 1 - N(inclusive). Find that number. -2) -3) -4) +//2) Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not +func isDuplicate(numArray: [Int]) -> Bool { + + return false +} -*/ +//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..f7bb27b 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. @@ -13,18 +7,29 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 1) + b) O(n^2) + 2) -3) +3) + a) Binary Search Tree + b) Linked List + c) Graph -4) -5) +4) +*/ +func multiply(num1: Int, num2: Int) { + + for var i = 0; i < num1; i++ { + + } +} +//5) -6) +//6) -7) +//7) -*/ From d9ed637033ec1cdbf4fc0cce789b8b63a1ef81cb Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 14 Jan 2016 18:56:50 -0500 Subject: [PATCH 2/6] more progress --- HWfrom1-10-016(BigO).playground/Contents.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index f7bb27b..4818a21 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -8,8 +8,11 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 1) b) O(n^2) + c) O(n^4) 2) + b) O(n) + c) O(n^2) 3) a) Binary Search Tree From 4421964060220d389ee8878cbe21db8cc9aa9d4b Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 14 Jan 2016 21:45:14 -0500 Subject: [PATCH 3/6] more work --- .DS_Store | Bin 0 -> 6148 bytes HWfrom1-10-016(BigO).playground/Contents.swift | 15 ++++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1f1531ad314c4cdb2f2f536c5d472d940ab73e2a GIT binary patch literal 6148 zcmeHKu}T9$5PhowgElE`#4A#05%C()?g%F+2o_h^Mspa2oEIcP?C!tBpYT8Y0W06^ zERt{$ghUB413PbTc4v0?!S37uaNS8B11$heHo?Xw)f|y=X*F33mJOowF-jDO(ZL~3 zYO~E?3>X8e#(>=2HqLN`TRdX={&skikFbXfPq^pk80UDPmRb8azz9QJ;~4{QnQ$y0 z4&tm|79&06&c`xi7iF5}lW`w-d%0-)6_&Q|qRf9S_ULY!lb&pRx5jtO7gJtMWxPwi z2e|wh%hAo^5vRC)eLt2hF1p7#r}kk@tq=pu*&@v?MbpNBF<=aQF(B_l#3q;otQ7sz zLFG#TVuNNc>`N^nK2g9VV5P_ric?pjy1GzeICZBz3iFbHm7==Ch2q17m0hS%oL`;s zBR?H3Q8aA~7z48mtm|h>^8e}g_5ZAseHjDBz`tU^HPS5Y@<<`K)($7RHe&0siO9T4 l(YsKEbnH8linrOka4eDzF$q{H(n7Hx0Z)S|W8hC2_yo0idzSzJ literal 0 HcmV?d00001 diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 4818a21..362e27d 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -6,30 +6,35 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 1) - + a) b) O(n^2) c) O(n^4) 2) + a) O(n^4) b) O(n) - c) O(n^2) + c) O(nLog(n)) + d) O(nLog(n)) 3) a) Binary Search Tree - b) Linked List - c) Graph + b) Array + c) Tree 4) */ + + +//5) func multiply(num1: Int, num2: Int) { for var i = 0; i < num1; i++ { } } -//5) +//O(n) //6) //7) From c438116cb3fea73729b786075b5f47d460d11cba Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 21 Jan 2016 16:03:30 -0500 Subject: [PATCH 4/6] update --- .DS_Store | Bin 6148 -> 6148 bytes .../Contents.swift | 2 ++ 2 files changed, 2 insertions(+) diff --git a/.DS_Store b/.DS_Store index 1f1531ad314c4cdb2f2f536c5d472d940ab73e2a..702a50dd4d51b23a23d2a41506540020793738a9 100644 GIT binary patch delta 26 icmZoMXffDumX$GmvI3jaWC1pA=6x^zZkA(vCI|p~UkP>q delta 26 icmZoMXffDumX$GSvI3jaWC1pA=Dlw%Hp{U+69fQuf(Wnx diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..ef3edb6 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -23,6 +23,8 @@ Question 3: https://www.hackerrank.com/challenges/connecting-towns Copy and paste your code: + + What is the big O runtime of your code?: */ From fcdcd8597f88fab5a14472b9001054fa6551d6dc Mon Sep 17 00:00:00 2001 From: Brian Date: Wed, 10 Feb 2016 23:17:01 -0500 Subject: [PATCH 5/6] updates --- .DS_Store | Bin 6148 -> 8196 bytes .../Contents.swift | 24 ++++++++-- .../Contents.swift | 29 +++++++++++- .../Contents.swift | 43 +++++++++++++++--- .../Contents.swift | 32 ++++++++++++- 5 files changed, 114 insertions(+), 14 deletions(-) diff --git a/.DS_Store b/.DS_Store index 702a50dd4d51b23a23d2a41506540020793738a9..48aafa1d5d61a55520d0f722d8d971f5857746ea 100644 GIT binary patch literal 8196 zcmeHL&1zFY6#ga+SaBn`=|;R@7p);BN$cM(#-ucmC^mvStxaMoCf<-A#Es-J`YwV` z;L4Q`;KFARBz|XR8Ya03Tull>X3ET&&dfRIp6|?=$r%8uFl=OjEdZOWLa$$AGpESB z)JRF#ksG9n{6L5fvPdI?hmqBOz$4%h@CbMWJOUnp{{aEs*-|za-1}zMw;lnHz*-_8 z=R?3MG;it1PN&pbf$1eWG49T}=wDbr*R%Cah(VkpDT`9PMF<}Do= zYS^I+JCxO|Y=)w=cJc*!IaJOSQ~7(EnfyKMpoR~4%h>_S zc+XZ=?P3pIwDB4r(b6MB&Na&IuzuPOPbpKv@Q`(fRgK4NSE#4m%GT;(r>mvn=C4)C z?u5l+V>qY--#%BiwT6}9(+L}wCcAs)VsauMek zVZFJe^l2Xkt>wSh@!Xf6zbS8N1m<9xo`dNDF*S%*7#l63T1N^+(ms1VG#NEbyh4@n zGQe3pywx$kU+~_rc;C^Q4|TMeSGFwfJDNq?fHj~N#v*BrU(?fLyZ974V$Z2FX?5O? zSewKCMCUQBs$)nUecBpQYpioYeHY8dy08a+shmHqWjP$s_J5S0csV2A#P78H2NNr* zw4z6^H66ra*^&4$qbC!wV_(Oc*&EM@lzrKKsQHy8Qh&{eNJqf{EI7HFm-~ZPZA8)uv zU>y)p$zrWoprvYowUTQGtS78eWZlS6&B8{f;*$7&gcA%wYxq DMT!;N 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-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 2469d4d..ea1b17d 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -14,13 +14,38 @@ https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/ //1) Given an integer N, there is a list of size N-1 that is missing one number from 1 - N(inclusive). Find that number. - +func findMissingNumber(input: Int, list: [Int]) -> Int { + + var sumArr = 0 + var sumList = 0 + + for i in 1...input { + + sumArr += i + } + + for i in list { + + sumList += i + } + + return sumArr - sumList + +} //2) Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not + func isDuplicate(numArray: [Int]) -> Bool { + let set = Set(numArray) - return false + 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. diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 362e27d..5ce8c79 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -6,14 +6,15 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 1) - a) + a) 229mn + 19n + 10 picoseconds + O(m * n) b) O(n^2) c) O(n^4) 2) - a) O(n^4) - b) O(n) - c) O(nLog(n)) + a) O(n^3) + b) O(n^2) + c) O(n^2) d) O(nLog(n)) 3) @@ -24,20 +25,50 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 4) */ +func factorial(num: Int) -> Int { + if num == 0 { + return 1 + } else { + return num * factorial(num - 1) + } +} +factorial(5) //5) -func multiply(num1: Int, num2: Int) { +func multiplyByAddition(let numA: Int, let numB: Int) -> Int{ + + var sum = 0 - for var i = 0; i < num1; i++ { + 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) + + diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index ef3edb6..a508a0b 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -11,21 +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: +let T = Int(readLine(stripNewline: true)!)! +for i in (0.. Date: Thu, 18 Feb 2016 00:36:06 -0500 Subject: [PATCH 6/6] added --- .DS_Store | Bin 8196 -> 8196 bytes .../Contents.swift | 48 +++- .../Contents.swift | 216 +++++++++++++++++- 3 files changed, 254 insertions(+), 10 deletions(-) diff --git a/.DS_Store b/.DS_Store index 48aafa1d5d61a55520d0f722d8d971f5857746ea..a0d831ad414ffc4ea268a67640ae2771455315a1 100644 GIT binary patch delta 48 zcmV-00MGw~K!iY$PXQ9KP`eKS5|hvnOp|aF4zp$uPXn_l6y5`ofONA581@9SaTS&V Gk$`cQ)DbNJ delta 120 zcmZp1XmOa}&nUbxU^hRb@Z<{uK9m0l^KDKO@MpHyV(?%HXK-UEV#sI6WiVvW1!8kB zX~v+z;KPu~P|Q#Q#0m_F40#MGKsp$xvIs1u$xt_0KuDF-je&t-1`xAsUMKvNc{97j LH 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..