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

As much as I could get out for now #5

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
115 changes: 115 additions & 0 deletions HWFrom1-17-16(Lists and Sorts).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//: Playground - noun: a place where people can play

import UIKit

//var str = "Hello, playground"
//
//41Array + Lists Homework
//
//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
//
//func getValidNumbers(sudokuBoard:[[Int?]], row:Int, col:Int) -> [Int] {
// return [Int]()
//}
//
//Sample input: ,4,4
//
//sample output: [1,3,4,5,6,8]
//
//
//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] ]
//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?



/*
Work on your solutions here.
Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit#heading=h.uopysoy45zmw
1)
//So this is my plan
// Step 1: Print out the the suduko board as as squar with columns
// Step 2: Make an array *possibleOutcomes
// Step 3: Remove the numbers that appear on the x and y axises off possibleOutcomes as they appear
-The numbers remainin gwould be the possible outcome for that coordiante

2)
3)
*/

//i = r
//j = c

/////////////////////////////////Problem 1
let sudokuBoard: [[Int]] = [
[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]]

let possibleOutcomes: Set<Int> = [1,2,3,4,5,6,7,8,9]


for var i = 0; i < sudokuBoard.count; i++ {
var line = ""
for var j = 0; j < sudokuBoard[i].count; j++ {
line += String(sudokuBoard[i][j])
line += " "



}
//print(sudokuBoard [4][4])
print(line)

}
//print(sudokuBoard [4][4])
// print(sudokuBoard)
//print("sudokuBoard equals \(sudokuBoard) at iteration \(i)")

func getValidNumbers(sudokuBoard:[[Int?]], i:Int, j:Int) -> [Int] {
return [Int]()
}
/////////////////////////////////Problem 2

let matrix: [[Int]] =
[[1,2,3,4],
[5,6,7,8],
[9,0,1,2],
[3,4,5,6]]


func rotateMatrixNinetyDegree(matrix: [[Int]]) -> [[Int]] {
var rotatedMatrix = [[Int]]()
for i in 0..<matrix[0].count {
var rowMatrix = [Int]()
for j in 0..<matrix.count {
rowMatrix.insert(matrix[j][i], atIndex: 0)
}
rotatedMatrix.append(rowMatrix)
}

return rotatedMatrix
}

/////////////////////////////////Problem 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
65 changes: 65 additions & 0 deletions HWFrom1-24(Recursion).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

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



//Question 1

//Appendix A: Recursive Fibonacci from class (non-memoized)
//func fib(n: Int) -> Int {
// print("X")
// if (n == 0 || n == 1) {
// return 1
// }
// return fib(n - 1) + fib(n - 2)
//}
//fib(5)

//Iterative
func fib(n: Int) -> Int {
var a = 1
var b = 1
for i in 0..<n {
let
}

return fib(n - 1) + fib(n - 2)
}



//Question 2
var stepNum = 0
func tryStep() -> Bool {
let success = Int(arc4random_uniform(2)) > 0
if (success) {
stepNum++
print("Yay! \(stepNum)")
} else {
stepNum--;
print("Ow! \(stepNum)")
}
return success
}

func stepUp() {
if tryStep() {
// We’re done!
return
}
// Now we’re two steps below where we want to be :-(
stepUp()
stepUp()
}




//Question 3
4 changes: 4 additions & 0 deletions HWFrom1-24(Recursion).playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
6 changes: 6 additions & 0 deletions HWFrom1-24(Recursion).playground/timeline.xctimeline
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
33 changes: 33 additions & 0 deletions HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"
//Answer the questions in the homework below
//https://docs.google.com/document/d/1KlK3PmRMybmHS5db_AP11uHXIcbIeM5vHuuAZOV3xME/edit#

//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.

//bubble sort = O(n^2)
//insertion sort = O(n^2)
//selection sort = O(n^2)
//mergesort = O(nlog(n))
//quicksort = O(nlog(n))

//2) What is the advantage of partitioning quicksort in place?
//Answer: You dont have to calculate the median?


//3)Without looking, implement quicksort.

//4)Write a function to generate an array of random numbers bounded between 1..<10,000 of size 10,000.

//5)Describe the algorithmic difference between mergesort and quicksort. Where does the sorting happen? As the recursive calls are being pushed onto the stack or as they are being popped off?

//6)Given an array of strings containing “[“,”]”,”{“,”}”,”(“,”)”. Output whether or not the parentheses are balanced.
//Good examples: () [] () ([]()[])
//Bad examples: ( ( ] ([)]
//
//func isBalanced(paren: [String]) -> Bool {
//
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
44 changes: 44 additions & 0 deletions HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"
//: https://docs.google.com/document/d/1T7tYRqpDPWoxarfmXqfRCHB-YqvA8-Qx_mEyy5smtfc


//Homework
//1) Write good hash functions for the following data types. Justify your choices and how they avoid collisions.
//
//
// Int
//
//
// struct Point: Hashable {
// let x, y: Int
// }
//
// Array<Int>
//
//
//2) You moderate a popular mobile device discussion forum. You want to cut down on the vitriol and make your work easier, so you decide to implement a blacklist based system to automatically reject or approve posts. For example:
//
//moderate("I would never use a crApple product!") // false (reject)
//moderate("I wish all these FANDROIDS would just shut up!") // false
//moderate("M$ is the worst, Linux rules!") // false
//moderate("Can’t we all just get along?") // true (approve)
//
//Write moderate(message: String) -> Bool, using a built-in Swift Set to manage your blacklist. Make your method case insensitive; it should block the word no matter what combination of upper and lowercase letters is used.
//
//
//3) Your company makes a phonebook app, and your users have been complaining about how long it takes to look people’s numbers up. You decide to upgrade your archaic array-based system to a sleek, modern hash map.
//
//Write a phonebook class that uses either our HashMap from class or the built in Swift dictionary (your choice). It should implement the protocol below. It needs to support importing from the old array based format which used an array of tuples, like [(“Caleb”, “501-555-1234”), (“Mike”, “212-555-4321”), (“Jenny”, “345-867-5309”)]
//
//
//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 #
//}
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
65 changes: 65 additions & 0 deletions HWFrom2-05-16(Trees).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"


//https://docs.google.com/document/d/1te7mLS06MEYwETFSbVBqMrIzJ43GTEo5uuCiWdB0fyE/edit?usp=drivesdk


//1


//Bonus1


//Bonus2

//Homework
//
//Using the Swift binary tree implementation below (the Node class), implement a function that takes in a string containing a simple postfix mathematical expression, and returns a binary tree representing that expression using the process described here.
//
//Required: Build and return the binary tree representation of the expression. The following characters should be treated as operators: +-*/
//
//Bonus 1: Print the inorder traversal of the binary tree.
//Bonus 2: Evaluate the expression (substituting integers for the letters in the expression).
//
//Tips:
//You may use additional data structures—as suggested in the link above, you will at least want to use a stack. This can be done using the Array type in Swift.
//Test your tree by checking to see if the string returned by postorderDescription is equal to the input string.
//
//Sample input:
//"ab+cde+**"
//
//Template for the function you should implement:
//func parseExpression(input: String) -> Node<Character>? {
// // Your implementation here!
// let operators: Set<Character> = ["+", "-", "*", "/"]
// var stack: [Node<Character>] = []
// for character in input.characters {
// // Do something for each character
// }
// return nil
//}
//
//Binary tree implementation:
//class Node<T> {
// let value: T
// var left: Node?
// var right: Node?
// init(value: T) {
// self.value = value
// }
//}
//
//extension Node: CustomStringConvertible {
// var description: String {
// return "\(value)"
// }
// var postorderDescription: String {
// let lt = left?.postorderDescription ?? ""
// let rt = right?.postorderDescription ?? ""
// return lt + rt + description
// }
//}
Loading