Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done with all but #7 #8

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 173 additions & 7 deletions HWFrom1-17-16(Lists and Sorts).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,182 @@ Work on your solutions here.

Link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZKBwsY/edit#heading=h.za36ai6n5fth

1)



2)

1)
*/
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]]


func getNumbersInRow(sudokuBoard:[[Int]], row: Int) -> Set<Int> {
var numbersInRow = [Int]()

for i in sudokuBoard[row] {
if i != 0 {
numbersInRow.append(i)
}
}

return Set(numbersInRow)
}

func getNumbersInColumn(sudokuBoard:[[Int]], col: Int) -> Set<Int> {
var numbersInColumn = [Int]()

for i in 0..<sudokuBoard.count {

if sudokuBoard[i][col] != 0 {
numbersInColumn.append(sudokuBoard[i][col])

}
}

return Set(numbersInColumn)
}

func getNumbersInSquare(sudokuBoard: [[Int]], row: Int, col: Int) -> Set<Int> {
var numbersInGrid = [Int]()

let coordinateOfSquare = checkForTopLeftCoordinates(row, col: col)

for i in coordinateOfSquare.row...coordinateOfSquare.row + 2 {
for j in coordinateOfSquare.col...coordinateOfSquare.col + 2 {
if sudokuBoard[i][j] != 0 {
numbersInGrid.append(sudokuBoard[i][j])
}
}
}

return Set(numbersInGrid)
}

func checkForTopLeftCoordinates(row: Int, col: Int) -> (row: Int, col: Int) {
let topLeftRowCoordinateOfSquare = (row / 3) * 3
let topLeftColCoordinateOfSquare = (col / 3) * 3

return (topLeftRowCoordinateOfSquare, topLeftColCoordinateOfSquare)
}

func getNumbers1to9NotInSet(set: Set<Int>) -> [Int] {
var numbersAvailable = [Int]()

for i in 1...9 {
if set.contains(i) == false {
numbersAvailable.append(i)
}
}
return numbersAvailable
}


func getValidNumbers(sudokuBoard:[[Int]], row: Int, col: Int) -> [Int] {


let numbersUsedInRow = getNumbersInRow(sudokuBoard, row: row)
let numbersUsedInColumn = getNumbersInColumn(sudokuBoard, col: col)
let numbersUsedInSquare = getNumbersInSquare(sudokuBoard, row: row, col: col)

let numbersUsed = numbersUsedInRow.union(numbersUsedInColumn).union(numbersUsedInSquare)

let numbersAvailable = getNumbers1to9NotInSet(numbersUsed)

return numbersAvailable
}

getValidNumbers(sudokuBoard, row: 4, col: 4)


// 2)
let array = [
[1,2,3,4,5],
[5,6,7,8,9],
[9,0,1,2,16],
[3,4,5,6,20],
[5,19,29,102,2]]

func rotateArray90Degrees(arrayToRotate: [[Int]]) -> [[Int]] {
var newArray = arrayToRotate

for i in 0..<arrayToRotate.count {
for j in 0..<arrayToRotate.count {
newArray[j][arrayToRotate.count-1-i] = arrayToRotate[i][j]
}
}

return newArray
}

rotateArray90Degrees(array)

/*
i = 0, j =0, newArray[0, 3] = arrayToRotate[0][0]
i = 0, j = 1, newArray[1, 3] = arrayToRotate[0][1]
i = 0, j = 2, newArray[2, 3] = arrayToRotate[0][2]
i = 0, j = 3, newArray[3, 3] = arrayToRotate[0][3]
i = 1, j =0, newArray[0,4-1-1] = arrayToRotate[1][0]
i = 1, j = 1, newArray[1, 4-1-1] = arrayToRotate[1][1]
i = 1, j = 2, newArray[2, 4-1-1] = arrayToRotate[1][2]
i = 1, j = 3, newArray[3, 4-1-1] = arrayToRotate[1][3]
etc.
etc.

3)
*/



// 3)

var unSortedArray = [11, 50, 20, 1]

func sortArray(inout arr: [Int]) -> [Int] {
var temp: Int

if arr[0] > arr[1] {
temp = arr[0]
arr[0] = arr[1]
arr[1] = temp
}

if arr[2] > arr[3] {
temp = arr[2]
arr[2] = arr[3]
arr[3] = temp
}

if arr[0] > arr[2] {
temp = arr[0]
arr[0] = arr[2]
arr[2] = temp
}

if arr[1] > arr[3] {
temp = arr[1]
arr[1] = arr[3]
arr[3] = temp
}

if arr[1] > arr[2] {
temp = arr[1]
arr[1] = arr[2]
arr[2] = temp
}


return arr
}

sortArray(&unSortedArray)



<<<<<<< HEAD
=======
*/
>>>>>>> 73de515ec342afcee5a0edcbce4663c779ecf94b
33 changes: 33 additions & 0 deletions HWFrom1-28-16(Merge Sort).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,36 @@


//Insert code here:
func selectionSort(inout values: [Int]) -> [Int] {

recursiveLoop(&values, index: 0)

return values
}

func recursiveLoop(inout values: [Int], index: Int){
if index < values.count {
var minimumIndex = index
innerRecursiveLoop(&values, index: index + 1, minimumIndex: &minimumIndex)

if minimumIndex != index {
swap(&values[index], &values[minimumIndex])
}
recursiveLoop(&values, index: index + 1)
}

}

func innerRecursiveLoop(inout values: [Int], index: Int, inout minimumIndex: Int) {
if index < values.count {

if values[index] < values[minimumIndex] {
minimumIndex = index
}

innerRecursiveLoop(&values, index: index + 1, minimumIndex: &minimumIndex)
}
}

var array = [50, 46, 82, 19, 33, 35, 99, 2, 5, 1009]
selectionSort(&array)
128 changes: 122 additions & 6 deletions HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,130 @@
//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.

//2)
// Bubble Sort = O(n^2) ... Because you have to iterate through n elements n times.

//3)
// Insertion Sort = O(n^2) ... Because, in the worst case, aside from looping through the unsorted array to grab the element to be compared, if the number being inserted is less than every element in the sorted list you will have to make a comparison with every element already sorted.

//4)
// Selection Sort = O(n^2) ... Because after looping through the whole array to grab the minimum element and looping through the array minus 1 each time until everything is sorted, the run time is O(n^2)/2 which means the time complexity is still O(n^2) because when it comes to Big Oh complexitiy, we disregard the constant.

//5)
// Merge Sort = O(n log n) ... Because it divides the input in half recursively and has to make n comparisons each time.
// Quick Sort = O(n log n) ... Because you have to do n-1 comparisons on each iteration and log n becuase you have to divide the elements log n times

//2) What is the advantage of partitioning quicksort in place?
// No need to create another array and deal with merging them.

//3) Without looking, implement quicksort. * I looked. Couldn't do it and it was taking too long.*

func quickSort(inout arr: [Int]) {
quickSort(&arr, firstIdx: 0, lastIdx: arr.count-1)
}

func quickSort(inout arr: [Int], firstIdx: Int, lastIdx: Int) {
// base case
if firstIdx - lastIdx >= 1 { return }

// partition
let splitPoint = partition(&arr, firstIdx: firstIdx, lastIdx: lastIdx)

// quickSort on leftHalf
quickSort(&arr, firstIdx: firstIdx, lastIdx: splitPoint - 1)

// quickSort on rightHalf
quickSort(&arr, firstIdx: splitPoint + 1, lastIdx: lastIdx)
}

func partition(inout arr: [Int], firstIdx: Int, lastIdx: Int) -> Int {
// set pivotValue to firstElement
let pivotValue = arr[firstIdx]
// set leftMark
var leftMark = firstIdx + 1
// set rightMark
var rightMark = lastIdx

/*
as leftMark and rightMark close in on each other,
swap the items that are greater than the pivot value
on the left side with the items that are less than the pivot
value on the right side. Stop when rightMark crosses leftMark
*/
while leftMark <= rightMark {
while arr[leftMark] < pivotValue {
leftMark += 1
}
while arr[rightMark] > pivotValue {
rightMark -= 1
}

if leftMark < rightMark {
swap(&arr[leftMark], &arr[rightMark])
}
}
// set the correct value at the splitPoint
if firstIdx != rightMark {
swap(&arr[firstIdx], &arr[rightMark])
}
return rightMark // return the splitPoint
}

var arrayForQuickSort = [22, 15, 38, 93, 95, 0, 34, 58, 72, 59]


quickSort(&arrayForQuickSort)

//4) Write a function to generate an array of random numbers bounded between 1..<10,000 of size 10,000.

// Int(arc4random_uniform(UInt32(10000)))

// Compare the time it takes to run mergesort, quicksort, and quicksort with the median.
// https://gist.github.com/gummibeatz/8ff29bcec54d7e3ef683



//5) Describe the algorithmic difference between mergesort and quicksort. Where does the sorting happen? As the recursive calls are being pushed onto the stack or as they are being popped off?
// For quicksort, the sorting happens as the recursive calls are being pushed onto the stack as opposed to mergesort which is sorted "on the way up" or as the stack is being popped off.

//6) Given an array of strings containing “[“,”]”,”{“,”}”,”(“,”)”. Output whether or not the parentheses are balanced.
//Good examples: () [] () ([]()[])
//Bad examples: ( ( ] ([)]

func samePair(element1: String?, element2: String) -> Bool {

if element1 == "{" && element2 == "}" {
return true
} else if element1 == "[" && element2 == "]" {
return true
} else if element1 == "(" && element2 == ")" {
return true
} else {
return false
}

}

func isBalanced(paren: [String]) -> Bool {

guard paren.count < 2 || paren.isEmpty || paren.count % 2 == 0 else {
return false
}

var emptyArray = [String]()
let openingSet = Set(["(", "[", "{"])

for i in 0..<paren.count {
if openingSet.contains(paren[i]) {
emptyArray.append(paren[i])
} else if emptyArray.isEmpty || !samePair(emptyArray.last, element2: paren[i]) {
return false
} else {
emptyArray.removeLast()
}
}

return emptyArray.isEmpty
}

var array = ["[","]","{","}","(",")"]
isBalanced(array)

//6)
Loading