diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index 13cc14e..ccd74ab 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -13,14 +13,216 @@ Link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZ 1) - - -2) +func getValidNumbers(sudokuBoard: [[Int]], row: Int, col: Int) -> [Int] { + +var notValidNumsArr = [Int]() +//loop thru cols picking up non-empty values, row is held constant +for j in 0.. [Int] { +var coordinatesArr = [Int]() +if row <= (sudokuBoard.count/3) - 1 { +coordinatesArr.append(0) +coordinatesArr.append( (sudokuBoard.count/3) - 1 ) +} +else if row >= sudokuBoard.count/3 && row <= (sudokuBoard.count/3) + 2 { +coordinatesArr.append(sudokuBoard.count/3) +coordinatesArr.append((sudokuBoard.count/3) + 2 ) +} +else { +coordinatesArr.append((sudokuBoard.count/3) + 3) +coordinatesArr.append(sudokuBoard.count - 1) +} +return coordinatesArr +} + +func getNumbersSet() -> Set +{ +var numbers = Set() + +numbers.insert(1) +numbers.insert(2) +numbers.insert(3) +numbers.insert(4) +numbers.insert(5) +numbers.insert(6) +numbers.insert(7) +numbers.insert(8) +numbers.insert(9) + +return numbers +} + +//Takes in a column and outputs the starting and ending coordinates +//for the columns that make up the sqaure containing the given column +func getSquareColumnRowCoordinates(sudokuBoard: [[Int]],col: Int) -> [Int] { +var coordinatesArr = [Int]() +if col <= (sudokuBoard.count/3) - 1 { +coordinatesArr.append(0) +coordinatesArr.append( (sudokuBoard.count/3) - 1 ) +} +else if col >= sudokuBoard.count/3 && col <= (sudokuBoard.count/3) + 2 { +coordinatesArr.append(sudokuBoard.count/3) +coordinatesArr.append((sudokuBoard.count/3) + 2 ) +} +else { +coordinatesArr.append((sudokuBoard.count/3) + 3) +coordinatesArr.append(sudokuBoard.count - 1) +} +return coordinatesArr +} + +var sudokuBoard = [ +[5, 0, 8, 0, 7, 3, 1, 9, 0], +[9, 0, 0, 6, 0, 0, 4, 0, 8], +[0, 0, 0, 9, 0, 8, 0, 3, 5], +[0, 7, 0, 0, 0, 0, 0, 6, 0], +[0, 0, 2, 0, 0, 0, 9, 0, 0], +[0, 1, 0, 0, 0, 0, 0, 8, 0], +[1, 9, 0, 3, 0, 6, 0, 0, 0], +[2, 0, 3, 0, 0, 7, 0, 0, 9], +[0, 8, 7, 1, 9, 0, 3, 0, 4]] + +getValidNumbers(sudokuBoard, row: 5, col: 5) -> [2, 4, 9, 5] +getValidNumbers(sudokuBoard, row: 4, col: 4) -> [4, 5, 6, 3, 1, 8] +getValidNumbers(sudokuBoard, row: 8, col: 7) -> [2, 5] + + +2) + + +func rotateMatrixBy90(matrix: [[Int]]) ->[[Int]] { + +var rotatedMatrix = [[Int]](count: matrix.count, repeatedValue: matrix[0]) + + +for j in 0.. [[3, 9, 5, 1], [4, 0, 6, 2], [5, 1, 7, 3], [6, 2, 8, 4]] +print( (rotateMatrixBy90(sudokuBoard))) -> [[0, 2, 1, 0, 0, 0, 0, 9, 5], [8, 0, 9, 1, 0, 7, 0, 0, 0], [7, 3, 0, 0, 2, 0, 0, 0, 8], [1, 0, 3, 0, 0, 0, 9, 6, 0], [9, 0, 0, 0, 0, 0, 0, 0, 7], [0, 7, 6, 0, 0, 0, 8, 0, 3], [3, 0, 0, 0, 9, 0, 0, 4, 1], [0, 0, 0, 8, 0, 6, 3, 0, 9], [4, 9, 0, 0, 0, 0, 5, 8, 0]] 3) - +func min(first : Int,second: Int)->Int { +if first <= second { +return first +} +else { +return second +} +} + +func max(first : Int,second: Int)->Int { +if first > second { +return first +} +else { +return second +} +} + +//Optimal Algorithm to sort four elements A,B,C,D +func sort(unsorted: [Int]) ->[Int] { + +let mid = unsorted.count/2 +var sorted = [Int]() + +let leftMin = min(unsorted[0],unsorted[mid-1]) +let leftMax = max(unsorted[0],unsorted[mid-1]) + +let rightMin = min(unsorted[mid],unsorted[unsorted.count-1]) +let rightMax = max(unsorted[mid],unsorted[unsorted.count-1]) + +let absoulteMin = min(leftMin, rightMin) +let absoulteMax = max(leftMax,rightMax) + +if(leftMin == absoulteMin) +{ +sorted.append(leftMin) +sorted.append(rightMin) +} +else { +sorted.append(rightMin) +sorted.append(leftMin) +} +if(leftMax == absoulteMax){ +sorted.insert(rightMax, atIndex: mid) +sorted.append(leftMax) +} +else { +sorted.insert(leftMax, atIndex: mid) +sorted.append(rightMax) +} +return sorted +} +sort([3,1,4,0]) -> [0,1,3,4] +sort([2,2,2,2]) -> [2,2,2,2] +sort([1,6,0,2]) -> [0,1,2,6] */ diff --git a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift index bcf8eda..3822d29 100644 --- a/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift +++ b/HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift @@ -3,7 +3,14 @@ //Answer the questions in the homework below //https://docs.google.com/document/d/1KlK3PmRMybmHS5db_AP11uHXIcbIeM5vHuuAZOV3xME/edit# -//1) +//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. + + // Time Complexity Space Complexity +//Bubble Sort O(n^2) O(1) +//Insertion Sort O(n^2) O +//Merge Sort O(nlogn) O(n) +//Selection Sort O(n^2) O(1) +//Quick Sort O(nlogn), worst case O(n^2) O(1) //2) @@ -13,4 +20,48 @@ //5) -//6) \ No newline at end of file +//6)Given an array of strings containing “[“,”]”,”{“,”}”,”(“,”)”. Output whether or not the parentheses are balanced. +//Good examples: () [] () ([]()[]) +//Bad examples: ( ( ] ([)] + +//func isBalanced(paren: [String]) -> Bool { +// let items :[String] = [] +// var stack : Stack = Stack(items: items) +// if stack.size() == 0 { +// if paren[0] == ")" || paren[0] == "]" { +// return false +// } +// } +// for str in paren { +// if str == "(" || str == "[" { +// stack.push(str) +// } +// else if str == ")" { +// if stack.peek() == "(" { +// stack.pop() +// continue +// } +// else{return false} +// } +// else if str == "]" { +// if stack.peek() == "[" { +// stack.pop() +// continue +// } +// else {return false} +// } +// } +// return stack.size() == 0 +//} +//let inputArr = ["(",")"] +//isBalanced(inputArr) +// +//let inputArr2 = ["(", "(","]","(","[",")","]"] +//isBalanced(inputArr2) +// +//let inputArr3 = ["(",")","[","]","(",")", "(","[","]",")","(",")","[","]"] +// +//isBalanced(inputArr3) +// +//let inputArr4 = ["[","(","(","(",")",")",")","]","]"] +//isBalanced(inputArr4) diff --git a/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift index c5841b0..5fb5712 100644 --- a/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift +++ b/HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift @@ -17,4 +17,64 @@ -//3) \ No newline at end of file +//3) + +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!") + + +/************************************************/ + + //Q3 + +protocol PhoneBookProtocol { + mutating func addPerson(name: String, phoneNumber: String) + mutating func removePerson(name: String) + mutating func importFrom(oldPhonebook: [(String, String)]) + func findPerson(name: String) -> String? // Return phone # +} + +class PhoneBook: PhoneBookProtocol { + var storage: [String : String] = [:] // @{} + //var storage = Dictionary() + + func addPerson(name: String, phoneNumber: String) { + storage[name] = phoneNumber + } + + func removePerson(name: String) { + storage.removeValueForKey(name) + } + + func findPerson(name: String) -> String? { + return storage[name] + } + + func importFrom(oldPhonebook: [(String, String)]) { + for entry in oldPhonebook { + addPerson(entry.0, phoneNumber: entry.1) + } + } +} + + +let oldData = [("Caleb", "501-555-1234"), ("Mike", "212-555-4321"), ("Jenny", "345-867-5309")] + +let phoneBook = PhoneBook() +phoneBook.importFrom(oldData) + +phoneBook.findPerson("Jenny") + diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..de96e2e 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -12,12 +12,65 @@ https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/ 1) + func findMissingNumber(n : Int, list : [Int]) -> Int + { + let totalSum = n*(n+1)/2 + var totalSumOfList = 0 + + for i in list + { + totalSumOfList += i + } + return totalSum - totalSumOfList + } + +2) + func findDuplicates(list : [Int]) ->Bool + { + let set = Set(list) + if (set.count != list.count) + { + return true + } + else + { + return false + } + } + +3) + func findSmallesValue(list1 : [Int], list2 : [Int]) -> Int + { + let set1 = Set(list1) + let set2 = Set(list2) + + let intersectionArray = Array(set1.intersect(set2)) + + if(intersectionArray.isEmpty) + { + return -1 + } + + return intersectionArray.minElement()! + } -2) -3) 4) + func checkIfIntegerIsPalindrome(var number : Int) -> Bool + { + let originalNum = number + var finalNum = 0 + while(number > 0) + { + finalNum = finalNum * 10 + number % 10 + number /= 10 + } + return originalNum == finalNum + } + + + */ diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..56ab3af 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -11,18 +11,79 @@ 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) +1)a. (1 + 3 + 10) * 1000 + (1 + 3 + 10) * 2000 + 200 * (1000 * 2000) + 14,000ps + 28,000ps + 400000000ps = 400042000ps -2) + b. O(n^2) + c. O(n^4) + +2)a. O(n^3) + O(n^4) + O(n^2) = O(n^4) + b. frobnicate(xs, n) = xs[n] + xs[n-1] + xs[n-2] + ... + xs[1] + 0, so for n we make n+1 calls to frobnicate so it's 0(n) + + c. O(n^2) + O(nlog n) + O(n) = O(n^2) + d. O(log n) 3) + a. Hash Table + b. A Stack, Singly-Linked List, Doubly Linked List, or a Hash Table + c. A Skip List, HashTable or BST 4) - + func factorial(num : Int) -> Int { + + if(num == 0 || num == 1) + { + return 1 + } + return num * factorial(num - 1) + } + Time complexity O(n) 5) + func multiplication(num1 : Int, num2 : Int) -> Int { + var product = 0 + for _ in 1...num2 + { + product += num1 + } + return product + } + +multiplication(8, num2: 5) = 40 +multiplication(6, num2: 4) = 24 + +Time Complexity O(num2) which is still constant so O(1) + + + 6) +func russian_peasant_multiplication(var left_num : Int, var right_num : Int) -> Int { + +var product = 0 +if(left_num % 2 != 0) +{ +product += right_num +} +while(left_num > 1) +{ +left_num /= 2 +right_num *= 2 + +if( left_num % 2 != 0) +{ +product += right_num +} +} +return product +} + +russian_peasant_multiplication(36, right_num: 12) = 432 +russian_peasant_multiplication(17, right_num: 13) = 221 +russian_peasant_multiplication(2, right_num: 3) = 6 + +Time Complexity : 0(log n) + 7) diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..026ecff 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -7,23 +7,76 @@ var str = "Hello, playground" /* -Question 1: https://www.hackerrank.com/challenges/minimum-draws +Question 1: + +https://www.hackerrank.com/challenges/minimum-draws + Copy and paste your code: +let firstLine = Int(readLine(stripNewline: true)!)! +for _ in 0.. Int { + var result = [1,1] + if n < 2 { + return n + } + for i in 2...n { + result.insert(result[i-1] + result[i-2], atIndex: i) + } + return result[n] +} +The iterative version is much better than the non-memoized recursive function. For example, calculating fib(10) takes 88 calls to the recursive case where as it takes only 9 calls to the iterative segment of the function. Therefore, the iterative function is much more efficient -//2 +//2 +The engineers have been hard at work on the clumsy robot project, and have released a new API with a new tryStep method (see Appendix B). Now it returns an Int, which is -1 if the robot fell down a step, 0 if the robot stayed on the same step, or 1 if the robot went to the next step. Write a new stepUp method using this new tryStep method that works the same as before. + +func stepUp(){ +if tryStep() == 1 { +return +} +if tryStep() == -1 { +stepUp() +stepUp() +} +else{ +stepUp() +} +} +//3 //3 + +*/ \ No newline at end of file diff --git a/HWfrom1-23-16(Recursion).playground/Contents.swift.orig b/HWfrom1-23-16(Recursion).playground/Contents.swift.orig new file mode 100644 index 0000000..1571057 --- /dev/null +++ b/HWfrom1-23-16(Recursion).playground/Contents.swift.orig @@ -0,0 +1,53 @@ +//: Playground - noun: a place where people can play +//THIS IS AN OPTIONAL ASSIGNMENT + +import UIKit + +var str = "Hello, playground" + +//https://docs.google.com/document/d/1KfnTOtPnBrYPFhBRAQPZBXor_mKDQvuJp4zwZbtHkRs/edit#heading=h.16sfqfmanxte +/* +//Caleb's Homework +//1 +Write an iterative (not recursive) fibonacci function that calculates the nth fibonacci number. + + +func fibnoacci(n: Int) -> Int { + var result = [1,1] + if n < 2 { + return n + } + for i in 2...n { + result.insert(result[i-1] + result[i-2], atIndex: i) + } + return result[n] +} + +The iterative version is much better than the non-memoized recursive function. For example, calculating fib(10) takes 88 calls to the recursive case where as it takes only 9 calls to the iterative segment of the function. Therefore, the iterative function is much more efficient + + +//2 +The engineers have been hard at work on the clumsy robot project, and have released a new API with a new tryStep method (see Appendix B). Now it returns an Int, which is -1 if the robot fell down a step, 0 if the robot stayed on the same step, or 1 if the robot went to the next step. Write a new stepUp method using this new tryStep method that works the same as before. + +func stepUp(){ +if tryStep() == 1 { +return +} +if tryStep() == -1 { +stepUp() +stepUp() +} +else{ +stepUp() +} +} + + +//3 + + +<<<<<<< HEAD +*/ +======= +//3 +>>>>>>> 38eac0e09074171b6ee04fb720bb3f14855c4b0b