From 5b2b4b03d69252700190f350b2c49b880a931529 Mon Sep 17 00:00:00 2001 From: Felicia Weathers Date: Thu, 14 Jan 2016 17:32:09 -0500 Subject: [PATCH 01/11] Still working on it, a little confused --- .../Contents.swift | 52 ++++++++- .../Contents.swift | 109 +++++++++++++++++- 2 files changed, 152 insertions(+), 9 deletions(-) diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..4f8ac6f 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -11,13 +11,55 @@ Use the link here to get the questions. Then code your solutions below. If it https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/edit -1) +1) */ +func findMissingNumber(N:Int, list:[Int]) -> Int { +var totalSumOfNArray = 0 +var totalSumOfList = 0 -2) +for i in 1...N { +totalSumOfNArray += i +} +for i in list { +totalSumOfList += i +} -3) +return totalSumOfNArray - totalSumOfList + +} + + +//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 { + + return true +} + + +//3) +//Given two lists, find the smallest value that exists in both lists +func getSmallestCommonValue(list1:[Int], list2: [Int]) ->Int? { + let list1 = [1,2,5,9] + let list2 = [9, 20, 5] + + for i in (0..Bool { + return String(word.characters.reverse()) == word +} -4) -*/ diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..06a6b53 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -11,15 +11,116 @@ Use the link here to get the questions. Then code your solutions below. If it https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/edit#heading=h.za36ai6n5fth -1) -2) +Big O Homework +With my new top of the line XJ452 supercomputer, memory access takes 1 picosecond, math operations take 3 picoseconds, and storing data in memory takes 10 picoseconds. My friend wrote a filter that makes a pixel more awesome, and takes 200 picoseconds to run. +How long would my computer take to execute the following code if the input image is 1000px wide by 2000px tall? What if it’s n by m? -3) + +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; +} + + +What is the time complexity of this method, expressed in big O notation? Assume the image is square, and both dimensions are ‘n’. +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? + + +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? + + +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 +} +} + + +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. + + +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. + + +An algorithm that searches the now-sorted list of friends for a specific friend (not including the time it takes to sort). + + +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): + + +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. + + +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). + + +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? + + +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? + + +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). + + +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. + + +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). + + + + +1) a. + b. O(n^2) + c. O(n^4) + +2) a. cubic O(n^3) + quadratic O(n^4) + quadratic O(n^2) + b. constant + c. + +3) a. tree + b. hash table + c. tree 4) -5) +5) func multiplication(x: Int, y: Int) -> Int { + + guard x != 0 || y != 0 else { + return 0 + } + + return x + multiplication(x, y: y-1) 6) From 6512cbf56de09874fff50c0aafc14c8579c8db35 Mon Sep 17 00:00:00 2001 From: Felicia Weathers Date: Sat, 16 Jan 2016 00:11:42 -0500 Subject: [PATCH 02/11] LogicDiscreteMathHW --- .DS_Store | Bin 0 -> 6148 bytes LogicDiscreteMathHW.playground/Contents.swift | 24 ++++++++++++++++++ .../contents.xcplayground | 4 +++ .../contents.xcworkspacedata | 7 +++++ .../timeline.xctimeline | 6 +++++ 5 files changed, 41 insertions(+) create mode 100644 .DS_Store create mode 100644 LogicDiscreteMathHW.playground/Contents.swift create mode 100644 LogicDiscreteMathHW.playground/contents.xcplayground create mode 100644 LogicDiscreteMathHW.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 LogicDiscreteMathHW.playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c645b8f6e610ba75b715905389deff8575ab4308 GIT binary patch literal 6148 zcmeH~&q@O^5XQgg9*SOi^yY#d^iXjT3f`8of}T9|Al@ontU}jaq(!{#GkNzV`b2({ z3DRX1#8wfRfy|dpGWqkPNg4odFwYa93!uX!8g0>iU{WusWgDSHDxL46M1cf79OJar z+6s(-5%_BaWbf|d99Ou-BbN7Xk2|@K0}Szmd)Cfyfd_hNb%-NWnBoS{nD~-8>+%(8qw8v~2iMbD40ws{w2{$qVtcgsvhGV$FO?}R6&oSI5}K;9Ev zevR_U%j!Kg*!E?LtIn}ObS*tLIfkE@pQ^cU65yRJ*4b7yZv>2h5qKvc=R?dSng*>^ z{n0_^p8(W|#om~gw}iyRLDQhMDr;y?L#Z0-!V|-3ILqVEE)80%YB*eYe7G>O3ojJs zM`!=BZw{BLnl}PQph;j;Kf7}OpMC!RZwA?y5ikP(ihzrL2Ff((Cm+Z$6(F~{3?Mrwo{B- literal 0 HcmV?d00001 diff --git a/LogicDiscreteMathHW.playground/Contents.swift b/LogicDiscreteMathHW.playground/Contents.swift new file mode 100644 index 0000000..bd07183 --- /dev/null +++ b/LogicDiscreteMathHW.playground/Contents.swift @@ -0,0 +1,24 @@ +//: Playground - noun: a place where people can play + +import UIKit + +/* +1. +let n = number of socks +max number of socks drawn for a pair = (n/2) + 1 +O(n) bc only need one for loop + +2. + +C(n,2) + +n! / (2! * (n-2)!) + +3. + +*/ + + + + + \ No newline at end of file diff --git a/LogicDiscreteMathHW.playground/contents.xcplayground b/LogicDiscreteMathHW.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/LogicDiscreteMathHW.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/LogicDiscreteMathHW.playground/playground.xcworkspace/contents.xcworkspacedata b/LogicDiscreteMathHW.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/LogicDiscreteMathHW.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/LogicDiscreteMathHW.playground/timeline.xctimeline b/LogicDiscreteMathHW.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/LogicDiscreteMathHW.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From ade800e7eac21c0dd6079d75a210dc75b63dd1f0 Mon Sep 17 00:00:00 2001 From: Felicia Weathers Date: Sun, 17 Jan 2016 15:29:30 -0500 Subject: [PATCH 03/11] finally understanding better --- .DS_Store | Bin 6148 -> 6148 bytes .../Contents.swift | 12 +++--- LogicDiscreteMathHW.playground/Contents.swift | 40 +++++++++++++++++- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/.DS_Store b/.DS_Store index c645b8f6e610ba75b715905389deff8575ab4308..98b38dbdb9bdd40675ce42351378533d6b6b986b 100644 GIT binary patch delta 210 zcmZoMXfc@J&&ahgU^g=(*JK4&pUL}J`8KDqq%ks?_)S(|bK!AuNhvK!W?;B@Z(nSoZ3?)G9%a919GZ;J=!Y9vT5d#^fqx_74fq{5U aiuwm+#eh0U(8H#}m@u($*=BZ*zx)7F7BQUw delta 65 zcmZoMXfc@J&&aVcU^g=($7BUopUL}J`8KDqq%krXZ list all the numbers from n counting down to 1 -> multiply each number, n * (n-1) * (n-2) *... * 1 5) func multiplication(x: Int, y: Int) -> Int { @@ -122,7 +124,7 @@ Using the technique from exercise 4, profile the built in sorting method in obje return x + multiplication(x, y: y-1) -6) +*6) O(log n) 7) diff --git a/LogicDiscreteMathHW.playground/Contents.swift b/LogicDiscreteMathHW.playground/Contents.swift index bd07183..6a26073 100644 --- a/LogicDiscreteMathHW.playground/Contents.swift +++ b/LogicDiscreteMathHW.playground/Contents.swift @@ -14,11 +14,49 @@ C(n,2) n! / (2! * (n-2)!) -3. +3. use product rule + + */ +//1. (n-1) + (n-2) + (n-3) ....1 + +//let numberOfPeople = Int() +//for _ in 1...numberOfPeople { +// +//// n * (n-1)/2 +// +//} + + +//func parseInputAsInteger() -> Int { +// let input = readLine() +// let num = Int(input!) +// return num! +//} + +let testCases = Int(readLine(stripNewline: true)!)! +for var x = 0; x < testCases; x++ { + //BOOM + + // var firstLineInTheCase = Int(readLine(stripNewline: true)!)! + print (readLine(stripNewline: true)!)! +} + + +//func maxSocksDrawn(N:Int) -> Int { +// let n = Int?() +// +// for _ in 0...N { +// return (n!/2) + 1 +// } +// return 0 +//} +// +//let n = Int.self; +//maxSocksDrawn() \ No newline at end of file From c8bb4403da5d07c1b41135ea6990bcad65b9da43 Mon Sep 17 00:00:00 2001 From: Felicia Weathers Date: Thu, 21 Jan 2016 18:50:27 -0500 Subject: [PATCH 04/11] incomplete --- .DS_Store | Bin 6148 -> 8196 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/.DS_Store b/.DS_Store index 98b38dbdb9bdd40675ce42351378533d6b6b986b..67c6c56f74eac51ffb7f1b8387ec47ea2b9c4dda 100644 GIT binary patch delta 334 zcmZoMXmOBWU|?W$DortDU;r^WfEYvza8E20o2aKK$_J7M@);N^8HyPk8S)rX7<@Jh za)_~PR^aGmUd+zHF|mO~8Kg`KC|v}EiO5PBGJ)Dk7(5ulC(mOMo4k*ePet`LQHmAS zkMr#UswBo>34;`ul+u!928K(-YZ9BB!zw&knbi~Is?BLE>5Pn7lNH#UCJV6fFsadR_IH#3(6H_#QXpilw&{5$hxei6^f{yZEUz<>b-7{lgxo;l0_J6A(h delta 149 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50D9Qzr2aDx1@;xEG&256|jEsJh9fX}G8wm3-QegPS@ From 1ced214db2958e45e8315dc1046e851d71fcda5b Mon Sep 17 00:00:00 2001 From: Felicia Weathers Date: Sun, 24 Jan 2016 13:00:18 -0500 Subject: [PATCH 05/11] fixed questions 2 & 3 --- .DS_Store | Bin 8196 -> 8196 bytes ArraysAndListHW.playground/Contents.swift | 158 ++++++++++++++++++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 + .../timeline.xctimeline | 6 + 5 files changed, 175 insertions(+) create mode 100644 ArraysAndListHW.playground/Contents.swift create mode 100644 ArraysAndListHW.playground/contents.xcplayground create mode 100644 ArraysAndListHW.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 ArraysAndListHW.playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store index 67c6c56f74eac51ffb7f1b8387ec47ea2b9c4dda..7a555e1264db5b93f993847cfa461080b742ef19 100644 GIT binary patch delta 263 zcmZp1XmQxUFTkifSwO(Vib0Q|m?4Qlmm!hCkimcM~IvAINvTHA1We}o1fy6Qd*MCz;Fpg dVltPg@aE%!A [Int] { + //checking row + var validNumbers = Array(count: 10, repeatedValue: true) + for i in 0.. [[Int]] { + var matrixBoard = Array(count: sampleInput[0].count, repeatedValue: Array(count: sampleInput.count, repeatedValue: 0)) + + //horizontal + for i in 0.. sampleInput.count - 1 - i][revers + matrixBoard[j][sampleInput.count - 1 - i] = sampleInput[i][j] + } + } + return matrixBoard +} +print(rotateNinetyDegrees(sampleInput)) + + +//let number of rows == number of columns and number of columns == number of rows +//output column(s) in a row(s) in reverse order + + +//3. +//var array = [4,3,2,1] +// +//func sort(values:[Int]) -> [Int] { +// let left = values[0...1] +// return values +// 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 +// print(right) +// } +//} + +//print(sort(array)) + +var data = [4, 3, 2, 1] + +func merge_sort(inout array: [T]){ + if array.count <= 1{ + return + } + + // Split lists into equal sized sublists + var middle = array.count + middle /= 2 + var left = [T]() + var right = [T]() + + for x in 0..(inout left: [T], inout right:[T])-> [T]{ + + var result = [T]() + + // Merge taking lowest value first seen + while (!left.isEmpty && !right.isEmpty){ + if left[0] <= right[0]{ + result.append(left[0]) + left.removeAtIndex(0) + }else{ + result.append(right[0]) + right.removeAtIndex(0) + } + } + + // Handle remaining elements + while !left.isEmpty{ + result.append(left[0]) + left.removeAtIndex(0) + } + while !right.isEmpty{ + result.append(right[0]) + right.removeAtIndex(0) + } + + return result +} + +merge_sort(&data) + diff --git a/ArraysAndListHW.playground/contents.xcplayground b/ArraysAndListHW.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/ArraysAndListHW.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ArraysAndListHW.playground/playground.xcworkspace/contents.xcworkspacedata b/ArraysAndListHW.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/ArraysAndListHW.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ArraysAndListHW.playground/timeline.xctimeline b/ArraysAndListHW.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/ArraysAndListHW.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From ac6f04529ee0c3044f33fc01e92ae10acf9d2809 Mon Sep 17 00:00:00 2001 From: Felicia Weathers Date: Thu, 28 Jan 2016 15:36:48 -0500 Subject: [PATCH 06/11] Recursion HW --- .DS_Store | Bin 8196 -> 8196 bytes .../FindFileHW.xcodeproj/project.pbxproj | 246 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + FindFileHW/FindFileHW/main.swift | 32 +++ RecursionHW.playground/Contents.swift | 55 ++++ RecursionHW.playground/contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 + RecursionHW.playground/timeline.xctimeline | 6 + 8 files changed, 357 insertions(+) create mode 100644 FindFileHW/FindFileHW.xcodeproj/project.pbxproj create mode 100644 FindFileHW/FindFileHW.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 FindFileHW/FindFileHW/main.swift create mode 100644 RecursionHW.playground/Contents.swift create mode 100644 RecursionHW.playground/contents.xcplayground create mode 100644 RecursionHW.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 RecursionHW.playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store index 7a555e1264db5b93f993847cfa461080b742ef19..4df384f7c5aae5eb7752e4be4371ff680ec64603 100644 GIT binary patch delta 484 zcmZp1XmOa}&nUDpU^hRj5Ca2))MNnxlgR=?d~EuyaBhweH|MDX(|~-a0+HPO6ql6J zl4J&kODGbPxkQCG9~TT`7S?14Vn}63W+-JSVklx4U=2%S<$t}VzwcCEn1!gq|D R*(JWQFkgDSqvr&5e$9|6*=jK!O8i#1weTq zP<;&~u&T?=cX3H6ElFlzxOALv7g#Y4btyR2aU + + + + diff --git a/FindFileHW/FindFileHW/main.swift b/FindFileHW/FindFileHW/main.swift new file mode 100644 index 0000000..cf0c53b --- /dev/null +++ b/FindFileHW/FindFileHW/main.swift @@ -0,0 +1,32 @@ +// +// main.swift +// FindFileHW +// +// Created by Felicia Weathers on 1/28/16. +// Copyright © 2016 Felicia Weathers. All rights reserved. +// + +import Foundation + +func findFile(name: String, atPath: String) -> String { + let fileManager = NSFileManager.defaultManager() + let contents = + try! fileManager.contentsOfDirectoryAtPath(atPath) + for fileOrDir in contents { + var isDir = ObjCBool(false); + let fullPath = atPath + "/" + fileOrDir + let exists = fileManager.fileExistsAtPath(fullPath, isDirectory: &isDir) + if exists && Bool(isDir) { + // YOUR CODE HERE + print("DIR: " + fileOrDir) + } else if exists { + // YOUR CODE HERE + print("FILE: " + fileOrDir) + } else { + print("NEITHER: " + fileOrDir) + } + } + return "NOT FOUND" +} + +print(findFile("awesome-idea.txt", atPath: "/Users/calebegg/Documents")) \ No newline at end of file diff --git a/RecursionHW.playground/Contents.swift b/RecursionHW.playground/Contents.swift new file mode 100644 index 0000000..36533c6 --- /dev/null +++ b/RecursionHW.playground/Contents.swift @@ -0,0 +1,55 @@ +////: Playground - noun: a place where people can play +// +//import Cocoa +// +////1. +var str = "Hello, playground" +func fibonacci(n: Int) -> Int { + // Some temporary variables. + var a = 0 + var b = 1 + // Add up numbers to the desired iteration. + for _ in 0.. Int { + print("X") + if (n == 0 || n == 1) { + return 1 + } + return fib(n - 1) + fib(n - 2) + } +} + +// Apendix A +func fib(n: Int) -> Int { + print("X") + if (n == 0 || n == 1) { + return 1 + } + return fib(n - 1) + fib(n - 2) +} +print(fib(13)) + +//2. +//var stepNum = 0 +//func tryStep() -> Int { +// let stepCount = Int(arc4random_uniform(3)) - 1 +// stepNum += stepCount; +// switch(stepCount) { +// case -1: print("Ouch \(stepNum)") +// case 1: print("Yay \(stepNum)") +// default: print("Beep \(stepNum)") +// } +// return stepCount +//} \ No newline at end of file diff --git a/RecursionHW.playground/contents.xcplayground b/RecursionHW.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/RecursionHW.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/RecursionHW.playground/playground.xcworkspace/contents.xcworkspacedata b/RecursionHW.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/RecursionHW.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/RecursionHW.playground/timeline.xctimeline b/RecursionHW.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/RecursionHW.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From b23f0d76dd7f6b148c4be2e4cb3bf1c812f2330d Mon Sep 17 00:00:00 2001 From: Felicia Weathers Date: Thu, 28 Jan 2016 15:55:47 -0500 Subject: [PATCH 07/11] added number 2 --- .DS_Store | Bin 8196 -> 10244 bytes RecursionHW.playground/Contents.swift | 44 +++++++++++++++++++------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/.DS_Store b/.DS_Store index 4df384f7c5aae5eb7752e4be4371ff680ec64603..02ce5f1d6b0929b43dc5ca0ce2d533b63a661229 100644 GIT binary patch delta 441 zcmZp1XbF&DU|?W$DortDU{C-uIe-{M3-C-V6q~50$SA%sU^hRb_+}mfWkyEb$pQi< zlj}sK<;xke7)%%<82lJ2a?%Zhlk;;6fVzM{^)-;dsxCL*#U-V*B$U6j$-g-NMkSqVh>bzsi^?fc}pS5Be~~- z{u74DjUqnC(ghjF-b&b5IE{T`gOwmN*x?Kc+(6nD6iyopzcWwfmkAVMf`pz1NC6`Q Vg8_)1Tp-iC*;9BfBURmL3;@uUV+H^K delta 211 zcmZn(XmOBWU|?W$DortDU;r^WfEYvza8E20o2aMAD6}zPH$S7$W*z}$#?8-#wlgy^ zR7_qV7RHyG@8XhDT9VAbaOr}M+~f=5zLQ^zaRY@@Foh)<1vVDGV-#ctnhXRI+(5z= jWXZ Int { print(fib(13)) //2. -//var stepNum = 0 -//func tryStep() -> Int { -// let stepCount = Int(arc4random_uniform(3)) - 1 -// stepNum += stepCount; -// switch(stepCount) { -// case -1: print("Ouch \(stepNum)") -// case 1: print("Yay \(stepNum)") -// default: print("Beep \(stepNum)") -// } -// return stepCount -//} \ No newline at end of file +var stepNum = 0 +func tryStep() -> Int { + let stepCount = Int(arc4random_uniform(3)) - 1 + stepNum += stepCount; + switch(stepCount) { + case -1: print("Ouch \(stepNum)") + case 1: print("Yay \(stepNum)") + default: print("Beep \(stepNum)") + } + return stepCount +} + +func stepUp(_: Int) -> Int { + let steps = 0 + if stepNum > 0 { + stepNum += steps + print("yay, made it up a stair") + } else if stepNum == 0 { + stepNum == 0 + print("try again") + tryStep() + } else { + stepNum -= steps + print("gotta make up that step") + tryStep() + tryStep() + } + return stepNum +} + +print(stepUp(7)) \ No newline at end of file From 80f78afaea17e8ca9877cfd80459d5ddc798ee09 Mon Sep 17 00:00:00 2001 From: Felicia Weathers Date: Thu, 28 Jan 2016 15:56:21 -0500 Subject: [PATCH 08/11] add --- FindFileHW/.DS_Store | Bin 0 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 FindFileHW/.DS_Store diff --git a/FindFileHW/.DS_Store b/FindFileHW/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..148f464b1576bdf3c5e57625fcbccebdf5dbbd48 GIT binary patch literal 6148 zcmeHLJxc>Y5PhR50yZfuEVmbg{DUQ&VkxAs74Cu`;!_dJ%Fc1v9F(Btd;wG3Ki=iGJth5ARlxMUG+fqwN zOmfVQ#gIo>$U+${l)S_s3uk}wdD*cTS~$py5Av1w#S7``oImMwP&N!53 Date: Sat, 30 Jan 2016 09:59:55 -0500 Subject: [PATCH 09/11] used loops --- .DS_Store | Bin 10244 -> 10244 bytes FindFileHW/.DS_Store | Bin 6148 -> 6148 bytes FindFileHW/FindFileHW/main.swift | 11 ++++++-- RecursionHW.playground/Contents.swift | 4 +-- SortingHW.playground/Contents.swift | 26 ++++++++++++++++++ SortingHW.playground/contents.xcplayground | 4 +++ .../contents.xcworkspacedata | 7 +++++ SortingHW.playground/timeline.xctimeline | 6 ++++ 8 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 SortingHW.playground/Contents.swift create mode 100644 SortingHW.playground/contents.xcplayground create mode 100644 SortingHW.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 SortingHW.playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store index 02ce5f1d6b0929b43dc5ca0ce2d533b63a661229..781ded3d4219853273b7319fcae251166aeabe24 100644 GIT binary patch delta 131 zcmZn(XbG6$&nUGqU^hRb)MOq3HzvJbljjInZSE6T&&nt^xjIfU0S4Is delta 19 acmZoMXffE}#>f=lH2DCd&gK%v31R?15(Yv5 diff --git a/FindFileHW/FindFileHW/main.swift b/FindFileHW/FindFileHW/main.swift index cf0c53b..bb6579f 100644 --- a/FindFileHW/FindFileHW/main.swift +++ b/FindFileHW/FindFileHW/main.swift @@ -18,10 +18,15 @@ func findFile(name: String, atPath: String) -> String { let exists = fileManager.fileExistsAtPath(fullPath, isDirectory: &isDir) if exists && Bool(isDir) { // YOUR CODE HERE - print("DIR: " + fileOrDir) +// print("DIR: " + fileOrDir) + let result = findFile(name, atPath: fullPath) + if result != "NOT FOUND" { + return result + } } else if exists { // YOUR CODE HERE - print("FILE: " + fileOrDir) +// print("FILE: " + fileOrDir) + return fullPath } else { print("NEITHER: " + fileOrDir) } @@ -29,4 +34,4 @@ func findFile(name: String, atPath: String) -> String { return "NOT FOUND" } -print(findFile("awesome-idea.txt", atPath: "/Users/calebegg/Documents")) \ No newline at end of file +print(findFile("awesome-idea.txt", atPath: "/Users/feliciaweathers/Documents")) \ No newline at end of file diff --git a/RecursionHW.playground/Contents.swift b/RecursionHW.playground/Contents.swift index 2e29750..dc8cb8c 100644 --- a/RecursionHW.playground/Contents.swift +++ b/RecursionHW.playground/Contents.swift @@ -3,7 +3,6 @@ import Cocoa // ////1. -var str = "Hello, playground" func fibonacci(n: Int) -> Int { // Some temporary variables. var a = 0 @@ -39,7 +38,8 @@ func fib(n: Int) -> Int { } return fib(n - 1) + fib(n - 2) } -print(fib(13)) +(0...5).map { i in fib(i) } + //2. var stepNum = 0 diff --git a/SortingHW.playground/Contents.swift b/SortingHW.playground/Contents.swift new file mode 100644 index 0000000..de4e87d --- /dev/null +++ b/SortingHW.playground/Contents.swift @@ -0,0 +1,26 @@ +//: Playground - noun: a place where people can play + +import Cocoa + +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; +} + + diff --git a/SortingHW.playground/contents.xcplayground b/SortingHW.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/SortingHW.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/SortingHW.playground/playground.xcworkspace/contents.xcworkspacedata b/SortingHW.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/SortingHW.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/SortingHW.playground/timeline.xctimeline b/SortingHW.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/SortingHW.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From 8e355a206311ab97f199f1fafd9716641005a2a9 Mon Sep 17 00:00:00 2001 From: Felicia Weathers Date: Mon, 1 Feb 2016 18:42:57 -0500 Subject: [PATCH 10/11] almost done --- .DS_Store | Bin 10244 -> 10244 bytes .../Contents.swift | 99 ++++++++++++++++++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 ++ .../timeline.xctimeline | 6 ++ 5 files changed, 116 insertions(+) create mode 100644 QuickSort_Stacks_Queues_HW.playground/Contents.swift create mode 100644 QuickSort_Stacks_Queues_HW.playground/contents.xcplayground create mode 100644 QuickSort_Stacks_Queues_HW.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 QuickSort_Stacks_Queues_HW.playground/timeline.xctimeline diff --git a/.DS_Store b/.DS_Store index 781ded3d4219853273b7319fcae251166aeabe24..def6094e4d56e605d65d6fe3b6cecb76d823bad5 100644 GIT binary patch delta 139 zcmZn(XbG6$&nUMsU^hRb+-4qu_Z<8p48aWf3`Gnj44Djh4C#{#L^VW|%orFL&?OFt ps)}E#{|J=9CMTni;*wHYlFYzx36~-PG2P9-C1lt(vn%{%2LP1tBb)#L delta 32 ocmZn(XbG6$&nUGqU^hRb)Mg%m_Z*vFOUkfKZ1}L5UEwb~0Kc0I2LJ#7 diff --git a/QuickSort_Stacks_Queues_HW.playground/Contents.swift b/QuickSort_Stacks_Queues_HW.playground/Contents.swift new file mode 100644 index 0000000..236d863 --- /dev/null +++ b/QuickSort_Stacks_Queues_HW.playground/Contents.swift @@ -0,0 +1,99 @@ +//: Playground - noun: a place where people can play + +import Cocoa + +var arr = "Hello, Felicia" + +/* 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. + + Sort time complexity space complexity +-Bubble Sort: O(n^2) O(n) +-Insertion Sort: O(n^2) O(1) +-Selection Sort: O(n^2) O(1) +-Merge Sort: O(nlog(n)) O(n) +-Quick Sort: O(nlogn) log(n) +*/ + +/* 2. +What is the advantage of partitioning quicksort in place? + +The partitioning takes place as the quicksort moves downward, allowing for a faster sort because the elements are being sorted as it iterates through the elements/array. +*/ + +/* 3. +Without looking, implement quicksort. + +*/ + +func partition(inout arr: [Int], firstIdx: Int, lastIdx: Int) -> Int { + let pivotValue = arr[firstIdx] + + var leftMark = firstIdx + 1 + + var rightMark = lastIdx + + while leftMark <= rightMark { + while arr[leftMark] < pivotValue { + leftMark += 1 + } + + while arr[rightMark] > pivotValue { + rightMark -= 1 + } + + if leftMark < rightMark { + swap(&arr[leftMark], &arr[rightMark]) + } + } + + if firstIdx != rightMark { + swap(&arr[firstIdx], &arr[rightMark]) + } + + return rightMark +} + +func quickSort(inout arr: [Int], firstIdx: Int, lastIdx: Int) { + // base case + if firstIdx >= lastIdx { + return + } + + let splitPoint = partition(&arr, firstIdx: firstIdx, lastIdx: lastIdx) + + //left half + quickSort(&arr, firstIdx: firstIdx, lastIdx: splitPoint - 1) + + //right half + quickSort(&arr, firstIdx: splitPoint + 1, lastIdx: lastIdx) +} + +func quickSort(inout arr: [Int]) { + quickSort(&arr, firstIdx: 0, lastIdx: arr.count - 1) +} +1 +var array = [22, 15, 38, 93, 95, 0, 34, 58, 72, 59] +quickSort(&array) + +/* 5. +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? + +- MergeSort breaks into smaller subarrays and sorts the elements, merging and sorting as the iteration goes back upward, whereas, QuickSort sort is in place and sorts as it goes down. +*/ + +/* 6. +Given an array of strings containing “[“,”]”,”{“,”}”,”(“,”)”. Output whether or not the parentheses are balanced. +Good examples: () [] () ([]()[]) +Bad examples: ( ( ] ([)] + + +*/ +var arrParen = ["][“,”]”,”{“,”}”,”(“,”)"] + +func isBalanced(paren: [String]) -> Bool { + +} +var arr1 = ["(,),[,]"] +isBalanced(arr1) +isBalanced(arrParen) \ No newline at end of file diff --git a/QuickSort_Stacks_Queues_HW.playground/contents.xcplayground b/QuickSort_Stacks_Queues_HW.playground/contents.xcplayground new file mode 100644 index 0000000..06828af --- /dev/null +++ b/QuickSort_Stacks_Queues_HW.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/QuickSort_Stacks_Queues_HW.playground/playground.xcworkspace/contents.xcworkspacedata b/QuickSort_Stacks_Queues_HW.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/QuickSort_Stacks_Queues_HW.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/QuickSort_Stacks_Queues_HW.playground/timeline.xctimeline b/QuickSort_Stacks_Queues_HW.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/QuickSort_Stacks_Queues_HW.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From 12513951d143e08f73a410138ff69c8b10758718 Mon Sep 17 00:00:00 2001 From: Felicia Weathers Date: Wed, 3 Feb 2016 19:29:49 -0500 Subject: [PATCH 11/11] added number 4 --- .DS_Store | Bin 10244 -> 8196 bytes .../Contents.swift | 33 +++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/.DS_Store b/.DS_Store index def6094e4d56e605d65d6fe3b6cecb76d823bad5..e47f2fff16eb71c1590acf980d47adc29f4b4ee2 100644 GIT binary patch delta 499 zcmZn(XmOBWU|?W$DortDU;r^WfEYvza8E20o2aMAD7i6UH$S7~W*z}$#?8-#!k7(J z83Gwf88R7?8L}CI8S)v57)lu8fvggSM6g&fkOxwc3dTU*|+Y*0_;SNI?N z1%e>dli*468^ntzub%wg%n~NeD!a+1sJtmNZ!8lGttaEsYia`6*{`eiooB10Kc=P&7QMVM@2vp zPy_}DNPO_wg%%^)v($tR4kiU4o#C`;d}bbCc>IVKBiggnuu`VY9=wRvi&6~Fvh%$! z%Snq7?OAHs;aPTgk!CN-P&}iZe7;!@uV|@`ihv?8ih$(qmzlF4@o#4+f8S#+A7T>^ zv4S=Z%-bec1?WnKf6y#cYC)@QMXvlDS57M^m+RfI2J}B4zcJFr+D|K}XCkM)>)KV7 z4dutdhp2V%l)O7AV3ie-XQ=UWjWaK>k2P##3onOJy#9T<+8nGuU3f31P182#L{33l z%(*QZ?`;^LZSfTHJckXA1*~HU6`sQ$8ualy^ykp5T2A8Y3y+9fX0n|#`OwdsGs#f) z;7(*NI|lEoBY2DF3+fi}67g>0J`$n4wofc3l;~jl@$EZi^<3#cti;u6&8H*>9H*9ZJ1kT}siDg|{*`d7wZ%@K+ ziQHCNe#f3KPc}q-I{h*GUrw|CKSc58xJmoB(Zc45D4ydkr=Vquwmhb%HW(EfCvC|s zW@w9}#XZ*>U*%XcL(I_C%+muOW~TOYAcYVAII zm0g;w>se}8IGA!gsVT?vgOuZ!*k$diP-%QFMzm)sXV^~t#{jn`l&VHC{2#XF|C;~* HtNH>OJZ0 diff --git a/QuickSort_Stacks_Queues_HW.playground/Contents.swift b/QuickSort_Stacks_Queues_HW.playground/Contents.swift index 236d863..b3cc342 100644 --- a/QuickSort_Stacks_Queues_HW.playground/Contents.swift +++ b/QuickSort_Stacks_Queues_HW.playground/Contents.swift @@ -76,6 +76,23 @@ func quickSort(inout arr: [Int]) { var array = [22, 15, 38, 93, 95, 0, 34, 58, 72, 59] quickSort(&array) + +/* 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 uniqueRandoms(numberOfRandoms: Int, minNum: Int, maxNum: UInt32) -> [Int] { + var uniqueNumbers = Set() + while uniqueNumbers.count < numberOfRandoms { + uniqueNumbers.insert(Int(arc4random_uniform(maxNum + 1)) + minNum) + } + return Array(uniqueNumbers) +} +print(uniqueRandoms(10000, minNum: 0, maxNum: 10000)) + + /* 5. 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? @@ -89,11 +106,11 @@ Bad examples: ( ( ] ([)] */ -var arrParen = ["][“,”]”,”{“,”}”,”(“,”)"] - -func isBalanced(paren: [String]) -> Bool { - -} -var arr1 = ["(,),[,]"] -isBalanced(arr1) -isBalanced(arrParen) \ No newline at end of file +//var arrParen = ["][“,”]”,”{“,”}”,”(“,”)"] +// +//func isBalanced(paren: [String]) -> Bool { +// +//} +//var arr1 = ["(,),[,]"] +//isBalanced(arr1) +//isBalanced(arrParen)