From 052e405f1c4573ee3710e3d36c6ba2f228dd995d Mon Sep 17 00:00:00 2001 From: Elbotron Date: Mon, 11 Jan 2016 13:36:32 -0500 Subject: [PATCH 01/21] completed 01-09 assignment --- .DS_Store | Bin 0 -> 6148 bytes .../Contents.swift | 56 ++++++++++++++---- 2 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..218afbff4c7d73ddbcf117bc19e61868a41ed2d7 GIT binary patch literal 6148 zcmeH~&q~8U5XQf?Jruq4=*@s4dZ?I-;K@)TC3kn03dHE) z5XX(yR$v5-z+WREdv^zCxWqN?QQyBk?&KrvA;SZ1Sv$iy?&zh}J`OO&1Xp;(*q6*% zmrurVHY$s$E^_;Am30Y~Dqg|5)GCn>v%BOnj@sJLZWgr=}9` zllK^d?@?~;F5hFF?bC%!aoIW6iLR!{MUIJ|q`9RC@Xi+NY^a(y0!F|Hd=QZHAz~6u zf>x^j=%DjY0BW1X-k6uSgv3Nalc1F Int { + + let complete = Set(Range(start: 1, end: numbers.count + 1 + 1)) + let incomplete = Set(numbers) + + return Array(complete.subtract(incomplete))[0] +} -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. +print(findMissingNumber([2, 1, 3, 4, 6, 9, 10, 7, 8])) +print(findMissingNumber([1, 8, 7, 5, 2, 9, 3, 6, 4])) -https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/edit +/* 2) Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not */ -1) +func doesHaveRepeatingInts(numbers: [Int]) -> Bool { + + return numbers.count > Set(numbers).count +} -2) +print(doesHaveRepeatingInts([2, 2, 3, 4])) +print(doesHaveRepeatingInts([1, 3, 5, 6])) -3) -4) +/* 3) Given two lists, find the smallest value that exists in both lists. +L1 = [1,2,5,9] +L2 = [9, 20 , 5] */ +func smallestValueInCommon(numbers1: [Int], numbers2: [Int]) -> Int? { + + return Set(numbers1).intersect(Set(numbers2)).minElement() +} + +print(smallestValueInCommon([2, 3, 4, 5], numbers2: [0, 7, 8, 9])) +print(smallestValueInCommon([2, 3, 4, 5], numbers2: [3, 7, 2, 9])) + + +/* 4) Check to see if an integer is a palindrome don’t use casting */ + +func isPalindromeInt(var num:Int) -> Bool { + + var asArray = [Int]() + + while num > 0 { + + asArray.insert(num % 10, atIndex: 0) + num = num / 10 + } + + return asArray == asArray.reverse() +} + +print(isPalindromeInt(165283)) +print(isPalindromeInt(165561)) -*/ From 63a758fd487e92fb44573607f2c3b5f86bf270a7 Mon Sep 17 00:00:00 2001 From: elberdev Date: Mon, 11 Jan 2016 15:17:30 -0500 Subject: [PATCH 02/21] started big O --- .DS_Store | Bin 6148 -> 6148 bytes .../Contents.swift | 24 +++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/.DS_Store b/.DS_Store index 218afbff4c7d73ddbcf117bc19e61868a41ed2d7..1b7cbdb82dc159351b9c3577ebf5f0b0f4fc6984 100644 GIT binary patch delta 53 zcmZoMXffDuhLuSvXtDyE3y+ISN@+ Date: Thu, 14 Jan 2016 18:57:54 -0500 Subject: [PATCH 03/21] unfinished --- .DS_Store | Bin 6148 -> 6148 bytes .../Contents.swift | 64 +++++++++++++++++- .../contents.xcplayground | 2 +- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/.DS_Store b/.DS_Store index 1b7cbdb82dc159351b9c3577ebf5f0b0f4fc6984..52e788b70645cd13adbe391b95edc43e527ddcb0 100644 GIT binary patch delta 76 zcmZoMXfc@J&&a+pU^g=(`(y=HpUL}J`IuCMz)S%)9;RjeljT_bCJV6cWvrg8z~;i^ e;*wHYlFYzxX}4JC<^s0qj1wDzHnVg5@nQ11Afx?qv*`tia|p87RWM cbDPOzIW`xdC=b&%lg)B$(-}9jbNuB8060?>DF6Tf diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 3f31b29..07ce7b5 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -22,15 +22,77 @@ var str = "Hello, playground" return image; } + + ANSWER: assuming that calling each pixel takes 2 picoseconds of memory access and not counting the cost of + passing in the parameters to the awesomeFilter function or the return statement (because I'm not sure how to + calculate that), it comes out to 442,029,010 picoseconds. If the number of pixels is n by m, assuming the same + methodology, the formula (in picoseconds) is: + + 221,000mn + 29n + 10 + b) What is the time complexity of this method, expressed in big O notation? Assume the image is square, and both dimensions are ‘n’. - O(n^2) + ANSWER: O(n^2) + + 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: if we calculate using not only the pixels in the image but also the pixels around the image, the new + dimension of the array is (n + 2) by (n + 2) which reduces to n^2 + 4n + 4. Since n^2 is still the most relevant + determining factor, the complexity of the method is still O(n^2). + + +****************************************************************************************************************************** + +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 + } + } + + ANSWER: assuming (// do cool stuff) is no more complex than bar(xs), O(n^4) + + + 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. + + ANSWER: + + + 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. + + ANSWER: + + + d) An algorithm that searches the now-sorted list of friends for a specific friend (not including the time it takes to sort). + + ANSWER: Using binary search, O(log(n)) + */ diff --git a/HWfrom1-10-016(BigO).playground/contents.xcplayground b/HWfrom1-10-016(BigO).playground/contents.xcplayground index 5da2641..9f5f2f4 100644 --- a/HWfrom1-10-016(BigO).playground/contents.xcplayground +++ b/HWfrom1-10-016(BigO).playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file From 2fc2ae3d540dd7c8369ef68f2a912cf3449bf1e7 Mon Sep 17 00:00:00 2001 From: elberdev Date: Thu, 14 Jan 2016 19:14:03 -0500 Subject: [PATCH 04/21] correcting --- .../Contents.swift | 51 +++++++++++++++++-- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 07ce7b5..b43cf1e 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -14,12 +14,19 @@ var str = "Hello, playground" Pixel **awesomeFilter(Pixel image[][], int width, int height) { + for (int i = 0; i < width; i++) { + + for (int j = 0; j < height; j++) { + + [image[i][j] makeMoreAwesome]; } } - return image; + + + return image; } @@ -42,9 +49,7 @@ var str = "Hello, playground" 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: if we calculate using not only the pixels in the image but also the pixels around the image, the new - dimension of the array is (n + 2) by (n + 2) which reduces to n^2 + 4n + 4. Since n^2 is still the most relevant - determining factor, the complexity of the method is still O(n^2). + ANSWER: O(n^4) ****************************************************************************************************************************** @@ -94,5 +99,43 @@ var str = "Hello, playground" ANSWER: Using binary search, O(log(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. + + + 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. + + 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). + + + 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? + + +****************************************************************************************************************************** + + 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? + + +****************************************************************************************************************************** + + 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). + + +****************************************************************************************************************************** + + 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. + + +***************************************************************************************************************************** + + 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). + */ From 68dfa51c29ba0359a484d20861d944d0b2cfda0b Mon Sep 17 00:00:00 2001 From: elberdev Date: Thu, 14 Jan 2016 19:24:49 -0500 Subject: [PATCH 05/21] update --- .../Contents.swift | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index b43cf1e..203f056 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -73,7 +73,7 @@ var str = "Hello, playground" } } - ANSWER: assuming (// do cool stuff) is no more complex than bar(xs), O(n^4) + ANSWER: O(n^4) b) @@ -87,12 +87,12 @@ var str = "Hello, playground" 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. - ANSWER: + ANSWER: O(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. - ANSWER: + ANSWER: O(n log(n)) [a bit better than O(n^2), worse than O(n) d) An algorithm that searches the now-sorted list of friends for a specific friend (not including the time it takes to sort). @@ -107,24 +107,34 @@ var str = "Hello, playground" 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. + ANSWER: binary search tree + 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. 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). + ANSWER: array + 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? + 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: + ****************************************************************************************************************************** @@ -132,10 +142,14 @@ var str = "Hello, playground" 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. + 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 3e7d7cb2b364cf0015564103b9ba51cc0d5248f6 Mon Sep 17 00:00:00 2001 From: elberdev Date: Sat, 16 Jan 2016 14:42:58 -0500 Subject: [PATCH 06/21] some changes --- HWfrom1-10-016(BigO).playground/Contents.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 203f056..8e9e67d 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -126,14 +126,14 @@ var str = "Hello, playground" 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: + ANSWER: O(n) ****************************************************************************************************************************** 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: + ANSWER: O(m) ****************************************************************************************************************************** @@ -142,7 +142,7 @@ var str = "Hello, playground" 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. - ANSWER: + ANSWER: O(log(n)) [every time you divide by two each pass it's a hint of O(long(n)) ***************************************************************************************************************************** From 5c5bd06a34a2bbf2016175fe5f06b2fec03ed6d8 Mon Sep 17 00:00:00 2001 From: elberdev Date: Sun, 17 Jan 2016 10:56:10 -0500 Subject: [PATCH 07/21] further corrections to BigO homework --- HWfrom1-10-016(BigO).playground/Contents.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 8e9e67d..edd920c 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -14,7 +14,7 @@ var str = "Hello, playground" Pixel **awesomeFilter(Pixel image[][], int width, int height) { - + 14 * width for (int i = 0; i < width; i++) { @@ -35,7 +35,9 @@ var str = "Hello, playground" calculate that), it comes out to 442,029,010 picoseconds. If the number of pixels is n by m, assuming the same methodology, the formula (in picoseconds) is: - 221,000mn + 29n + 10 + 229mn + 19n + 10 + + O(mn) @@ -61,17 +63,17 @@ var str = "Hello, playground" for (int j = 0; j < n; j++) { foo(xs); } - } + } // n^3 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { bar(xs); } - } + } // n^4 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { // do cool stuff } - } + } // n^2 ANSWER: O(n^4) @@ -92,7 +94,7 @@ var str = "Hello, playground" 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. - ANSWER: O(n log(n)) [a bit better than O(n^2), worse than O(n) + ANSWER: O(n^2) + O(n log(n)) + O(n) = 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). From d51346d735ca1e2a90739b0e91f032f926264e3a Mon Sep 17 00:00:00 2001 From: elberdev Date: Sun, 17 Jan 2016 15:08:46 -0500 Subject: [PATCH 08/21] corrections --- HWfrom1-10-016(BigO).playground/Contents.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index edd920c..879a774 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -94,7 +94,7 @@ var str = "Hello, playground" 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. - ANSWER: O(n^2) + O(n log(n)) + O(n) = O(n^2) + ANSWER: O(n^2) + O(n log(n)) + O(n) = 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). @@ -144,7 +144,7 @@ var str = "Hello, playground" 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. - ANSWER: O(log(n)) [every time you divide by two each pass it's a hint of O(long(n)) + ANSWER: O(log(n)) [every time you divide by two each pass it's a hint of O(log(n)) ***************************************************************************************************************************** From d5a9c7c481837c154b16660d63f5950c64787342 Mon Sep 17 00:00:00 2001 From: elberdev Date: Mon, 18 Jan 2016 11:30:11 -0500 Subject: [PATCH 09/21] upstream merge and hackerrank submissions --- .DS_Store | Bin 6148 -> 8196 bytes .../Contents.swift | 2 +- .../Contents.swift | 67 +++++++++++++++--- .../contents.xcplayground | 2 +- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/.DS_Store b/.DS_Store index 52e788b70645cd13adbe391b95edc43e527ddcb0..9956791dee4207c710776ae47a62c8e985390411 100644 GIT binary patch literal 8196 zcmeI1K}*9h6vzKj4}zB+hnMxBpvc652!e+)Dkvx(cJSzAn^V}_&bDY)o;~?N z{0#nI611fo4DCep1(JW3rZa ziwervz;NuafdIeRlIF*J_g$}+N1{Kx_i9q1V_cdzg$fr8-mNLk#20*&g_Dq1V`{Gl%iPhw(ETAE6jKJN*ZC zI85kNOC_KLvIOLHS;8KUafYi@{9a}xzl9alaDxl>4zQ0)mNIG;>*%9{Q{19$TZZgw zcG{I%qu1@bX_nve^xf@6QFAzG0I$za_jZQduDjc7PR)1!JjtXk7ry)j-uIIbw^VZU zo?`xxzxM?7+`Xr{+iNdnxL0^mC8t$xrB}Jm+c=tRb?+?F=Qlm3_`0{|+a424bMctw zXkC^^aNHzENAZCBLz^TUSVKg;X6*_s?i?+g;E;R90Oyl2F5Qvw6lY8Fe|)iPWU8}$ zKAY*!mVRz^dvjVSfdqkBS1(HZzdQc?KVhL&C7=ZU8UZsG)uJlz%E#83`;%BZV6HPu tl5w$9Yr-OR5WwFbe;BeJaH!aXUSp?hVfyidfJD5a-+%iEwD^Yv-U02+SnmJ; delta 180 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jH7iU^g=(`(y<{pUI~L`IuCM zKuo^ed>5CL(voBbhD$q_^-q=)^qVXo$j!WSnLUu5IQgxxJy^9;_2gH=PROcvi*-(Z zAsn>%ljwBD#q1m$g3LhMfIxs7NVtM5-dOmZc{0C@C&(ELOb{o5T*b%7 diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 879a774..823e226 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -151,7 +151,7 @@ var str = "Hello, playground" 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: ?? */ diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..b6a178f 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -4,26 +4,77 @@ import UIKit var str = "Hello, playground" - /* - Question 1: https://www.hackerrank.com/challenges/minimum-draws -Copy and paste your code: +Copy and paste your code: */ + +let testCases = Int(readLine(stripNewline: true)!)! -What is the big O runtime of your code?: +for _ in 0.. - + \ No newline at end of file From 0bd30a9356943361f3a293e33a409fe4b45e422d Mon Sep 17 00:00:00 2001 From: elberdev Date: Wed, 20 Jan 2016 08:22:47 -0500 Subject: [PATCH 10/21] updates --- .DS_Store | Bin 8196 -> 8196 bytes .gitignore | 0 .../Contents.swift | 0 .../contents.xcplayground | 0 .../contents.xcworkspacedata | 0 .../timeline.xctimeline | 0 .../Contents.swift | 0 .../contents.xcplayground | 0 .../contents.xcworkspacedata | 0 .../timeline.xctimeline | 0 .../Contents.swift | 0 .../contents.xcplayground | 0 .../contents.xcworkspacedata | 0 .../timeline.xctimeline | 0 LICENSE | 0 README.md | 0 16 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 .DS_Store mode change 100644 => 100755 .gitignore mode change 100644 => 100755 HWfrom1-09-16(SwiftIntro).playground/Contents.swift mode change 100644 => 100755 HWfrom1-09-16(SwiftIntro).playground/contents.xcplayground mode change 100644 => 100755 HWfrom1-09-16(SwiftIntro).playground/playground.xcworkspace/contents.xcworkspacedata mode change 100644 => 100755 HWfrom1-09-16(SwiftIntro).playground/timeline.xctimeline mode change 100644 => 100755 HWfrom1-10-016(BigO).playground/Contents.swift mode change 100644 => 100755 HWfrom1-10-016(BigO).playground/contents.xcplayground mode change 100644 => 100755 HWfrom1-10-016(BigO).playground/playground.xcworkspace/contents.xcworkspacedata mode change 100644 => 100755 HWfrom1-10-016(BigO).playground/timeline.xctimeline mode change 100644 => 100755 HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift mode change 100644 => 100755 HWfrom1-14-16(Logic+Discrete_Math).playground/contents.xcplayground mode change 100644 => 100755 HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/contents.xcworkspacedata mode change 100644 => 100755 HWfrom1-14-16(Logic+Discrete_Math).playground/timeline.xctimeline mode change 100644 => 100755 LICENSE mode change 100644 => 100755 README.md diff --git a/.DS_Store b/.DS_Store old mode 100644 new mode 100755 index 9956791dee4207c710776ae47a62c8e985390411..d7954780c63ea556988c5f49f5000e26ebc87deb GIT binary patch delta 266 zcmZp1XmOa}FDSymz`)4BAi$85ZWx@LpIfl8a2or>2Eonj94s95AXyd$J%)6KOokGe z3ZNJeJSYPaaJk%k7nhXMl4J&kONS2c1S>$2OTj5ukO8q3tb@$~M5!=%FoZLtF%&W6 zPu3H(n|w-;Z*!V}6AP2tuE{ncag#NKc$g1wV3=$z5 Date: Thu, 21 Jan 2016 06:37:41 -0500 Subject: [PATCH 11/21] sudoku done --- .DS_Store | Bin 8196 -> 8196 bytes .../Contents.swift | 95 ++++++++++++++++-- .../contents.xcplayground | 2 +- 3 files changed, 89 insertions(+), 8 deletions(-) diff --git a/.DS_Store b/.DS_Store index d7954780c63ea556988c5f49f5000e26ebc87deb..6c1b40af7f411f66f3e0fe3d03334d5c9c097a1e 100755 GIT binary patch literal 8196 zcmeHM&1xGl5dPGf66hs|9853SLsOcRlrEHpKnsi6mK0ig@S(>z_%DUHHtxD1m#~kN zYjbSq3-k%{1bv2nBMGBrZ7{@Z0v>_pTT2?v>^HLHkpY;-q!R;m02{1=n~y1G6zP{* zDi3_i7ST{2XZ^U@9u7vOk=mdPC@afGkvVT>ULh_R1XIHZj62G7yNIeqCN!$*9llu^6%aD+3Q;DVk? z%Y=QMqQ5);mN|F$zSo!H_VWA=`C9he1-{V|zqd<#$7EbscZ@g4cOPFCWBKiqyU$qQ z*4-A}(Z>R#OPR6Aqn&bp29K|&>-G~@rF@Ipc!8AogHL00xq`bm#%HeG45y26{Op<> zS7`TNVV32-Jx=8#kjTP-S%wUJ_P$sjDsL<603|gVvM+}5SxtU*tYHBa*@4f zd#@@xL9G6~b3bw_d7t1LRH>aA@sSv5Sv>;}oyof7|L^DD|F7OLwWtgz11n>|)Y4|! zpgEsgmkdj;9kI4prO3G4p;e(MTmO3#10 I9M?wt2SKKfZ~y=R delta 109 zcmZp1XmQx^UXW3Ba)W^JWL;5JHU [Int] { + + // for our purposes x is the horizontal axis and y the vertical + // for this to make sense with the visual layout of the array of arrays + // we need to index the numbers like so: number[y][x] + + //check if !nil + if board[y][x] != nil { + + // if !nil return the number that is already there as the single member of + // the return array + return [board[y][x]!] + } + + var possibilities: Set = Set([1, 2, 3, 4, 5, 6, 7, 8, 9]) + + // check column + for i in 0...8 { + + if board[i][x] != nil { + + // exclude numbers in column + possibilities.remove(board[i][x]!) + } + } + + // check row + for i in 0...8 { + + // exclude numbers in row + if board[y][i] != nil { + + possibilities.remove(board[y][i]!) + } + } + + let indices: [[Int?]] = [[0, 2], [3, 5], [6, 8]] + var xSquareBounds: [Int?] = [nil, nil] + var ySquareBounds: [Int?] = [nil, nil] + + // figure out the square bounds + for numArray in indices { + + if (x >= numArray[0] && x <= numArray[1]) { + + xSquareBounds = numArray + } + if (y >= numArray[0] && y <= numArray[1]) { + + ySquareBounds = numArray + } + } + + // check square + for i in ySquareBounds[0]!...ySquareBounds[1]! { + + for j in xSquareBounds[0]!...xSquareBounds[1]! { + + if board[i][j] != nil { + + // exclude numbers in square + possibilities.remove(board[i][j]!) + } + } + } + + return Array(possibilities) + +} + +var sudokuBoard: [[Int?]] = [ + [5 , nil, 8 , nil, 7 , 3 , 1 , 9 , nil], + [9 , nil, nil, 6 , nil, nil, 4 , nil, 8 ], + [nil, nil, nil, 9 , nil, 8 , nil, 3 , 5 ], + [nil, 7 , nil, nil, nil, nil, nil, 6 , nil], + [nil, nil, 2 , nil, nil, nil, 9 , nil, nil], + [nil, 1 , nil, nil, nil, nil, nil, 8 , nil], + [1 , 9 , nil, 3 , nil, 6 , nil, nil, nil], + [2 , nil, 3 , nil, nil, 7 , nil, nil, 9 ], + [nil, 8 , 7 , 1 , 9 , nil, 3 , nil, 4 ]] + +print(sudokuSpaceOptions(sudokuBoard, x: 2, y: 6)) +print(sudokuSpaceOptions(sudokuBoard, x: 2, y: 0)) - -2) +/* +2) @@ -23,4 +104,4 @@ Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB -*/ \ No newline at end of file +*/ diff --git a/HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground b/HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground index 5da2641..9f5f2f4 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground +++ b/HWFrom1-17-16(Lists and Sorts).playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file From 1fdcbee755c80876442669db1bededda18f71696 Mon Sep 17 00:00:00 2001 From: elberdev Date: Thu, 21 Jan 2016 06:55:59 -0500 Subject: [PATCH 12/21] rotated matrix --- .../Contents.swift | 24 ++++++++++++++++++- 1 file changed, 23 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 aeeff9a..2d1a15a 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -82,6 +82,7 @@ func sudokuSpaceOptions(board: [[Int?]], x: Int, y: Int) -> [Int] { } var sudokuBoard: [[Int?]] = [ + [5 , nil, 8 , nil, 7 , 3 , 1 , 9 , nil], [9 , nil, nil, 6 , nil, nil, 4 , nil, 8 ], [nil, nil, nil, 9 , nil, 8 , nil, 3 , 5 ], @@ -96,10 +97,31 @@ print(sudokuSpaceOptions(sudokuBoard, x: 2, y: 6)) print(sudokuSpaceOptions(sudokuBoard, x: 2, y: 0)) /* -2) +2) */ +func rotateMatrixByNinetyDegrees(matrix: [[Int]]) -> [[Int]] { + + var rotated = [[Int]](count: matrix.count, repeatedValue: [Int](count: matrix[0].count, repeatedValue: 0)) + + for i in 0.. Date: Thu, 21 Jan 2016 18:49:03 -0500 Subject: [PATCH 13/21] 4 element sort --- .../Contents.swift | 39 +++++++++++++++++-- 1 file changed, 36 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 2d1a15a..aa3a297 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -122,8 +122,41 @@ let test = [[1,2,3,4], print(rotateMatrixByNinetyDegrees(test)) /* -3) - +3) */ +func sortFourElements(inout nums: [Int]) -> [Int] { + + var temp: Int + let indices = [[0,1], [2,3], [0,2], [1,3], [1,2]] + + for i in 0.. nums[indices[i][1]] { + + temp = nums[indices[i][0]] + nums[indices[i][0]] = nums[indices[i][1]] + nums[indices[i][1]] = temp + } + } + + return nums + +} -*/ +var nums = [4, 3, 2, 1] +var nums2 = [4, 3, 1, 2] +var nums3 = [4, 2, 1, 3] +var nums4 = [2, 4, 3, 1] +var nums5 = [3, 4, 1, 2] +var nums6 = [3, 4, 2, 1] +var nums7 = [2, 3, 1, 4] +var nums8 = [3, 2, 4, 1] + +print(sortFourElements(&nums)) +print(sortFourElements(&nums2)) +print(sortFourElements(&nums3)) +print(sortFourElements(&nums4)) +print(sortFourElements(&nums5)) +print(sortFourElements(&nums6)) +print(sortFourElements(&nums7)) +print(sortFourElements(&nums8)) From f68d4fe2e8d2698fdfff189f88e4a76593d44029 Mon Sep 17 00:00:00 2001 From: elberdev Date: Thu, 21 Jan 2016 20:05:25 -0500 Subject: [PATCH 14/21] added extra examples --- .DS_Store | Bin 8196 -> 8196 bytes .../Contents.swift | 9 +++++++++ 2 files changed, 9 insertions(+) diff --git a/.DS_Store b/.DS_Store index 6c1b40af7f411f66f3e0fe3d03334d5c9c097a1e..c171e330966cdffd01cc49008df815d0d1e9c8b5 100755 GIT binary patch delta 514 zcmZp1XmQx^UWCzR@&^&y$(sd4tr+weiW!m^bQ#PUEE$Y}& [[Int]] { + // create an array of the same dimensions with all zeroes var rotated = [[Int]](count: matrix.count, repeatedValue: [Int](count: matrix[0].count, repeatedValue: 0)) for i in 0.. Date: Thu, 28 Jan 2016 21:21:19 -0500 Subject: [PATCH 15/21] recursion update --- .DS_Store | Bin 8196 -> 10244 bytes .../Contents.swift.orig | 175 ++++++++++++++++++ .../Contents.swift | 53 ++++-- .../contents.xcplayground | 2 +- 4 files changed, 215 insertions(+), 15 deletions(-) create mode 100644 HWFrom1-17-16(Lists and Sorts).playground/Contents.swift.orig diff --git a/.DS_Store b/.DS_Store index c171e330966cdffd01cc49008df815d0d1e9c8b5..85da27bce15d55c7e5c50e3d3cd201ca65b7ca57 100755 GIT binary patch literal 10244 zcmeI1Piqu07{;HeODX7~XF(nD4=qyZ)KU>V49hACir^kRx@EWB7P{TC-BmnT_9OT~ z`~vo9zd-RWc=q7ctIwOHL&9ufwV5jN29jqsle~HJOD40)07&YiP6ng^<`_liFEN=A zwbhZh z%}3%|PTUS9F|E^nYKkMtt!kd)i!Qb?4EOKf-uU-={YIlR8n%HipN~En zLq)w_zjNxny)ExdU^bVEH}M3ou!#Zs=pn-rmhpgThBYjpiMRA+6GJ@3E>kJBNDue0 zgLS;4r;;<`y-w)wtsgOM`~5vU5Vzy`?emIlJK&9$_=pyI_O$4XJ_baiSPVHDlS>mgt`_t58E2(DMQz+cgSyYF0X8^;H}D$IId_NHnvU`9 zLo%+gHIM(%+IyKI)wWiw4(HHXIBqTJ+oBn_$KHCA;l4L+GFC&zlw-tIh1%p;4ou!u zD^LElnpV7VI$ocZu2j(%@7bxam2g}>NMCN0o~ISuE5_v|pPM$<`2j~}o8z%N-Lo_E zF`i|ge@ot!(R}uq$F&@C&c)}nH2+ty$~WyF5>9`{uo?Q<5C+my3j*uYv0RWPJbGHBh diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift.orig b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift.orig new file mode 100644 index 0000000..55157fc --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift.orig @@ -0,0 +1,175 @@ +//: Playground - noun: a place where people can play + +import UIKit + +/* + + +Work on your solutions here. + +Link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit#heading=h.za36ai6n5fth + +1) */ + +func sudokuSpaceOptions(board: [[Int?]], x: Int, y: Int) -> [Int] { + + // for our purposes x is the horizontal axis and y the vertical + // for this to make sense with the visual layout of the array of arrays + // we need to index the numbers like so: number[y][x] + + //check if !nil + if board[y][x] != nil { + + // if !nil return the number that is already there as the single member of + // the return array + return [board[y][x]!] + } + + var possibilities: Set = Set([1, 2, 3, 4, 5, 6, 7, 8, 9]) + + // check column + for i in 0...8 { + + if board[i][x] != nil { + + // exclude numbers in column + possibilities.remove(board[i][x]!) + } + } + + // check row + for i in 0...8 { + + // exclude numbers in row + if board[y][i] != nil { + + possibilities.remove(board[y][i]!) + } + } + + let indices: [[Int?]] = [[0, 2], [3, 5], [6, 8]] + var xSquareBounds: [Int?] = [nil, nil] + var ySquareBounds: [Int?] = [nil, nil] + + // figure out the square bounds + for numArray in indices { + + if (x >= numArray[0] && x <= numArray[1]) { + + xSquareBounds = numArray + } + if (y >= numArray[0] && y <= numArray[1]) { + + ySquareBounds = numArray + } + } + + // check square + for i in ySquareBounds[0]!...ySquareBounds[1]! { + + for j in xSquareBounds[0]!...xSquareBounds[1]! { + + if board[i][j] != nil { + + // exclude numbers in square + possibilities.remove(board[i][j]!) + } + } + } + + return Array(possibilities) + +} + +var sudokuBoard: [[Int?]] = [ + + [5 , nil, 8 , nil, 7 , 3 , 1 , 9 , nil], + [9 , nil, nil, 6 , nil, nil, 4 , nil, 8 ], + [nil, nil, nil, 9 , nil, 8 , nil, 3 , 5 ], + [nil, 7 , nil, nil, nil, nil, nil, 6 , nil], + [nil, nil, 2 , nil, nil, nil, 9 , nil, nil], + [nil, 1 , nil, nil, nil, nil, nil, 8 , nil], + [1 , 9 , nil, 3 , nil, 6 , nil, nil, nil], + [2 , nil, 3 , nil, nil, 7 , nil, nil, 9 ], + [nil, 8 , 7 , 1 , 9 , nil, 3 , nil, 4 ]] + +print(sudokuSpaceOptions(sudokuBoard, x: 2, y: 6)) +print(sudokuSpaceOptions(sudokuBoard, x: 2, y: 0)) + +/* +2) */ + +func rotateMatrixByNinetyDegrees(matrix: [[Int]]) -> [[Int]] { + + // create an array of the same dimensions with all zeroes + var rotated = [[Int]](count: matrix.count, repeatedValue: [Int](count: matrix[0].count, repeatedValue: 0)) + + for i in 0.. [Int] { + + var temp: Int + let indices = [[0,1], [2,3], [0,2], [1,3], [1,2]] + + for i in 0.. nums[indices[i][1]] { + + temp = nums[indices[i][0]] + nums[indices[i][0]] = nums[indices[i][1]] + nums[indices[i][1]] = temp + } + } + + return nums + +} + +var nums = [4, 3, 2, 1] +var nums2 = [4, 3, 1, 2] +var nums3 = [4, 2, 1, 3] +var nums4 = [2, 4, 3, 1] +var nums5 = [3, 4, 1, 2] +var nums6 = [3, 4, 2, 1] +var nums7 = [2, 3, 1, 4] +var nums8 = [3, 2, 4, 1] + +print(sortFourElements(&nums)) +print(sortFourElements(&nums2)) +print(sortFourElements(&nums3)) +print(sortFourElements(&nums4)) +print(sortFourElements(&nums5)) +print(sortFourElements(&nums6)) +print(sortFourElements(&nums7)) +print(sortFourElements(&nums8)) +======= +*/ +>>>>>>> upstream/master diff --git a/HWFrom1-24(Recursion).playground/Contents.swift b/HWFrom1-24(Recursion).playground/Contents.swift index 1c44504..981e764 100644 --- a/HWFrom1-24(Recursion).playground/Contents.swift +++ b/HWFrom1-24(Recursion).playground/Contents.swift @@ -1,25 +1,50 @@ /* - - Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit#heading=h.za36ai6n5fth - - - */ +import Foundation //Question 1 - - - - +func fib(n: Int) -> Int { + + var a = 1 + var b = 1 + + for _ in 0.. Int { + let stepCount = Int(arc4random_uniform(3)) - 1 + stepNum += stepCount; + switch(stepCount) { + case -1: print("Ouch \(stepNum)") + case 1: print("Yay \(stepNum)") + default: print("Beep \(stepNum)") + } + return stepCount +} + +func stepUp() { + + switch tryStep() { + case 1: + return + case -1: + stepUp() + stepUp() + default: + stepUp() + } +} + +//Question 3 diff --git a/HWFrom1-24(Recursion).playground/contents.xcplayground b/HWFrom1-24(Recursion).playground/contents.xcplayground index 5da2641..9f5f2f4 100644 --- a/HWFrom1-24(Recursion).playground/contents.xcplayground +++ b/HWFrom1-24(Recursion).playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file From 0167b95fc0356e5ebf5daba6825f0f8b69334bfc Mon Sep 17 00:00:00 2001 From: elberdev Date: Thu, 28 Jan 2016 21:25:46 -0500 Subject: [PATCH 16/21] removed duplicate swift file in 1/17 assignment --- .../Contents.swift.orig | 175 ------------------ 1 file changed, 175 deletions(-) delete mode 100644 HWFrom1-17-16(Lists and Sorts).playground/Contents.swift.orig diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift.orig b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift.orig deleted file mode 100644 index 55157fc..0000000 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift.orig +++ /dev/null @@ -1,175 +0,0 @@ -//: Playground - noun: a place where people can play - -import UIKit - -/* - - -Work on your solutions here. - -Link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit#heading=h.za36ai6n5fth - -1) */ - -func sudokuSpaceOptions(board: [[Int?]], x: Int, y: Int) -> [Int] { - - // for our purposes x is the horizontal axis and y the vertical - // for this to make sense with the visual layout of the array of arrays - // we need to index the numbers like so: number[y][x] - - //check if !nil - if board[y][x] != nil { - - // if !nil return the number that is already there as the single member of - // the return array - return [board[y][x]!] - } - - var possibilities: Set = Set([1, 2, 3, 4, 5, 6, 7, 8, 9]) - - // check column - for i in 0...8 { - - if board[i][x] != nil { - - // exclude numbers in column - possibilities.remove(board[i][x]!) - } - } - - // check row - for i in 0...8 { - - // exclude numbers in row - if board[y][i] != nil { - - possibilities.remove(board[y][i]!) - } - } - - let indices: [[Int?]] = [[0, 2], [3, 5], [6, 8]] - var xSquareBounds: [Int?] = [nil, nil] - var ySquareBounds: [Int?] = [nil, nil] - - // figure out the square bounds - for numArray in indices { - - if (x >= numArray[0] && x <= numArray[1]) { - - xSquareBounds = numArray - } - if (y >= numArray[0] && y <= numArray[1]) { - - ySquareBounds = numArray - } - } - - // check square - for i in ySquareBounds[0]!...ySquareBounds[1]! { - - for j in xSquareBounds[0]!...xSquareBounds[1]! { - - if board[i][j] != nil { - - // exclude numbers in square - possibilities.remove(board[i][j]!) - } - } - } - - return Array(possibilities) - -} - -var sudokuBoard: [[Int?]] = [ - - [5 , nil, 8 , nil, 7 , 3 , 1 , 9 , nil], - [9 , nil, nil, 6 , nil, nil, 4 , nil, 8 ], - [nil, nil, nil, 9 , nil, 8 , nil, 3 , 5 ], - [nil, 7 , nil, nil, nil, nil, nil, 6 , nil], - [nil, nil, 2 , nil, nil, nil, 9 , nil, nil], - [nil, 1 , nil, nil, nil, nil, nil, 8 , nil], - [1 , 9 , nil, 3 , nil, 6 , nil, nil, nil], - [2 , nil, 3 , nil, nil, 7 , nil, nil, 9 ], - [nil, 8 , 7 , 1 , 9 , nil, 3 , nil, 4 ]] - -print(sudokuSpaceOptions(sudokuBoard, x: 2, y: 6)) -print(sudokuSpaceOptions(sudokuBoard, x: 2, y: 0)) - -/* -2) */ - -func rotateMatrixByNinetyDegrees(matrix: [[Int]]) -> [[Int]] { - - // create an array of the same dimensions with all zeroes - var rotated = [[Int]](count: matrix.count, repeatedValue: [Int](count: matrix[0].count, repeatedValue: 0)) - - for i in 0.. [Int] { - - var temp: Int - let indices = [[0,1], [2,3], [0,2], [1,3], [1,2]] - - for i in 0.. nums[indices[i][1]] { - - temp = nums[indices[i][0]] - nums[indices[i][0]] = nums[indices[i][1]] - nums[indices[i][1]] = temp - } - } - - return nums - -} - -var nums = [4, 3, 2, 1] -var nums2 = [4, 3, 1, 2] -var nums3 = [4, 2, 1, 3] -var nums4 = [2, 4, 3, 1] -var nums5 = [3, 4, 1, 2] -var nums6 = [3, 4, 2, 1] -var nums7 = [2, 3, 1, 4] -var nums8 = [3, 2, 4, 1] - -print(sortFourElements(&nums)) -print(sortFourElements(&nums2)) -print(sortFourElements(&nums3)) -print(sortFourElements(&nums4)) -print(sortFourElements(&nums5)) -print(sortFourElements(&nums6)) -print(sortFourElements(&nums7)) -print(sortFourElements(&nums8)) -======= -*/ ->>>>>>> upstream/master From 84e26c6496e29656ef8b4d58485db1217773eb88 Mon Sep 17 00:00:00 2001 From: elberdev Date: Thu, 28 Jan 2016 21:28:47 -0500 Subject: [PATCH 17/21] .gitignore --- .DS_Store | Bin 10244 -> 10244 bytes .gitignore | 2 ++ 2 files changed, 2 insertions(+) diff --git a/.DS_Store b/.DS_Store index 85da27bce15d55c7e5c50e3d3cd201ca65b7ca57..d365d8a607b7954de8513c19cb72c3726cbda014 100755 GIT binary patch delta 83 zcmZn(XbIR5Aj+hFVzQiA*kl7qp2<>TQIi`4xhG4CWil2_o+$1;*+7DC^Bu7gK4Mg9 GFaiKUg&AD{ delta 83 zcmZn(XbIR5Aj;%$ce0#V*kl7qp2<>TQIi`4xhG4CWin2fJW Date: Sun, 31 Jan 2016 07:53:05 -0500 Subject: [PATCH 18/21] added 1-30 hw --- .DS_Store | Bin 10244 -> 10244 bytes .../Contents.swift | 17 ++++++++++++++++- .../contents.xcplayground | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/.DS_Store b/.DS_Store index d365d8a607b7954de8513c19cb72c3726cbda014..4da7e6f053fe88a473fe4a24a52e4300b501c9a4 100755 GIT binary patch delta 357 zcmZn(XbIRbUBc6l!Gj^3!HuDaA)g_a!H_|h!I;4SNE$MjF=#LZGL$l8G9)u(GXw+W zix^56v>AMWBE>*fF^~61-U2pigUnqiEoaP PTFpAKpkXt+!e4d(v9ugv diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift index bcf8eda..b7a3a72 100644 --- a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift @@ -1,5 +1,20 @@ //: Playground - noun: a place where people can play +// FOR REFERENCE, insertion sort +func insertionSort(inout numberList:[Int]){ + var x, y, key : Int + for (x = 0; x < numberList.count; x++) { + + key = numberList[x] + for (y = x; y > -1; y--) { + if (key < numberList[y]) { + numberList.removeAtIndex(y + 1) + numberList.insert(key, atIndex: y) + } + } + } +} + //Answer the questions in the homework below //https://docs.google.com/document/d/1KlK3PmRMybmHS5db_AP11uHXIcbIeM5vHuuAZOV3xME/edit# @@ -13,4 +28,4 @@ //5) -//6) \ No newline at end of file +//6) diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground index 5da2641..9f5f2f4 100644 --- a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file From 2a94a2c691ab311f2602f7a0482eaa3504edcc9c Mon Sep 17 00:00:00 2001 From: elberdev Date: Wed, 3 Feb 2016 17:01:24 -0500 Subject: [PATCH 19/21] weekend hw --- .DS_Store | Bin 10244 -> 12292 bytes .../Contents.swift | 127 +++++++++++++++++- 2 files changed, 121 insertions(+), 6 deletions(-) diff --git a/.DS_Store b/.DS_Store index 4da7e6f053fe88a473fe4a24a52e4300b501c9a4..137d5a45f0b97c1a88e4a87ad0d43407e9966309 100755 GIT binary patch literal 12292 zcmeHNJ8u&~5S}vuMFOGlC=dlX5r`562iq|rNED}F5P={Rj0jMGjbDipW6KW^1(7>S z8u$V9bkLA85fWuYnw0zkS_&Gz-94XobGBrWJNuOENqb}O?as`8Gy9rdA~I&{*(lK% z5e*_SI&=)LcSPnwDO3(;hzR4fZI<{y+Qphm5{B;_Bm z*yjsemMEy-8Uc-fMnEH=5jbcFu(=n{;I8>zB@un7P_4W8L+F{8yd`|MzbrAez0w3U^K=bfmZPimMD?>vpZaV~Ni*5cPQUNFpDCKP{Or_;AZZpbKYK)XXpSCZCQqw= z*7xyD(oFWV(Vf0qQ_-yDXQL;Bq*=iHY>^(q%kE;`65r)#AAnjvS3#0yZ7_>^xISC! z>ZEQHc=SN3)Tg$;vRzM)q&oejhf>?vb)t=3*(t5q=Oi)nvK6+8Q&pU$@*QCs|Cg`> zN@MNUakZ`bcIZ4%^+}Rszn$Y^59FI9$7ViDQtf0hpX~K>1_B3~XXUCjf4!2wN%(JU ze&cF7^`t$d>zmR>`TRZ4&+f%tfe2QAEnj?%Jx3n?I0s*>@lsu#`Ct4Et;Bxb1Kx6b z*VlM^J>>0p*oN1<6}&}^W0?1S8IvgAb4TpnKMTAIbidP%TJ?(SXMx2YIV#wS8ykD( zC?VXq0 z-*VsfRhna)9!OK|mST+?cg=lK>@%C=O)I|Cn*ExwzAopz%Rl^)BH3wcP3-D?Xp1y@ z`zS@V3-3=OuKpB5#I9+V|GfDX`V3om#978ab&k1`cbuI*vE?mi6=#d2Syqj-5*_9( zqA_;gpy$t20fmCf4Wt_n($PBU+=oW4u;R 4 { + + var medianIndex: Int + var candidates: [Int] = [array.first!, array[(last+first) / 2], array.last!] + + if (candidates[0] >= candidates[1] && candidates[0] <= candidates[2]) || + (candidates[0] >= candidates[2] && candidates[0] <= candidates[1]) { + + medianIndex = first + + } else if (candidates[1] >= candidates[2] && candidates[1] <= candidates[0]) || + (candidates[1] >= candidates[0] && candidates[1] <= candidates[2]) { + + medianIndex = array.count / 2 + + } else { + + medianIndex = last + } + + if medianIndex != first { + + swap(&array[first], &array[medianIndex]) + } + } +} + +// function to sort values in the array in relation to the pivot point +func partition(inout array: [Int], first: Int, last: Int) -> Int { + + let pivotIndex = first + var rightIndex = last + var leftIndex = pivotIndex + 1 + + while leftIndex <= rightIndex { + + if array[leftIndex] > array[pivotIndex] { + + if array[rightIndex] < array[pivotIndex] { + + swap(&array[rightIndex], &array[leftIndex]) + + } else { + + rightIndex-- + } + + } else { + + leftIndex++ + } + } + + if pivotIndex != rightIndex { + + swap(&array[pivotIndex], &array[rightIndex]) + } + + return rightIndex +} + +// putting everything together for recursive goodness +func quickSort(inout array: [Int], first: Int, last: Int) { + + if last - first <= 0 { + return + } + median(&array, first: first, last: last) + let splitPoint = partition(&array, first: first, last: last) + quickSort(&array, first: 0, last: splitPoint - 1) + quickSort(&array, first: splitPoint + 1, last: last) +} + + +/* 4) */ + +func generateArray(size: Int, valueUpperBound: Int) -> [Int] { + + var array = Array(count: size, repeatedValue: 0) + + for i in 0.. Bool { +// +// for i in 0.. Date: Wed, 3 Feb 2016 20:10:56 -0500 Subject: [PATCH 20/21] worked on ([{}]) problem --- .DS_Store | Bin 12292 -> 12292 bytes .../Contents.swift | 140 +++++++++++++++++- 2 files changed, 132 insertions(+), 8 deletions(-) diff --git a/.DS_Store b/.DS_Store index 137d5a45f0b97c1a88e4a87ad0d43407e9966309..6b204aa2e8097a218dffc33453cdc6927fd05cff 100755 GIT binary patch delta 327 zcmZokXi1phRCU^hRb=;QY=$BRvz&Cp;N<+=0-!1&u#%trK%`z0Nj^8<#U-V*B$ruB4ypxPjUtP%8X_x& jB#UPC=J&EPOq&%L53p|LR(Qld`J$-AW>$?CAci;q7<*o< delta 640 zcmZokXi1ph&uFnRU^hRb{Nw}yx5=wixi-fL$TLk&7Os)DU~pk@V~AofVhGMjHw;eB z&n;j81Kp#O6NKxfkmL(8kmVC5#|t+|XES6n1Ohb|Fqooh*P9B~jwGL(@8XhDT9VAb zaOs9}I|E3&6pC~TK51;0`!HAntxjbyXNW|#TxTKJawK_V%SHMjmZL}`EEg8Ukj7?t z7=sssCqo275kn%XKOxUb1 XA;YwpTj3G= [Int] { return array } -var randomLongArray = generateArray(10, valueUpperBound: 10000) +var randomLongArray = generateArray(10000, valueUpperBound: 10000) var quickShortArray = [22, 59, 38, 93, 95, 0, 34, 58, 72, 15] quickSort(&randomLongArray, first: 0, last: randomLongArray.count - 1) print(randomLongArray) @@ -137,10 +137,134 @@ space complexity by breaking the array down into many other smaller arrays. */ /* 6) Do this on paper first [easier to visualize] */ -//func isBalanced(array: [String]) -> Bool { -// -// for i in 0.. { + + var items: [T] + + init() { + items = [T]() + } + + // push + mutating func push(element: T) { + + items.append(element) + } + // pop + mutating func pop() -> T? { + + if items.count > 0 { + + return items.removeLast() + } + return nil + } + // peek + func peek() -> T? { + + return items.last + } + // size + func size() -> Int { + + return items.count + } +} + +// this struct helps me (and the computer) conceptualize what the two characteristics are +// that must match up in order for bracket pairs to be considered valid +struct Bracket { + + var orientation: String + var name: String + + init(symbol: String) { + + switch symbol { + case "(": + orientation = "open" + name = "parenthesis" + case ")": + orientation = "close" + name = "parenthesis" + case "[": + orientation = "open" + name = "bracket" + case "]": + orientation = "close" + name = "bracket" + case "{": + orientation = "open" + name = "brace" + case "}": + orientation = "close" + name = "brace" + default: + orientation = "invalid" + name = "invalid" + } + } +} + +func isBalanced(array: [String]) -> Bool { + + // no way there will be matching pairs if the array count is odd. an array of 0 + // brackets is considered balanced in this implementation, though if we wanted to + // we could also trim that out here + if array.count % 2 != 0 { return false } + + var stack = Stack() + + for i in 0.. Date: Thu, 4 Feb 2016 20:08:02 -0500 Subject: [PATCH 21/21] hw --- .DS_Store | Bin 12292 -> 14340 bytes .../Contents.swift | 68 ++++++++++++++++-- .../contents.xcplayground | 2 +- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/.DS_Store b/.DS_Store index 6b204aa2e8097a218dffc33453cdc6927fd05cff..042cd706a3a8f13637596a22cd542666386046dc 100755 GIT binary patch delta 1056 zcmZokXem%&U|?W$DortDU@!nOIe-{M3-ADmb_NCoo{0+jjJ6X4oF^X^^qd?Z%*W)w zufB5{CP*%E+; zGT2R?ClNL|L0nqigrSJRoxz*IjKMS~-7q*gKeqs+fPq2oE|9>gE;rxBC8e|^nStTb z%{jNgigBn*Axd3A2C}^gAOjKRWizBPR53U*gfN(+x=+;@mpWwk&07dE4!e1H?ac>< ziVH&-gAaobUVD+$A=|q|7tLN2b%=1mrw)tzbb#^Z$XGXdgP0gF+WFYzZUe1B76isU z=Z&zNV9Sxkfyn?Uegi`sm?9VxCJQniV3gm?qwtV@;>3{2i2|;ZSE+JsjuAM>IC;H< zD>$7X#RIZGN5dBq8HB=jk0dKN>=_KWfwU`f=H4vG_?~$(zm6{t6D03jfK+bI&}n6! HI8ht`Y)Aug delta 190 zcmZoEXh~3DU|?W$DortDV9)?EIe-{M3-ADmb_NCo?uiQejCvCToF_YLI8HXu5M4vx9~p)8wz3-jnaB3r}tqNZec?u!oh2Nolg4SSs@kzKluYF_SOI z@i2i{S&R;w1sP4)7$r9IC_H4J+$bs`&A`A4v //2) +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!") +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) -//3) \ No newline at end of file +phoneBook.findPerson("Jenny") \ 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 index 5da2641..9f5f2f4 100644 --- a/HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground +++ b/HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file