diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index 13cc14e..bd4962b 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -9,18 +9,122 @@ var str = "Hello, playground" Work on your solutions here. -Link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit#heading=h.za36ai6n5fth +Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit# 1) +func getNumbers(board: [[Int]], row: Int, col: Int) -> [Int] { +var possibleNums: [Int] = [] + +for i in 0...board.count-1 { + for j in 0...board[i].count-1 { + if row == i && col == j { + board[i][j] + for k in 1...9 { + if board[i][j] != k { + + possibleNums.append(k) + } + } + } + } + } + + return possibleNums +} + + + +2) + + + +// [[3,9,5,1], +// [4,0,6,2], +// [5,1,7,3], +// [6,2,8,4] ] + + +var input = [[1,2,3,4], + [5,6,7,8], + [9,0,1,2], + [3,4,5,6]] + +func ninetyDegrees(input: [[Int]]) { + var output: [[Int]] = [] + var newArr: [Int] = [] + + + for var i = input.count - 1; i < input.count; i-- { + for var j = 0; j < input.count; j++ { + if j == 0 { + newArr.append(input[i][j]) + output.append(newArr) + print(newArr) + } + + + + + } + } +} -2) 3) +var input = [4,0,6,2] + +func sortElements (inputArr: [Int]) -> [Int] { + + var sortedOutput: [Int] = [] + + for var i = 0; i < inputArr.count; i++ { + for var j = i + 1; j < inputArr.count; i++ { + if i > j { + var temp = input[i] + input[j] = input[i] + input[i] = input[j] + + } + + } + + } + + return sortedOutput +} + + + + + + */ + + + + + + + + + + + + + + + + + + + + + + diff --git a/HWFrom1-28-16(Merge Sort).playground/Contents.swift b/HWFrom1-28-16(Merge Sort).playground/Contents.swift index afdc1b6..fa1caba 100644 --- a/HWFrom1-28-16(Merge Sort).playground/Contents.swift +++ b/HWFrom1-28-16(Merge Sort).playground/Contents.swift @@ -4,3 +4,29 @@ //Insert code here: + +func insertionSort(numberList: [Int]) -> [Int] +{ + + //mutated copy + var output = numberList + + for primaryIndex in 0.. -1; secondaryIndex-- { + + //move into correct position + if key < output[secondaryIndex] { + output.removeAtIndex(secondaryIndex + 1) + output.insert(key, atIndex: secondaryIndex) + } + } + } + + return output + +} + +insertionSort([4,5,2,88,7,11]) diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..b7a1a23 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -10,14 +10,84 @@ 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) -2) +//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. -3) -4) +func missingNum (arr: [Int], n: Int) -> Int { + + var total = 0 + total = (n+1)*(n+2)/2 + for(var i = 0; i < n; i++) { + total -= arr[i] + } + + return total +} +var input1 = [1,2,4,5,6] +var input2: [Int] = Array(1...50) +input2.removeAtIndex(8) +input2.count -*/ +var miss = missingNum(input1, n: 5) +missingNum(input2, n: 49) + + +//2 Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not + +var numbers = [1,2,2,4,6,54,7,9,1,4] +var numbers2 = [5, 3, 8, 11] + +func duplicateElements(arr1: [Int]) -> Bool { + + let arr2 = Array(Set(arr1)) + + if arr2.count < arr1.count { + return true + } else { + return false + } +} + +duplicateElements(numbers) +duplicateElements(numbers2) + + +//3 Given two lists, find the smallest value that exists in +// both lists. +// L1 = [1,2,5,9] +// L2 = [9, 20 , 5] + +var list1 = [1,2,5,9] +var list2 = [9, 20 , 5] + +list1.sort() +let smallestVal = list1[0] + +list2.sort() +let list2SmallestVal = list2[0] + +//4 Check to see if an integer is a palindrome don’t use casting + +func isPalindrome (number: Int) -> Bool { + var palindrome = number + var reverse = 0 + + while(palindrome != 0) { + let remainder = palindrome % 10 + reverse = reverse * 10 + remainder + palindrome = palindrome / 10 + } + + if (number == reverse) { + return true + } + + return false +} + +isPalindrome(818) +isPalindrome(1005) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..075ceba 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -11,20 +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. O(m * n) + b. O(n^2) + c. O(n^4) -2) -3) -4) +2) a. O(n^4) + b. O(n) + c. O(n^2) + d. O(n) -5) +3) a. graph + b. graph + c. hash table -6) +4) func factorialCalc(num: Int) -> Int { + let range = Array(1...num) + var multiplier = 1 + for (var i = 0; i < range.count; i++) { + multiplier *= range[i] + } + + return multiplier + } -7) +factorialCalc(5) +factorialCalc(4) +factorialCalc(9) + +Time Complexity: O(n) + +5) func multiplication(num: Int, times: Int) -> Int { + let range = Array(1...times) + var total = 0 + for (var i = 0; i < range.count; i++) { + total += num + } + + return total + } + +multiplication(5, times: 2) +multiplication(10, times: 3) +multiplication(7, times: 3) + +Time Complexity: O(n) + + +6) -- + +7) Don't get the question */ + + + + + + + + + + + + + + + + + + + + + diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..bd7f2c7 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -27,3 +27,52 @@ What is the big O runtime of your code?: */ + +//1. The number of draws he makes in the worst case scenario is 3. I don't know how to express that in code. + +//2. + + +func factorial(var value: Int, var result: Int = 1) -> Int { + + if (value == 0) { + return result + } + + result *= value + value-- + + return factorial(value, result: result) +} + +func handshakes(attendees: Int) -> Int { + + let num = factorial(attendees) + + let den = (factorial(2) * factorial(attendees - 2)) + + let total = num / den + + return total + +} + + +handshakes(4) +handshakes(2) + +//3. +func gandalfsRoute(routes: Int, towns: Int) ->Int { + let num = factorial(routes) + let den = factorial(routes - towns) + + let total = num / den + + return total + +} + +gandalfsRoute(2, towns: 2) +gandalfsRoute(3, towns: 2) + +