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

Mesfin's HW #10

Open
wants to merge 20 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
210 changes: 206 additions & 4 deletions HWFrom1-17-16(Lists and Sorts).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,216 @@ Link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZ

1)



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

var notValidNumsArr = [Int]()
//loop thru cols picking up non-empty values, row is held constant
for j in 0..<sudokuBoard.count {

if ( sudokuBoard[row][j] != 0)//since 0 represents empty space
{
notValidNumsArr.append(sudokuBoard[row][j])
}
}
//loop thru rows picking up non-empty values, col is held constant
for i in 0..<sudokuBoard.count {

if ( sudokuBoard[i][col] != 0)//since 0 represents empty space
{
notValidNumsArr.append(sudokuBoard[i][col])

}
}

let squareRowCoorArr = getSqaureRowCoordinates(sudokuBoard, row: row)
let squareColCoorArr = getSquareColumnRowCoordinates(sudokuBoard, col: col)

let rowStartIndex = squareRowCoorArr[0]
let rowEndIndex = squareRowCoorArr[1]


let colStartIndex = squareColCoorArr[0]
let colEndIndex = squareColCoorArr[1]


for i in rowStartIndex...rowEndIndex
{
for j in colStartIndex...colEndIndex
{
if sudokuBoard[i][j] != 0 {
notValidNumsArr.append(sudokuBoard[i][j])
}
}
}

let arrToSet = Set(notValidNumsArr)
var numbersSet = getNumbersSet()
numbersSet.subtractInPlace(arrToSet)


return Array(numbersSet)
}

//Takes in a row and outputs the starting and ending coordinates
//for the rows that make up the sqaure containing the given row
func getSqaureRowCoordinates(sudokuBoard: [[Int]],row: Int) -> [Int] {
var coordinatesArr = [Int]()
if row <= (sudokuBoard.count/3) - 1 {
coordinatesArr.append(0)
coordinatesArr.append( (sudokuBoard.count/3) - 1 )
}
else if row >= sudokuBoard.count/3 && row <= (sudokuBoard.count/3) + 2 {
coordinatesArr.append(sudokuBoard.count/3)
coordinatesArr.append((sudokuBoard.count/3) + 2 )
}
else {
coordinatesArr.append((sudokuBoard.count/3) + 3)
coordinatesArr.append(sudokuBoard.count - 1)
}
return coordinatesArr
}

func getNumbersSet() -> Set<Int>
{
var numbers = Set<Int>()

numbers.insert(1)
numbers.insert(2)
numbers.insert(3)
numbers.insert(4)
numbers.insert(5)
numbers.insert(6)
numbers.insert(7)
numbers.insert(8)
numbers.insert(9)

return numbers
}

//Takes in a column and outputs the starting and ending coordinates
//for the columns that make up the sqaure containing the given column
func getSquareColumnRowCoordinates(sudokuBoard: [[Int]],col: Int) -> [Int] {
var coordinatesArr = [Int]()
if col <= (sudokuBoard.count/3) - 1 {
coordinatesArr.append(0)
coordinatesArr.append( (sudokuBoard.count/3) - 1 )
}
else if col >= sudokuBoard.count/3 && col <= (sudokuBoard.count/3) + 2 {
coordinatesArr.append(sudokuBoard.count/3)
coordinatesArr.append((sudokuBoard.count/3) + 2 )
}
else {
coordinatesArr.append((sudokuBoard.count/3) + 3)
coordinatesArr.append(sudokuBoard.count - 1)
}
return coordinatesArr
}

var 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]]

getValidNumbers(sudokuBoard, row: 5, col: 5) -> [2, 4, 9, 5]
getValidNumbers(sudokuBoard, row: 4, col: 4) -> [4, 5, 6, 3, 1, 8]
getValidNumbers(sudokuBoard, row: 8, col: 7) -> [2, 5]


2)


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

var rotatedMatrix = [[Int]](count: matrix.count, repeatedValue: matrix[0])


for j in 0..<matrix.count
{
for i in 0..<matrix.count
{
let row = matrix.count - i - 1
rotatedMatrix[j][i] = matrix[row][j]
}
}
return rotatedMatrix
}


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

print( (rotateMatrixBy90(input))) -> [[3, 9, 5, 1], [4, 0, 6, 2], [5, 1, 7, 3], [6, 2, 8, 4]]
print( (rotateMatrixBy90(sudokuBoard))) -> [[0, 2, 1, 0, 0, 0, 0, 9, 5], [8, 0, 9, 1, 0, 7, 0, 0, 0], [7, 3, 0, 0, 2, 0, 0, 0, 8], [1, 0, 3, 0, 0, 0, 9, 6, 0], [9, 0, 0, 0, 0, 0, 0, 0, 7], [0, 7, 6, 0, 0, 0, 8, 0, 3], [3, 0, 0, 0, 9, 0, 0, 4, 1], [0, 0, 0, 8, 0, 6, 3, 0, 9], [4, 9, 0, 0, 0, 0, 5, 8, 0]]



3)


func min(first : Int,second: Int)->Int {
if first <= second {
return first
}
else {
return second
}
}

func max(first : Int,second: Int)->Int {
if first > second {
return first
}
else {
return second
}
}

//Optimal Algorithm to sort four elements A,B,C,D
func sort(unsorted: [Int]) ->[Int] {

let mid = unsorted.count/2
var sorted = [Int]()

let leftMin = min(unsorted[0],unsorted[mid-1])
let leftMax = max(unsorted[0],unsorted[mid-1])

let rightMin = min(unsorted[mid],unsorted[unsorted.count-1])
let rightMax = max(unsorted[mid],unsorted[unsorted.count-1])

let absoulteMin = min(leftMin, rightMin)
let absoulteMax = max(leftMax,rightMax)

if(leftMin == absoulteMin)
{
sorted.append(leftMin)
sorted.append(rightMin)
}
else {
sorted.append(rightMin)
sorted.append(leftMin)
}
if(leftMax == absoulteMax){
sorted.insert(rightMax, atIndex: mid)
sorted.append(leftMax)
}
else {
sorted.insert(leftMax, atIndex: mid)
sorted.append(rightMax)
}
return sorted
}
sort([3,1,4,0]) -> [0,1,3,4]
sort([2,2,2,2]) -> [2,2,2,2]
sort([1,6,0,2]) -> [0,1,2,6]

*/
55 changes: 53 additions & 2 deletions HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
//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.

// Time Complexity Space Complexity
//Bubble Sort O(n^2) O(1)
//Insertion Sort O(n^2) O
//Merge Sort O(nlogn) O(n)
//Selection Sort O(n^2) O(1)
//Quick Sort O(nlogn), worst case O(n^2) O(1)

//2)

Expand All @@ -13,4 +20,48 @@

//5)

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

//func isBalanced(paren: [String]) -> Bool {
// let items :[String] = []
// var stack : Stack<String> = Stack<String>(items: items)
// if stack.size() == 0 {
// if paren[0] == ")" || paren[0] == "]" {
// return false
// }
// }
// for str in paren {
// if str == "(" || str == "[" {
// stack.push(str)
// }
// else if str == ")" {
// if stack.peek() == "(" {
// stack.pop()
// continue
// }
// else{return false}
// }
// else if str == "]" {
// if stack.peek() == "[" {
// stack.pop()
// continue
// }
// else {return false}
// }
// }
// return stack.size() == 0
//}
//let inputArr = ["(",")"]
//isBalanced(inputArr)
//
//let inputArr2 = ["(", "(","]","(","[",")","]"]
//isBalanced(inputArr2)
//
//let inputArr3 = ["(",")","[","]","(",")", "(","[","]",")","(",")","[","]"]
//
//isBalanced(inputArr3)
//
//let inputArr4 = ["[","(","(","(",")",")",")","]","]"]
//isBalanced(inputArr4)
62 changes: 61 additions & 1 deletion HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,64 @@



//3)
//3)

let blacklist: Set<String> = ["crapple", "fandroid", "m$"]

func moderate(message: String) -> Bool {

let words = message.componentsSeparatedByString(" ")

for word in words {
if blacklist.contains(word.lowercaseString) {
return false
}
}
return true
}

moderate("I would never use a crApple product!")


/************************************************/

//Q3

protocol PhoneBookProtocol {
mutating func addPerson(name: String, phoneNumber: String)
mutating func removePerson(name: String)
mutating func importFrom(oldPhonebook: [(String, String)])
func findPerson(name: String) -> String? // Return phone #
}

class PhoneBook: PhoneBookProtocol {
var storage: [String : String] = [:] // @{}
//var storage = Dictionary<String, String>()

func addPerson(name: String, phoneNumber: String) {
storage[name] = phoneNumber
}

func removePerson(name: String) {
storage.removeValueForKey(name)
}

func findPerson(name: String) -> String? {
return storage[name]
}

func importFrom(oldPhonebook: [(String, String)]) {
for entry in oldPhonebook {
addPerson(entry.0, phoneNumber: entry.1)
}
}
}


let oldData = [("Caleb", "501-555-1234"), ("Mike", "212-555-4321"), ("Jenny", "345-867-5309")]

let phoneBook = PhoneBook()
phoneBook.importFrom(oldData)

phoneBook.findPerson("Jenny")

Loading