Skip to content

HW #6

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 24 commits into
base: master
Choose a base branch
from
Open

HW #6

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
158 changes: 155 additions & 3 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,168 @@ Work on your solutions here.

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

1)
1) 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

//check if duplicate exists in row
//check if duplicate exists in column
//check if duplicate exists in box (3X3)

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

for (i = 1; i < 10; i++){
for (j = 0; j < 10; j++)

if duplicates exist;

preclude those numbers

return all numbers not precluded

func getValidNumbers(sudokuBoard:[[Int?]], row:Int, col:Int) -> [Int] {
return [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)

}

2)
let sqRow = row/3
}

//new function
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)
}
}

3)
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)
}
}
}

<<<<<<< HEAD
// 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)

//start off by empty matrix
func roatate(matrix: [[Int]]) -> [[Int]] {
var result: [[Int]] = []

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) check if A is greater then B

check if B is greater than C

check if C is greater than D

repeat for number of elements

//non-optimized

//optimized - split array in half, then merge


func sort(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
}

return []

=======
>>>>>>> e4827c92554b75e4dccf0e98751ea7fef5cbde66
*/
81 changes: 74 additions & 7 deletions HWFrom1-24(Recursion).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,87 @@ Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3l


*/

//hw


//Question 1

/*

fibonacciIterative(let n: Int)
{
int i = 0, j = 1, k, t;
for (k = 1; k <= n; ++k)
{
t = i + j;
i = j;
j = t;
}
return j;
}



/*

//Question 2

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 0:
stepUp()
case -1:
stepUp()
stepUp()
default:
stepUp()
if tryStep() == 1

}

//Question 3

func findFile(name: String, atPath: String) -> String {
let fileManager = NSFileManager.defaultManager()
let contents =
try! fileManager.contentsOfDirectoryAtPath(atPath)
for fileOrDir in contents {
var isDir = ObjCBool(false);
let fullPath = atPath + "/" + fileOrDir
let exists = fileManager.fileExistsAtPath(fullPath, isDirectory: &isDir)
if exists && Bool(isDir) {
// YOUR CODE HERE
let result = findFile(name, atPath, fullPath)
if result != "NOT FOUND"{
return result
}
print("DIR: " + fileOrDir)
} else if exists {
// YOUR CODE HERE
if fileOrDir == nmae{
return fullPath
}
print("FILE: " + fileOrDir)
} else {
print("NEITHER: " + fileOrDir)
}
}
return "NOT FOUND"
}

print(findFile("awesome-idea.txt", atPath: "/Users/umahmud"))




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


//Insert code here:

/*

import Foundation
func selectionSort var number:[Int], index:Int) -> [Int]{

{
while (size < n || size > k);

array= array(size,100);

arrayComponent newArray = new array(array);

}

if (array[n] < array[min]) {

min = i;

}

if(min != k) {

swap(array[n], array[min]);

}
}





*/
46 changes: 41 additions & 5 deletions HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,48 @@

//1)

//2)
// O(n^2), O(n^2), O(n^2), O(n log(n)), O(n log(n))

//3)
//2) It takes less memory/resources to sort in place

//4)
//3)
/*
quicksort(array ,low ,high) {
if (low < high) {
q <- partition(array, low ,high)
quicksort(array ,low, high)
quicksort(array ,low+1, high)
}
}
partition(array, low, high)
x <- array[low]
i <- low-1
j <- high+1
while (true) {
repeat
j <- j-1
until (array[j] <= x)
repeat
i <- i+1
until (array[i] >= x)
if (i array[j]
else
return(j)
}
}
*/
//4) func generateRandomArray (int: Int) -> [Int]{
// Int(arc4random_uniform(UInt32(10000)))
//}

//5)
//5) Mergesort breaks array to be sorted into new arrays. Mergesort advantage is that it has better worst-case time complexity than quicksort. Mergesort takes more space.
// Quicksort is in place sorting.

//6)
//6)func isBalanced(paren: [String]) -> Bool {
// let arr1 = [string]
// let arr2 = []
// arr1.pop
// arr2.push
// if arr2 != empty
// arr not balanced
//}
Loading