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

Homework #4

Open
wants to merge 23 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
Binary file added .DS_Store
Binary file not shown.
Binary file added Graph_Ex.3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Graph_Ex.4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
123 changes: 110 additions & 13 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,113 @@ Work on your solutions here.

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

1)



2)



3)



*/
1) */

func getValidNumbers(sudokuBoard:[[Int?]], row:Int, col:Int) -> [Int] {
var valid: Set<Int> = [1, 2, 3, 4, 5, 6, 7, 8, 9]

for c in 0..<sudokuBoard[row].count {
if c == col {
continue
}
if let value = sudokuBoard[row][c] {
valid.remove(value)
}
}

for r in 0..<sudokuBoard.count {
if r == row {
continue
}
if let value = sudokuBoard[r][col] {
valid.remove(value)
}
}

let sqRow = row / 3
let sqCol = col / 3

for r in sqRow..<(sqRow + 3) {
for c in sqCol..<(sqCol + 3) {
if r == row && c == col {
continue
}
if let value = sudokuBoard[r][c] {
valid.remove(value)
}
}
}

// Turn the set into an array
return Array<Int>(valid)
}

let sampleInput: [[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: 0, col: 1)



// 2)

func rotate(matrix: [[Int]]) -> [[Int]] {
let n = matrix.count

var result: [[Int]] = []

for _ in 0..<n {
var row: [Int] = []
for _ in 0..<n {
row.append(0)
}
result.append(row)
}

for (r, row) in matrix.enumerate() {
for (c, val) in row.enumerate() {
result[c][n - r - 1] = val
}
}

return result
}

let rotateInput = [[1,2,3,4],
[5,6,7,8],
[9,0,1,2],
[3,4,5,6]]

rotate(rotateInput)



// 3)

func mySort(values: [Int]) -> [Int] {
var left = values[0...1]
if left[0] > left[1] {
let t = left[0]
left[0] = left[1]
left[1] = t
}

var right = values[2...4]
if right[0] > right[1] {
let t = right[1]
right[0] = right[1]
right[1] = t
}

// 1 3
// 2 4

return []
}
38 changes: 37 additions & 1 deletion HWFrom1-24(Recursion).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Foundation
import UIKit
/*


Expand All @@ -11,15 +13,49 @@ Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3l

//Question 1

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 more efficient and faster, because it doesn't have to iterate through the array several times




//Question 2

var steppNum = 0

func tryStepp() -> Int {
let stepCount = Int(arc4random_uniform(3)) - 1 // generate rando number
steppNum += stepCount;

switch(stepCount) {
case -1: print("Ouch \(steppNum)")
case 1: print("Yay \(steppNum)")
default: print("Beep \(steppNum)")
}
return stepCount
}

func steppUp(var steps: Int = 0) {
steps += tryStepp()
if steps == 1 {
return
}
steppUp(steps)
}

//Test
steppUp()

//Question 3


//Question 3
31 changes: 31 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,34 @@


//Insert code here:

func insertionSort(var array:[Int], index:Int) -> [Int]{

if index == array.count{
return array
}
else{

var tempArray = array[index],
i = 0,
j = 0,
maxIter = index - 1

while (i <= maxIter && array[i] < tempArray) {
i++
}
while j<=maxIter{
array[maxIter-j+1] = array[maxIter-j]
j++
}

array[i] = tempArray;

return insertionSort(array, index: index+1)
}

}

//Test
insertionSort([44, 7, 2, 25, 0], index: 1)

124 changes: 118 additions & 6 deletions HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,128 @@
//: Playground - noun: a place where people can play
import Foundation

//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.

// Bubble sort - T: O(n^2), S: O(1)
// Insertion sort - T: O(n^2), S: O(1)
// Selection sort - O(n^2), S: O(1)
// Mergesort - T: O(n log n), S: O(n)
// Quicksort - O(n log n), S: O(log n)


//2) What is the advantage of partitioning quicksort in place?
// Wouldn't need to crate another array, so the space complexity would be smaller

//3) Without looking, implement quicksort.

func partition(inout array: [Int], first: Int, last: Int) -> Int {
let pivot = array[first]
var leftMark = first + 1
var rightMark = last

while leftMark <= rightMark {
while leftMark <= rightMark && array[leftMark] < pivot {
leftMark = leftMark + 1
}
while leftMark <= rightMark && array[rightMark] > pivot {
rightMark = rightMark - 1
}
if leftMark < rightMark {
swap(&array[leftMark], &array[rightMark])
}
}
if first != rightMark {
swap(&array[first], &array[rightMark])
}
return rightMark
}


func quickSort(inout array: [Int], first: Int, last: Int) {
if first >= last { return }
let splitPoint = partition(&array, first: first, last: last)
quickSort(&array, first: first, last: splitPoint - 1)
quickSort(&array, first: splitPoint + 1, last: last)
}
func quickSort(inout arr: [Int]){
quickSort(&arr, first: 0, last: arr.count - 1)
}

//Test
var numberList = [4, 1, 35, 22, 66, 12]
quickSort(&numberList)


/*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
*/


func randomNumbers(size: Int, highestValue: Int) -> [Int] {

var array = Array<Int>(count: size, repeatedValue: 0)

for i in 0..<array.count {

array[i] = Int(arc4random_uniform(UInt32(highestValue))) //10.000 times
}
return array
}

var randomArray = randomNumbers(10000, highestValue: 10000)

quickSort(&randomArray, first: 0, last: randomArray.count - 1)


//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?

// Quicksort - the sorting happens when the calls are being pushed onto the stack
// Mergesort - the sorting happens when the calls are being popped off.


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


func isBalanced(paren: [String]) -> Bool {
if (paren.count%2 != 0) {
return false
}

var i = 0
var j = paren.count

while (i<j) {
let isEqual = (paren[i] == paren[j])
if isEqual {
i++
j--
}
return true
}

return false
}

print(isBalanced(["(())"]))








//2)

//3)

//4)

//5)

//6)
Loading