From c64aa2f539216faa6075d885d85c78c4f0a72c7f Mon Sep 17 00:00:00 2001 From: Shena Yoshida Date: Wed, 13 Jan 2016 17:13:34 -0500 Subject: [PATCH 01/21] completed swift exercises --- .DS_Store | Bin 0 -> 8196 bytes .../Contents.swift | 69 ++++++++++++++++-- 2 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..08bc681e69a8243d51029cb2c7a94da514349b0e GIT binary patch literal 8196 zcmeI1K}!Nb6vzLQLPD30o@ybAIv6cc)@>1kI(6_6-AtD>&{j%`AYwn5pQ2CH|IG}d z?0P6glHS0)|1vXg_W9d!$2S5nwNV@bWdJ2s(d{jooFe^FGiAfKh@YW9kf4tc6`bHQ zGg=#zfD%vwN9PmoU5!sMjB{syAj7GV*VL(VhjH#O{?5i{D8}BMet{hh6FF6@ z1e8FQfb9KMT;mRpc$vxHE6n82afk-q@WkFBZtzSiqmFQl0eX1AJ36*y#J-rVuV^v< zZ{dQzF|AykvE_lIPH~SeM!inhXea%F3ufTQHA-NO2ozkbW%&(oHUIp7jXsfvssxn4PYIY} zy-}~x%jeb^f6282)+VbI8J9Y>E;KeBr`mL!I{Cwp*8zu$PvkXq$`%$q2(TE`D1ns{ F_y$Od(&qpG literal 0 HcmV?d00001 diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..71340ab 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -5,19 +5,74 @@ 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. - https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/edit +*/ -1) +// * VERY EXCITING DISCOVERY * +// Here are some other helpful functions that can be performed on two sets: +let array1 = ["a", "b", "c"] +let array2 = ["a", "b", "d"] -2) +let set1 = Set(array1) // create set +let set2 = Set(array2) -3) +set1.union(set2) // {"a", "b", "c", "d"} +set1.intersect(set2) // {"a", "b"} +set1.subtract(set2) // {"c"} +set1.exclusiveOr(set2) // {"c", "d"} -4) -*/ +// 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 findMissingValue(list1: [Int], list2: [Int]) -> Int? { + let set1 = Set(list1) // create set, remove duplicates + let set2 = Set(list2) + let matches = set1.exclusiveOr(set2) // gather items that aren't in both arrays + return matches.minElement() //return the smallest one +} +findMissingValue([1, 2, 4, 5, 6, 7, ], list2: [1, 2, 3, 4, 5, 6, 7]) + + + +// 2) Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not. +func hasDuplicates(arr: [Int]) -> Bool { + + let noDoublesSet = Set(arr) // convert array into a set (this removes duplicates) + if noDoublesSet.count != arr.count { + return true + } + return false +} +hasDuplicates([5, 3, 2, 1, 1]) // test it! + + + +// 3) Given two lists, find the smallest value that exists in both lists. +// create two sets and intersect them to get common elements. +func smallestCommonNum(list1: [Int], list2: [Int]) -> Int? { + let set1 = Set(list1) // create set, remove duplicates + let set2 = Set(list2) + return set1.intersect(set2).minElement() // intersect() pulls common numbers from two sets! +} +smallestCommonNum([1, 2, 5, 9], list2: [9, 20, 5]) // test it + + + +// 4) Check to see if an integer is a palindrome don’t use casting. +// divide each digit and rebuild number again then compare with original: +func isPalindrome(var num: Int) -> Bool { + let originalNum = num + var finalNum = 0 + + while(num > 0) { + finalNum *= 10 + finalNum += num % 10 + num /= 10 + } + return finalNum == originalNum // return true if is palindrome +} +isPalindrome(134543) // test it + + From ee183d3420905bc9e0d6703cba8738cc2024ca3b Mon Sep 17 00:00:00 2001 From: Shena Yoshida Date: Thu, 14 Jan 2016 11:48:52 -0500 Subject: [PATCH 02/21] started on big o questions --- .DS_Store | Bin 8196 -> 6148 bytes .../Contents.swift | 92 +++++++++++++++++- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/.DS_Store b/.DS_Store index 08bc681e69a8243d51029cb2c7a94da514349b0e..3f3da67b8e97d3293e833d647d363c8a9026a64c 100644 GIT binary patch delta 115 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jG@dU^g=(=VSrFqRn*zcbJ&u zohOL|FeXeaj9Sdj!6C>DR15?H+(5z=q;F&4cjn3bGM*q)7?>a?f-GU!9M3a{833j| B6Nvx- delta 132 zcmZoMXmOBWU|?W$DortDU;r^WfEYvza8E20o2aMA$h|ROH}hr%jz7$c**Q2SHn1>q zPZnS;+FZwShlz< int i = 0 (store data +10); i (memory acess +1) < (math operation +3) width (memory +1); i (memory +1) ++ (math +3). +total for this line of code = 10 + 1 + 3 + 1 + 1 + 3 = 19 picoseconds + +N = 1000 +1000 * 19 = 19000 = time for this line? + +for (int j = 0; j < height; j++) { +go through the line -> int j = 0 (store data +10); j (memory access +1) < (math op +3) height (memory access +1); j (memory +1) ++ (math +3). +total of line = 10 + 1 + 3 + 1 + 1 + 3 = 19 picoseconds + +M = 2000 +2000 * 19 = 38000 = time for this line? + +[image[i][j] makeMoreAwesome]; +go through the line -> image[i] (memory access +1) image[j] (memory access) makeMoreAwesome (runprogram +200) +total of line = 1 + 1 + 200 = 202 picoseconds + +return image; +go through line -> image (memory access +1) +total of line = 1 + +so: 19000 + 38000 + 202 + 1 = 57203 picoseconds? + +answer: I'm not 100% sure how to allocate numbers to each element. My guess is 57203 picoseconds. + +b) What is the time complexity of this method, expressed in big O notation? Assume the image is square, and both dimensions are ‘n’. + +answer: O(N^2) - Performance is proportional to the square of the size of the input data (N and M) and there are two loops (three loops would = O(N^3)) + +c) My friend sends me an improved version of his algorithm, makeEvenMoreAwesome, that takes into account the pixels around the image. He says it’s O(n2) in the amount of pixels in the image. What is the new time complexity of the method? + +answer: O(N^2) If we are thinking about the worst case scenrio, this is equal to the existing run time. + + +2) If foo(xs) is a function with time complexity n (where n is the size of the input array), and bar(xs) is a function with time complexity n2, what is the time complexity of each of the following snippets of code or algorithms? + +a) for (int i = 0; i < n; i++) { +for (int j = 0; j < n; j++) { +foo(xs); + } +} +for (int i = 0; i < n; i++) { +for (int j = 0; j < n; j++) { +bar(xs); + } +} +for (int i = 0; i < n; i++) { +for (int j = 0; j < n; j++) { +// do cool stuff + } +} + +b) int frobnicate(ys, m) { +if (m == 0) { +return 0; +} +return ys[m] + frobnicate(ys, m - 1); +} +frobnicate(xs, n); + +Tip: Write down a table with n from 0 to 5 and trace through to find out how many times frobnicate is called with each value of n. + +c) An algorithm that takes as its input a list of friends of length n, filters out duplicates using a method similar to our hasDuplicates method, sorts the list using merge sort (see bigocheatsheet.com), then prints each item to the screen. + +d) An algorithm that searches the now-sorted list of friends for a specific friend (not including the time it takes to sort). 3) @@ -28,3 +114,5 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ */ +// 1) + From 6569b20d8c7c97056238cf1419e096a6f12ac445 Mon Sep 17 00:00:00 2001 From: Shena Yoshida Date: Thu, 14 Jan 2016 13:00:36 -0500 Subject: [PATCH 03/21] up to question 4 --- .../Contents.swift | 84 +++++++++++++++++-- 1 file changed, 75 insertions(+), 9 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index a50800d..2a28a2e 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -73,12 +73,12 @@ answer: O(N^2) If we are thinking about the worst case scenrio, this is equal to a) for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { -foo(xs); +foo(xs); // foo(xs) = O(N^2) } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { -bar(xs); +bar(xs); // bar(xs) = O(N^2) } } for (int i = 0; i < n; i++) { @@ -87,7 +87,17 @@ for (int j = 0; j < n; j++) { } } -b) int frobnicate(ys, m) { +Notes: + +when performance is proportional to the square of the size of the input data (and for loops are involved) the run time is often O(N^2). Deeper nested loops result in higher exponents. + +O(N^2) + O(N^2) = O(N^4)? + +Answer: O(N^4)? + +b) Calculate the time complexity of the following: + +int frobnicate(ys, m) { if (m == 0) { return 0; } @@ -97,22 +107,78 @@ frobnicate(xs, n); Tip: Write down a table with n from 0 to 5 and trace through to find out how many times frobnicate is called with each value of n. +Notes: + +when growth doubles (ie: frobnicate numbers) run time is O(2^n) + +Answer: O(2^n)? + c) An algorithm that takes as its input a list of friends of length n, filters out duplicates using a method similar to our hasDuplicates method, sorts the list using merge sort (see bigocheatsheet.com), then prints each item to the screen. +Notes: + +loop through to filter out duplicates = linear search = O(N) +merge sort = O(N) + +But: both of these actions would require a for loop (ie: 2 for loops = O(N^2)) + +Answer: O(N^2)? + d) An algorithm that searches the now-sorted list of friends for a specific friend (not including the time it takes to sort). -3) +Notes: + +sorted array search = linear search = O(N) +N is the max number of items in the array + +Answer: O(N) + +3) Look at the complexities for some common data structures at bigocheatsheet.com. Pick a good data structure for each of the following scenarios (there are sometimes multiple answers): + +a) You get a large dataset of points of interest from an API when your app first runs. You build it once at the beginning, and then have to search it many times while the user pans around a map. + +Notes: This sounds like a tree or graph (less efficient for search) -4) +Answer: tree / graph -5) +b) You get a small dataset of points of interest from an API every time the user pans the map. You construct the data set many times and only render it once, then you discard it and do another API search. -6) +Tip: Constructing a dataset of size n means you have to call the data structure’s insert method n times. So if the data structure has an insert method that takes O(n2), the time to build it all from scratch is O(n3). -7) +Notes: you could save the points of interest in a hash table (dictionary) +Answer: Hash table +c) You used a linked list for your music app’s playlist feature, but now when people search their playlist, there’s a noticeable lag before loading results. Your competitor’s app is buttery smooth when searching, even showing results as you type. What data structure would allow you to more quickly search without compromising too much on the speed of inserting and deleting tracks, even in the worst case? + +Notes: A tree is fast when inserting, deleting and searching items + +Answer: Tree + +4) Write an algorithm using one of the methods from exercise 1 (your choice) to calculate the factorial of a number n. What is the time complexity of your method in terms of the input value? +*/ + +// answer: + +/* +5) Write an Objective C or Swift function to multiply two numbers without using the * operator. Use the grade school method of multiplying by doing repeated addition. For instance, 5 * 8 = 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 = 40. Find the big O of your function in terms of n and m (the two operands). +*/ + +// answer: + +/* +6) Look up Russian Peasant Multiplication. It’s a faster way to multiply numbers, especially on a binary computer (like yours!). Implement a new multiplication function using this technique and find the big O of your method. If you have trouble with implementing this, write a flow chart and find the big O based on that. (But it’s more satisfying to implement it and run it) + +Tip: Run through the method by hand a few times to see how it works and verify to yourself that it does. It’s a non-intuitive algorithm. This will hopefully also make the time complexity more clear. */ -// 1) +// answer: + +/* +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). +*/ + +// answer: + + From 4bb953a7f537809a430d421b595ac4cc643fefa7 Mon Sep 17 00:00:00 2001 From: Shena Yoshida Date: Thu, 14 Jan 2016 16:45:08 -0500 Subject: [PATCH 04/21] up to question 6 --- .../Contents.swift | 65 ++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2a28a2e..d268c37 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -160,12 +160,37 @@ Answer: Tree // answer: +let userNames = ["cheekymonkey56", "007brenda", "hippydude"] + +if userNames.contains("geniushacker202") { + print("ACCESS GRANTED") +} +else { + print("INVALID USERNAME...") +} + +// so: the Big O run time is O(N) because the run time corresponds to userNames.count + /* 5) Write an Objective C or Swift function to multiply two numbers without using the * operator. Use the grade school method of multiplying by doing repeated addition. For instance, 5 * 8 = 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 = 40. Find the big O of your function in terms of n and m (the two operands). */ // answer: +func multiplyTwoNumbers(num1: Int, num2: Int) -> Int { + var product = 0 + + for _ in 1...num2 { // loop through 1 through num2 + product += num1 // each time add num1 to the product + } + return product +} + +print(multiplyTwoNumbers(3, num2: 4)) // test it + +// BIG O = O(N) growth is linear + + /* 6) Look up Russian Peasant Multiplication. It’s a faster way to multiply numbers, especially on a binary computer (like yours!). Implement a new multiplication function using this technique and find the big O of your method. If you have trouble with implementing this, write a flow chart and find the big O based on that. (But it’s more satisfying to implement it and run it) @@ -174,11 +199,49 @@ Tip: Run through the method by hand a few times to see how it works and verify t // answer: +// Steps: +// track two numbers in arrays +// double the number in the first array +// halve the number in the second array +// if second number is even, remove it from both arrays +// continue doubling and halfing until the number in the second column is 1 +// add up the remaining numbers in the first array, this is the product to return + +func peasantMultiplication(firstNumber: Int, secondNumber: Int) -> Int { + + var firstArray = [Int](arrayLiteral: firstNumber * 2) + var secondArray = [Int](arrayLiteral: secondNumber / 2) + var sum: Int = 0 + + while secondArray[0] != 1 { + + if secondArray[0] % 2 == 0 { + print("this number is even!") + firstArray.removeAtIndex(0) + secondArray.removeAtIndex(0) + } + else { +// firstArray = firstArray.append(firstArray[0] * 2) +// secondArray = + } + + } + + return 0 + +} + +// I need to do more work on this, Integers in Swift aren't 100% accurate as they are rounded up. +//peasantMultiplication(12, secondNumber: 14) + + + /* 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). */ -// answer: +// answer: I'm not sure how to graph this in a playground + From d1b72da79bfb7ec6c1965fe5e11980db6f327c6d Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Fri, 15 Jan 2016 10:50:34 -0500 Subject: [PATCH 05/21] reviewed big o answers --- .DS_Store | Bin 6148 -> 6148 bytes .../Contents.swift | 45 +++++++++--------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/.DS_Store b/.DS_Store index 3f3da67b8e97d3293e833d647d363c8a9026a64c..fcff3bc40cc0af9794931c335ee17ce0478a3465 100644 GIT binary patch delta 120 zcmZoMXfc@J&&a+pU^g=>I|BoQ%47i+kI4^M`PjHU;aod5ZqB_=3xRx?f;ku>^Vs>A n92h3YuseZtGdUjwGl4o;_r6nS07(QeHf$`M#<-cC<1aq|-6J0? delta 318 zcmZoMXfc@J&&atkU^g=>Cj$e6`D6hW4=V;ehGK>!23;VwU@&7y0g@n=DNrPlp@_kl zA(A13p)@DmFgQ6sw*aUO2$XAp1S#5c^Icq0N=uR%7%uH8*#kC%6zv5W$hIed^s53L zZ_HRXc^-?{ Int { } -// I need to do more work on this, Integers in Swift aren't 100% accurate as they are rounded up. -//peasantMultiplication(12, secondNumber: 14) - - +// Answer from class: This is a O(log N) algorithm /* 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). */ -// answer: I'm not sure how to graph this in a playground +// answer: look at zovfrellia's github acct for example From c4ce84efcd81267b1149811d4d4e1277c1cc3c5d Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Fri, 15 Jan 2016 11:07:19 -0500 Subject: [PATCH 06/21] revised peasant math --- .DS_Store | Bin 6148 -> 6148 bytes .../Contents.swift | 44 ++++++++---------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/.DS_Store b/.DS_Store index fcff3bc40cc0af9794931c335ee17ce0478a3465..b7747ff091a96b6c7f810d94f4a5ad9c993639d7 100644 GIT binary patch delta 27 jcmZoMXffDuhLy=FX|e*F(_{fQZsvU_=WUi_dnO0~f5Hhh delta 27 jcmZoMXffDuhLy?r;A90hr^y0r+{}C5sc)8JdnO0~gtQ4l diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 255f6d8..efb09b8 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -204,37 +204,33 @@ Tip: Run through the method by hand a few times to see how it works and verify t // answer: // Steps: -// track two numbers in arrays -// double the number in the first array -// halve the number in the second array -// if second number is even, remove it from both arrays -// continue doubling and halfing until the number in the second column is 1 -// add up the remaining numbers in the first array, this is the product to return - -func peasantMultiplication(firstNumber: Int, secondNumber: Int) -> Int { +// pass two mutable integers into function +// create a counter variable to hold the product +// while n1 is greater than 1 +// double n1 +// divide n2 by half +// if n1 is odd +// add num2 to the existing product +// if num1 = 1, break +// return the product + +func peasantMultiplication(var num1: Int, var num2: Int) -> Int { - var firstArray = [Int](arrayLiteral: firstNumber * 2) - var secondArray = [Int](arrayLiteral: secondNumber / 2) - var sum: Int = 0 + var product = 0 - while secondArray[0] != 1 { + while(num1 > 1) { + num2 = num2 * 2 + num1 = num1 / 2 - if secondArray[0] % 2 == 0 { - print("this number is even!") - firstArray.removeAtIndex(0) - secondArray.removeAtIndex(0) - } - else { -// firstArray = firstArray.append(firstArray[0] * 2) -// secondArray = + if(num1 % 2 != 0) { + product += num2 } - } - - return 0 - +return product } +print(peasantMultiplication(14, num2: 36)) + // Answer from class: This is a O(log N) algorithm /* From 41f5685a92783a65fca3b770dc3bb3a390eea696 Mon Sep 17 00:00:00 2001 From: Shena Yoshida Date: Fri, 15 Jan 2016 22:09:42 -0500 Subject: [PATCH 07/21] added playground for 01-14-16 discrete math homework --- .DS_Store | Bin 6148 -> 8196 bytes .../Contents.swift | 5 +++++ .../contents.xcplayground | 4 ++++ .../contents.xcworkspacedata | 7 +++++++ 4 files changed, 16 insertions(+) create mode 100644 HWfrom1-14-16(discrete-math).playground/Contents.swift create mode 100644 HWfrom1-14-16(discrete-math).playground/contents.xcplayground create mode 100644 HWfrom1-14-16(discrete-math).playground/playground.xcworkspace/contents.xcworkspacedata diff --git a/.DS_Store b/.DS_Store index b7747ff091a96b6c7f810d94f4a5ad9c993639d7..10aad56f65d2336bd87bfaafea0ebf2584bb25d5 100644 GIT binary patch delta 441 zcmZoMXmOBWU|?W$DortDU;r^WfEYvza8E20o2aMA$g?qEH}hr%jz7$c**Q2SHn1@A zOjcm^nY@pcZ*m$-B=i0h%gJ)Aev<`QxuNWJOsbrd71*363$Q_j*jy0OY(X{&K=T>Y z89W%m8PXVv81fl%84MY8f!G90nlWfFq%dSM6f-0PWm6eSfLIr(CK1TWnC!r!W~1mw zkpU7Qm)Y-6i3hoZ0*3(2VWo~aIjnq)0h_&f4l{E}a0A`s3W|Wuf*jwOC-aMVPWI>F P;9!Kr4a4Smo;l0_+v`(h delta 157 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jH7iU^g=(`(y<{pUL|K`6j0c zL^AJvS~yuw&~LJUAUBk~j>#!$vVxEckBdu6X-P5z!=-&E=S?mU3Sw;7SU8PwF*^r` oAT!WdAQ0dN60RT{HWq$op3E=f39^rY31T + + + \ No newline at end of file diff --git a/HWfrom1-14-16(discrete-math).playground/playground.xcworkspace/contents.xcworkspacedata b/HWfrom1-14-16(discrete-math).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWfrom1-14-16(discrete-math).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + From d8816df6f8abb18e3aac4328de7b64e0fdc02c4f Mon Sep 17 00:00:00 2001 From: Shena Yoshida Date: Fri, 15 Jan 2016 22:35:06 -0500 Subject: [PATCH 08/21] question one complete --- .DS_Store | Bin 8196 -> 6148 bytes .../Contents.swift | 41 ++++++++++++++++++ .../timeline.xctimeline | 6 +++ 3 files changed, 47 insertions(+) create mode 100644 HWfrom1-14-16(discrete-math).playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store index 10aad56f65d2336bd87bfaafea0ebf2584bb25d5..b8ab784af1703935e7cd0cd39931630545bb03f0 100644 GIT binary patch delta 131 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jH4hU^g=(_hbb@pUL|K`8KBs z>| diff --git a/HWfrom1-14-16(discrete-math).playground/Contents.swift b/HWfrom1-14-16(discrete-math).playground/Contents.swift index 07ee6d2..01a89a2 100644 --- a/HWfrom1-14-16(discrete-math).playground/Contents.swift +++ b/HWfrom1-14-16(discrete-math).playground/Contents.swift @@ -3,3 +3,44 @@ import Cocoa var str = "Hello, playground" + +/* Question 1: https://www.hackerrank.com/challenges/minimum-draws +Copy and paste your code: */ + +// this follows the pigeon hole rule: N socks will always require N+1 draws to find a match. +// kaira helped with this code: + +func userInput() -> Int { + let input = readLine() // this is necessary for hacker rank input :( + let num = Int(input!) + return num! +} + +let testNum : Int = userInput() +for i in 0.. + + + + From ab44f4637a7bac15dfa93e68d5796bb250a7d84b Mon Sep 17 00:00:00 2001 From: Shena Yoshida Date: Sun, 17 Jan 2016 11:14:00 -0500 Subject: [PATCH 09/21] Update Contents.swift --- HWfrom1-10-016(BigO).playground/Contents.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index efb09b8..dd644f9 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -122,11 +122,11 @@ c) An algorithm that takes as its input a list of friends of length n, filters o Notes: -loop through to filter out duplicates = linear search = O(N) +loop through to filter out duplicates = binary search = O(N^2) merge sort = O(N log N) Print to screen = O(N) -Answer: O(N log N) (largest value only) +Answer: O(N^2) (largest value only) d) An algorithm that searches the now-sorted list of friends for a specific friend (not including the time it takes to sort). @@ -231,13 +231,19 @@ return product print(peasantMultiplication(14, num2: 36)) -// Answer from class: This is a O(log N) algorithm +// Answer from class: This is a O(log N) algorithm (if you can double the input and then add something it is log n) /* 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). */ -// answer: look at zovfrellia's github acct for example +// answer: look at zovfrellia's github acct for example of graphs +// take time stamps for start and end times: +// NSDate *startTime = [NSDate date]; +// NSDate *endTime = [NSDate date]; +// use google sheets to graph numbers + + From 3cd36c72db6de2f24f9e6c988000b743c5b714aa Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Wed, 20 Jan 2016 18:44:42 -0500 Subject: [PATCH 10/21] added to arrays --- .DS_Store | Bin 6148 -> 8196 bytes .../Contents.swift | 3 +++ .../contents.xcplayground | 4 ++++ .../contents.xcworkspacedata | 7 +++++++ .../timeline.xctimeline | 6 ++++++ 5 files changed, 20 insertions(+) create mode 100644 multi-dimensional-arrays.playground/Contents.swift create mode 100644 multi-dimensional-arrays.playground/contents.xcplayground create mode 100644 multi-dimensional-arrays.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 multi-dimensional-arrays.playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store index b8ab784af1703935e7cd0cd39931630545bb03f0..4bd74310f5af94402583417604860de9c2d41bb3 100644 GIT binary patch literal 8196 zcmeI1!Ab)$5QhJ#i0Gw9Z>}h)V8wz6-j-59FM25A)!MC9=(bf`#M@qd3ExEV>eYAf zJ^UvLvSlq4j3PLJM*GQivZ?WMN128z9=#-wNR#fPl;-15A=|t zf-<(TSD39GN#}?Py+vwfb0(ut7z;sb85E^HZ}#I z&am4SUQ0S4Hj&rZYvzWXkNmvYYv$CM!}#RG_?3;%P>fxj{v$gb zCU&Z=5>Ns~0`hZN#Q{!nft!*1z06F07wf3w4p$r<;1FH5GHU}{=%a%M?$NeALyje# zcBS6zrG2-|!eL0?X|GmGhJz;X_Vj*jR|pn&+Dm@ScmFcaB$mLJ3cM>kF_BXfN4!ej zZ5&TVxqAM6k2$u_ua6SqcVo;E4dii@V0g0C(+*9)IlKvIl*x%rj zxUoCN?IgR$vYa3W@|@>>^dKRJ^ZdDUyoB{U%K6L&t&I{0NMPFeT9o{MfBgA>z!lY% z5>Ntvjewb{)oWEo<#X#oe3ENNtY@rJWM1afny|6yIMt@()XzT*c^z@8*u-8lr|e<* P^@9NY{#)+a{BHsul4(W* delta 197 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jH4hU^g=(_hbb@pUL|K`6j0c zL^AJBv79U?=r>tFkQ>Tg$E3 + + + \ No newline at end of file diff --git a/multi-dimensional-arrays.playground/playground.xcworkspace/contents.xcworkspacedata b/multi-dimensional-arrays.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/multi-dimensional-arrays.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/multi-dimensional-arrays.playground/timeline.xctimeline b/multi-dimensional-arrays.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/multi-dimensional-arrays.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From 005ef8368a818b20173a2b380fb13f69ed93ec29 Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Wed, 20 Jan 2016 22:18:33 -0500 Subject: [PATCH 11/21] sudoku question solved --- .DS_Store | Bin 8196 -> 8196 bytes .../Contents.swift | 74 ++++++++++++++++++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 ++ .../timeline.xctimeline | 6 ++ 5 files changed, 91 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/.DS_Store b/.DS_Store index 4bd74310f5af94402583417604860de9c2d41bb3..759e5fbf307cd41e66eb99814192f53d151edbcc 100644 GIT binary patch delta 406 zcmZp1XmOa}&nUbxU^hRb@MHx+pUL|K`8KBsq%kr%drwvna^Z1tNhvK!W?;B<^i28W z0wI^l0)pJkK=$SWq5F*1nhYKc;S6aEMGW~2xeSI3xFfdSJNP@7k^U;4QKvO6)$3Z}1a*iM$W5VSBBHrLY fSR!J`w3%Jv8%tz5*n6cQ59H<}!h=S(>M;QT>9AS< delta 95 zcmZp1XmOa}&nU1lU^hRbz+?qMpUL|K`8KBsq%kr%B~4Zka^Z1tNhvK!W?;Cq@8rD6 w1wt;91q8X7f$Yr%LiZUbn~J%EbzBoQWZKwZ#JHJV;v36meX$#i8yoZ)0p@!kYybcN 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..7040a4d --- /dev/null +++ b/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift @@ -0,0 +1,74 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" + +/* +Work on your solutions here. +Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit#heading=h.uopysoy45zmw +1) Given a partially filled in Sudoku board and a set of coordinates in that board pointing to an empty square, write a function that returns a list containing all numbers that the empty square could be. + +2) +3) +*/ + +// Sudoku Rules: Each row, column and square (9 spaces each) needs to be filled out with the numbers 1-9, without repeating any numbers within the row, column or square. + +// 1) grab all numbers from the row and save them in an array +// 2) grab all numbers from that column and save them in an array +// 3) grab all numbers from square and save in an array +// 4) loop through arrays, if number is not in 1...9, it is a potential solution to the puzzle! +// 5) return those number(s) + +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]] + +print(sudokuBoard[0][1]) // coordinates for 1, 2 + +var rowNumbers = [Int]() +var columnNumbers = [Int]() + +func getRowNumbers(sudokuBoard: [[Int]], row: Int, column: Int) -> [Int] { + for i in sudokuBoard[row] { + if i > 0 { + rowNumbers.append(i) + } + } +return rowNumbers +} + +func getColumnNumbers(sudokuBoard: [[Int]], row: Int, column: Int) -> [Int] { + for i in sudokuBoard { + if i[column] > 0 { + columnNumbers.append(i[column]) + } + } + return columnNumbers // should be [5, 9, 1, 2] +} + +func returnPotentialNumbers(rowNumbers: [Int], columnNumbers: [Int]) -> Set { + + let allNumbers = rowNumbers + columnNumbers + let setOfAllNumbers = Set(allNumbers) + let setCompare = Set([1, 2, 3, 4, 5, 6, 7, 8, 9]) + + return setOfAllNumbers.exclusiveOr(setCompare) + } + +getRowNumbers(sudokuBoard, row: 1, column: 0) // test it! +getColumnNumbers(sudokuBoard, row: 1, column: 0) // test it! +returnPotentialNumbers(rowNumbers, columnNumbers: columnNumbers) + + + + + diff --git a/HWfrom1-17-16(Lists And Sorts).playground/contents.xcplayground b/HWfrom1-17-16(Lists And Sorts).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWfrom1-17-16(Lists And Sorts).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ 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 7ce1cdf6380ce508e0504a394898f5b2bac1fe22 Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Thu, 21 Jan 2016 16:02:58 -0500 Subject: [PATCH 12/21] added method to find quadrants --- .DS_Store | Bin 8196 -> 8196 bytes .../Contents.swift | 75 +++++++++++++++--- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/.DS_Store b/.DS_Store index 759e5fbf307cd41e66eb99814192f53d151edbcc..0e8952788122ef8e35614d996bce806fc1e99468 100644 GIT binary patch delta 59 zcmZp1XmOa}&nUDpU^hRb&}0QcpUL|K`8KBsc(E`Ux=r>G3EP|}I+JN)L)vC`2}ah* NIY7x8(QcqXG5{%&5|sb| delta 82 zcmZp1XmOa}&nUbxU^hRb@MHx+pUL|K`8KBsc(E{Pc}(^Z37ae+#KZhgg<-O{NciLe bG2zY9qHRo@*(DfRC+`vEW;u!~)yxC{?d=!j diff --git a/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift b/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift index 7040a4d..d910546 100644 --- a/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift +++ b/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift @@ -8,18 +8,16 @@ var str = "Hello, playground" Work on your solutions here. Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit#heading=h.uopysoy45zmw 1) Given a partially filled in Sudoku board and a set of coordinates in that board pointing to an empty square, write a function that returns a list containing all numbers that the empty square could be. - -2) -3) +2) Rotate a matrix by ninety degrees +3) Design an optimal algorithm for sorting four elements A, B, C, and D. By optimal, I mean one that sorts using the minimum number of comparisons. Hint: you may want to start by putting the first two items in order and the last two items in order... that takes two comparisons. How many more comparisons do you need to find the minimum element? The maximum? Once you’ve found the min and max, what if any additional comparisons are needed? */ -// Sudoku Rules: Each row, column and square (9 spaces each) needs to be filled out with the numbers 1-9, without repeating any numbers within the row, column or square. - -// 1) grab all numbers from the row and save them in an array -// 2) grab all numbers from that column and save them in an array -// 3) grab all numbers from square and save in an array -// 4) loop through arrays, if number is not in 1...9, it is a potential solution to the puzzle! -// 5) return those number(s) +// 1) review the rules of Sudoku: Each row, column and square (9 spaces each) needs to be filled out with the numbers 1-9, without repeating any numbers within the row, column or square +// 2) grab all numbers from the row and save them in an array +// 3) grab all numbers from that column and save them in an array +// 4) grab all numbers from square and save in an array +// 5) loop through arrays, if number is not in 1...9, save it in an array +// 6) return those numbers - these are your potential answers var sudokuBoard = [ [5, 0, 8, 0, 7, 3, 1, 9, 0], @@ -36,6 +34,7 @@ print(sudokuBoard[0][1]) // coordinates for 1, 2 var rowNumbers = [Int]() var columnNumbers = [Int]() +var boxNumbers = [Int]() func getRowNumbers(sudokuBoard: [[Int]], row: Int, column: Int) -> [Int] { for i in sudokuBoard[row] { @@ -55,6 +54,46 @@ func getColumnNumbers(sudokuBoard: [[Int]], row: Int, column: Int) -> [Int] { return columnNumbers // should be [5, 9, 1, 2] } +func getQuadrantNumbers(sudokuBoard: [[Int]], row: Int, column: Int) -> [Int] { +// get box quadrant numbers +// get numbers from within the selected quadrant + + let point = (x: row, y: column) + + switch point { + case let q1 where (point.x >= 0 && point.x <= 2) && (point.y >= 0 && point.y <= 2): + print("\(q1) is in quadrant 1") + + case let q2 where (point.x >= 0 && point.x <= 2) && (point.y >= 3 && point.y <= 5): + print("\(q2) is in quadrant 2") + + case let q3 where (point.x >= 0 && point.x <= 2) && (point.y >= 6 && point.y <= 8): + print("\(q3) is in quadrant 3") + + case let q4 where (point.x >= 3 && point.x <= 5) && (point.y >= 0 && point.y <= 2): + print("\(q4) is in quadrant 4") + + case let q5 where (point.x >= 3 && point.x <= 5) && (point.y >= 3 && point.y <= 5): + print("\(q5) is in quadrant 5") + + case let q6 where (point.x >= 3 && point.x <= 5) && (point.y >= 6 && point.y <= 8): + print("\(q6) is in quadrant 6") + + case let q7 where (point.x >= 6 && point.x <= 8) && (point.y >= 0 && point.y <= 2): + print("\(q7) is in quadrant 7") + + case let q8 where (point.x >= 6 && point.x <= 8) && (point.y >= 3 && point.y <= 5): + print("\(q8) is in quadrant 8") + + case let q9 where (point.x >= 6 && point.x <= 8) && (point.y >= 6 && point.y <= 8): + print("\(q9) is in quadrant 9") + + default: + print("coordinates unknown.") + } + return boxNumbers +} + func returnPotentialNumbers(rowNumbers: [Int], columnNumbers: [Int]) -> Set { let allNumbers = rowNumbers + columnNumbers @@ -65,9 +104,21 @@ func returnPotentialNumbers(rowNumbers: [Int], columnNumbers: [Int]) -> Set } getRowNumbers(sudokuBoard, row: 1, column: 0) // test it! -getColumnNumbers(sudokuBoard, row: 1, column: 0) // test it! -returnPotentialNumbers(rowNumbers, columnNumbers: columnNumbers) +getColumnNumbers(sudokuBoard, row: 1, column: 0) +getQuadrantNumbers(sudokuBoard, row: 6, column: 6) + +returnPotentialNumbers(rowNumbers, columnNumbers: columnNumbers) // <--- here are the potential numbers!! + +// 2. + +var input = [[1, 2, 3, 4], + [5, 6, 7, 8], + [9, 0, 1, 2], + [3, 4, 5, 6]] + +var output: [[Int]] +// this is a 4X4 array From 6f42f072013bda4a929eff4cf8dfb710812a6092 Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Fri, 22 Jan 2016 16:50:08 -0500 Subject: [PATCH 13/21] added notes --- .DS_Store | Bin 8196 -> 8196 bytes .../Contents.swift | 220 ++++++++++++++++-- 2 files changed, 200 insertions(+), 20 deletions(-) diff --git a/.DS_Store b/.DS_Store index 0e8952788122ef8e35614d996bce806fc1e99468..57573c0befbc64cc010d2193931a104e66a225e2 100644 GIT binary patch delta 173 zcmZp1XmQx!FT!XuIY1=Hib0Q|m?4Ql7l>0Bj2O%qQW;Dc3>YkcA|VXP42BFv4E_wE rIq8PM$@#ej3}9gB2Bg5+)rix+nL~6l(`I&wZ!D7wMGT48w1Ei#MN%MV delta 57 zcmZp1XmQx!FT$ufIY1<6@;*U6HbXZC1_lUskC^c0bkPM&6B~RsvrBwqnY>X<4l3XY E0Q)5pg8%>k diff --git a/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift b/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift index d910546..62c2474 100644 --- a/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift +++ b/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift @@ -30,6 +30,7 @@ var sudokuBoard = [ [2, 0, 3, 0, 0, 7, 0, 0, 9], [0, 8, 7, 1, 9, 0, 3, 0, 4]] + print(sudokuBoard[0][1]) // coordinates for 1, 2 var rowNumbers = [Int]() @@ -54,25 +55,103 @@ func getColumnNumbers(sudokuBoard: [[Int]], row: Int, column: Int) -> [Int] { return columnNumbers // should be [5, 9, 1, 2] } -func getQuadrantNumbers(sudokuBoard: [[Int]], row: Int, column: Int) -> [Int] { -// get box quadrant numbers -// get numbers from within the selected quadrant +func returnPotentialNumbers(rowNumbers: [Int], columnNumbers: [Int]) -> Set { + + let allNumbers = rowNumbers + columnNumbers + let setOfAllNumbers = Set(allNumbers) + let setCompare = Set([1, 2, 3, 4, 5, 6, 7, 8, 9]) + + return setOfAllNumbers.exclusiveOr(setCompare) +} +getRowNumbers(sudokuBoard, row: 1, column: 0) // test it! +getColumnNumbers(sudokuBoard, row: 1, column: 0) +returnPotentialNumbers(rowNumbers, columnNumbers: columnNumbers) // <--- here are the potential numbers!! + +// 1b) get numbers from specific quadrants +// This code is way too long, see refactored version below. + +func getQuadrantNumbers(sudokuBoard: [[Int]], row: Int, column: Int) -> [Int] { + let point = (x: row, y: column) switch point { case let q1 where (point.x >= 0 && point.x <= 2) && (point.y >= 0 && point.y <= 2): print("\(q1) is in quadrant 1") + for i in sudokuBoard[0][0...2] { + if i > 0 { + boxNumbers.append(i) + } + } + for i in sudokuBoard[1][0...2] { + if i > 0 { + boxNumbers.append(i) + } + } + for i in sudokuBoard[2][0...2] { + if i > 0 { + boxNumbers.append(i) + } + } + case let q2 where (point.x >= 0 && point.x <= 2) && (point.y >= 3 && point.y <= 5): print("\(q2) is in quadrant 2") + for i in sudokuBoard[0][3...5] { + if i > 0 { + boxNumbers.append(i) + } + } + for i in sudokuBoard[1][3...5] { + if i > 0 { + boxNumbers.append(i) + } + } + for i in sudokuBoard[2][3...5] { + if i > 0 { + boxNumbers.append(i) + } + } + case let q3 where (point.x >= 0 && point.x <= 2) && (point.y >= 6 && point.y <= 8): print("\(q3) is in quadrant 3") + for i in sudokuBoard[0][6...8] { + if i > 0 { + boxNumbers.append(i) + } + } + for i in sudokuBoard[1][6...8] { + if i > 0 { + boxNumbers.append(i) + } + } + for i in sudokuBoard[2][6...8] { + if i > 0 { + boxNumbers.append(i) + } + } + case let q4 where (point.x >= 3 && point.x <= 5) && (point.y >= 0 && point.y <= 2): print("\(q4) is in quadrant 4") + for i in sudokuBoard[3][0...2] { + if i > 0 { + boxNumbers.append(i) + } + } + for i in sudokuBoard[4][0...2] { + if i > 0 { + boxNumbers.append(i) + } + } + for i in sudokuBoard[5][0...2] { + if i > 0 { + boxNumbers.append(i) + } + } + case let q5 where (point.x >= 3 && point.x <= 5) && (point.y >= 3 && point.y <= 5): print("\(q5) is in quadrant 5") @@ -93,33 +172,134 @@ func getQuadrantNumbers(sudokuBoard: [[Int]], row: Int, column: Int) -> [Int] { } return boxNumbers } +getQuadrantNumbers(sudokuBoard, row: 1, column: 4) // test it! +// Note: alternately, if you divide quadrants by 3, you will get the coordinate. -func returnPotentialNumbers(rowNumbers: [Int], columnNumbers: [Int]) -> Set { - - let allNumbers = rowNumbers + columnNumbers - let setOfAllNumbers = Set(allNumbers) - let setCompare = Set([1, 2, 3, 4, 5, 6, 7, 8, 9]) - return setOfAllNumbers.exclusiveOr(setCompare) - } -getRowNumbers(sudokuBoard, row: 1, column: 0) // test it! -getColumnNumbers(sudokuBoard, row: 1, column: 0) -getQuadrantNumbers(sudokuBoard, row: 6, column: 6) +// HOMEWORK REVIEW NOTES: + + + +func getValidNumbers(sudokuBoard:[[Int?]], row:Int, col:Int) -> [Int] { + var valid: Set = [1, 2, 3, 4, 5, 6, 7, 8, 9] + + for c in 0..(valid) +} +getValidNumbers(sampleInput, row: 0, col: 1) -returnPotentialNumbers(rowNumbers, columnNumbers: columnNumbers) // <--- here are the potential numbers!! // 2. +let matrix = [[1,2,3,4], // this is a 4X4 array + [5,6,7,8], + [9,0,1,2], + [3,4,5,6]] -var input = [[1, 2, 3, 4], - [5, 6, 7, 8], - [9, 0, 1, 2], - [3, 4, 5, 6]] +func rotate(matrix: [[Int]]) -> [[Int]] { + let n = matrix.count + + var result: [[Int]] = [] // start with empty array + + for _ 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 + } + + // 1 3 + // 2 4 + + return [] +} +// n x n matrix +// 4 - 0 - 1 = 3 +// +// 0, 0 -> 0, n - 1 +// r, c -> r, n - c - 1 +// 0, n - 1 -> n - 1, n - 1 +// r, c -> n - r - 1, c +// n - 1, n - 1 -> n - 1, 0 +// r, c -> n - r - 1 +// n - 1, 0 -> 0, 0 +/* +------------ +fib(5) +a = 3 +b = 4 +------------ +fib(3) +a = 1 +b = 2 +------------ +fib(2) +a = 0 +b = 1 +------------ +fib(0) 1 +*/ \ No newline at end of file From 36113f7da647c4e07fffb504810b247e2b493dab Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Fri, 22 Jan 2016 16:51:36 -0500 Subject: [PATCH 14/21] added matrix --- .../Contents.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift b/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift index 62c2474..24db079 100644 --- a/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift +++ b/HWfrom1-17-16(Lists And Sorts).playground/Contents.swift @@ -179,7 +179,16 @@ getQuadrantNumbers(sudokuBoard, row: 1, column: 4) // test it! // HOMEWORK REVIEW NOTES: - +let sampleInput: [[Int?]] = +[[5,0,8,9,0,0,0,0,0], + [0,7,3,6,0,0,9,0,8], + [1,9,0,4,0,8,0,3,5], + [0,7,0,0,0,2,0,1,0], + [0,0,0,0,0,0,0,0,0], + [0,6,0,9,0,0,0,8,0], + [1,9,0,2,0,3,0,8,7], + [3,0,6,0,0,7,1,9,0], + [0,0,0,0,0,9,3,0,4]] func getValidNumbers(sudokuBoard:[[Int?]], row:Int, col:Int) -> [Int] { var valid: Set = [1, 2, 3, 4, 5, 6, 7, 8, 9] From f9c5b97589dc19983f18bfaf7c0ab1d7ea9cce1a Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Wed, 27 Jan 2016 22:30:20 -0500 Subject: [PATCH 15/21] recursion one and td two complete --- .DS_Store | Bin 8196 -> 10244 bytes 20160124 Homework.playground/Contents.swift | 9 ++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 + .../timeline.xctimeline | 6 + .../Contents.swift | 149 ++++++++++++++++++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 + .../timeline.xctimeline | 6 + 9 files changed, 192 insertions(+) create mode 100644 20160124 Homework.playground/Contents.swift create mode 100644 20160124 Homework.playground/contents.xcplayground create mode 100644 20160124 Homework.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 20160124 Homework.playground/timeline.xctimeline create mode 100644 20160124-homework-recursion.playground/Contents.swift create mode 100644 20160124-homework-recursion.playground/contents.xcplayground create mode 100644 20160124-homework-recursion.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 20160124-homework-recursion.playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store index 57573c0befbc64cc010d2193931a104e66a225e2..7ac0600aab6b3915037fd826c2a942b374aca2c3 100644 GIT binary patch delta 660 zcmZp1XbF&DU|?W$DortDU{C-uIe-{M3-C-V6q~50C@Kx)Far4u3RT r3^tEJf*VM?f;_Xa@H_Klew9EDpjSX%WSAV!Gj;MoF++-M3S$BQeu6u5 diff --git a/20160124 Homework.playground/Contents.swift b/20160124 Homework.playground/Contents.swift new file mode 100644 index 0000000..d53efd4 --- /dev/null +++ b/20160124 Homework.playground/Contents.swift @@ -0,0 +1,9 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" + +/* + +/* diff --git a/20160124 Homework.playground/contents.xcplayground b/20160124 Homework.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/20160124 Homework.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/20160124 Homework.playground/playground.xcworkspace/contents.xcworkspacedata b/20160124 Homework.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/20160124 Homework.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/20160124 Homework.playground/timeline.xctimeline b/20160124 Homework.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/20160124 Homework.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/20160124-homework-recursion.playground/Contents.swift b/20160124-homework-recursion.playground/Contents.swift new file mode 100644 index 0000000..54c7ac8 --- /dev/null +++ b/20160124-homework-recursion.playground/Contents.swift @@ -0,0 +1,149 @@ +// Exercises from: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit# + +import UIKit +import Foundation + +/* 1) 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? + +Note: In the Fibonacci sequence, each number is the sum of the two previous numbers. +ie: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] */ + +//Appendix A: Recursive Fibonacci from class (non-memoized) +func fib1(n: Int) -> Int { + if (n == 0 || n == 1) { + return 1 + } + return fib1(n - 1) + fib1(n - 2) +} +print(fib1(10)) + +// Answer: this non recursive method is waaay faster. + +var fibNo = 1 +var index = 0 + +func fib2(numbers: Int) -> Int { + +for number in 1...numbers { // loop through numbers 1...10 + print("index \(number): \(fibNo)") + let temp = fibNo + index + index = fibNo + fibNo = temp + } +return fibNo +} + +print(fib2(10)) // Note: this is actually returning the 11th fib no because the function is starting with a 0 indexed number + +/* 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. */ + +// Original Program: +/* +var stepNum = 0 +func tryStep() -> Bool { + let success = Int(arc4random_uniform(2)) > 0 // generate rando number + if (success) { + stepNum++ + print("Yay! \(stepNum)") + } else { + stepNum--; + print("Ow! \(stepNum)") + } + return success +} + +func stepUp() { + if tryStep() { + // We’re done! + return + } + // Now we’re two steps below where we want to be :-( + stepUp() + stepUp() +} +print(stepUp()) +*/ + +// Appendix B: +var steppNum = 0 + +func tryStepp() -> Int { + let stepCount = Int(arc4random_uniform(3)) - 1 // generate rando number + steppNum += stepCount; + + switch(stepCount) { + case -1: print("Ouch \(steppNum)") + case 1: print("Yay \(steppNum)") + default: print("Beep \(steppNum)") + } + return stepCount +} + +// My code: +func steppUp(var stepsTaken: Int = 0) { + stepsTaken += tryStepp() + if stepsTaken == 1 { + // we're done! + return + } + steppUp(stepsTaken) +} +steppUp() + + + + + + + + + + + + +//func stepUp2(steppNum: Int) { +// switch steppNum { +// case -1, 0: +// tryStepp() + tryStepp() +// case 1: +// print("hooray!!!") +// default: +// tryStepp() + tryStepp() +// } +//} +//stepUp2(steppNum) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/20160124-homework-recursion.playground/contents.xcplayground b/20160124-homework-recursion.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/20160124-homework-recursion.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/20160124-homework-recursion.playground/playground.xcworkspace/contents.xcworkspacedata b/20160124-homework-recursion.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/20160124-homework-recursion.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/20160124-homework-recursion.playground/timeline.xctimeline b/20160124-homework-recursion.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/20160124-homework-recursion.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From 167be9cc73cd69ed6395ab4cee7762e83c1921d9 Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Thu, 28 Jan 2016 16:54:37 -0500 Subject: [PATCH 16/21] deleted extra comments --- .DS_Store | Bin 10244 -> 10244 bytes .../Contents.swift | 17 ----------------- 2 files changed, 17 deletions(-) diff --git a/.DS_Store b/.DS_Store index 7ac0600aab6b3915037fd826c2a942b374aca2c3..08b2934de47cb705bc5eff3e00f773e4e0cf117b 100644 GIT binary patch delta 23 fcmZn(XbIRbOOQ#sc=8; Date: Thu, 4 Feb 2016 12:06:09 -0500 Subject: [PATCH 17/21] added files --- .DS_Store | Bin 10244 -> 10244 bytes .../Contents.swift | 35 ++++-- .../Contents.swift | 103 ++++++++++++++++++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 ++ .../timeline.xctimeline | 6 + .../Contents.swift | 76 +++++++++++++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 ++ .../timeline.xctimeline | 6 + 10 files changed, 238 insertions(+), 10 deletions(-) create mode 100644 20160128-homework-merge-sort.playground/Contents.swift create mode 100644 20160128-homework-merge-sort.playground/contents.xcplayground create mode 100644 20160128-homework-merge-sort.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 20160128-homework-merge-sort.playground/timeline.xctimeline create mode 100644 20160130-quicksort-stacks-queues-homework.playground/Contents.swift create mode 100644 20160130-quicksort-stacks-queues-homework.playground/contents.xcplayground create mode 100644 20160130-quicksort-stacks-queues-homework.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 20160130-quicksort-stacks-queues-homework.playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store index 08b2934de47cb705bc5eff3e00f773e4e0cf117b..4b07b7d43f9461a382a8920e5c5a34eae7dd35f7 100644 GIT binary patch delta 229 zcmZn(XbG6$&nUk!U^hRb{NxV;Hk0cl_&0|OI5RToWKEtU7{iisfq? Int { return fib1(n - 1) + fib1(n - 2) } print(fib1(10)) +(0...5).map { i in fib1(i) } // create map // Answer: this non recursive method is waaay faster. @@ -33,6 +34,8 @@ for number in 1...numbers { // loop through numbers 1...10 return fibNo } + + print(fib2(10)) // Note: this is actually returning the 11th fib no because the function is starting with a 0 indexed number /* 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. */ @@ -79,18 +82,30 @@ func tryStepp() -> Int { return stepCount } -// My code: -func steppUp(var stepsTaken: Int = 0) { - stepsTaken += tryStepp() - if stepsTaken == 1 { - // we're done! +//// My code: +//func steppUp(var stepsTaken: Int = 0) { +// stepsTaken += tryStepp() +// if stepsTaken == 1 { +// // we're done! +// return +// } +// steppUp(stepsTaken) +//} +//steppUp() + +// Cameron's Code: +func stepUp() { + switch tryStepp() { + case 1: return + case -1: + stepUp() + stepUp() + default: + stepUp() } - steppUp(stepsTaken) } -steppUp() - - +stepUp() diff --git a/20160128-homework-merge-sort.playground/Contents.swift b/20160128-homework-merge-sort.playground/Contents.swift new file mode 100644 index 0000000..816a8e3 --- /dev/null +++ b/20160128-homework-merge-sort.playground/Contents.swift @@ -0,0 +1,103 @@ +//: Playground - noun: a place where people can play + +import Foundation + +var str = "Hello, playground" + +/* + +Use recursion to implement Insertion Sort in Swift: + +Requirements: +You may not use loops +Your implementations should be in-place (try not to create additional arrays) +You should implement and use additional helper functions + +Tips: +Add additional parameters to your helper functions to pass information between recursive calls +You can use the swap function to exchange two values in a mutable (var) array: + +var array = [1, 2] +swap(&array[0], &array[1]) +array // [2, 1] + +// Insertion Sort from Stack Overflow: +public static int[] RecursiveInsertionSort(int[] array, int n) { +int i; +if (n > 1) +RecursiveInsertionSort(array, n - 1); +else { +int k = array[n]; +i = n - 1; +while (i >= 0 & & array[i] > k) { +array[i + 1] = array[i]; +i = i - 1; +} +array[i + 1] = k; +} +return array; +} +*/ + +// regular insertion sort: +func exchange(inout data: [T], i:Int, j:Int) { + let temp:T = data[i] + data[i] = data[j] + data[j] = temp +} + +func insertionSort(var unsortedArray:Array)->Array{ + if(unsortedArray.count<2) { + return unsortedArray + } + for var j = 1; j < unsortedArray.count; j++ { + var i = j + while i>0 && unsortedArray[i-1]>unsortedArray[i] { + exchange(&unsortedArray, i: i-1, j: i) + i--; + } + } + return unsortedArray; +} +insertionSort([3, 4, 5, 1, 2]) + +// Cameron's notes for homework: +func printAllElements(values: [Int]) { + for value in values { + print(value) + } +} + +func printAllElementsRecursive(values: [Int]) { + printElementsHelper(values, index: 0) +} + +func printElementsHelper(values: [Int], index: Int) { + if index < values.count { + print(values[index]) + printElementsHelper(values, index: index + 1) + } +} + +func setValue(inout array: [Int], value: Int, atIndex index: Int) { + array[index] = value +} + +var values = [10, 20, 30] +printAllElements(values) + +setValue(&values, value: 100, atIndex: 1) +values + + + +func reverse(inout array: [Int]) { + for i in 0.. + + + \ No newline at end of file diff --git a/20160128-homework-merge-sort.playground/playground.xcworkspace/contents.xcworkspacedata b/20160128-homework-merge-sort.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/20160128-homework-merge-sort.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/20160128-homework-merge-sort.playground/timeline.xctimeline b/20160128-homework-merge-sort.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/20160128-homework-merge-sort.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/20160130-quicksort-stacks-queues-homework.playground/Contents.swift b/20160130-quicksort-stacks-queues-homework.playground/Contents.swift new file mode 100644 index 0000000..fa898e7 --- /dev/null +++ b/20160130-quicksort-stacks-queues-homework.playground/Contents.swift @@ -0,0 +1,76 @@ +//: Playground - noun: a place where people can play + +import Foundation + +/* +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. + +Bubble Sort: O(n^2) +Insertion Sort: O(n^2) +Selection Sort: O(n^2) +Merge Sort: O(n log n) +Quick Sort: O(n log n) + +2) What is the advantage of partitioning quicksort in place? + +you don't need to take up additional memory when creating additional storage arrays (like in mergesort) + +3) Without looking, implement quicksort. + +var randomNumbers = [42, 12, 88, 62, 63, 56, 1, 77, 88, 97, 97, 20, 45, 91, 62, 2, 15, 31, 59, 5] + +func partition(v: [Int], left: Int, right: Int) -> [Int] { + var i = left + for j in (left + 1)..(right + 1) { + if v[j] < v[left] { + i += 1 + (v[i], v[j]) = (v[j], v[i]) + } + } + (v[i], v[left]) = (v[left], v[i]) + return i +} + +func quicksort(v: Int[], left: Int, right: Int) { + if right > left { + let pivotIndex = partition(v, left, right) + quicksort(v, left, pivotIndex - 1) + quicksort(v, pivotIndex + 1, right) + } +} + +quicksort(randomNumbers, left: 0, right: randomNumbers.count-1) + +4) Write a function to generate an array of random numbers bounded between 1..<10,000 of size 10,000. + +Int(arc4random_uniform(UInt32(10000))) +*/ + +func makeList(n: Int, max: Int) -> [Int] { + var result: [Int] = [] // create storage variable + for _ in 0.. Bool { + +} + +*/ + diff --git a/20160130-quicksort-stacks-queues-homework.playground/contents.xcplayground b/20160130-quicksort-stacks-queues-homework.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/20160130-quicksort-stacks-queues-homework.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/20160130-quicksort-stacks-queues-homework.playground/playground.xcworkspace/contents.xcworkspacedata b/20160130-quicksort-stacks-queues-homework.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/20160130-quicksort-stacks-queues-homework.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/20160130-quicksort-stacks-queues-homework.playground/timeline.xctimeline b/20160130-quicksort-stacks-queues-homework.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/20160130-quicksort-stacks-queues-homework.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From fbe049c33a0d583880cba183d5c804091dde0a7d Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Thu, 4 Feb 2016 12:24:40 -0500 Subject: [PATCH 18/21] up to 5th question --- .../Contents.swift | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/20160130-quicksort-stacks-queues-homework.playground/Contents.swift b/20160130-quicksort-stacks-queues-homework.playground/Contents.swift index fa898e7..8875619 100644 --- a/20160130-quicksort-stacks-queues-homework.playground/Contents.swift +++ b/20160130-quicksort-stacks-queues-homework.playground/Contents.swift @@ -55,14 +55,29 @@ func makeList(n: Int, max: Int) -> [Int] { } // makeList(10000, max: 10000) // call method, set length and range -/* + +/* 5) Compare the time it takes to run mergesort, quicksort, and quicksort with the median. https://gist.github.com/gummibeatz/8ff29bcec54d7e3ef683 +*/ +func profile(block:(()->())) { + let start = NSDate() + block() + let end = NSDate() + print(end.timeIntervalSinceDate(start)) +} + +//run your function/code inside the block below +profile({ + +}) +/* 6) Describe the algorithmic difference between mergesort and quicksort. Where does the sorting happen? As the recursive calls are being pushed onto the stack or as they are being popped off? +with Merge sort, the sorting happens in subarrays that are sorted and merged together at the end. Quicksort happens in the main array. 7) Given an array of strings containing “[“,”]”,”{“,”}”,”(“,”)”. Output whether or not the parentheses are balanced. Good examples: () [] () ([]()[]) From 2ee2afb891f3d2b667fd5d4a52762e0991c5dc8a Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Thu, 4 Feb 2016 17:27:25 -0500 Subject: [PATCH 19/21] 7th question --- .DS_Store | Bin 10244 -> 10244 bytes .../Contents.swift | 79 +++++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/.DS_Store b/.DS_Store index 4b07b7d43f9461a382a8920e5c5a34eae7dd35f7..276b1574c894eeeb6798448e36789f07ec4a7437 100644 GIT binary patch delta 331 zcmZn(XbG6$I9U^hRb(&P^UHk0{;`8J0M^fSAfFc>iyFc>nJfk|T^smoBvP|A?W zkj#+HP|T3eP{dFI6e|XjiBK_!id3MgRG_#nL&oF+0TqyuUWTt27#OH!q`vRXhB}~m g)H6|KvW_4hW5ec$!kf4@vn%{!Q6$c5FkS1J08M~J>i_@% delta 62 zcmZn(XbG6$&nUk!U^hRb{NxV;Hj@)1_%??L^fONu6!QdgHi(9EZEPrE+{~`{+;(VKrh;3m40HOdD+yDRo diff --git a/20160130-quicksort-stacks-queues-homework.playground/Contents.swift b/20160130-quicksort-stacks-queues-homework.playground/Contents.swift index 8875619..a71763d 100644 --- a/20160130-quicksort-stacks-queues-homework.playground/Contents.swift +++ b/20160130-quicksort-stacks-queues-homework.playground/Contents.swift @@ -86,6 +86,83 @@ Bad examples: ( ( ] ([)] func isBalanced(paren: [String]) -> Bool { } - */ +// add first item to array +// add second item to array +// if second item matches first item, remove both items +// else +// add third item to array +// if third item matches second/first item, remove both items + +var holdingStack: [String] = [] + + +func keyCheck(var holdingStack: [String], i: String) { + + if (holdingStack[0] == "(" && i == ")") { + holdingStack.removeAtIndex(0) + } + if (holdingStack[0] == "{" && i == "}") { + holdingStack.removeAtIndex(0) + } + if (holdingStack[0] == "[" && i == "]") { + holdingStack.removeAtIndex(0) + } else { + print("holding stack: \(holdingStack)") + print("i: \(i)") + } +} + +func isBalanced(var paren: [String]) -> Bool { + + if paren.count % 2 != 0 { + return false + } + for i in paren { + holdingStack.insert(paren[0], atIndex: 0) + paren.removeFirst() + keyCheck(holdingStack, i: i) + } + return true +} + +var brackets = ["(", ")", "{", "}", "[", "]"] +isBalanced(brackets) + + + + + + + +// make a STACK! + +struct Stack { + var items:[T] + + //push + mutating func push(element:T) { + items.append(element) + } + + //pop + mutating func pop() -> T? { + if items.count == 0 { return nil} + return items.removeLast() // remove from end + } + + //peek + func peek() -> T? { + return items.last + } + + //size + func size() -> Int { + return items.count + } +} + + + + From 6961767a28360d25cb1e4d52a3d969d140ab2f5c Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Fri, 5 Feb 2016 13:53:50 -0500 Subject: [PATCH 20/21] class notes added --- .DS_Store | Bin 10244 -> 10244 bytes .../Contents.swift | 56 ++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/.DS_Store b/.DS_Store index 276b1574c894eeeb6798448e36789f07ec4a7437..f17b5e2783d5e50a90bcbe7dc23db3f5c4a47c41 100644 GIT binary patch delta 104 zcmZn(XbG6$gHU^hRb^5hQ!Hj@)1_%??L9AjeAZ<^d795lH=n49_LyyDGu!V!$j ljSUQw9}4GAejvcjaude+Coa4>Qq+iRGrNL4D>f;8CIFkmB{cv5 delta 81 zcmZn(XbG6$I9U^hRb(&P^UHk0{;`8J0M9AjeAd^)*7IB0T#FgNp!i0_;0gd-T4 h8|oM)KNQZLtRu*``GTl9*TjaJ&Fl*HtWbdsOaO*h8~OkM diff --git a/20160128-homework-merge-sort.playground/Contents.swift b/20160128-homework-merge-sort.playground/Contents.swift index 816a8e3..d823335 100644 --- a/20160128-homework-merge-sort.playground/Contents.swift +++ b/20160128-homework-merge-sort.playground/Contents.swift @@ -101,3 +101,59 @@ reverse(&values) values +// 2) + +let blacklist: Set = ["crapple", "fandroid", "m$"] + +func moderate(message: String) -> Bool { + + let words = (message as NSString).componentsSeparatedByString(" ") + for word in words { + if blacklist.contains(word.lowercaseString) { + return false + } + } + return true +} +moderate("I would never use a crApple product!") +moderate("something else") + +// 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 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") + From 9222d7cec7720bb67a2c2307733d513c98144e15 Mon Sep 17 00:00:00 2001 From: shenayoshida Date: Thu, 11 Feb 2016 16:54:03 -0500 Subject: [PATCH 21/21] set up playground --- .DS_Store | Bin 10244 -> 12292 bytes .../Contents.swift | 48 ++++++++++++++++++ .../contents.xcplayground | 4 ++ .../contents.xcworkspacedata | 7 +++ .../timeline.xctimeline | 6 +++ 5 files changed, 65 insertions(+) create mode 100644 20160204-trees-homework.playground/Contents.swift create mode 100644 20160204-trees-homework.playground/contents.xcplayground create mode 100644 20160204-trees-homework.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 20160204-trees-homework.playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store index f17b5e2783d5e50a90bcbe7dc23db3f5c4a47c41..026eb203bff3bf56c3cd2074596468773f564abe 100644 GIT binary patch delta 829 zcmZn(Xh~3DU|?W$DortDV9)?EIe-{M3-ADmb_NCo?uiQOvdlmZBM<`>F#y>DAeI?} z0fP|`n@lWJo@}7Pq6kvJ0>nySVI&0%x(p=@MGUEv=ZToJKH99rz%W@rKz3sh6Z>X% z4i*ks83LMgChG_aPrfSZDeak)pPZDFp9ItZbOVDM5YPAz1`L}oif(2^S8xVdfus0r zMk!Pi7=sWBCa;qWnan55xA~Ps9lAk`HxTL=HVaD4Vbo-2@L}*|aAt62@M8#Oa78s2 zK&^i#UhbHjGG-)92h4r5aZh{rdG)~ z`LUoUn4=~b0TSik{94EpBE3PRj&bt>F;~XP!V>PA4@f97ZWd+y#3aZJvKZ(dZXn?b zaud+L@640=bqra7PGM-6?5HEn!31%%#^eN@nUn3++$ZOWb57<~Z^5u{@@t_&069ZA AkN^Mx diff --git a/20160204-trees-homework.playground/Contents.swift b/20160204-trees-homework.playground/Contents.swift new file mode 100644 index 0000000..9675b7b --- /dev/null +++ b/20160204-trees-homework.playground/Contents.swift @@ -0,0 +1,48 @@ +// trees homework: https://docs.google.com/document/d/1te7mLS06MEYwETFSbVBqMrIzJ43GTEo5uuCiWdB0fyE/edit#heading=h.za36ai6n5fth + +import Foundation + +/* Build and return the binary tree representation of the expression. The following characters should be treated as operators: + - * / + + You may use additional data structures—as suggested in the link above, you will at least want to use a stack. This can be done using the Array type in Swift. + +Test your tree by checking to see if the string returned by postorderDescription is equal to the input string. */ + +// Binary tree implementation: + +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 + } +} + +// template code: + +func parseExpression(input: String) -> Node? { + // Your implementation here! + + let operators: Set = ["+", "-", "*", "/"] + var stack: [Node] = [] + for character in input.characters { + // Do something for each character + + } + return nil +} + +let sampleImput = "ab+cde+**" +parseExpression(sampleImput) diff --git a/20160204-trees-homework.playground/contents.xcplayground b/20160204-trees-homework.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/20160204-trees-homework.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/20160204-trees-homework.playground/playground.xcworkspace/contents.xcworkspacedata b/20160204-trees-homework.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/20160204-trees-homework.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/20160204-trees-homework.playground/timeline.xctimeline b/20160204-trees-homework.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/20160204-trees-homework.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + +