Skip to content

incomplete #17

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

Open
wants to merge 26 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.
2 changes: 2 additions & 0 deletions .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store

# Xcode
#
build/
Expand Down
171 changes: 158 additions & 13 deletions HWFrom1-17-16(Lists and Sorts).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,170 @@

import UIKit

var str = "Hello, playground"

/*


Work on your solutions here.

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

1)



2)



3)

1) */

func sudokuSpaceOptions(board: [[Int?]], x: Int, y: Int) -> [Int] {

// for our purposes x is the horizontal axis and y the vertical
// for this to make sense with the visual layout of the array of arrays
// we need to index the numbers like so: number[y][x]

//check if !nil
if board[y][x] != nil {

// if !nil return the number that is already there as the single member of
// the return array
return [board[y][x]!]
}

var possibilities: Set = Set([1, 2, 3, 4, 5, 6, 7, 8, 9])

// check column
for i in 0...8 {

if board[i][x] != nil {

// exclude numbers in column
possibilities.remove(board[i][x]!)
}
}

// check row
for i in 0...8 {

// exclude numbers in row
if board[y][i] != nil {

possibilities.remove(board[y][i]!)
}
}

let indices: [[Int?]] = [[0, 2], [3, 5], [6, 8]]
var xSquareBounds: [Int?] = [nil, nil]
var ySquareBounds: [Int?] = [nil, nil]

// figure out the square bounds
for numArray in indices {

if (x >= numArray[0] && x <= numArray[1]) {

xSquareBounds = numArray
}
if (y >= numArray[0] && y <= numArray[1]) {

ySquareBounds = numArray
}
}

// check square
for i in ySquareBounds[0]!...ySquareBounds[1]! {

for j in xSquareBounds[0]!...xSquareBounds[1]! {

if board[i][j] != nil {

// exclude numbers in square
possibilities.remove(board[i][j]!)
}
}
}

return Array(possibilities)

}

var sudokuBoard: [[Int?]] = [

[5 , nil, 8 , nil, 7 , 3 , 1 , 9 , nil],
[9 , nil, nil, 6 , nil, nil, 4 , nil, 8 ],
[nil, nil, nil, 9 , nil, 8 , nil, 3 , 5 ],
[nil, 7 , nil, nil, nil, nil, nil, 6 , nil],
[nil, nil, 2 , nil, nil, nil, 9 , nil, nil],
[nil, 1 , nil, nil, nil, nil, nil, 8 , nil],
[1 , 9 , nil, 3 , nil, 6 , nil, nil, nil],
[2 , nil, 3 , nil, nil, 7 , nil, nil, 9 ],
[nil, 8 , 7 , 1 , 9 , nil, 3 , nil, 4 ]]

print(sudokuSpaceOptions(sudokuBoard, x: 2, y: 6))
print(sudokuSpaceOptions(sudokuBoard, x: 2, y: 0))

/*
2) */

func rotateMatrixByNinetyDegrees(matrix: [[Int]]) -> [[Int]] {

// create an array of the same dimensions with all zeroes
var rotated = [[Int]](count: matrix.count, repeatedValue: [Int](count: matrix[0].count, repeatedValue: 0))

for i in 0..<matrix.count {

for j in 0..<matrix[i].count {

// replace the default zeroes with the rotated values according to the formula
rotated[j][matrix.count - 1 - i] = matrix[i][j]
}
}

return rotated
}

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

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

print(rotateMatrixByNinetyDegrees(test))
print(rotateMatrixByNinetyDegrees(test2))

*/
/*
3) */

func sortFourElements(inout nums: [Int]) -> [Int] {

var temp: Int
let indices = [[0,1], [2,3], [0,2], [1,3], [1,2]]

for i in 0..<indices.count {

if nums[indices[i][0]] > nums[indices[i][1]] {

temp = nums[indices[i][0]]
nums[indices[i][0]] = nums[indices[i][1]]
nums[indices[i][1]] = temp
}
}

return nums

}

var nums = [4, 3, 2, 1]
var nums2 = [4, 3, 1, 2]
var nums3 = [4, 2, 1, 3]
var nums4 = [2, 4, 3, 1]
var nums5 = [3, 4, 1, 2]
var nums6 = [3, 4, 2, 1]
var nums7 = [2, 3, 1, 4]
var nums8 = [3, 2, 4, 1]

print(sortFourElements(&nums))
print(sortFourElements(&nums2))
print(sortFourElements(&nums3))
print(sortFourElements(&nums4))
print(sortFourElements(&nums5))
print(sortFourElements(&nums6))
print(sortFourElements(&nums7))
print(sortFourElements(&nums8))
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>
53 changes: 39 additions & 14 deletions HWFrom1-24(Recursion).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
/*


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



*/


import Foundation

//Question 1





func fib(n: Int) -> Int {

var a = 1
var b = 1

for _ in 0..<n {
let t = a
a = b
b = t + b
}
return b
}

//Question 2





//Question 3
var stepNum = 0
func tryStep() -> Int {
let stepCount = Int(arc4random_uniform(3)) - 1
stepNum += stepCount;
switch(stepCount) {
case -1: print("Ouch \(stepNum)")
case 1: print("Yay \(stepNum)")
default: print("Beep \(stepNum)")
}
return stepCount
}

func stepUp() {

switch tryStep() {
case 1:
return
case -1:
stepUp()
stepUp()
default:
stepUp()
}
}

//Question 3
2 changes: 1 addition & 1 deletion HWFrom1-24(Recursion).playground/contents.xcplayground
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