From 0642ddaa386aa4508cf25ca4c4c4af0e2f9bd82c Mon Sep 17 00:00:00 2001 From: Henna Date: Tue, 12 Jan 2016 13:13:12 -0500 Subject: [PATCH 01/22] hw 1 submission --- .../Contents.swift | 113 +++++++++++++++++- 1 file changed, 108 insertions(+), 5 deletions(-) diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..dbf57bb 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -10,14 +10,117 @@ 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. +func findMissingNumber(max : Int, numbers : [Int]) -> Int{ + + // for i in 1...max{ + // if !numbers.contains(i){ + // return i + // } + // + // } + // + // return -1 + + let totalSum = max * (max + 1) / 2 + var totalSumOfList = 0 + for i in numbers { + totalSumOfList += i + } + return totalSum - totalSumOfList + +} -2) +findMissingNumber(5, numbers: [1, 4, 3, 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 hasDupes(numbers : [Int]) -> Bool{ + // let set = Set(numbers) + // + // return numbers.count == set.count ? true : false + + for var i = 0; i Int { + + // return (arr1.minElement()! > arr2.minElement()! ? arr2.minElement()! : arr1.minElement()!) + + var minArr1 = arr1.first + var minArr2 = arr2.first + + for var i = 0; i < arr1.count; i++ { + + if arr1[i] minArr2! ? minArr2 : minArr1)! +} + +findMin([1, 2, 2, 3, 4], arr2: [-1, 2, 2, 3, 4]) + + +// 4) Check to see if an integer is a palindrome, no casting + + +func isPalindromeInt (var number : Int) -> Bool { + + if number < 0{ + return false + } + + // initialize how many zeros + var div = 1; + while ( number/div >= 10) { + div *= 10 + } + + while (number != 0) { + var left = number / div; + var right = number % 10; + + if (left != right){ + return false + } + + number = (number % div) / 10; + div /= 100; + } + + return true; + +} + +isPalindromeInt(232) \ No newline at end of file From 8ff8ef8a373a6d8cf56e46ff5442e57bb1341660 Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 14 Jan 2016 10:49:11 -0500 Subject: [PATCH 02/22] big O hw, not completed --- .../Contents.swift | 65 +++++++++++++++++-- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..2cf987c 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -11,15 +11,71 @@ Use the link here to get the questions. Then code your solutions below. If it https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/edit#heading=h.za36ai6n5fth -1) +1) Memory access takes 1 picosecond + Math operations take 3 picoseconds + Storing data in memory takes 10 picoseconds + Friends algo takes 200 picoseconds to run. + +How long would my computer take to execute the following code if the input image is 1000px wide by 2000px tall? + (10 + 3 + 3) * 1000 + (10 + 3 + 3) * 2000 + 2 + 200 + 1 + = 16000+ 32000 + 202 + 1 = 48,203 + + What if it’s n by m? + 15n + 15m + 203 + +b) O(n) + +c) I'm not sure, O(n^2) -2) +2) + a) O(n^4) + b) O(n) + c) O(n^2) + d) O(n) 3) + a) Tree + + b) Hash Table + + c) Tree + -4) +4)*/ -5) +func factorial(n : Int) -> Int { + + if n <= 1 { + return 1 + } + + return factorial(n-1)*n +} + +/* time complexity: O(n) */ + + +/* + +5) */ +func product(number: Int, times: Int) -> Int { + + var totProduct = 0 + + for _ in 1...times{ + totProduct = totProduct + number + } + + return totProduct +} +product(2, times: 3) + +/* complexity is O(n)*/ + +/* 6) @@ -28,3 +84,4 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ */ + From 27a426ab2e6f8bdc56c3a834015d5d70115193e2 Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 14 Jan 2016 18:55:12 -0500 Subject: [PATCH 03/22] number 6 --- .../Contents.swift | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2cf987c..582df75 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -19,9 +19,9 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ How long would my computer take to execute the following code if the input image is 1000px wide by 2000px tall? (10 + 3 + 3) * 1000 (10 + 3 + 3) * 2000 - 2 + 200 + 2 + 200 * 2000 1 - = 16000+ 32000 + 202 + 1 = 48,203 + = 16000+ 32000 + (202 + 1) * 2000 = 48,203 What if it’s n by m? 15n + 15m + 203 @@ -77,7 +77,32 @@ product(2, times: 3) /* -6) +6)Russian Peasant Multiplication + +Write each number at the head of a column. +Double the number in the first column, and halve the number in the second column. +If the number in the second column is odd, divide it by two and drop the remainder. +If the number in the second column is even, cross out that entire row. +Keep doubling, halving, and crossing out until the number in the second column is 1. +Add up the remaining numbers in the first column. The total is the product of your original numbers. +*/ + +func russain(n1: Int, n2: Int) -> Int{ + if n1 == 1 { + return n2 + } + + if n1 % 2 == 1{ + return n2 + russain(n1 / 2, n2: 2 * n2) + } + + return russain(n1 / 2, n2: 2 * n2) + +} + +russain(4, n2: 2) + +/* 7) From 56895f5d1b56ebaf6a3af01d4af5f6387b41c6ec Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 14 Jan 2016 19:13:02 -0500 Subject: [PATCH 04/22] updates --- .../Contents.swift | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 582df75..046dec4 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -17,18 +17,24 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ Friends algo takes 200 picoseconds to run. How long would my computer take to execute the following code if the input image is 1000px wide by 2000px tall? - (10 + 3 + 3) * 1000 - (10 + 3 + 3) * 2000 - 2 + 200 * 2000 - 1 - = 16000+ 32000 + (202 + 1) * 2000 = 48,203 + 10 3 3 + for (int i = 0; i < width; i++) { + 10 3 3 + for (int j = 0; j < height; j++) { + 1 200 + [image[i][j] makeMoreAwesome]; + } + } + return image; + +((10 + 3 + 3) * 1000) * ((10 + 3 + 3 + 201) * 2000) = 16000 * 488000 = 7.8* 10^9 What if it’s n by m? - 15n + 15m + 203 + 16n * 217m -b) O(n) +b) O(n^2) -c) I'm not sure, O(n^2) +c) O(n^4) 2) From df50f58dfd50d5edb9988331ad5b0c898e0c7129 Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 14 Jan 2016 19:29:23 -0500 Subject: [PATCH 05/22] updates2 --- HWfrom1-10-016(BigO).playground/Contents.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 046dec4..16f3d65 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -61,6 +61,7 @@ func factorial(n : Int) -> Int { return factorial(n-1)*n } + /* time complexity: O(n) */ @@ -91,6 +92,7 @@ If the number in the second column is odd, divide it by two and drop the remaind If the number in the second column is even, cross out that entire row. Keep doubling, halving, and crossing out until the number in the second column is 1. Add up the remaining numbers in the first column. The total is the product of your original numbers. +source: http://polynomialtimes.com/algorithms/decrease-and-conquer/russian-peasant-multiplication/ */ func russain(n1: Int, n2: Int) -> Int{ @@ -108,6 +110,12 @@ func russain(n1: Int, n2: Int) -> Int{ russain(4, n2: 2) +/* O(log(n) + +any time you divide by two its likely a log(n) algorithm + +*/ + /* 7) From a8add8a3d9268ec8da1f8c7d91cb720cee3f83af Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 14 Jan 2016 21:46:48 -0500 Subject: [PATCH 06/22] discrete logic hw --- .DS_Store | Bin 0 -> 6148 bytes .../Contents.swift | 28 ++++++++++++++++++ .../contents.xcplayground | 4 +++ .../contents.xcworkspacedata | 7 +++++ .../timeline.xctimeline | 6 ++++ 5 files changed, 45 insertions(+) create mode 100644 .DS_Store create mode 100644 HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift create mode 100644 HWfrom1-14-16(Logic+Discrete_Math).playground/contents.xcplayground create mode 100644 HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 HWfrom1-14-16(Logic+Discrete_Math).playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..beda843ea98ad3db1b786a378de4387e468f3089 GIT binary patch literal 6148 zcmeHKu}T9$5Phpbf;IsgE4{)(i-^|yga=g>k13o_vm_h-yI3QRpBJ%t0pg7WTiz;3Y>qB ziKF%TBTk+F`Swk=IPV^3Pi-Gf1H1Vb3@~Sl)Yl@qbq1UPXW)YYc^?w0U}jiG)Te_+ zO8{bx-70KLEg?RMVP;rHQlw6xoJ5&*wS4LD9 i8cWC8kW{=uRpD474Ps_kMr04ge*}~UH_pJ1GVl)jCTW}i literal 0 HcmV?d00001 diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift new file mode 100644 index 0000000..361103a --- /dev/null +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -0,0 +1,28 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" + + +/* + +Question 1: https://www.hackerrank.com/challenges/minimum-draws + +Copy and paste your code: + +What is the big O runtime of your code?: + +Question 2: https://www.hackerrank.com/challenges/handshake + +Copy and paste your code: + +What is the big O runtime of your code?: + +Question 3: https://www.hackerrank.com/challenges/connecting-towns + +Copy and paste your code: + +What is the big O runtime of your code?: + +*/ diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/contents.xcplayground b/HWfrom1-14-16(Logic+Discrete_Math).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/contents.xcworkspacedata b/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/timeline.xctimeline b/HWfrom1-14-16(Logic+Discrete_Math).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From 276d06cf6397672a493d28508d6aad3851c3a787 Mon Sep 17 00:00:00 2001 From: Henna Date: Fri, 15 Jan 2016 15:52:10 -0500 Subject: [PATCH 07/22] logic and discrete --- .../Contents.swift | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index 361103a..07092d4 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -8,16 +8,54 @@ var str = "Hello, playground" /* Question 1: https://www.hackerrank.com/challenges/minimum-draws +Jim is off to a party and is searching for a matching pair of socks. His drawer is filled with socks, each pair of a different color. In its worst case scenario, how many socks (x) should Jim remove from his drawer until he finds a matching pair? + +Input Format +The first line contains the number of test cases T. +Next T lines contains an integer N which indicates the total pairs of socks present in the drawer. + +Output Format +Print the number of Draws (x) Jim makes in the worst case scenario. Copy and paste your code: -What is the big O runtime of your code?: +let firstLine = Int(readLine(stripNewline: true)!)! + +for _ in 0...firstLine{ +let nextLine = Int(readLine(stripNewline: true)!)! +print(nextLine+1) +} + +*/ + + + + +/* +Jim would have to draw x + 1 socks in the worst case. where x is the number of pairs of socks + +What is the big O runtime of your code?: O(n) Question 2: https://www.hackerrank.com/challenges/handshake +At the annual meeting of Board of Directors of Acme Inc, every one starts shaking hands with everyone else in the room. Given the fact that any two persons shake hand exactly once, Can you tell the total count of handshakes? + +Input Format +The first line contains the number of test cases T, T lines follow. +Each line then contains an integer N, the total number of Board of Directors of Acme. + +Output Format +Print the number of handshakes for each test-case in a new line. Copy and paste your code: + let firstLine = Int(readLine(stripNewline: true)!)! + for _ in 0...firstLine{ + let numberofpeople = Int(readLine(stripNewline: true)!)! -What is the big O runtime of your code?: + print((numberofpeople*(numberofpeople-1))/2) + + } + +What is the big O runtime of your code?: O(log(n)) Question 3: https://www.hackerrank.com/challenges/connecting-towns From 1c3443238fd7c61067b183da254b001d2fcf9872 Mon Sep 17 00:00:00 2001 From: Henna Date: Fri, 15 Jan 2016 17:25:01 -0500 Subject: [PATCH 08/22] logic and discrete submission --- .../Contents.swift | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index 07092d4..a90aeaf 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -51,7 +51,7 @@ Copy and paste your code: for _ in 0...firstLine{ let numberofpeople = Int(readLine(stripNewline: true)!)! - print((numberofpeople*(numberofpeople-1))/2) + print(numberofpeople-1) } @@ -60,7 +60,29 @@ What is the big O runtime of your code?: O(log(n)) Question 3: https://www.hackerrank.com/challenges/connecting-towns Copy and paste your code: +let firstLine = Int(readLine(stripNewline: true)!)! +for _ in 0...firstLine{ + let town = Int(readLine(stripNewline: true)!)! + var routes: [Int] + var ans + + + + for i in 0...town-1 + { + routes[i] = Int(readLine(stripNewline: true)!)! + + for j in 0...town-1 + { + ans *= routes[j]; + ans = ans%1234567; + } + print(ans) + + } +} + -What is the big O runtime of your code?: +What is the big O runtime of your code?: O(n^2) */ From 03ca6f52e25b609cd0a08ea167ddde27b6c5cb5b Mon Sep 17 00:00:00 2001 From: Henna Date: Fri, 15 Jan 2016 17:28:17 -0500 Subject: [PATCH 09/22] logic and discrete clean up --- .../Contents.swift | 22 +------------------ 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index a90aeaf..2596d72 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -8,14 +8,7 @@ var str = "Hello, playground" /* Question 1: https://www.hackerrank.com/challenges/minimum-draws -Jim is off to a party and is searching for a matching pair of socks. His drawer is filled with socks, each pair of a different color. In its worst case scenario, how many socks (x) should Jim remove from his drawer until he finds a matching pair? -Input Format -The first line contains the number of test cases T. -Next T lines contains an integer N which indicates the total pairs of socks present in the drawer. - -Output Format -Print the number of Draws (x) Jim makes in the worst case scenario. Copy and paste your code: @@ -26,25 +19,11 @@ let nextLine = Int(readLine(stripNewline: true)!)! print(nextLine+1) } -*/ - - - - -/* Jim would have to draw x + 1 socks in the worst case. where x is the number of pairs of socks What is the big O runtime of your code?: O(n) Question 2: https://www.hackerrank.com/challenges/handshake -At the annual meeting of Board of Directors of Acme Inc, every one starts shaking hands with everyone else in the room. Given the fact that any two persons shake hand exactly once, Can you tell the total count of handshakes? - -Input Format -The first line contains the number of test cases T, T lines follow. -Each line then contains an integer N, the total number of Board of Directors of Acme. - -Output Format -Print the number of handshakes for each test-case in a new line. Copy and paste your code: let firstLine = Int(readLine(stripNewline: true)!)! @@ -60,6 +39,7 @@ What is the big O runtime of your code?: O(log(n)) Question 3: https://www.hackerrank.com/challenges/connecting-towns Copy and paste your code: + let firstLine = Int(readLine(stripNewline: true)!)! for _ in 0...firstLine{ let town = Int(readLine(stripNewline: true)!)! From ea1bf4fbecd24acacdd97682bd9427ab5a7f2f19 Mon Sep 17 00:00:00 2001 From: Henna Date: Sat, 16 Jan 2016 10:54:49 -0500 Subject: [PATCH 10/22] logic and discrete clean up2 --- .../Contents.swift | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index 2596d72..412b05f 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -25,12 +25,30 @@ What is the big O runtime of your code?: O(n) Question 2: https://www.hackerrank.com/challenges/handshake +analagous to graph theory + +METHOD 1: += C(n, 2) // given a group of n people how many ways can i choose 2 people += n! / (2! * (n-2)!) += n! / 2 (n-2)! += n (n-1) (n-2) .. 1 + 2 * (n-2)(n-3) .. 1 += n (n-1) + 2 + +METHOD 2: first person shakes hands with (n-1) people ... last person shakes hands with no one +(n-1) + (n-2) + ... + 1 + 0 +summation from k =1 to n is (n(n+1))/2 +from 1 to n-1 is .. += n (n-1) + 2 + Copy and paste your code: let firstLine = Int(readLine(stripNewline: true)!)! for _ in 0...firstLine{ - let numberofpeople = Int(readLine(stripNewline: true)!)! + let numberofpeople = Int(readLine(stripNewline: true)!)! - print(numberofpeople-1) + print((numberofpeople)(numberofpeople-1)/2) } @@ -46,8 +64,6 @@ for _ in 0...firstLine{ var routes: [Int] var ans - - for i in 0...town-1 { routes[i] = Int(readLine(stripNewline: true)!)! @@ -55,7 +71,6 @@ for _ in 0...firstLine{ for j in 0...town-1 { ans *= routes[j]; - ans = ans%1234567; } print(ans) From 4dcf7a3919e58880fca4ff93e75a50bcba5f2e0a Mon Sep 17 00:00:00 2001 From: Henna Date: Sat, 16 Jan 2016 16:23:53 -0500 Subject: [PATCH 11/22] logic and discrete answers --- .../Contents.swift | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index 412b05f..5211207 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -7,6 +7,7 @@ var str = "Hello, playground" /* + Question 1: https://www.hackerrank.com/challenges/minimum-draws @@ -58,26 +59,33 @@ Question 3: https://www.hackerrank.com/challenges/connecting-towns Copy and paste your code: -let firstLine = Int(readLine(stripNewline: true)!)! -for _ in 0...firstLine{ - let town = Int(readLine(stripNewline: true)!)! - var routes: [Int] - var ans - - for i in 0...town-1 - { - routes[i] = Int(readLine(stripNewline: true)!)! - - for j in 0...town-1 - { - ans *= routes[j]; - } - print(ans) +let firstLine = readLine(stripNewline: true)! +let T = Int(firstLine)! + +for i in (0.. Date: Sun, 17 Jan 2016 11:20:21 -0500 Subject: [PATCH 12/22] updates --- HWfrom1-10-016(BigO).playground/Contents.swift | 6 ++++-- .../Contents.swift | 14 ++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 16f3d65..81cd23a 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -110,7 +110,7 @@ func russain(n1: Int, n2: Int) -> Int{ russain(4, n2: 2) -/* O(log(n) +/* O(log(n)) any time you divide by two its likely a log(n) algorithm @@ -118,9 +118,11 @@ any time you divide by two its likely a log(n) algorithm /* -7) +7)Using the technique from exercise 4, profile the built in sorting method in objective C (use an NSMutableArray and google how to sort an array of numbers in objective C). Graph the result. Use spreadsheet formulas to add graph lines for n, n2, and n*log(n). (You’ll have to modify the factors to make them fit in the graph window and to be close to the graph of method execution time). Show that the sort method best fits n * log(n). +https://docs.google.com/spreadsheets/d/1CqF_l1DJCnWxTE3uEYb13lvi2KiqtKBONENwqYunqq4/edit?usp=sharing + */ diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index 5211207..7bd9066 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -13,11 +13,12 @@ Question 1: https://www.hackerrank.com/challenges/minimum-draws Copy and paste your code: -let firstLine = Int(readLine(stripNewline: true)!)! +let firstLine = readLine(stripNewline: true)! +let T = Int(firstLine)! -for _ in 0...firstLine{ -let nextLine = Int(readLine(stripNewline: true)!)! -print(nextLine+1) +for _ in (0.. Date: Mon, 18 Jan 2016 17:13:13 -0500 Subject: [PATCH 13/22] lists and sorts hw --- .../Contents.swift | 114 ++++++++++++++++++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 ++ .../timeline.xctimeline | 6 + 4 files changed, 131 insertions(+) create mode 100644 HWFrom1-17-16(Lists and Sorts).playground/Contents.swift create mode 100644 HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground create mode 100644 HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 HWFrom1-17-16(Lists and Sorts).playground/timeline.xctimeline diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift new file mode 100644 index 0000000..f2bc011 --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -0,0 +1,114 @@ +//: Playground - noun: a place where people can play + +import UIKit + +/* +Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit#heading=h.uopysoy45zmw + +1) Get possible numbers in sudoku board + +*/ + +func setValuesToFalse(inout arr:[Bool]) { + for i in 0.. [Int] { + var numberUsed = Array(count: 9, repeatedValue: false) // map each index to a number used + var invalidNumbers = [Int]() + var validNumbers = [Int]() + // add all the numbers in that row to an array + + for pos in (0..=0{ + invalidNumbers.append(num) + + } + } + + // add all the numbers in that column to an array + + for pos in (0..<9) { + let num = sudokuBoard[pos][col] // subtract one to account for offset + if num>=0 && !invalidNumbers.contains(num){ + invalidNumbers.append(num) + } + } + + + // check the 3x3 box that it's in + + for i in(0..<3) { + for j in (0..<3) { + let num = sudokuBoard[row*3+i][col*3+j] + if num>=0 && !invalidNumbers.contains(num){ + invalidNumbers.append(num) + } + + } + } + + setValuesToFalse(&numberUsed) + for i in 0..[[Int]]{ + + let columns = matrix[0].count + let rows = matrix.count + var arr = [[Int]](count: rows, repeatedValue: [Int](count: columns, repeatedValue: 0)) + + for row in 0.. + + + \ No newline at end of file diff --git a/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-17-16(Lists and Sorts).playground/timeline.xctimeline b/HWFrom1-17-16(Lists and Sorts).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From 62081ca9c28e555612d3bfb7cf0a632e2e02a320 Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 21 Jan 2016 19:57:21 -0500 Subject: [PATCH 14/22] hw updates --- .../Contents.swift | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index f2bc011..370cabd 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -81,7 +81,7 @@ let matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 1, 2], [3, 4, 5, 6] ] - +// m x n matrix // 0,0 goes to 0,3 // 1,0 goes to 0,2 // 2,0 goes to 0,1 @@ -97,7 +97,6 @@ func rotateMatrix(matrix:[[Int]])->[[Int]]{ for col in 0.. [Int]{ + + var left = values[0...1] + + if left[0] > left [1]{ + let t = left[0] + left[0] = left[1] + left[1] = t + } + + var right = values[2...4] + + if right[0] > right [1]{ + let t = right[1] + right[0] = right[1] + right[1] = t + } + + + +} From 6a385085a77257be8bb87cfbe41a557225e4d24b Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 28 Jan 2016 10:34:51 -0500 Subject: [PATCH 15/22] HW for 1-28 --- .DS_Store | Bin 6148 -> 6148 bytes HW(Recursion)_final.playground/Contents.swift | 96 +++++++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 + .../timeline.xctimeline | 6 + HackkerRankStuff.playground/Contents.swift | 9 + .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 + .../timeline.xctimeline | 6 + Recursion/Recursion.xcodeproj/project.pbxproj | 246 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + Recursion/Recursion/main.swift | 44 ++++ 12 files changed, 436 insertions(+) create mode 100644 HW(Recursion)_final.playground/Contents.swift create mode 100644 HW(Recursion)_final.playground/contents.xcplayground create mode 100644 HW(Recursion)_final.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 HW(Recursion)_final.playground/timeline.xctimeline create mode 100644 HackkerRankStuff.playground/Contents.swift create mode 100644 HackkerRankStuff.playground/contents.xcplayground create mode 100644 HackkerRankStuff.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 HackkerRankStuff.playground/timeline.xctimeline create mode 100644 Recursion/Recursion.xcodeproj/project.pbxproj create mode 100644 Recursion/Recursion.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Recursion/Recursion/main.swift diff --git a/.DS_Store b/.DS_Store index beda843ea98ad3db1b786a378de4387e468f3089..8735d0b74200fe410df6900fd0a0f47b97969c38 100644 GIT binary patch delta 273 zcmZoMXfc@JFUrNhz`)4BAi%(o%8<-Z%232m%#gWRkYhQsHb_d2!Gj^3L4zR(S(+iA zA&)_mA)Xw+0C?>o#QV*03^gY#{d8T delta 49 zcmZoMXfc@JFUrQiz`)4BAi%(o&ydSt$e_z$z+kyqkYhPBBim*l*4<1S8ypxnvvd6A F2LR)^3Y7o= diff --git a/HW(Recursion)_final.playground/Contents.swift b/HW(Recursion)_final.playground/Contents.swift new file mode 100644 index 0000000..96702d2 --- /dev/null +++ b/HW(Recursion)_final.playground/Contents.swift @@ -0,0 +1,96 @@ +//: Playground - noun: a place where people can play + +import UIKit +import Foundation + + +/* +Write an iterative (not recursive) fibonacci function that calculates the nth fibonacci number. How does its performance compare with the non-memoized recursive one (see Appendix A below), based on the number of iterations that Swift says it takes in the right margin? + +When n is 4, the iterative function, although running with O(n) complexity, is run 4 times while the recursive is run 9 times. +*/ + +//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(4) + +// HW ANSWER + +func fibIter(n: Int) -> Int { + //0, 1, 1, 2, 3, 5, 8, 13, 21 + if n == 0{ + return 0; + } + + //if the number isn't 0, proceed + + var x = 0, y = 1, z = 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 +} + +// HW ANSWER + +func stepUp() { + if tryStep() == 1 { + // We’re done! + return + } + // Now we’re two steps below where we want to be :-( + stepUp() + stepUp() +} + + +/* + +Using the code in Appendix C as a starting point, create a Swift command line project to find files on your computer by name. Your solution should use recursion. Your method should return “NOT FOUND” if it couldn’t find the file, otherwise it should return the full path to that file. + +You can’t use a playground for this because they don’t have filesystem access for some reason. Instead, in XCode, go to File > New > Project, select “Application” under “OS X” in the sidebar, then select “Command Line Tool”; on the next screen, choose “Swift” as the language. + +*/ + + + + + + + + + diff --git a/HW(Recursion)_final.playground/contents.xcplayground b/HW(Recursion)_final.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HW(Recursion)_final.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HW(Recursion)_final.playground/playground.xcworkspace/contents.xcworkspacedata b/HW(Recursion)_final.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HW(Recursion)_final.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HW(Recursion)_final.playground/timeline.xctimeline b/HW(Recursion)_final.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HW(Recursion)_final.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/HackkerRankStuff.playground/Contents.swift b/HackkerRankStuff.playground/Contents.swift new file mode 100644 index 0000000..3bee8e5 --- /dev/null +++ b/HackkerRankStuff.playground/Contents.swift @@ -0,0 +1,9 @@ +let testCases = Int(readLine(stripNewline: true)!)! + +for var x = 0; x < testCases; x++ { + + //var first = Int(readLine(stripNewline: true)!)! + + print (readLine(stripNewline: true)!) + +} \ No newline at end of file diff --git a/HackkerRankStuff.playground/contents.xcplayground b/HackkerRankStuff.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HackkerRankStuff.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HackkerRankStuff.playground/playground.xcworkspace/contents.xcworkspacedata b/HackkerRankStuff.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HackkerRankStuff.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HackkerRankStuff.playground/timeline.xctimeline b/HackkerRankStuff.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HackkerRankStuff.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/Recursion/Recursion.xcodeproj/project.pbxproj b/Recursion/Recursion.xcodeproj/project.pbxproj new file mode 100644 index 0000000..74977b0 --- /dev/null +++ b/Recursion/Recursion.xcodeproj/project.pbxproj @@ -0,0 +1,246 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + F0CD40CF1C59AA33000BF819 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0CD40CE1C59AA33000BF819 /* main.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + F0CD40C91C59AA33000BF819 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + F0CD40CB1C59AA33000BF819 /* Recursion */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Recursion; sourceTree = BUILT_PRODUCTS_DIR; }; + F0CD40CE1C59AA33000BF819 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + F0CD40C81C59AA33000BF819 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + F0CD40C21C59AA33000BF819 = { + isa = PBXGroup; + children = ( + F0CD40CD1C59AA33000BF819 /* Recursion */, + F0CD40CC1C59AA33000BF819 /* Products */, + ); + sourceTree = ""; + }; + F0CD40CC1C59AA33000BF819 /* Products */ = { + isa = PBXGroup; + children = ( + F0CD40CB1C59AA33000BF819 /* Recursion */, + ); + name = Products; + sourceTree = ""; + }; + F0CD40CD1C59AA33000BF819 /* Recursion */ = { + isa = PBXGroup; + children = ( + F0CD40CE1C59AA33000BF819 /* main.swift */, + ); + path = Recursion; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + F0CD40CA1C59AA33000BF819 /* Recursion */ = { + isa = PBXNativeTarget; + buildConfigurationList = F0CD40D21C59AA33000BF819 /* Build configuration list for PBXNativeTarget "Recursion" */; + buildPhases = ( + F0CD40C71C59AA33000BF819 /* Sources */, + F0CD40C81C59AA33000BF819 /* Frameworks */, + F0CD40C91C59AA33000BF819 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Recursion; + productName = Recursion; + productReference = F0CD40CB1C59AA33000BF819 /* Recursion */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + F0CD40C31C59AA33000BF819 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0710; + LastUpgradeCheck = 0710; + ORGANIZATIONNAME = "Henna Ahmed"; + TargetAttributes = { + F0CD40CA1C59AA33000BF819 = { + CreatedOnToolsVersion = 7.1; + }; + }; + }; + buildConfigurationList = F0CD40C61C59AA33000BF819 /* Build configuration list for PBXProject "Recursion" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = F0CD40C21C59AA33000BF819; + productRefGroup = F0CD40CC1C59AA33000BF819 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + F0CD40CA1C59AA33000BF819 /* Recursion */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + F0CD40C71C59AA33000BF819 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + F0CD40CF1C59AA33000BF819 /* main.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + F0CD40D01C59AA33000BF819 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + F0CD40D11C59AA33000BF819 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + F0CD40D31C59AA33000BF819 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + F0CD40D41C59AA33000BF819 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + F0CD40C61C59AA33000BF819 /* Build configuration list for PBXProject "Recursion" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F0CD40D01C59AA33000BF819 /* Debug */, + F0CD40D11C59AA33000BF819 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + F0CD40D21C59AA33000BF819 /* Build configuration list for PBXNativeTarget "Recursion" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F0CD40D31C59AA33000BF819 /* Debug */, + F0CD40D41C59AA33000BF819 /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = F0CD40C31C59AA33000BF819 /* Project object */; +} diff --git a/Recursion/Recursion.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Recursion/Recursion.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..9e5d228 --- /dev/null +++ b/Recursion/Recursion.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Recursion/Recursion/main.swift b/Recursion/Recursion/main.swift new file mode 100644 index 0000000..9bcc5da --- /dev/null +++ b/Recursion/Recursion/main.swift @@ -0,0 +1,44 @@ +// +// main.swift +// Recursion +// +// Created by Henna Ahmed on 1/27/16. +// Copyright © 2016 Henna Ahmed. All rights reserved. +// + +import Foundation + +//Appendix C: File searching + + +func findFile(name: String, atPath: String) -> String { + let fileManager = NSFileManager.defaultManager() + + let contents = try! fileManager.contentsOfDirectoryAtPath(atPath) + + for fileOrDir in contents { + var isDir = ObjCBool(false); + let fullPath = atPath + "/" + fileOrDir + let exists = fileManager.fileExistsAtPath(fullPath, isDirectory: &isDir) + if exists && Bool(isDir) { + + //print("DIR: " + fileOrDir) + findFile(name, atPath: fullPath) + + } else if exists { + + if (fileOrDir == name){ +// print("FILE: " + fileOrDir) + print("File is at: \(fullPath)") + return fullPath + } + + } else { + print("NEITHER: " + fileOrDir) + } + } + return "NOT FOUND" +} + +print(findFile("email-collection.html", atPath: "/Users/hennaahmed/Desktop/")) + From 8cbc6f065886b78e6e2deb34d9da48ba4aef5312 Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 28 Jan 2016 19:41:02 -0500 Subject: [PATCH 16/22] correct answers --- .DS_Store | Bin 6148 -> 6148 bytes HW(Recursion)_final.playground/Contents.swift | 18 ++++++++++++------ .../timeline.xctimeline | 5 +++++ Recursion/Recursion/main.swift | 16 ++++++++-------- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/.DS_Store b/.DS_Store index 8735d0b74200fe410df6900fd0a0f47b97969c38..5711dfdab2e80cd7e3997dea92e8b12d7a6d1204 100644 GIT binary patch delta 35 rcmZoMXfc@J&&aVcU^g=($7CLs36lfZgg5)M?qHhOuwXMg$6tN`#<>fK delta 66 zcmZoMXfc@J&&ahgU^g=(*JK`+2?DwKE-ophCCLm7m(FWRGB5xI9e{! Int { } return x; } -fibIter(4) +(0...5).map{ i in fibIter(i) } @@ -68,13 +68,19 @@ func tryStep() -> Int { // HW ANSWER func stepUp() { - if tryStep() == 1 { - // We’re done! + + switch tryStep() { + case 1: return + case 0: + stepUp() + case -1: + stepUp() + stepUp() + default: + stepUp() + } - // Now we’re two steps below where we want to be :-( - stepUp() - stepUp() } diff --git a/HW(Recursion)_final.playground/timeline.xctimeline b/HW(Recursion)_final.playground/timeline.xctimeline index bf468af..fc3b703 100644 --- a/HW(Recursion)_final.playground/timeline.xctimeline +++ b/HW(Recursion)_final.playground/timeline.xctimeline @@ -2,5 +2,10 @@ + + diff --git a/Recursion/Recursion/main.swift b/Recursion/Recursion/main.swift index 9bcc5da..e9ecdf9 100644 --- a/Recursion/Recursion/main.swift +++ b/Recursion/Recursion/main.swift @@ -21,18 +21,18 @@ func findFile(name: String, atPath: String) -> String { let fullPath = atPath + "/" + fileOrDir let exists = fileManager.fileExistsAtPath(fullPath, isDirectory: &isDir) if exists && Bool(isDir) { - - //print("DIR: " + fileOrDir) - findFile(name, atPath: fullPath) + + let result = findFile(name, atPath: fullPath) + if result != "NOT FOUND"{ + return result + } } else if exists { - - if (fileOrDir == name){ -// print("FILE: " + fileOrDir) - print("File is at: \(fullPath)") + + if fileOrDir == name { return fullPath } - + } else { print("NEITHER: " + fileOrDir) } From dddf4dd794236d3c18bc712cd0508bb15da4ec40 Mon Sep 17 00:00:00 2001 From: Henna Date: Fri, 29 Jan 2016 21:00:08 -0500 Subject: [PATCH 17/22] done without for loops --- .../Contents.swift | 39 ++++++++++++ .../contents.xcplayground | 4 ++ .../contents.xcworkspacedata | 7 +++ .../timeline.xctimeline | 61 +++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 HWFrom1-28-16(Merge Sort).playground/Contents.swift create mode 100644 HWFrom1-28-16(Merge Sort).playground/contents.xcplayground create mode 100644 HWFrom1-28-16(Merge Sort).playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 HWFrom1-28-16(Merge Sort).playground/timeline.xctimeline diff --git a/HWFrom1-28-16(Merge Sort).playground/Contents.swift b/HWFrom1-28-16(Merge Sort).playground/Contents.swift new file mode 100644 index 0000000..6f2c577 --- /dev/null +++ b/HWFrom1-28-16(Merge Sort).playground/Contents.swift @@ -0,0 +1,39 @@ +//: Playground - noun: a place where people can play + +import UIKit + + +func insertionSort(var numbers:[Int], index:Int) -> [Int]{ + + if index == numbers.count{ + return numbers + } + else{ + + var temp = numbers[index], + iter = 0, //final index + iter2 = 0, + maxIter = index - 1 + + while (iter <= maxIter && numbers[iter] < temp) { + + iter++ + } + + while iter2<=maxIter{ + numbers[maxIter-iter2+1] = numbers[maxIter-iter2] + iter2++ + } + + + numbers[iter] = temp; + + return insertionSort(numbers, index: index+1) + } + +} + + + + +insertionSort([9, 3, 4, 2, 1], index: 1) \ No newline at end of file diff --git a/HWFrom1-28-16(Merge Sort).playground/contents.xcplayground b/HWFrom1-28-16(Merge Sort).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom1-28-16(Merge Sort).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom1-28-16(Merge Sort).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-28-16(Merge Sort).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-28-16(Merge Sort).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-28-16(Merge Sort).playground/timeline.xctimeline b/HWFrom1-28-16(Merge Sort).playground/timeline.xctimeline new file mode 100644 index 0000000..43b6727 --- /dev/null +++ b/HWFrom1-28-16(Merge Sort).playground/timeline.xctimeline @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + From bae82adccc5d0ab0a57f60df06c6b6fbeac3af8c Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 4 Feb 2016 17:34:51 -0500 Subject: [PATCH 18/22] lists q hw --- .DS_Store | Bin 6148 -> 8196 bytes .../Contents.swift | 237 ++++++++++++++++++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 + .../timeline.xctimeline | 6 + 5 files changed, 254 insertions(+) create mode 100644 HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift create mode 100644 HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground create mode 100644 HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 HWFrom1-30-16(QuickSort+Lists+Queues).playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store index 5711dfdab2e80cd7e3997dea92e8b12d7a6d1204..8687a7d0f91037af8290c821881371bdc1a80e75 100644 GIT binary patch literal 8196 zcmeHMO=}cE5PhvV1ic7)2n6B?9*n_|F(3*#44Y9RD587tXu^KEZ1#ifF5+#-(VKrq z5WITSn}5TDc=T0uJ4~1f47-y>*6z^thVHKF>Q}wpQxgEDHf@H$8h}++!E0A33X1eg z&6P{8gYSHhVBm)wmSHSatRXGMpGWOe|V; z7^@EBW;QNEF*-Z-0v!$$S@hB~;2Fp>Ai4WG4w$hwn7wE7_a5q)V2U>XwlTsn$Gk^} z`TdZs12m62VZAZzkF3#K+p}Bh597Ewoiu>&U)F!Cy%~M#51SQP8okY~?ogcCAWyGx zhu=3^WNS($#^{ob0XFf5EOq&GM4n2pb@yOidjp*}*~!ww!%hIl})>v%^G z+l=W``Z>{2n`M06xJ+Lwvz>|cr=R;mYjw5@IJueQBqS@sjAf-pPC9tO2$qQ2-gSCA zr^U?rKlgs=`scKGcGdcGw9$H!*$MaYFx?5y^($S6GT^Qla@3^U`gcF1(W=09hKX-q zKPnTI`JFijDz{z0_m1weEGN6V%j_O|$+M!X=j&g#xA2TDWAZ-67NozWbzCkEdsoQT zh0mFZ&yUks4i%lqVPcUjEDQe-Ao+`ZJD%~KsNlZ1 Z@O$xWuI2Cl^j)@scdLSU>dstl@ds}P#XSH3 delta 124 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50C<+o_1dF9IBr}vU6fqPtWNs{6 z&dkWM*+g(H)5cOi#>MO$9D>Y1g+L&{4J2Ga25c<+&ODi4#uH>50~5q(kZlZ`<9X&V F0|2e>69fPN diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift new file mode 100644 index 0000000..fceafe8 --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift @@ -0,0 +1,237 @@ +//: Playground - noun: a place where people can play + +import UIKit + +/* +1) + time complexity space complexity +bubble sort O(n^2) O(1) +insertion sort O(n^2) O(1) +selection sort O(n^2) O(1) +merge sort O(nlogn) O(n) +quick sort O(n^2) O(1) +*/ + + +/* 2) What is the advantage of partitioning Quicksort in place? +It has a constant space complexity +*/ + +/* + 3) Without looking, implement quicksort. +*/ + +//func quickSort(inout arr: [Int], first: Int, last: Int) { +// +// if first >= last { return } +// +// let splitPoint = partition(&arr, first: first, last: last) +// +// quickSort(&arr, first: first, last: splitPoint - 1) +// +// quickSort(&arr, first: splitPoint + 1, last: last) +//} +// +//func quickSort(inout arr: [Int]) { +// quickSort(&arr, first: 0, last: arr.count-1) +//} +// +//func partition(inout arr: [Int], first: Int, last: Int) -> Int { +// +// let pivotValue = arr[first] +// var leftMark = first + 1 +// var rightMark = last +// +// while leftMark <= rightMark { +// while arr[leftMark] < pivotValue && leftMark <= rightMark { +// leftMark += 1 +// } +// while arr[rightMark] > pivotValue && leftMark <= rightMark{ +// rightMark -= 1 +// } +// if leftMark < rightMark { +// swap(&arr[leftMark], &arr[rightMark]) +// } +// } +// +// if first != rightMark { +// swap(&arr[first], &arr[rightMark]) +// } +// return rightMark +// +// +//} +// +//var array = [22, 59, 38, 93, 95, 0, 34, 58, 72, 15] +//quickSort(&array) + + +/* 4) Write a function to generate an array of random numbers bounded between 1..<10,000 of size 10,000. + + +Compare the time it takes to run mergesort, quicksort, and quicksort with the median. +https://gist.github.com/gummibeatz/8ff29bcec54d7e3ef683 +*/ +// +//func quickSort(inout arr: [Int], firstIdx: Int, lastIdx: Int, medianPivot: Bool) { +// // base case +// if firstIdx >= lastIdx { return } +// // partition +// let splitPoint = partition(&arr, firstIdx: firstIdx, lastIdx: lastIdx, medianPivot: medianPivot) +// // quickSort on leftHalf +// quickSort(&arr, firstIdx: firstIdx, lastIdx: splitPoint - 1, medianPivot: medianPivot) +// // quickSort on rightHalf +// quickSort(&arr, firstIdx: splitPoint + 1, lastIdx: lastIdx, medianPivot: medianPivot) +//} +// +//func quickSort(inout arr: [Int], medianPivot: Bool) { +// quickSort(&arr, firstIdx: 0, lastIdx: arr.count-1, medianPivot: medianPivot) +//} +// +//func partition(inout arr: [Int], firstIdx: Int, lastIdx: Int, medianPivot: Bool) -> Int { +// // set pivotValue to firstElement +// var pivotValue = 0 +// if medianPivot { +// var pivotValues:[Int] = [] +// pivotValues.append(arr[0]) +// pivotValues.append(arr[arr.count-1]) +// pivotValues.append(arr[arr.count/2]) +// let maximum = pivotValues.maxElement() +// let minimum = pivotValues.minElement() +// +// for number in pivotValues{ +// if number != maximum && number != minimum{ +// pivotValue = number +// } +// } +// } +// else{ +// pivotValue = arr[firstIdx] +// } +// +// // set leftMark +// var leftMark = firstIdx + 1 +// // set rightMark +// var rightMark = lastIdx +// /* +// as leftMark and rightMark close in on each other, +// swap the items that are greater than the pivot value +// on the left side with the items that are less than the pivot +// value on the right side. Stop when rightMark crosses leftMark +// */ +// while leftMark <= rightMark { +// while arr[leftMark] < pivotValue && leftMark <= rightMark { +// leftMark += 1 +// } +// while arr[rightMark] > pivotValue && leftMark <= rightMark{ +// rightMark -= 1 +// } +// if leftMark < rightMark { +// swap(&arr[leftMark], &arr[rightMark]) +// } +// } +// // set the correct value at the splitPoint +// if firstIdx != rightMark { +// swap(&arr[firstIdx], &arr[rightMark]) +// } +// return rightMark // return the splitPoint +// +// +//} +//func mergeSort(var unsortedArray:Array)->Array{ +// if unsortedArray.count < 2 { +// return unsortedArray +// } +// +// let pivot:Int = unsortedArray.count/2 +// let leftArray:Array = Array(unsortedArray[0.. = Array(unsortedArray[pivot..,B:Array)->Array{ +// var leftIndex = 0 +// var rightIndex = 0 +// var orderedArray:Array = [] +// while leftIndex B[rightIndex] { +// orderedArray.append(B[rightIndex++]) +// } +// else { +// orderedArray.append(A[leftIndex++]) +// orderedArray.append(B[rightIndex++]) +// } +// } +// orderedArray = orderedArray + Array(A[leftIndex..())) { +// let start = NSDate() +// block() +// let end = NSDate() +// print(end.timeIntervalSinceDate(start)) +//} +// +////run your function/code inside the block below +//profile({ +// //quickSort(&numbersArray) //0.00165402889251709 +// //quickSort(&numbersArray, medianPivot: true) //0.00196903944015503 +// //mergeSort(numbersArray) +// print("hi") +// +//}) + + +/* 5) In quicksort, the elements are sorted while the array is being split. In mergesort, the elements are placed in order when we merge two sorted lists. +*/ + + +/* 6) Given an array of strings containing “[“,”]”,”{“,”}”,”(“,”)”. Output whether or not the parentheses are balanced. + Good examples: () [] () ([]()[]) + Bad examples: ( (] ([)] +*/ + +func isBal(inout str: [String]) -> Bool { + var container = [String]() + + for char in str{ + if char == "(" { + container.append(char) + } + else if char == ")" && container.contains("("){ + container.removeAtIndex(container.indexOf("(")!) + } + + else if char == "[" { + container.append(char) + } + else if char == "]" && container.contains("["){ + container.removeAtIndex(container.indexOf("[")!) + } + } + + return container.isEmpty + + +} + +var arr = ["(", ")", "[", "]", "(", ")", "(", "[", "]", "(", ")", "[", "]", ")"] + +isBal(&arr) + diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/timeline.xctimeline b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From 808bf4c0961f6a467fecae206f5ac5625f0a17b4 Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 4 Feb 2016 18:18:07 -0500 Subject: [PATCH 19/22] hash sets hw --- .../Contents.swift | 103 ++++++++++++++++++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 ++ .../timeline.xctimeline | 6 + 4 files changed, 120 insertions(+) create mode 100644 HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift create mode 100644 HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground create mode 100644 HWFrom1-31-16(Sets and HashMaps).playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 HWFrom1-31-16(Sets and HashMaps).playground/timeline.xctimeline diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift new file mode 100644 index 0000000..0f5e88f --- /dev/null +++ b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift @@ -0,0 +1,103 @@ +import UIKit + +//1) Write good hash functions for the following data types. Justify your choices and how they avoid collisions. + + +// a) Int + +func hash(x: Int) -> Int { + return x +} + +// this would ensure that there is a unique list of elements that are hashed to their own indexes. + + + +//b) +//struct Point: Hashable { +// let x, y: Int +//} +// +//func hash(point: Point) -> Int { +// +// return (x + y)(x + y)/2 +//} + + + +// c) Array +//sum of all elements in the array + +/* 2) +You moderate a popular mobile device discussion forum. You want to cut down on the vitriol and make your work easier, so you decide to implement a blacklist based system to automatically reject or approve posts. For example: + +moderate("I would never use a crApple product!") // false (reject) +moderate("I wish all these FANDROIDS would just shut up!") // false +moderate("M$ is the worst, Linux rules!") // false +moderate("Can’t we all just get along?") // true (approve) + +Write moderate(message: String) -> Bool, using a built-in Swift Set to manage your blacklist. Make your method case insensitive; it should block the word no matter what combination of upper and lowercase letters is used. + +*/ + +func moderate(input: String) -> Bool { + let badWords = Set(["crapple", "fandroids", "worst", "m$", "shut up"]) + + let cin = Set(input.lowercaseString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "!, "))) + + let containsBadWords = badWords.intersect(cin) + + + return containsBadWords.isEmpty + +} + +moderate("I would never use a crAPPLE product!") +moderate("Can’t we all just get along?") + +//3) + +protocol PhoneBookProtocol { + mutating func addPerson(name: String, phoneNumber: String) + mutating func removePerson(name: String) + mutating func importFrom(oldPhonebook: [(String, String)]) + func findPerson(name: String) -> String // Return phone # +} + +class PhoneBook: PhoneBookProtocol { + var phoneBook = [String: String]() + + func addPerson(name: String, phoneNumber: String) { + phoneBook[name] = phoneNumber + } + + func removePerson(name: String) { + phoneBook.removeValueForKey(name) + } + + func importFrom(oldPhonebook: [(String, String)]) { + for i in 0.. String { + + if ((phoneBook[name]) != nil){ + return phoneBook[name]! + } + else{ + return "not found" + } + + } +} + +var phoneBook = PhoneBook() + +phoneBook.addPerson("Mike", phoneNumber: "21233444") +phoneBook.addPerson("Michael", phoneNumber: "83333840") +phoneBook.addPerson("Jil", phoneNumber: "98397103") +phoneBook.findPerson("Jil") +phoneBook.importFrom([("Henna", "23840278")]) +phoneBook.removePerson("Michael") \ No newline at end of file diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground b/HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-31-16(Sets and HashMaps).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-31-16(Sets and HashMaps).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/timeline.xctimeline b/HWFrom1-31-16(Sets and HashMaps).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWFrom1-31-16(Sets and HashMaps).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From fbe5d5b1267049ed31aa8f5077434e11e4b3e7b8 Mon Sep 17 00:00:00 2001 From: Henna Date: Wed, 10 Feb 2016 15:52:19 -0500 Subject: [PATCH 20/22] hw thats due 2-6 submission --- Trees.playground/Contents.swift | 60 +++++++++++++++++++ Trees.playground/contents.xcplayground | 4 ++ .../contents.xcworkspacedata | 7 +++ Trees.playground/timeline.xctimeline | 11 ++++ 4 files changed, 82 insertions(+) create mode 100644 Trees.playground/Contents.swift create mode 100644 Trees.playground/contents.xcplayground create mode 100644 Trees.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 Trees.playground/timeline.xctimeline diff --git a/Trees.playground/Contents.swift b/Trees.playground/Contents.swift new file mode 100644 index 0000000..c90fa86 --- /dev/null +++ b/Trees.playground/Contents.swift @@ -0,0 +1,60 @@ +//: Playground - noun: a place where people can play + +import UIKit + +//https://docs.google.com/document/d/1te7mLS06MEYwETFSbVBqMrIzJ43GTEo5uuCiWdB0fyE/edit?usp=drivesdk + +class Node { + let value: T + var left: Node? + var right: Node? + init(value: T) { + self.value = value + } +} + +extension Node: CustomStringConvertible { + var description: String { + return "\(value)" + } + var postorderDescription: String { + let lt = left?.postorderDescription ?? "" + let rt = right?.postorderDescription ?? "" + return lt + rt + description + } +} + +//1 + +func parseExpression(input: String) -> Node? { + // Your implementation here! + let operators: Set = ["+", "-", "*", "/"] + var stack: [Node] = [] + for character in input.characters { + if operators.contains(character){ + let new : Node = Node(value: character) + new.left = stack.removeFirst() + new.right = stack.removeFirst() + + stack.append(new) + } + else{ + let new : Node = Node(value: character) + new.left = nil + new.right = nil + stack.append(new) + + } + } + let node = stack.removeFirst(); + return node; +} + +parseExpression("ab+cde+**") + + + +//Bonus1 + + +//Bonus2 diff --git a/Trees.playground/contents.xcplayground b/Trees.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/Trees.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Trees.playground/playground.xcworkspace/contents.xcworkspacedata b/Trees.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Trees.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Trees.playground/timeline.xctimeline b/Trees.playground/timeline.xctimeline new file mode 100644 index 0000000..c174415 --- /dev/null +++ b/Trees.playground/timeline.xctimeline @@ -0,0 +1,11 @@ + + + + + + + From fb0bb333609e43af15ce78f484d7703d81811e91 Mon Sep 17 00:00:00 2001 From: Henna Date: Wed, 10 Feb 2016 18:45:22 -0500 Subject: [PATCH 21/22] graphs hw submission --- graphs.playground/Contents.swift | 37 +++++++++++++++++++ graphs.playground/contents.xcplayground | 4 ++ .../contents.xcworkspacedata | 7 ++++ graphs.playground/timeline.xctimeline | 6 +++ 4 files changed, 54 insertions(+) create mode 100644 graphs.playground/Contents.swift create mode 100644 graphs.playground/contents.xcplayground create mode 100644 graphs.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 graphs.playground/timeline.xctimeline diff --git a/graphs.playground/Contents.swift b/graphs.playground/Contents.swift new file mode 100644 index 0000000..87cc59f --- /dev/null +++ b/graphs.playground/Contents.swift @@ -0,0 +1,37 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" + +// 1. Yes, CBAFDEGFCGHC. Every edge is visited once in this path. +// 2. No +//3 + +/* + + A B C D E F +A 0 1 0 1 0 1 +B 1 0 0 0 1 0 +C 0 0 0 1 0 0 +D 1 0 1 0 1 1 +E 0 1 0 1 0 0 +F 1 0 0 1 0 0 + + +*/ + +//4 + +/* + + A B C D E F +A 0 1 0 0 0 1 +B 0 0 0 0 1 0 +C 0 0 0 0 0 0 +D 0 0 1 0 0 0 +E 0 0 0 1 0 0 +F 0 0 0 1 0 0 + + +*/ \ No newline at end of file diff --git a/graphs.playground/contents.xcplayground b/graphs.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/graphs.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/graphs.playground/playground.xcworkspace/contents.xcworkspacedata b/graphs.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/graphs.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/graphs.playground/timeline.xctimeline b/graphs.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/graphs.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From ad60f0533967793346bad0d16219f3309bc39408 Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 11 Feb 2016 21:40:48 -0500 Subject: [PATCH 22/22] trees --- Trees.playground/Contents.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Trees.playground/Contents.swift b/Trees.playground/Contents.swift index c90fa86..36bdf2d 100644 --- a/Trees.playground/Contents.swift +++ b/Trees.playground/Contents.swift @@ -50,7 +50,8 @@ func parseExpression(input: String) -> Node? { return node; } -parseExpression("ab+cde+**") +var node = parseExpression("ab+cde+**") +print(node?.postorderDescription)