Skip to content

Did everything but the #7 from algorithms #9

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 14 commits into
base: master
Choose a base branch
from
88 changes: 87 additions & 1 deletion HWFrom1-17-16(Lists and Sorts).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,102 @@ Work on your solutions here.

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

1)
1)
*/
let board : [[Int?]] = [ [1, 7, 9, 8, 5, 4, 3, nil, 6],
[nil, 1, 4,nil,5, 4, 3, nil, 6],
[1, nil, 9, 8, 5, 4, 3, nil, 6],
[1, 2, 9, 8, 5, 4, 3, nil, 6],
[1, nil, 9, 8, 5, 4, 3, nil, 6],
[1, nil, 9, 8, 5, 4, 3, nil, 6],
[1, nil, 9, 8, 5, 4, 3, nil, 6],
[1, nil, 9, 8, 5, 4, 3, nil, 6],
[1, nil, 9, 8, 5, 4, 3, nil, 6] ]

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

let completeSet = Set.init(arrayLiteral: 1,2,3,4,5,6,7,8,9)

var alreadyUsedSet = Set<Int>()

for i in 0..<sudokuBoard.count {

if let colValue = sudokuBoard[row][i] {
alreadyUsedSet.insert(colValue)
}
if let rowValue = sudokuBoard[i][col] {
alreadyUsedSet.insert(rowValue)
}

}

let refRow = (row/3)*3
let refCol = (col/3)*3

for r in refRow..<(refRow+3) {
for c in refCol..<(refCol+3) {
if let squareValue = sudokuBoard[r][c] {
alreadyUsedSet.insert(squareValue)
}
}
}

let possibleValues = completeSet.subtract(alreadyUsedSet)

return Array(possibleValues)
}

getValidNumbers(board, row: 1, col: 3)

/*
2)
*/

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

var rotatedAray = [[Int]]()

for i in 0..<matrix.count {
var colToRow = [Int]()

for var j = (matrix.count-1); j >= 0; j-- {
colToRow.append(matrix[j][i])
}
rotatedAray.append(colToRow)
}

return rotatedAray
}

let array = [[1,3,4],
[5,2,6],
[9,8,4]]
rotateNinetyDegrees(array)

/*
3)
*/

func sortFour(inout array: [Int]) {

if array[0] > array[1] {
let temp = array[1]
array[1] = array[0]
array[0] = temp
}

if array[2] > array[3] {
let temp = array[3]
array[3] = array[2]
array[2] = temp
}


}





*/

17 changes: 16 additions & 1 deletion HWFrom1-24(Recursion).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,31 @@ Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3l


//Question 1
func fibIter(n: Int) -> Int{

if n <= 1 {
return 1
}

var oneBack = 1
var twoBack = 1

for _ in 2..<n {
let temp = oneBack
oneBack += twoBack
twoBack = temp
}

return oneBack + twoBack

}

fibIter(7)

//Question 2





//Question 3
//Question 3
74 changes: 74 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,77 @@


//Insert code here:

func recursiveInsertionSort<T: Comparable>(inout arr: [T], index: Int){

if index == arr.count {
return
}

findAndInsert(&arr, toIndex: index, compared: 0)
recursiveInsertionSort(&arr, index: index+1)
}

func findAndInsert<T: Comparable>(inout arr: [T], toIndex: Int, compared: Int) {

if toIndex == 0 || compared == toIndex {
return
}

if arr[toIndex] < arr[compared] {
let temp = arr[toIndex]
arr.removeAtIndex(toIndex)
arr.insert(temp, atIndex: compared)
return
}

findAndInsert(&arr, toIndex: toIndex, compared: compared+1)
}

var array = [8,2,33,1,13,66]
recursiveInsertionSort(&array, index: 0)

//Find next min and swap with current index
func recursiveSelectionSort<T: Comparable>(inout arr: [T], index: Int) {

if index == arr.count {return}

let indexOfMin = indexOfNextMin(arr, from: index, lastMin: index)
swap(&arr[index], &arr[indexOfMin])
recursiveInsertionSort(&arr, index: index+1)

}

func indexOfNextMin<T: Comparable>(arr: [T], from: Int, var lastMin: Int) -> Int {

if from == arr.count {return lastMin}

if arr[from] < arr[lastMin] {
lastMin = from
}
return indexOfNextMin(arr, from: from+1, lastMin: lastMin)
}

var array2 = [2,41,13,63,12,6,1]
recursiveSelectionSort(&array2, index: 0)


func mult(a: Int,_ b: Int) -> Int{

if a == 0 || b == 0{
return 0
}

if b == 1 {
return a
}

if b < 0 {
return 0 - (a + mult(a, (0 - b) - 1))
}
else{
return a + mult(a, b - 1)
}
}

mult(-3, -6)
121 changes: 118 additions & 3 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,129 @@
//Answer the questions in the homework below
//https://docs.google.com/document/d/1KlK3PmRMybmHS5db_AP11uHXIcbIeM5vHuuAZOV3xME/edit#

//1)
//1)bubble/Selection/insertion: O(n^2)
// merge/quicksort: O(nlogn)

//2)
//2) less memory usage

//3)
func quicksort<T: Comparable>(inout arr: [T], left: Int, right: Int){

if left >= right {
return
}

let split = partition(&arr, left: left, right: right)
quicksort(&arr, left: left, right: split-1)
quicksort(&arr, left: split+1, right: right)

}

func partition<T: Comparable>(inout arr: [T],var left: Int,var right: Int) -> Int {

var isLeft = true

while left != right {
if isLeft{
if arr[right] < arr[left] {
swap(&arr[right], &arr[left])
isLeft = false
}else{
right--
}
}
else{
if arr[left] > arr[right]{
swap(&arr[left], &arr[right])
isLeft = true
}else{
left++
}
}

}

return left
}

var arr = [3,67,1,13,91,54,34,6,3,11]
quicksort(&arr, left: 0, right: arr.count-1)


//4)


//5)

//6)

//6)

struct Stack<T> {
var arrayStack = [T]()

mutating func push(item: T) {
arrayStack.append(item)
}

mutating func pop() -> T? {
if !isEmpty() {
return arrayStack.removeLast()
}

return nil
}

func isEmpty() -> Bool {
return arrayStack.count == 0
}

func peek() -> T? {
if !isEmpty() {
return arrayStack.last
}
return nil
}

}

let matches = ["(":")", "{":"}", "[":"]"]

func isCompletelyMatched(str: String) -> Bool {

var strStack = Stack<String>()

var index = 0

while index < str.characters.count {

let currentPar = String(Array(str.characters)[index])

if strStack.isEmpty() {

if matches[currentPar] == nil {
return false
}
else{
strStack.push(currentPar)
}
}
else {
if matches[strStack.peek()!] == currentPar {
strStack.pop()
}
else if matches[currentPar] != nil {
strStack.push(currentPar)
}
else{
return false
}
}

index++
}

return strStack.isEmpty()
}

isCompletelyMatched("{[()]}[][()]")
isCompletelyMatched("{))(}")
Loading