From 23ca577502835ba6f132f285c228e77f83062d4e Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Wed, 13 Jan 2016 15:15:20 -0500 Subject: [PATCH 01/15] Finished swift intro, still working on big O --- .../Contents.swift | 57 ++++++++++++++++++- .../Contents.swift | 4 +- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..de96e2e 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -12,12 +12,65 @@ https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/ 1) + func findMissingNumber(n : Int, list : [Int]) -> Int + { + let totalSum = n*(n+1)/2 + var totalSumOfList = 0 + + for i in list + { + totalSumOfList += i + } + return totalSum - totalSumOfList + } + +2) + func findDuplicates(list : [Int]) ->Bool + { + let set = Set(list) + if (set.count != list.count) + { + return true + } + else + { + return false + } + } + +3) + func findSmallesValue(list1 : [Int], list2 : [Int]) -> Int + { + let set1 = Set(list1) + let set2 = Set(list2) + + let intersectionArray = Array(set1.intersect(set2)) + + if(intersectionArray.isEmpty) + { + return -1 + } + + return intersectionArray.minElement()! + } -2) -3) 4) + func checkIfIntegerIsPalindrome(var number : Int) -> Bool + { + let originalNum = number + var finalNum = 0 + while(number > 0) + { + finalNum = finalNum * 10 + number % 10 + number /= 10 + } + return originalNum == finalNum + } + + + */ diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..43fdeb6 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -11,9 +11,9 @@ 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) O(1000 * 2000) = O(2000000) = O(1) , and if n by m O(nm) -2) +2) O(n^2) 3) From 9fbd8cda73555c327ff2f1e3681cdd64f1441418 Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Thu, 14 Jan 2016 00:41:51 -0500 Subject: [PATCH 02/15] Q1 and Q2 for Big O practice --- HWfrom1-10-016(BigO).playground/Contents.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 43fdeb6..2fe9225 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -11,10 +11,18 @@ 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) O(1000 * 2000) = O(2000000) = O(1) , and if n by m O(nm) +1)a. (1 + 3 + 10) * 1000 + (1 + 3 + 10) * 2000 + 200 * (1000 * 2000) + 14,000ps + 28,000ps + 400000000ps = 400042000ps -2) O(n^2) + b. O(n^2) + c. O(n^4) + +2)a. O(n^3) + O(n^4) + O(n^2) = O(n^4) + b. frobnicate(xs, n) = xs[n] + xs[n-1] + xs[n-2] + ... + xs[1] + 0, so for n we make n+1 calls to frobnicate so it's 0(n) + + c. O(n) + O(nlog n) + O(n) = O(nlog n) + d. O(log n) 3) 4) From b47bf1f8c673d559da8442c6981eac11a68c7f97 Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Thu, 14 Jan 2016 00:58:40 -0500 Subject: [PATCH 03/15] Q3 --- HWfrom1-10-016(BigO).playground/Contents.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2fe9225..df0a40e 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -21,9 +21,12 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 2)a. O(n^3) + O(n^4) + O(n^2) = O(n^4) b. frobnicate(xs, n) = xs[n] + xs[n-1] + xs[n-2] + ... + xs[1] + 0, so for n we make n+1 calls to frobnicate so it's 0(n) - c. O(n) + O(nlog n) + O(n) = O(nlog n) + c. O(n^2) + O(nlog n) + O(n) = O(n^2) d. O(log n) 3) + a. Hash Table + b. A Stack, Singly-Linked List, Doubly Linked List, or a Hash Table + c 4) From c45f488adb5288894592cbe1ab390e52bd36f7e1 Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Thu, 14 Jan 2016 02:01:35 -0500 Subject: [PATCH 04/15] Q4, Q5, Q6 --- .../Contents.swift | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index df0a40e..ccb105c 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -29,11 +29,61 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ c 4) - + func factorial(num : Int) -> Int { + + if(num == 0 || num == 1) + { + return 1 + } + return num * factorial(num - 1) + } + Time complexity O(n!) 5) + func multiplication(num1 : Int, num2 : Int) -> Int { + var product = 0 + for _ in 1...num2 + { + product += num1 + } + return product + } + +multiplication(8, num2: 5) = 40 +multiplication(6, num2: 4) = 24 + +Time Complexity O(num2) which is still constant so O(1) + + + 6) +func russian_peasant_multiplication(var left_num : Int, var right_num : Int) -> Int { + +var product = 0 +if(left_num % 2 != 0) +{ +product += right_num +} +while(left_num > 1) +{ +left_num /= 2 +right_num *= 2 + +if( left_num % 2 != 0) +{ +product += right_num +} +} +return product +} + +russian_peasant_multiplication(36, right_num: 12) +russian_peasant_multiplication(17, right_num: 13) +russian_peasant_multiplication(2, right_num: 3) + +Time Complexity : 0(1) + 7) From ceb8bc75e8b46ac3d44229087cbdb18aed03fabe Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Thu, 14 Jan 2016 02:26:33 -0500 Subject: [PATCH 05/15] Left with Q7 --- HWfrom1-10-016(BigO).playground/Contents.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index ccb105c..78b21b0 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -26,7 +26,7 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 3) a. Hash Table b. A Stack, Singly-Linked List, Doubly Linked List, or a Hash Table - c + c. A Skip List, HashTable or BST 4) func factorial(num : Int) -> Int { @@ -78,9 +78,9 @@ product += right_num return product } -russian_peasant_multiplication(36, right_num: 12) -russian_peasant_multiplication(17, right_num: 13) -russian_peasant_multiplication(2, right_num: 3) +russian_peasant_multiplication(36, right_num: 12) = 432 +russian_peasant_multiplication(17, right_num: 13) = 221 +russian_peasant_multiplication(2, right_num: 3) = 6 Time Complexity : 0(1) From 407c4fdd33f648a23ac8f29a36963f86af3761c7 Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Thu, 14 Jan 2016 02:35:37 -0500 Subject: [PATCH 06/15] hw --- HWfrom1-10-016(BigO).playground/Contents.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 78b21b0..190b7ab 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -37,7 +37,7 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ } return num * factorial(num - 1) } - Time complexity O(n!) + Time complexity O(n) 5) func multiplication(num1 : Int, num2 : Int) -> Int { From ab6a839b0a019390be5cbd5a34ee31cd35b7cb78 Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Fri, 15 Jan 2016 17:15:39 -0500 Subject: [PATCH 07/15] commit --- HWfrom1-10-016(BigO).playground/Contents.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 190b7ab..56ab3af 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -82,7 +82,7 @@ russian_peasant_multiplication(36, right_num: 12) = 432 russian_peasant_multiplication(17, right_num: 13) = 221 russian_peasant_multiplication(2, right_num: 3) = 6 -Time Complexity : 0(1) +Time Complexity : 0(log n) 7) From e5fa0f5fe6dc7f7b4ee2d5f4c7ee275a784dbad0 Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Sat, 16 Jan 2016 00:37:41 -0500 Subject: [PATCH 08/15] Logic and Discrete Math homework --- .../Contents.swift | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..0e9ff69 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -7,22 +7,72 @@ var str = "Hello, playground" /* -Question 1: https://www.hackerrank.com/challenges/minimum-draws +Question 1: + +https://www.hackerrank.com/challenges/minimum-draws + Copy and paste your code: +let firstLine = Int(readLine(stripNewline: true)!)! +for _ in 0.. Date: Mon, 18 Jan 2016 22:31:31 -0500 Subject: [PATCH 09/15] HW --- HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index 0e9ff69..026ecff 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -77,3 +77,6 @@ What is the big O runtime of your code?: */ + + + From 3833b04c18ca4ffd6081796a92b04c56753b20f1 Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Wed, 20 Jan 2016 14:58:09 -0500 Subject: [PATCH 10/15] Q1 and Q2 --- .../Contents.swift | 156 +++++++++++++++++- 1 file changed, 152 insertions(+), 4 deletions(-) diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index 5d51051..302982a 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -13,9 +13,157 @@ Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB 1) - - -2) +func getValidNumbers(sudokuBoard: [[Int]], row: Int, col: Int) -> [Int] { + +var notValidNumsArr = [Int]() +//loop thru cols picking up non-empty values, row is held constant +for j in 0.. [Int] { +var coordinatesArr = [Int]() +if row <= (sudokuBoard.count/3) - 1 { +coordinatesArr.append(0) +coordinatesArr.append( (sudokuBoard.count/3) - 1 ) +} +else if row >= sudokuBoard.count/3 && row <= (sudokuBoard.count/3) + 2 { +coordinatesArr.append(sudokuBoard.count/3) +coordinatesArr.append((sudokuBoard.count/3) + 2 ) +} +else { +coordinatesArr.append((sudokuBoard.count/3) + 3) +coordinatesArr.append(sudokuBoard.count - 1) +} +return coordinatesArr +} + +func getNumbersSet() -> Set +{ +var numbers = Set() + +numbers.insert(1) +numbers.insert(2) +numbers.insert(3) +numbers.insert(4) +numbers.insert(5) +numbers.insert(6) +numbers.insert(7) +numbers.insert(8) +numbers.insert(9) + +return numbers +} + +//Takes in a column and outputs the starting and ending coordinates +//for the columns that make up the sqaure containing the given column +func getSquareColumnRowCoordinates(sudokuBoard: [[Int]],col: Int) -> [Int] { +var coordinatesArr = [Int]() +if col <= (sudokuBoard.count/3) - 1 { +coordinatesArr.append(0) +coordinatesArr.append( (sudokuBoard.count/3) - 1 ) +} +else if col >= sudokuBoard.count/3 && col <= (sudokuBoard.count/3) + 2 { +coordinatesArr.append(sudokuBoard.count/3) +coordinatesArr.append((sudokuBoard.count/3) + 2 ) +} +else { +coordinatesArr.append((sudokuBoard.count/3) + 3) +coordinatesArr.append(sudokuBoard.count - 1) +} +return coordinatesArr +} + +var sudokuBoard = [ +[5, 0, 8, 0, 7, 3, 1, 9, 0], +[9, 0, 0, 6, 0, 0, 4, 0, 8], +[0, 0, 0, 9, 0, 8, 0, 3, 5], +[0, 7, 0, 0, 0, 0, 0, 6, 0], +[0, 0, 2, 0, 0, 0, 9, 0, 0], +[0, 1, 0, 0, 0, 0, 0, 8, 0], +[1, 9, 0, 3, 0, 6, 0, 0, 0], +[2, 0, 3, 0, 0, 7, 0, 0, 9], +[0, 8, 7, 1, 9, 0, 3, 0, 4]] + +getValidNumbers(sudokuBoard, row: 5, col: 5) -> [2, 4, 9, 5] +getValidNumbers(sudokuBoard, row: 4, col: 4) -> [4, 5, 6, 3, 1, 8] +getValidNumbers(sudokuBoard, row: 8, col: 7) -> [2, 5] + + +2) + + +func rotateMatrixBy90(matrix: [[Int]]) ->[[Int]] { + +var rotatedMatrix = [[Int]](count: matrix.count, repeatedValue: matrix[0]) + + +for j in 0.. [[3, 9, 5, 1], [4, 0, 6, 2], [5, 1, 7, 3], [6, 2, 8, 4]] +print( (rotateMatrixBy90(sudokuBoard))) -> [[0, 2, 1, 0, 0, 0, 0, 9, 5], [8, 0, 9, 1, 0, 7, 0, 0, 0], [7, 3, 0, 0, 2, 0, 0, 0, 8], [1, 0, 3, 0, 0, 0, 9, 6, 0], [9, 0, 0, 0, 0, 0, 0, 0, 7], [0, 7, 6, 0, 0, 0, 8, 0, 3], [3, 0, 0, 0, 9, 0, 0, 4, 1], [0, 0, 0, 8, 0, 6, 3, 0, 9], [4, 9, 0, 0, 0, 0, 5, 8, 0]] @@ -23,4 +171,4 @@ Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB -*/ \ No newline at end of file +*/ From 362c56f9f972d8eb422b8695ffb0153ffb382d20 Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Wed, 20 Jan 2016 15:36:18 -0500 Subject: [PATCH 11/15] Q3 --- .../Contents.swift | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index 302982a..62f2c69 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -170,5 +170,44 @@ print( (rotateMatrixBy90(sudokuBoard))) -> [[0, 2, 1, 0, 0, 0, 0, 9, 5], [8, 0, 3) +//Optimal Algorithm to sort four elements A,B,C,D +func sort(unsorted: [Int]) ->[Int] { + +let mid = unsorted.count/2 +var sorted = [Int]() + +let leftMin = min(unsorted[0],unsorted[mid-1]) +let leftMax = max(unsorted[0],unsorted[mid-1]) + +let rightMin = min(unsorted[mid],unsorted[unsorted.count-1]) +let rightMax = max(unsorted[mid],unsorted[unsorted.count-1]) + +let absoulteMin = min(leftMin, rightMin) +let absoulteMax = max(leftMax,rightMax) + +if(leftMin == absoulteMin) +{ +sorted.append(leftMin) +sorted.append(rightMin) +} +else { +sorted.append(rightMin) +sorted.append(leftMin) +} +if(leftMax == absoulteMax){ +sorted.insert(rightMax, atIndex: mid) +sorted.append(leftMax) +} +else { +sorted.insert(leftMax, atIndex: mid) +sorted.append(rightMax) +} +return sorted +} +sort([3,1,4,0]) -> [0,1,3,4] +sort([2,2,2,2]) -> [2,2,2,2] +sort([1,6,0,2]) -> [0,1,2,6] + + */ From 7fa701189abd3163bcfee38b0871393b58f7d34d Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Wed, 20 Jan 2016 15:37:41 -0500 Subject: [PATCH 12/15] added helper min and max functions --- .../Contents.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index 62f2c69..e3e25a4 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -169,6 +169,23 @@ print( (rotateMatrixBy90(sudokuBoard))) -> [[0, 2, 1, 0, 0, 0, 0, 9, 5], [8, 0, 3) +func min(first : Int,second: Int)->Int { +if first <= second { +return first +} +else { +return second +} +} + +func max(first : Int,second: Int)->Int { +if first > second { +return first +} +else { +return second +} +} //Optimal Algorithm to sort four elements A,B,C,D func sort(unsorted: [Int]) ->[Int] { From cc2883a081852b7784164f663345f934e6a5eac2 Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Mon, 25 Jan 2016 22:42:47 -0500 Subject: [PATCH 13/15] Caleb's Recursion HW Q1 & Q2 --- .../Contents.swift | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/HWfrom1-23-16(Recursion).playground/Contents.swift b/HWfrom1-23-16(Recursion).playground/Contents.swift index 4776cab..bcecd2d 100644 --- a/HWfrom1-23-16(Recursion).playground/Contents.swift +++ b/HWfrom1-23-16(Recursion).playground/Contents.swift @@ -5,16 +5,44 @@ import UIKit var str = "Hello, playground" //https://docs.google.com/document/d/1KfnTOtPnBrYPFhBRAQPZBXor_mKDQvuJp4zwZbtHkRs/edit#heading=h.16sfqfmanxte - - +/* +//Caleb's Homework //1 +Write an iterative (not recursive) fibonacci function that calculates the nth fibonacci number. +func fibnoacci(n: Int) -> Int { + var result = [1,1] + if n < 2 { + return n + } + for i in 2...n { + result.insert(result[i-1] + result[i-2], atIndex: i) + } + return result[n] +} + +The iterative version is much better than the non-memoized recursive function. For example, calculating fib(10) takes 88 calls to the recursive case where as it takes only 9 calls to the iterative segment of the function. Therefore, the iterative function is much more efficient //2 +The engineers have been hard at work on the clumsy robot project, and have released a new API with a new tryStep method (see Appendix B). Now it returns an Int, which is -1 if the robot fell down a step, 0 if the robot stayed on the same step, or 1 if the robot went to the next step. Write a new stepUp method using this new tryStep method that works the same as before. + +func stepUp(){ +if tryStep() == 1 { +return +} +if tryStep() == -1 { +stepUp() +stepUp() +} +else{ +stepUp() +} +} +//3 -//3 \ No newline at end of file +*/ \ No newline at end of file From 367aa44c351412edd8b8c5806b2c36466de88df1 Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Thu, 4 Feb 2016 16:40:16 -0500 Subject: [PATCH 14/15] hw --- .../Contents.swift | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift index bcf8eda..3822d29 100644 --- a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift @@ -3,7 +3,14 @@ //Answer the questions in the homework below //https://docs.google.com/document/d/1KlK3PmRMybmHS5db_AP11uHXIcbIeM5vHuuAZOV3xME/edit# -//1) +//1) Without looking at the Big O Cheatsheet, write down the average time and space complexity for bubble sort, insertion sort, selection sort, mergesort, and quicksort. + + // Time Complexity Space Complexity +//Bubble Sort O(n^2) O(1) +//Insertion Sort O(n^2) O +//Merge Sort O(nlogn) O(n) +//Selection Sort O(n^2) O(1) +//Quick Sort O(nlogn), worst case O(n^2) O(1) //2) @@ -13,4 +20,48 @@ //5) -//6) \ No newline at end of file +//6)Given an array of strings containing “[“,”]”,”{“,”}”,”(“,”)”. Output whether or not the parentheses are balanced. +//Good examples: () [] () ([]()[]) +//Bad examples: ( ( ] ([)] + +//func isBalanced(paren: [String]) -> Bool { +// let items :[String] = [] +// var stack : Stack = Stack(items: items) +// if stack.size() == 0 { +// if paren[0] == ")" || paren[0] == "]" { +// return false +// } +// } +// for str in paren { +// if str == "(" || str == "[" { +// stack.push(str) +// } +// else if str == ")" { +// if stack.peek() == "(" { +// stack.pop() +// continue +// } +// else{return false} +// } +// else if str == "]" { +// if stack.peek() == "[" { +// stack.pop() +// continue +// } +// else {return false} +// } +// } +// return stack.size() == 0 +//} +//let inputArr = ["(",")"] +//isBalanced(inputArr) +// +//let inputArr2 = ["(", "(","]","(","[",")","]"] +//isBalanced(inputArr2) +// +//let inputArr3 = ["(",")","[","]","(",")", "(","[","]",")","(",")","[","]"] +// +//isBalanced(inputArr3) +// +//let inputArr4 = ["[","(","(","(",")",")",")","]","]"] +//isBalanced(inputArr4) From 7b6c7204d3393fb8670b3e31c7746f6fa7474d50 Mon Sep 17 00:00:00 2001 From: Mesfin Bekele Mekonnen Date: Tue, 9 Feb 2016 21:05:59 -0500 Subject: [PATCH 15/15] hw --- .../Contents.swift | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift index c5841b0..5fb5712 100644 --- a/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift +++ b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift @@ -17,4 +17,64 @@ -//3) \ No newline at end of file +//3) + +let blacklist: Set = ["crapple", "fandroid", "m$"] + +func moderate(message: String) -> Bool { + + let words = message.componentsSeparatedByString(" ") + + for word in words { + if blacklist.contains(word.lowercaseString) { + return false + } + } + return true +} + +moderate("I would never use a crApple product!") + + +/************************************************/ + + //Q3 + +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 storage: [String : String] = [:] // @{} + //var storage = Dictionary() + + func addPerson(name: String, phoneNumber: String) { + storage[name] = phoneNumber + } + + func removePerson(name: String) { + storage.removeValueForKey(name) + } + + func findPerson(name: String) -> String? { + return storage[name] + } + + func importFrom(oldPhonebook: [(String, String)]) { + for entry in oldPhonebook { + addPerson(entry.0, phoneNumber: entry.1) + } + } +} + + +let oldData = [("Caleb", "501-555-1234"), ("Mike", "212-555-4321"), ("Jenny", "345-867-5309")] + +let phoneBook = PhoneBook() +phoneBook.importFrom(oldData) + +phoneBook.findPerson("Jenny") +