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

Justine_unit-4-assignments_2016-01-14 #18

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b9bb329
HWfrom1-09-16(SwiftIntro) completed
Jan 14, 2016
bd5afb7
Added answers to the Big O, will clean up
Jan 15, 2016
f7cc701
Cleaned up the Big O homework
Jan 15, 2016
26aaa36
Merge https://github.com/accesscode-2-2/unit-4-assignments
Jan 15, 2016
ae1764c
Logic+DiscreteMath, Questions 1 & 2 completed
Jan 15, 2016
4edb6f4
Added big O runtime for question 2
Jan 16, 2016
dfb4452
Updated
Jan 19, 2016
4c441a3
Merge https://github.com/accesscode-2-2/unit-4-assignments
Jan 19, 2016
4eef9cd
Began working on HWFrom1-17-16
Jan 19, 2016
91dc8fe
Question 1 answered
Jan 21, 2016
8f65155
Question 2 solved for 4x4 matrix, must adjust func to any size
Jan 21, 2016
54e2962
Question 2 answer updated
Jan 21, 2016
fea462d
cleaned up
Jan 21, 2016
c90fc20
Question 3 answered with Ints. Will try to sort strings as well
Jan 21, 2016
1192faf
sorted single char strings for Question 3
Jan 21, 2016
6791eab
Refined the func for strings in Q3
Jan 21, 2016
cd617a7
fixed merge conflict with Homework link
Jan 26, 2016
eeec02b
Question 2 answered, wrote a new tryStep method as well :)
Jan 28, 2016
d57b6c7
Question 3 completed successfully
Jan 28, 2016
2b1a543
Minor refinement
Jan 28, 2016
bbf5c75
No Changes
Jan 29, 2016
7a4de98
Merge https://github.com/accesscode-2-2/unit-4-assignments
Jan 29, 2016
633d81a
Created a recursive insertion sort method
Jan 29, 2016
495e669
Added second solution using swap
Jan 29, 2016
88252d7
Merge https://github.com/accesscode-2-2/unit-4-assignments
Feb 3, 2016
d88392d
Question 6 answered
Feb 4, 2016
81a1450
Question 2 of Sets and Hash Maps answered
Feb 4, 2016
da424e9
Question 3 partially answered, importFrom func completed
Feb 4, 2016
775fb34
Cleaned up answer for Question 2 in Sets and Hash maps
Feb 5, 2016
e931466
Sets and hash maps reviewed
Feb 5, 2016
59e7761
Merge https://github.com/accesscode-2-2/unit-4-assignments
Feb 10, 2016
3517ec6
Added adjacency matrix images
Feb 10, 2016
2aefacd
Created binary tree from postfix mathmatical function
Feb 11, 2016
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.
262 changes: 258 additions & 4 deletions HWFrom1-17-16(Lists and Sorts).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,272 @@ 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#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

Input: sudokuBoard:[[Int?]]. (Each location on the board will be either be an Int from 1-9 or nil(empty cell))
row: Int
col: Int
*/

let 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 booleanArr (arr:[Int]) -> [Bool]{

//create an array of false bools to match the input array

let booleanArr = Array(count: arr.count+1, repeatedValue: false)

return booleanArr

}


2)
func possibleNumbers(arr:[Int], inout booleanArr: [Bool]) -> [Bool] {


//loop through the input arr and change
//the bool of the same index as the integer
//in the corresponding array

for num in arr {

//Change (the input num)index of the boolean arr
//to true

booleanArr[num] = true
}




return booleanArr
}

func allPossibleNumbers(booleanArr: [Bool]) -> [Int]
{

var possibleNums = [Int]()

//If the bool in our boolean arr is false
//add that index num to our availableNum arr

for i in 1..<booleanArr.count {

if booleanArr[i] == false {

possibleNums.append(i)
}

}

return possibleNums
}


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

var validNums = [Int]()


// create row array
let rowArr = sudokuBoard[row]
print("row \(rowArr)")

var columnArr: [Int] = []
var square: [Int] = []

for i in 0..<rowArr.count{

columnArr.append(sudokuBoard[i][col])
square.append(sudokuBoard[(col / 3) * 3 + i / 3][col * 3 % 9 + i % 3])
}

print("column \(columnArr)")
print("squares \(square)")

//for each array: row, column, square
//find possible numbers
//change the value of the possibleBools array

var possibleBools = booleanArr(rowArr)
possibleBools = possibleNumbers(rowArr, booleanArr: &possibleBools)
possibleNumbers(columnArr, booleanArr: &possibleBools)
possibleNumbers(square, booleanArr: &possibleBools)

validNums = allPossibleNumbers(possibleBools)

return validNums
}

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

var matrix = [[1,2,3,4],
[5,6,7,8],
[9,0,1,2],
[3,4,5,6]]


func rotateMatrix90Degrees(inout grid: [[Int]]) ->[[Int]]
{
let temp = Array(count: grid.count, repeatedValue: 0)
var tempGrid = Array(count: grid.count, repeatedValue:temp)
let count = grid.count

for i in 0..<count {

for j in 0..<count {

tempGrid[i][j] = grid[count-1-j][i]
}

}

print("TempGrid: \(tempGrid)")
print("grid: \(grid)")

grid = tempGrid

return grid
}

rotateMatrix90Degrees(&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?
*/


let A = 1
let B = 2
let C = 3
let D = 4
var fourElementArr = [D, A, C, B]

let E = 1
let F = 21
let G = 49
let H = 365
var anotherFourElementArr = [H, G, E, F]

func sortFourElements(inout arr:[Int]) ->[Int]
{
let maxIndex = arr.count - 1
let midIndex = maxIndex / 2

for _ in midIndex...maxIndex {

if arr[midIndex]<arr[midIndex-1] {

(arr[midIndex], arr[midIndex-1]) = (arr[midIndex-1], arr[midIndex])
}

if arr[maxIndex] < arr[maxIndex-1] {

(arr[maxIndex-1], arr[maxIndex]) = (arr[maxIndex], arr[maxIndex-1])
}

if arr[midIndex] > arr[maxIndex-1] {

(arr[midIndex], arr[maxIndex-1]) = (arr[maxIndex-1], arr[midIndex])
}
}




return arr
}

sortFourElements(&fourElementArr)
sortFourElements(&anotherFourElementArr)


//With single char strings
let I = "I"
let J = "J"
let K = "K"
let L = "L"
var fourStringArr = [I, L, K, J]

let M = "Monopoly"
let N = "Neuron"
let O = "Ocean"
let P = "Polar Bear"
var anotherFourStringArr = [P, O, M, N]

var mArr = ["Monkey", "Mission", "Marmalade", "Mars"]


func sortFourStrings(inout arr:[String]) ->[String]
{
let maxIndex = arr.count - 1
let midIndex = maxIndex / 2

for _ in midIndex...maxIndex {

if arr[midIndex]<arr[midIndex-1] {

(arr[midIndex], arr[midIndex-1]) = (arr[midIndex-1], arr[midIndex])
}

if arr[maxIndex] < arr[maxIndex-1] {

(arr[maxIndex-1], arr[maxIndex]) = (arr[maxIndex], arr[maxIndex-1])
}

if arr[midIndex] > arr[maxIndex-1] {

(arr[midIndex], arr[maxIndex-1]) = (arr[maxIndex-1], arr[midIndex])
}
}




return arr
}

sortFourStrings(&fourStringArr)
sortFourStrings(&anotherFourStringArr)
sortFourStrings(&mArr)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>
Loading