diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..664c5e7 Binary files /dev/null and b/.DS_Store differ diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index 13cc14e..cc3812a 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -1,26 +1,32 @@ //: Playground - noun: a place where people can play import UIKit - var str = "Hello, playground" -/* - - -Work on your solutions here. - -Link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit#heading=h.za36ai6n5fth - -1) - - +//Work on your solutions here. -2) +//Link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit#heading=h.za36ai6n5fth +//1) Write an iterative (not recursive) fibonacci function that calculates the nth fibonacci number. How does its performance compare with the non-memoized recursive one (see Appendix A below), based on the number of iterations that Swift says it takes in the right margin? +//recursive - NO LOOPS +func fib(n: Int) -> Int { + print("X") + if (n == 0 || n == 1) { + return 1 + } + return fib(n - 1) + fib(n - 2) +} -3) +//iterative - W/ Loops +func fibIt(n:Int) -> Int{ + for fib in 1.. Int { + print("X") + if (n == 0 || n == 1) { + return 1 + } + return fib(n - 1) + fib(n - 2) +} +//iterative - W/ Loops -//Question 1 +func fibIt(n:Int) -> Int{ + for _ in 1.. Bool { + + +} -//6) \ No newline at end of file diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift index c5841b0..299cc87 100644 --- a/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift +++ b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift @@ -1,3 +1,5 @@ +import UIKit + //: https://docs.google.com/document/d/1T7tYRqpDPWoxarfmXqfRCHB-YqvA8-Qx_mEyy5smtfc @@ -14,7 +16,38 @@ //2) - - - -//3) \ No newline at end of file +let blacklist: Set = ["crapple", "fandroid", "m$"] + +func moderate(message: String)->Bool { + let words = (message as NSString).componentsSeparatedByString(" ") + + for word in words { + if blacklist.contains(word.lowercaseString){ + return false + } + } + return true +} + +moderate("I love apple") + + + +//3) +[(“Caleb”, “501-555-1234”), (“Mike”, “212-555-4321”), (“Jenny”, “345-867-5309”)] + +class PhoneBook { + // var storge = Dictionary() + var storage: [String : String] = [:] + + func addPerson(name: String, phoneNumber: String){ + storage[name]=phoneNumber + } +} + +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/HWFrom2-05-16(Trees).playground/Contents.swift b/HWFrom2-05-16(Trees).playground/Contents.swift index ca035a2..dec34ba 100644 --- a/HWFrom2-05-16(Trees).playground/Contents.swift +++ b/HWFrom2-05-16(Trees).playground/Contents.swift @@ -1,10 +1,81 @@ //https://docs.google.com/document/d/1te7mLS06MEYwETFSbVBqMrIzJ43GTEo5uuCiWdB0fyE/edit?usp=drivesdk -//1 +//1 Build and return the binary tree representation of the expression. The following characters should be treated as operators: +-*/ + +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 + } +} + +extension Node { + func printInOrder(){ + left?.printInOrder() + print(self) + right?.printInOrder() + } +} +extension Node { + func pop(var stack:[Node]){ + stack.removeFirst() + } +} +extension Node { + func push(var stack:[Node], element:Node){ + stack.insert(element, atIndex: 0) + } +} + +func parseExpression(input: String) -> [Node?] { + + let operators: Set = ["+", "-", "*", "/"] + var stack: [Node] = [] + + for character in input.characters { + + let node = Node (value: character) + + if stack.isEmpty{ + stack.first?.push(stack, element: node) + } else { + if !operators.contains(character){ + stack.append(node) + } else { + stack.first?.pop(stack) + stack.append(node) + } + + } + } + stack.first?.printInOrder() + return stack +} + + +//check +let input = "ab+cde+**" + +parseExpression(input) + + + //Bonus1 -//Bonus2 \ No newline at end of file +//Bonus2 diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..5af1d81 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -11,13 +11,45 @@ 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)Given an integer N, there is a list of size N-1 that is missing one number from 1 - N(inclusive). Find that number. + +func findMissingNumber (N:Int + + +2)Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not + +func hasDuplicates(arr:[Int]) -> Bool{ + let set = Set(arr) + + if set.count == arr.count{ + return false //no duplicates + } else{ + return true //there are duplicates + } +} -2) 3) + func smallestValue(arr1:[Int], arr2:[Int2]){ + var smallestNum = 0; + for i in 0..arr1.count{ + if i + } + +} + +4)func isPalindrome(word: String) -> Bool { + + let wordLength = word.characters.count + + for i in 0...wordLength/2{ + if(Array(word.characters)[i] != Array(word.characters)[wordLength - i - 1]){ + return false + } + } + return true + } -4) */ diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..d6446ee 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -12,14 +12,69 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 1) +a. 1000px * 2000px = 2000000px +b. O(n^2) +c. 2) +a. 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++) { n + for (int j = 0; j < n; j++) { n + foo(xs); n + } + } + for (int i = 0; i < n; i++) { n + for (int j = 0; j < n; j++) { n + bar(xs); n^2 + } + } + for (int i = 0; i < n; i++) { n + for (int j = 0; j < n; j++) { n + do cool stuff + } + } + + answer= n^4 + n^3 + n^2 = O(n^4) + +b. int frobnicate(ys, m) { + if (m == 0) { + return 0; + } + return ys[m] + frobnicate(ys, m - 1); + } + frobnicate(xs, n); + + 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: O (n ( n log(n))) + +d. An algorithm that searches the now-sorted list of friends for a specific friend (not including the time it takes to sort). + answer: 3) +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. Binary search tree -4) -5) +b. You get a small dataset of points of interest from an API every time the user pans the map. You construct the data set many times and only render it once, then you discard it and do another API search. stack + +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? Doubly linked list + + +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). + + int sum = 0 + for (int i = n; i <= m; n++){ + sum += n; + } + return sum; 6) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..c3c0499 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -11,12 +11,29 @@ Question 1: https://www.hackerrank.com/challenges/minimum-draws Copy and paste your code: +var socksPulledOut = [] + +for sock in socksPulledOut { + if sock == sock.last { + return true + } else { + return false +} + What is the big O runtime of your code?: Question 2: https://www.hackerrank.com/challenges/handshake Copy and paste your code: +var N = [] +var handshakes = 0 + +for boardmember in 0..boardmembers { + +} + + What is the big O runtime of your code?: Question 3: https://www.hackerrank.com/challenges/connecting-towns diff --git a/HWfrom1-23-16(Recursion).playground/Contents.swift b/HWfrom1-23-16(Recursion).playground/Contents.swift index f7c3b5c..275320a 100644 --- a/HWfrom1-23-16(Recursion).playground/Contents.swift +++ b/HWfrom1-23-16(Recursion).playground/Contents.swift @@ -10,6 +10,7 @@ var str = "Hello, playground" //1 +let this = 5