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..b834190 --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -0,0 +1,115 @@ +//: Playground - noun: a place where people can play + +import UIKit + +//var str = "Hello, playground" +// +//41Array + Lists Homework +// +//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. +// +//Input: sudokuBoard:[[Int?]]. (Each location on the board will be either be an Int from 1-9 or nil(empty cell)) +//row: Int +//col: Int +// +//func getValidNumbers(sudokuBoard:[[Int?]], row:Int, col:Int) -> [Int] { +// return [Int]() +//} +// +//Sample input: ,4,4 +// +//sample output: [1,3,4,5,6,8] +// +// +//rotate a matrix by ninety degrees +//Input: matrix:[[Int]] +//Output: matrix: [[Int]] +// +//Sample Input: [[1,2,3,4], +//[5,6,7,8], +//[9,0,1,2], +//[3,4,5,6]] +// +//Sample Output: [ [3,9,5,1], +//[4,0,6,2], +//[5,1,7,3], +//[6,2,8,4] ] +//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? + + + +/* +Work on your solutions here. +Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit#heading=h.uopysoy45zmw +1) +//So this is my plan +// Step 1: Print out the the suduko board as as squar with columns +// Step 2: Make an array *possibleOutcomes +// Step 3: Remove the numbers that appear on the x and y axises off possibleOutcomes as they appear + -The numbers remainin gwould be the possible outcome for that coordiante + +2) +3) +*/ + +//i = r +//j = c + +/////////////////////////////////Problem 1 +let sudokuBoard: [[Int]] = [ + [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]] + +let possibleOutcomes: Set = [1,2,3,4,5,6,7,8,9] + + +for var i = 0; i < sudokuBoard.count; i++ { + var line = "" + for var j = 0; j < sudokuBoard[i].count; j++ { + line += String(sudokuBoard[i][j]) + line += " " + + + + } + //print(sudokuBoard [4][4]) + print(line) + +} +//print(sudokuBoard [4][4]) +// print(sudokuBoard) +//print("sudokuBoard equals \(sudokuBoard) at iteration \(i)") + +func getValidNumbers(sudokuBoard:[[Int?]], i:Int, j:Int) -> [Int] { + return [Int]() +} +/////////////////////////////////Problem 2 + +let matrix: [[Int]] = +[[1,2,3,4], + [5,6,7,8], + [9,0,1,2], + [3,4,5,6]] + + +func rotateMatrixNinetyDegree(matrix: [[Int]]) -> [[Int]] { + var rotatedMatrix = [[Int]]() + for i in 0.. + + + \ No newline at end of file diff --git a/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate b/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..c87eb2e Binary files /dev/null and b/HWFrom1-17-16(Lists and Sorts).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/HWFrom1-24(Recursion).playground/Contents.swift b/HWFrom1-24(Recursion).playground/Contents.swift new file mode 100644 index 0000000..774b919 --- /dev/null +++ b/HWFrom1-24(Recursion).playground/Contents.swift @@ -0,0 +1,65 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" + +/* +Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit#heading=h.za36ai6n5fth +*/ + + + +//Question 1 + +//Appendix A: Recursive Fibonacci from class (non-memoized) +//func fib(n: Int) -> Int { +// print("X") +// if (n == 0 || n == 1) { +// return 1 +// } +// return fib(n - 1) + fib(n - 2) +//} +//fib(5) + +//Iterative +func fib(n: Int) -> Int { + var a = 1 + var b = 1 + for i in 0.. Bool { + let success = Int(arc4random_uniform(2)) > 0 + 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() +} + + + + +//Question 3 diff --git a/HWFrom1-24(Recursion).playground/contents.xcplayground b/HWFrom1-24(Recursion).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom1-24(Recursion).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom1-24(Recursion).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-24(Recursion).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-24(Recursion).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-24(Recursion).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate b/HWFrom1-24(Recursion).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..21ba79e Binary files /dev/null and b/HWFrom1-24(Recursion).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/HWFrom1-24(Recursion).playground/timeline.xctimeline b/HWFrom1-24(Recursion).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWFrom1-24(Recursion).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift new file mode 100644 index 0000000..f3c5113 --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift @@ -0,0 +1,33 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" +//Answer the questions in the homework below +//https://docs.google.com/document/d/1KlK3PmRMybmHS5db_AP11uHXIcbIeM5vHuuAZOV3xME/edit# + +//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) +//mergesort = O(nlog(n)) +//quicksort = O(nlog(n)) + +//2) What is the advantage of partitioning quicksort in place? +//Answer: You dont have to calculate the median? + + +//3)Without looking, implement quicksort. + +//4)Write a function to generate an array of random numbers bounded between 1..<10,000 of size 10,000. + +//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? + +//6)Given an array of strings containing “[“,”]”,”{“,”}”,”(“,”)”. Output whether or not the parentheses are balanced. +//Good examples: () [] () ([]()[]) +//Bad examples: ( ( ] ([)] +// +//func isBalanced(paren: [String]) -> Bool { +// +//} \ No newline at end of file diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..4acc4d3 Binary files /dev/null and b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/timeline.xctimeline b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift new file mode 100644 index 0000000..95fc42c --- /dev/null +++ b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift @@ -0,0 +1,44 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" +//: https://docs.google.com/document/d/1T7tYRqpDPWoxarfmXqfRCHB-YqvA8-Qx_mEyy5smtfc + + +//Homework +//1) Write good hash functions for the following data types. Justify your choices and how they avoid collisions. +// +// +// Int +// +// +// struct Point: Hashable { +// let x, y: Int +// } +// +// Array +// +// +//2) You moderate a popular mobile device discussion forum. You want to cut down on the vitriol and make your work easier, so you decide to implement a blacklist based system to automatically reject or approve posts. For example: +// +//moderate("I would never use a crApple product!") // false (reject) +//moderate("I wish all these FANDROIDS would just shut up!") // false +//moderate("M$ is the worst, Linux rules!") // false +//moderate("Can’t we all just get along?") // true (approve) +// +//Write moderate(message: String) -> Bool, using a built-in Swift Set to manage your blacklist. Make your method case insensitive; it should block the word no matter what combination of upper and lowercase letters is used. +// +// +//3) Your company makes a phonebook app, and your users have been complaining about how long it takes to look people’s numbers up. You decide to upgrade your archaic array-based system to a sleek, modern hash map. +// +//Write a phonebook class that uses either our HashMap from class or the built in Swift dictionary (your choice). It should implement the protocol below. It needs to support importing from the old array based format which used an array of tuples, like [(“Caleb”, “501-555-1234”), (“Mike”, “212-555-4321”), (“Jenny”, “345-867-5309”)] +// +// +//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 # +//} +// diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground b/HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom1-31-16(Sets and HashMaps).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate b/HWFrom1-31-16(Sets and HashMaps).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..a803605 Binary files /dev/null and b/HWFrom1-31-16(Sets and HashMaps).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/timeline.xctimeline b/HWFrom1-31-16(Sets and HashMaps).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWFrom1-31-16(Sets and HashMaps).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/HWFrom2-05-16(Trees).playground/Contents.swift b/HWFrom2-05-16(Trees).playground/Contents.swift new file mode 100644 index 0000000..9b6f9b4 --- /dev/null +++ b/HWFrom2-05-16(Trees).playground/Contents.swift @@ -0,0 +1,65 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" + + +//https://docs.google.com/document/d/1te7mLS06MEYwETFSbVBqMrIzJ43GTEo5uuCiWdB0fyE/edit?usp=drivesdk + + +//1 + + +//Bonus1 + + +//Bonus2 + +//Homework +// +//Using the Swift binary tree implementation below (the Node class), implement a function that takes in a string containing a simple postfix mathematical expression, and returns a binary tree representing that expression using the process described here. +// +//Required: Build and return the binary tree representation of the expression. The following characters should be treated as operators: +-*/ +// +//Bonus 1: Print the inorder traversal of the binary tree. +//Bonus 2: Evaluate the expression (substituting integers for the letters in the expression). +// +//Tips: +//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. +// +//Sample input: +//"ab+cde+**" +// +//Template for the function you should implement: +//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 +//} +// +//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 +// } +//} diff --git a/HWFrom2-05-16(Trees).playground/contents.xcplayground b/HWFrom2-05-16(Trees).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWFrom2-05-16(Trees).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWFrom2-05-16(Trees).playground/playground.xcworkspace/contents.xcworkspacedata b/HWFrom2-05-16(Trees).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWFrom2-05-16(Trees).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWFrom2-05-16(Trees).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate b/HWFrom2-05-16(Trees).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..996cbd3 Binary files /dev/null and b/HWFrom2-05-16(Trees).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/HWFrom2-05-16(Trees).playground/timeline.xctimeline b/HWFrom2-05-16(Trees).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWFrom2-05-16(Trees).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..d32e855 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -8,6 +8,19 @@ 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. +The following are exercises taken from in class slides. Code each of the solutions, then create a google doc that links to a gist for each solution. + +Given an integer N, there is a list of size N-1 that is missing one number from 1 - N(inclusive). Find that number. + +Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not + +Given two lists, find the smallest value that exists in both lists. +L1 = [1,2,5,9] +L2 = [9, 20 , 5] + +Check to see if an integer is a palindrome don’t use casting + + https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/edit diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..2b692df 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -5,6 +5,125 @@ import UIKit var str = "Hello, playground" /* +Big O Homework +1) 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? + Picoseonds = 1+3+10+200 (214) + Pixels = 1000 * 2000 (2,000,000) + Answer: TotalTime = (1000 * 2000) *(1+3+10+200) = 428,000,000 + + What if it’s n by m? + Answer: TotalTime = (n * m) *(1+3+10+200) + +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’. + Answer: O(n^2) + +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^4) + + + + +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); + } + } + Answer: (n) + (n) + (n) = O(n^3) + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + bar(xs); + } + } + Answer:(n) + (n) + (n^2) = O(n^4) + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + // do cool stuff + } + } + Answer: (n) + (n) = O(n^2) + + + + + +int frobnicate(ys, m) { + if (m == 0) { + return 0; + } + return ys[m] + frobnicate(ys, m - 1); + } + frobnicate(xs, n); + Answer: O(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. + Answer: Hash Table + +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. + Answer: Hash Table + +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? + + Answer: Hash Table + + + + + + +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). + +int n; +int m = 0; +for (int i = 0, i < n, i++) { + m = m + i; +} + + +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). 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. diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift new file mode 100644 index 0000000..6c7adc8 --- /dev/null +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -0,0 +1,74 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" + +/* + +Question 1: https://www.hackerrank.com/challenges/minimum-draws + +Copy and paste your code: + +int main() { +/* Enter your code here. Read input from STDIN. Print output to STDOUT */ +int testCases; +cin >> testCases; + +for (int i = 0; i < testCases; i++) { +int pairs; +cin >> pairs; +pairs = pairs + 1; +cout << pairs << endl; +} + +return 0; +} + +What is the big O runtime of your code?: 0(1) + +Question 2: https://www.hackerrank.com/challenges/handshake + +Copy and paste your code: + += n choose r +int n = numberOFPeopleInRoom +int r = numberOfPeopleShakingHands + += n! / (r! * (n-r)!) +Case 1 + += 1!/(2! *(1-2)!) += 0 + +Case 2 += 2! / (2! * (2-2)!) + +What is the big O runtime of your code?: + +Question 3: https://www.hackerrank.com/challenges/connecting-towns + +Copy and paste your code: + +int main() { +/* Enter your code here. Read input from STDIN. Print output to STDOUT */ +int testCases; +cin >> testCases; + +for (int i = 0; i < testCases; i++) { +int squadCount; +cin >> squadCount; +int handshakes = squadCount*(squadCount - 1)/2; +//n choose r = n! / (r! * (n-r)!) +//n choose 2 = n! / (2! * (n-2)!) +// = n! / (2*(n-2)!) +// +cout << handshakes << endl; +} + +return 0; +} + + +What is the big O runtime of your code?: ? +*/ \ No newline at end of file diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/contents.xcplayground b/HWfrom1-14-16(Logic+Discrete_Math).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/contents.xcworkspacedata b/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate b/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..d006c71 Binary files /dev/null and b/HWfrom1-14-16(Logic+Discrete_Math).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/timeline.xctimeline b/HWfrom1-14-16(Logic+Discrete_Math).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/HWfrom1-23-16(Recursion).playground/Contents.swift b/HWfrom1-23-16(Recursion).playground/Contents.swift new file mode 100644 index 0000000..9b871e6 --- /dev/null +++ b/HWfrom1-23-16(Recursion).playground/Contents.swift @@ -0,0 +1,24 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" +//https://docs.google.com/document/d/1KfnTOtPnBrYPFhBRAQPZBXor_mKDQvuJp4zwZbtHkRs/edit#heading=h.16sfqfmanxte + + + +func printBinaryRepresentation(number:Int) { + +} + +//1 + + + + +//2 + + + + +//3 \ No newline at end of file diff --git a/HWfrom1-23-16(Recursion).playground/contents.xcplayground b/HWfrom1-23-16(Recursion).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/HWfrom1-23-16(Recursion).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HWfrom1-23-16(Recursion).playground/playground.xcworkspace/contents.xcworkspacedata b/HWfrom1-23-16(Recursion).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/HWfrom1-23-16(Recursion).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/HWfrom1-23-16(Recursion).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate b/HWfrom1-23-16(Recursion).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..1141cd5 Binary files /dev/null and b/HWfrom1-23-16(Recursion).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/HWfrom1-23-16(Recursion).playground/timeline.xctimeline b/HWfrom1-23-16(Recursion).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/HWfrom1-23-16(Recursion).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + diff --git a/WFrom1-28-16(Merge Sort).playground/Contents.swift b/WFrom1-28-16(Merge Sort).playground/Contents.swift new file mode 100644 index 0000000..3b60ec7 --- /dev/null +++ b/WFrom1-28-16(Merge Sort).playground/Contents.swift @@ -0,0 +1,51 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" + +//: Playground - noun: a place where people can play + +//https://docs.google.com/document/d/1rlQ3lstnNb3fOR58EwGDysMQ0FSDkUGykcCoU14B9Go/edit#heading=h.za36ai6n5fth + + +//Insert code here: + +var arr = [10, 4, 6, 12, 3] +var index = 0 +var count = arr.count + +func insertionSort(inout values: [Int]) { + print(arr) + + func keepSorting(var i: Int) -> Int { + + if(i < count) { + func compare(var j: Int) ->Int { + if (j < count) { + if arr[j] < arr[i] { + swap(&arr[i], &arr[j]) + } + j++ + print(arr) + return compare(j) + } + print(arr) + return j + + } + compare(i) + + i++ + print(arr) + return keepSorting(i) + + } + return i + + } + keepSorting(index) + +} +insertionSort(&arr) +print(arr) \ No newline at end of file diff --git a/WFrom1-28-16(Merge Sort).playground/contents.xcplayground b/WFrom1-28-16(Merge Sort).playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/WFrom1-28-16(Merge Sort).playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/WFrom1-28-16(Merge Sort).playground/playground.xcworkspace/contents.xcworkspacedata b/WFrom1-28-16(Merge Sort).playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/WFrom1-28-16(Merge Sort).playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/WFrom1-28-16(Merge Sort).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate b/WFrom1-28-16(Merge Sort).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..57d3217 Binary files /dev/null and b/WFrom1-28-16(Merge Sort).playground/playground.xcworkspace/xcuserdata/nataliaestrella.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/WFrom1-28-16(Merge Sort).playground/timeline.xctimeline b/WFrom1-28-16(Merge Sort).playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/WFrom1-28-16(Merge Sort).playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + +