From 84c4d8b5feb2158773b61a7910ed100374052802 Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Thu, 14 Jan 2016 18:53:58 -0500 Subject: [PATCH 01/10] hw --- .../Contents.swift | 76 +++++++++++++++++-- .../Contents.swift | 7 +- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift index 488e9ed..67d9256 100644 --- a/HWfrom1-09-16(SwiftIntro).playground/Contents.swift +++ b/HWfrom1-09-16(SwiftIntro).playground/Contents.swift @@ -10,14 +10,78 @@ 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. +func findTheMissingNumberFromAnArray(numberList:[Int], totalN: Int) -> Int { + var totalSumOfAnArray = 0 + var totalSumOfNonMissingArray = 0 + + for i in 1...totalN { + totalSumOfNonMissingArray += i + } + for i in 1...numberList.count { + totalSumOfAnArray += i + } + + return totalSumOfNonMissingArray - totalSumOfAnArray +} -3) +// 3)Given two lists, find the smallest value that exists in both lists. +// L1 = [1,2,5,9] +// L2 = [9, 20 , 5] +func smallestValueFromTwoArray(list1: [Int], list2: [Int]) -> Int { + let sortedList1 = list1.sort({$0 < $1}) + let sortedList2 = list2.sort({$0 < $1}) + if sortedList1.first > sortedList2.first { + return sortedList2.first! + } else { + return sortedList1.first! + } +} -4) +// 4) Check to see if an integer is a palindrome don’t use casting +func isIntegerPalindrome(number: Int) -> Bool { + var digits = 1 + var tens = 10 + while (number / tens > 0) { + digits += 1 + tens *= 10 + } + + var moduleOfTen = 10 + for i in 0...digits { + var prevBackEndNumber : Int? + var backEndNum = 0 + if (prevBackEndNumber != nil) { + backEndNum = number % moduleOfTen + backEndNum -= prevBackEndNumber! + } else { + backEndNum = number % moduleOfTen + } + + + var frontEndNum = 0 + var prevFrontEndNumber : Int? + if (prevFrontEndNumber != nil){ + frontEndNum = number / Int(pow(Double(10), Double(digits - i))) + frontEndNum -= prevFrontEndNumber! + } else { + frontEndNum = number / Int(pow(Double(10), Double(digits - i))) + } + + if (frontEndNum != backEndNum){ + return false + } else { + moduleOfTen *= 10 + prevBackEndNumber = backEndNum + prevFrontEndNumber = frontEndNum + } + } + print(digits) + return true +} +let num = 12321 +isIntegerPalindrome(num) -*/ diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index 2040d38..f57fe62 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -12,8 +12,13 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 1) + a) ANS = 160 ps + b) O(n^2) + c) O(n^4) -2) +2) a) n^4 + n^3 + n^2 + b) n^m + 3) From 5d102f11068a4cd05f2a31bb020e1458bce91fae Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Thu, 14 Jan 2016 19:04:45 -0500 Subject: [PATCH 02/10] hw --- HWfrom1-10-016(BigO).playground/Contents.swift | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/HWfrom1-10-016(BigO).playground/Contents.swift b/HWfrom1-10-016(BigO).playground/Contents.swift index f57fe62..3c580e8 100644 --- a/HWfrom1-10-016(BigO).playground/Contents.swift +++ b/HWfrom1-10-016(BigO).playground/Contents.swift @@ -14,11 +14,21 @@ https://docs.google.com/document/d/1aF1imJUVahCSJAuN1OEm5lQXwpSFaAmVmAETKMM6PLQ/ 1) a) ANS = 160 ps b) O(n^2) - c) O(n^4) + c) O(n^2) + +2) a) n^4 , n^3 , n^2 + b) O(n!) + c) + func isDuplication -> Bool { + let friendsArray = ["mike","joe","hello"] + let unique = Array(Set(friendsArray)) + if (friendsAray == unique) { + return false + } else { + return true + } + } -2) a) n^4 + n^3 + n^2 - b) n^m - 3) From c0dd05caa59dc9cbf2c2869756b934cad4d0dbcc Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Sat, 16 Jan 2016 02:27:59 -0500 Subject: [PATCH 03/10] Discrete Math --- .DS_Store | Bin 6148 -> 6148 bytes .../Contents.swift | 34 +++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/.DS_Store b/.DS_Store index 71f4c830597ce505d13ffa73055274395bcd980e..825085081eebf34e4587afb69a31fcb6e6b04024 100644 GIT binary patch delta 286 zcmZoMXfc@J&&aVcU^g=($7BK4qRn+IyBVEz89W%m8PXVv81fl%84MY8f!G90nlWfF z_y9%I88R7?8MGN(fOIjCRm70WPy)p9489DBKvu?N2NsF2oOHwBv9 ibZ)+jOG;@;G6Tb<{R?!#=2OY&$!cuVH?wp6cmH delta 32 ocmZoMXfc@J&&awlU^g=(>tq4eqRn+IyBRl@o@3n1&heKY0IaME;Q#;t diff --git a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift index bc0df91..a60f33c 100644 --- a/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift +++ b/HWfrom1-14-16(Logic+Discrete_Math).playground/Contents.swift @@ -10,20 +10,46 @@ var str = "Hello, playground" Question 1: https://www.hackerrank.com/challenges/minimum-draws Copy and paste your code: +*/ +func maxDraw (sock: Int) -> Int { + if sock == 1 { + return 2 + } else { + return sock/2 + 1 + } +} +/* +What is the big O runtime of your code?: O(1) +*/ + + -What is the big O runtime of your code?: + +/* Question 2: https://www.hackerrank.com/challenges/handshake Copy and paste your code: +*/ +func totalHandshakes(people: Int) -> Int { + + return (people - 1) * (people)/2 +} +/* +What is the big O runtime of your code?: O(1) +*/ + + -What is the big O runtime of your code?: +/* Question 3: https://www.hackerrank.com/challenges/connecting-towns Copy and paste your code: +*/ -What is the big O runtime of your code?: -*/ +/* +What is the big O runtime of your code?: +*/ From 69222feee3500f47789550b322a0333b1cb5de7e Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Sat, 16 Jan 2016 02:35:24 -0500 Subject: [PATCH 04/10] last question done --- .DS_Store | Bin 6148 -> 6148 bytes .../Contents.swift | 13 ++++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.DS_Store b/.DS_Store index 825085081eebf34e4587afb69a31fcb6e6b04024..5d02db76774a79e86f4b9df19da019513fc2760a 100644 GIT binary patch delta 34 qcmZoMXffEZh>c0{_+&G-IKJF`7nhXMl4J&kOZyiqZ5CkvA^-r;nG7BP delta 34 qcmZoMXffEZh>c0%#bh(KIKJF`7nhXMl4J&kOZyk-ZWdtwA^-r> Int { + if (towns.count > 0) { + var totalPath = 1 + for n in towns { + totalPath *= n + } + return totalPath + } else { + return 0 + } + +} /* What is the big O runtime of your code?: From c9656ed3025eb147cdd0c53ccd8afb2f3cfc27e8 Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Thu, 21 Jan 2016 15:27:55 -0500 Subject: [PATCH 05/10] 2 down 1 to go --- .DS_Store | Bin 6148 -> 8196 bytes .../Contents.swift | 131 ++++++++++++++++-- 2 files changed, 122 insertions(+), 9 deletions(-) diff --git a/.DS_Store b/.DS_Store index 5d02db76774a79e86f4b9df19da019513fc2760a..430518248f452d78ce8e9d3b1438cdffe95ea149 100644 GIT binary patch literal 8196 zcmeI1PiqrF7{;H`f)y`vQ?-YP)j{82a0$`a@^z0cX6+y}+ zPK6UT=Nwhh9!LSo8?Tv4 ztt(i=5D}i^Ee1Mg%(8wvN_yQW>9S4Ph8v8l6CL-c=xS?8*_ju-s7$()pFf~Q;| zad}oQeO>$rigWkryC7IJgNj)=KQ#+NQQpYR{%Vrl0FU;$r#|y{D(1$qHeXjQ3r}m~MnUSVPf;^k49g-fAW;%=JU-tQ9G-)4kPSWxzuf7R$ zMm3o*1&(Z7Rb=O2dr_H0>TH8;uNZ5R`fPlwl1ECtm z$p-}Zm=qi)ONnd%GZl|d&Jl^5EF;LxvVXA>!{ls{_{lLM!V^nd7PE732r>gT1AzcH gkZ=VVy|M5+^JIP*Pmo0nOb|;!?qJv)&ohS^0O#BtM*si- diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index 5d51051..c883310 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -11,16 +11,129 @@ Work on your solutions here. Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit#heading=h.uopysoy45zmw -1) +*/ + + +// 1) 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. + +func getAllColArrayIndex(paramCol: Int) -> [String: [Int]] { + // grab all the index Array that are associated with col + let col = paramCol-1 + var xArray = [Int]() + var xNumber = (col)/3 + xArray.append(xNumber) + xNumber += 3 + xArray.append(xNumber) + xNumber += 3 + xArray.append(xNumber) + var xArrayAtIndex = [Int]() + if (col % 3 == 0) { + xArrayAtIndex.appendContentsOf([0,3,6]) + } else if (col % 3 == 1) { + xArrayAtIndex.appendContentsOf([1,4,7]) + } else { + xArrayAtIndex.appendContentsOf([2,5,8]) + } + + return ["sudokuSmallBox": xArray, "indexNumber":xArrayAtIndex] +} + +func getAllRowArrayIndex(paramRow: Int) -> [String: [Int]] { + // grab all the index Array that are associated with row + let row = paramRow - 1 + var yArray = [Int]() + var yNumber = (row)/3 + yArray.append(yNumber) + yNumber += 1 + yArray.append(yNumber) + yNumber += 1 + yArray.append(yNumber) + + // grall all the index number that are row Array + var yArrayAtIndex = [Int]() + if(row % 3 == 0){ + yArrayAtIndex.appendContentsOf([0,1,2]) + } else if (row % 3 == 1) { + yArrayAtIndex.appendContentsOf([3,4,5]) + } else { + yArrayAtIndex.appendContentsOf([6,7,8]) + } + + return ["sudokuSmallBox":yArray, "indexNumber":yArrayAtIndex] +} + +func getValidNumbers(sudokuBoard: [[Int]], row: Int, col: Int) -> Set{ + let colDictionary = getAllColArrayIndex(col) + let rowDictionary = getAllRowArrayIndex(row) + + var colValue = [Int]() + for i in colDictionary["sudokuSmallBox"]! { + for j in colDictionary["indexNumber"]! { + colValue.append(sudokuBoard[i][j]) + } + } + + var rowValue = [Int]() + for i in rowDictionary["sudokuSmallBox"]! { + for j in rowDictionary["indexNumber"]! { + rowValue.append(sudokuBoard[i][j]) + } + } + + let combinedRowColValue = Set(colValue).union(Set(rowValue)) + let completeNumber = Set([0,1,2,3,4,5,6,7,8,9]) + + return completeNumber.subtract(combinedRowColValue) +} + +let sudokuBoard = [[5,0,8,9,0,0,0,0,0], + [0,7,3,6,0,0,9,0,8], + [1,9,0,4,0,8,0,3,5], + [0,7,0,0,0,2,0,1,0], + [0,0,0,0,0,0,0,0,0], + [0,6,0,9,0,0,0,8,0], + [1,9,0,2,0,3,0,8,7], + [3,0,6,0,0,7,1,9,0], + [0,0,0,0,0,9,3,0,4]] +getValidNumbers(sudokuBoard, row: 4, col: 4) + + + +// 2) 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]] + +func rotateMatrixNinetyDegree(matrix: [[Int]]) -> [[Int]] { + var rotatedMatrix = [[Int]]() + for i in 0.. Date: Thu, 21 Jan 2016 15:44:58 -0500 Subject: [PATCH 06/10] List and sorts hw finished --- HWFrom1-17-16(Lists and Sorts).playground/Contents.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index c883310..ce11567 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -135,5 +135,5 @@ rotateMatrixNinetyDegree(matrix) // 3) 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? - +// Once the the first two and the last two are in order then compare the 3rd element with the 2nd element. If the 3rd element is greater than 2nd element then four elements are in order, if not then switch 3rd and 2nd element. If additional comparision are needed then I would compare the first and the last element. From 8e2847b74eb68e332f1af18ca47d49c38c293e63 Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Thu, 21 Jan 2016 18:38:07 -0500 Subject: [PATCH 07/10] refactored sudoku code and motified question three's answer --- .DS_Store | Bin 8196 -> 10244 bytes .../Contents.swift | 65 ++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/.DS_Store b/.DS_Store index 430518248f452d78ce8e9d3b1438cdffe95ea149..08331edf382c6e9fb94d3b0a7d63f5d3392db101 100644 GIT binary patch delta 215 zcmZp1XbF&DU|?W$DortDU{C-uIe-{M3-C-V6q~50$SAfkU^hRb*yIZWK9ko7^KVWU zh+}2coE#?>H2Hu4ADh`SpkfGDMv$BHxQsQB4-<(I;bSb=d{ESlX=B4VHbG{v6oUdc rkah)Gv9a(w^JIRRKoKU0#Tp<5j0_A0AbN6vOz&hy(Icc2mlh+9IZ%!A8 zW1ZYDUb(qI{3N3wGf*iINN@uQSCFoah2NPc^Q#1MFha~?m>kbDb@E@46Hq [String: [Int]] { + // grab all the index Array that are associated with col + let col = paramCol-1 + var xArray = [Int]() + let numberOfColBox = Int(sqrt(Double(board[0].count))) + var xNumber = (col)/numberOfColBox + for _ in 0.. [String: [Int]] { + // grab all the index Array that are associated with row + let row = paramRow - 1 + var yArray = [Int]() + var yNumber = (row)/3 + yArray.append(yNumber) + yNumber += 1 + yArray.append(yNumber) + yNumber += 1 + yArray.append(yNumber) + + // grall all the index number that are row Array + var yArrayAtIndex = [Int]() + if(row % 3 == 0){ + yArrayAtIndex.appendContentsOf([0,1,2]) + } else if (row % 3 == 1) { + yArrayAtIndex.appendContentsOf([3,4,5]) + } else { + yArrayAtIndex.appendContentsOf([6,7,8]) + } + + return ["sudokuSmallBox":yArray, "indexNumber":yArrayAtIndex] +} + + + func getAllColArrayIndex(paramCol: Int) -> [String: [Int]] { // grab all the index Array that are associated with col let col = paramCol-1 @@ -63,7 +119,8 @@ func getAllRowArrayIndex(paramRow: Int) -> [String: [Int]] { } func getValidNumbers(sudokuBoard: [[Int]], row: Int, col: Int) -> Set{ - let colDictionary = getAllColArrayIndex(col) + let colDictionary = getAllColArrayIndex1(col, board: sudokuBoard) +// let colDictionary = getAllColArrayIndex(col) let rowDictionary = getAllRowArrayIndex(row) var colValue = [Int]() @@ -86,7 +143,7 @@ func getValidNumbers(sudokuBoard: [[Int]], row: Int, col: Int) -> Set{ return completeNumber.subtract(combinedRowColValue) } -let sudokuBoard = [[5,0,8,9,0,0,0,0,0], +let sampleInput = [[5,0,8,9,0,0,0,0,0], [0,7,3,6,0,0,9,0,8], [1,9,0,4,0,8,0,3,5], [0,7,0,0,0,2,0,1,0], @@ -95,7 +152,7 @@ let sudokuBoard = [[5,0,8,9,0,0,0,0,0], [1,9,0,2,0,3,0,8,7], [3,0,6,0,0,7,1,9,0], [0,0,0,0,0,9,3,0,4]] -getValidNumbers(sudokuBoard, row: 4, col: 4) +getValidNumbers(sampleInput, row: 4, col: 4) @@ -135,5 +192,5 @@ rotateMatrixNinetyDegree(matrix) // 3) 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? -// Once the the first two and the last two are in order then compare the 3rd element with the 2nd element. If the 3rd element is greater than 2nd element then four elements are in order, if not then switch 3rd and 2nd element. If additional comparision are needed then I would compare the first and the last element. +// Once the the first two and the last two are in order then compare the C element with the B element. If C element is greater than B element then all four elements are in order. if not then compare C with A element, if C < A then insert C before A otherwise after A. From a6f2040d4ae0e99105f7071512ed904894dfd689 Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Thu, 21 Jan 2016 19:10:54 -0500 Subject: [PATCH 08/10] need to refactor the last getallrowarrayindex --- .DS_Store | Bin 10244 -> 10244 bytes .../Contents.swift | 40 +++++++++++------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/.DS_Store b/.DS_Store index 08331edf382c6e9fb94d3b0a7d63f5d3392db101..ba8cbf112989d726d84e593abf83b1ca14709a8a 100644 GIT binary patch delta 63 vcmZn(XbIS$CdOo5G`UYKY;v6#53}+`hRFv-!zagx@NNDiX2OqKPKXfzq)8OV delta 63 vcmZn(XbIS$CdOp8Y;vDi*yK7f9%dP9hRFv-!zagx@NNDiX2OqKPKXfzke3tl diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index 61fa1a9..053bd32 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -17,13 +17,13 @@ Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB // 1) 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. -func getAllColArrayIndex1(paramCol: Int, board: [[Int]]) -> [String: [Int]] { +func getAllColArrayIndexRefactored(paramCol: Int, board: [[Int]]) -> [String: [Int]] { // grab all the index Array that are associated with col let col = paramCol-1 var xArray = [Int]() - let numberOfColBox = Int(sqrt(Double(board[0].count))) + let numberOfColBox = Int(sqrt(Double(board.count))) var xNumber = (col)/numberOfColBox - for _ in 0.. [String: [Int]] { } -func getAllRowArrayIndex1(paramRow: Int, board: [[Int]]) -> [String: [Int]] { +func getAllRowArrayIndexRefactored(paramRow: Int, board: [[Int]]) -> [String: [Int]] { // grab all the index Array that are associated with row let row = paramRow - 1 var yArray = [Int]() - var yNumber = (row)/3 - yArray.append(yNumber) - yNumber += 1 - yArray.append(yNumber) - yNumber += 1 - yArray.append(yNumber) + let numberOfRowBox = Int(sqrt(Double(board.count))) + var yNumber = row / numberOfRowBox + + for _ in 0.. [String: [Int]] { } func getValidNumbers(sudokuBoard: [[Int]], row: Int, col: Int) -> Set{ - let colDictionary = getAllColArrayIndex1(col, board: sudokuBoard) + let colDictionary = getAllColArrayIndexRefactored(col, board: sudokuBoard) // let colDictionary = getAllColArrayIndex(col) +// let rowDictionary = getAllRowArrayIndexRefactored(row, board: sudokuBoard) let rowDictionary = getAllRowArrayIndex(row) var colValue = [Int]() From 84ef5a1b1f2fda6b9dca47ac687653715183e4c1 Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Thu, 21 Jan 2016 19:56:03 -0500 Subject: [PATCH 09/10] hw with cameron's solution --- .DS_Store | Bin 10244 -> 10244 bytes .../Contents.swift | 89 +++++++++++++++++- 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/.DS_Store b/.DS_Store index ba8cbf112989d726d84e593abf83b1ca14709a8a..9be638f90815fc7e646e4ce44ef3db7073d5f7ee 100644 GIT binary patch delta 63 vcmZn(XbIS$CdOpDdUBsw*yK7f9%j96hRFv-!zagx@NNDiX2OqKPKXfzs=XA? delta 63 vcmZn(XbIS$CdOo5G`UYKY;v6#53}+`hRFv-!zagx@NNDiX2OqKPKXfzq)8OV diff --git a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift index 053bd32..89efb62 100644 --- a/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift +++ b/HWFrom1-17-16(Lists and Sorts).playground/Contents.swift @@ -16,6 +16,46 @@ Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB // 1) 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. +func getValidNumbersCameron(sudokuBoard:[[Int?]], row:Int, col:Int) -> [Int] { + var valid: Set = [1, 2, 3, 4, 5, 6, 7, 8, 9] + + for c in 0..(valid) +} + + + + func getAllColArrayIndexRefactored(paramCol: Int, board: [[Int]]) -> [String: [Int]] { // grab all the index Array that are associated with col @@ -127,8 +167,9 @@ func getAllRowArrayIndex(paramRow: Int) -> [String: [Int]] { func getValidNumbers(sudokuBoard: [[Int]], row: Int, col: Int) -> Set{ let colDictionary = getAllColArrayIndexRefactored(col, board: sudokuBoard) -// let colDictionary = getAllColArrayIndex(col) // let rowDictionary = getAllRowArrayIndexRefactored(row, board: sudokuBoard) +// let colDictionary = getAllColArrayIndex(col) + let rowDictionary = getAllRowArrayIndex(row) var colValue = [Int]() @@ -160,8 +201,19 @@ let sampleInput = [[5,0,8,9,0,0,0,0,0], [1,9,0,2,0,3,0,8,7], [3,0,6,0,0,7,1,9,0], [0,0,0,0,0,9,3,0,4]] -getValidNumbers(sampleInput, row: 4, col: 4) +let sampleInputCameron: [[Int?]] = [[5,0,8,9,0,0,0,0,0], + [0,7,3,6,0,0,9,0,8], + [1,9,0,4,0,8,0,3,5], + [0,7,0,0,0,2,0,1,0], + [0,0,0,0,0,0,0,0,0], + [0,6,0,9,0,0,0,8,0], + [1,9,0,2,0,3,0,8,7], + [3,0,6,0,0,7,1,9,0], + [0,0,0,0,0,9,3,0,4]] + +getValidNumbers(sampleInput, row: 4, col: 4) +getValidNumbersCameron(sampleInputCameron, row: 4, col: 4) // 2) rotate a matrix by ninety degrees @@ -190,6 +242,7 @@ func rotateMatrixNinetyDegree(matrix: [[Int]]) -> [[Int]] { return rotatedMatrix } + let matrix = [[1,2,3,4], [5,6,7,8], [9,0,1,2], @@ -197,8 +250,40 @@ let matrix = [[1,2,3,4], rotateMatrixNinetyDegree(matrix) + + // 3) 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? // Once the the first two and the last two are in order then compare the C element with the B element. If C element is greater than B element then all four elements are in order. if not then compare C with A element, if C < A then insert C before A otherwise after A. +func mySort(value: [Int]) -> [Int] { + var left = value[0...1] + if left[0] > left [1] { + let t = left[0] + left[0] = left[1] + left[1] = t + } + + var right = value[2...4] + if right[0] > right [1] { + let t = right[1] + right[0] = right[1] + right[1] = t + } + // 1 3 + // 2 4 + + return[] +} + + + + + + + + + + + From b64d6b6a57b702ca68ccbf1f1b01483e9f12cdc2 Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Thu, 28 Jan 2016 19:42:26 -0500 Subject: [PATCH 10/10] finished the 3rd question --- .DS_Store | Bin 10244 -> 10244 bytes .../Contents.swift | 2 +- calebRecursionHW/calebRecursionHW/main.swift | 10 ++++++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.DS_Store b/.DS_Store index 1df200873f6ef00ac00befdd43bf8593e3437452..d79841e1b1265520b0729374cf1229152285609d 100644 GIT binary patch delta 49 zcmV-10M7q}P=rvBPXQdUP`eKS9Fx!xOtV-Jw+I26lMoq1lh70k1<+!)006TP8KVZX H2O#|isfZAh delta 46 zcmV+}0MY-1P=rvBPXQaTP`eKS8 Int { return nth } -print(fib(1, secondNumber: 1, n: 5)) +//print(fib(1, secondNumber: 1, n: 5)) //Question 2 diff --git a/calebRecursionHW/calebRecursionHW/main.swift b/calebRecursionHW/calebRecursionHW/main.swift index 4f4d4cf..2f54b40 100644 --- a/calebRecursionHW/calebRecursionHW/main.swift +++ b/calebRecursionHW/calebRecursionHW/main.swift @@ -19,9 +19,15 @@ func findFile(name: String, atPath: String) -> String { if exists && Bool(isDir) { // YOUR CODE HERE print("DIR: " + fileOrDir) + let result = findFile(name, atPath: fullPath) + if result != "NOT FOUND" { + return result + } } else if exists { // YOUR CODE HERE - print("FILE: " + fileOrDir) + if fileOrDir == name { + return "FOUND" + "FILE: " + fullPath + } } else { print("NEITHER: " + fileOrDir) } @@ -29,4 +35,4 @@ func findFile(name: String, atPath: String) -> String { return "NOT FOUND" } -print(findFile("awesome-idea.txt", atPath: "/Users/calebegg/Documents")) +print(findFile("20160112JasonWang.pdf", atPath: "/Users/jasonwang/Documents"))