From c9637c29823a50d426014d7129f9f3f71f74695f Mon Sep 17 00:00:00 2001 From: cdolm92 Date: Fri, 12 Feb 2016 18:10:48 -0500 Subject: [PATCH 1/7] done --- .../Contents.swift | 80 +++++++++++++++++-- 1 file changed, 75 insertions(+), 5 deletions(-) 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) From b7c016c6095ad63df5676d68ccadb4a88cdf17ab Mon Sep 17 00:00:00 2001 From: cdolm92 Date: Fri, 12 Feb 2016 20:30:56 -0500 Subject: [PATCH 2/7] working on big o --- .../Contents.swift | 50 +++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..853f720 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -11,11 +11,20 @@ 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) + +2) a. O(n^4) + b. O(n) + c. O(n^2) + d. O(n) + +3) a. graph + b. graph + c. hash table 4) @@ -27,4 +36,39 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ */ + + + +func factorialCalc(num: Int) -> Int { + + let range = 1...num + var multiplier = 0 + + + + + + return multiplier +} + + + + + + + + + + + + + + + + + + + + + From 318022c5f8333e0e62f926d12b8f0ae86c119153 Mon Sep 17 00:00:00 2001 From: cdolm92 Date: Fri, 12 Feb 2016 21:28:00 -0500 Subject: [PATCH 3/7] Q's 1 to 5 done --- .../Contents.swift | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 853f720..b2ebb33 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -26,30 +26,44 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ b. graph c. hash table -4) +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 + } -5) +factorialCalc(5) +factorialCalc(4) +factorialCalc(9) -6) +Time Complexity: O(n) -7) +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) -func factorialCalc(num: Int) -> Int { - - let range = 1...num - var multiplier = 0 - - - - - return multiplier -} +*/ From 26ad2472cfacce1991a517cd0a9e9312bbe8f443 Mon Sep 17 00:00:00 2001 From: cdolm92 Date: Fri, 12 Feb 2016 21:39:37 -0500 Subject: [PATCH 4/7] done --- HWfrom1-10-016(BigO).playground/Contents.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index b2ebb33..075ceba 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -58,9 +58,10 @@ multiplication(7, times: 3) Time Complexity: O(n) -6) -7) +6) -- + +7) Don't get the question */ From a9d5b53492bdefe79936b40f1b5eb2805ce9f3df Mon Sep 17 00:00:00 2001 From: cdolm92 Date: Fri, 12 Feb 2016 21:44:02 -0500 Subject: [PATCH 5/7] done --- .../Contents.swift | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) 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) + + From a3c148e588a73402039a0a4e8cfb6a5cd6143b53 Mon Sep 17 00:00:00 2001 From: cdolm92 Date: Wed, 24 Feb 2016 19:54:23 -0500 Subject: [PATCH 6/7] done --- .../Contents.swift | 108 +++++++++++++++++- 1 file changed, 106 insertions(+), 2 deletions(-) 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 +} + + + + + + */ + + + + + + + + + + + + + + + + + + + + + + From d7b9da96db82ae1b871339804e205f9705623bb2 Mon Sep 17 00:00:00 2001 From: cdolm92 Date: Thu, 25 Feb 2016 18:39:44 -0500 Subject: [PATCH 7/7] insertion sorts --- .../Contents.swift | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) 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])