Skip to content

homework discrete/logic #23

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 16 commits into
base: master
Choose a base branch
from
Open
Binary file added .DS_Store
Binary file not shown.
34 changes: 20 additions & 14 deletions HWFrom1-17-16(Lists and Sorts).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
//: Playground - noun: a place where people can play

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)


//Work on your solutions here.

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

//1) Write an iterative (not recursive) fibonacci function that calculates the nth fibonacci number. How does its performance compare with the non-memoized recursive one (see Appendix A below), based on the number of iterations that Swift says it takes in the right margin?

//recursive - NO LOOPS
func fib(n: Int) -> Int {
print("X")
if (n == 0 || n == 1) {
return 1
}
return fib(n - 1) + fib(n - 2)
}

3)
//iterative - W/ Loops

func fibIt(n:Int) -> Int{
for fib in 1..<n{
let sum = (n-1)+(n-2)
}
return sum
}

print(fibIt(4))

*/
fibIt(4)
26 changes: 19 additions & 7 deletions HWFrom1-24(Recursion).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
/*


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


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

*/
//1) Write an iterative (not recursive) fibonacci function that calculates the nth fibonacci number. How does its performance compare with the non-memoized recursive one (see Appendix A below), based on the number of iterations that Swift says it takes in the right margin?

//recursive - NO LOOPS
func fib(n: Int) -> Int {
print("X")
if (n == 0 || n == 1) {
return 1
}
return fib(n - 1) + fib(n - 2)
}

//iterative - W/ Loops

//Question 1
func fibIt(n:Int) -> Int{
for _ in 1..<n{
var sum = (n-1)+(n-2)
}
return sum
}



Expand All @@ -22,4 +34,4 @@ Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3l



//Question 3
//Question 3
67 changes: 67 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,70 @@


//Insert code here:

//selection sort
// for i..<n-1{
// select the smallest thing among A[i]...A[n] and
// swap it with A[i] or pull it out of array and put it first
//}
func selectionSort(inout values: [Int]!) {
var lowestNum = values[0]
if values[1] < lowestNum{
lowestNum == values[1]
values[1] == values[0]
}
}

let arrayNums = [1,3,5,7,9]

selectionSort(&array(arrayNums))








//func printAllElements(values: [Int]) { //takes the array and just prints each element
// for value in values {
// print(value)
// }
//}
//
//func printAllElementsRecursive(values: [Int]) { //
// printElementsHelper(values, index: 0)
//}
//
//func printElementsHelper(values: [Int], index: Int) {
// if index < values.count {
// print(values[index])
// printElementsHelper(values, index: index + 1)
// }
//}
//
//func setValue(inout array: [Int], value: Int, atIndex index: Int) {
// array[index] = value
//}
//
//var values = [10, 20, 30]
//printAllElements(values)
//
//setValue(&values, value: 100, atIndex: 1)
//values
//
//func reverse(inout array: [Int]) {
// for i in 0..<array.count / 2 {
// swap(&array[i], &array[array.count - i - 1])
// }
//}
//reverse(&values)
//values
//
//
//printAllElements(arrayNums)





18 changes: 14 additions & 4 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,24 @@
//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.
//bubble sort: O(n^2)
//insertion sort: O(n^2)
//selection sort: O(n^2)
//merge sort: O(n log n)
//quicksort: O(n log n)

//2)
//2) What is the advantage of partitioning quicksort in place? allows for divide and conquer approach

//3)

//4)

//5)
//5) In Mergesort, you create multiple arrays while in quicksort you don't need to.

//6)
func isBal(str: String) -> Bool {


}

//6)
41 changes: 37 additions & 4 deletions HWFrom1-31-16(Sets and HashMaps).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import UIKit

//: https://docs.google.com/document/d/1T7tYRqpDPWoxarfmXqfRCHB-YqvA8-Qx_mEyy5smtfc


Expand All @@ -14,7 +16,38 @@

//2)




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

func moderate(message: String)->Bool {
let words = (message as NSString).componentsSeparatedByString(" ")

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

moderate("I love apple")



//3)
[(“Caleb”, “501-555-1234”), (“Mike”, “212-555-4321”), (“Jenny”, “345-867-5309”)]

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

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

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 #
}
75 changes: 73 additions & 2 deletions HWFrom2-05-16(Trees).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,81 @@
//https://docs.google.com/document/d/1te7mLS06MEYwETFSbVBqMrIzJ43GTEo5uuCiWdB0fyE/edit?usp=drivesdk


//1
//1 Build and return the binary tree representation of the expression. The following characters should be treated as operators: +-*/

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

extension Node {
func printInOrder(){
left?.printInOrder()
print(self)
right?.printInOrder()
}
}
extension Node {
func pop(var stack:[Node<Character>]){
stack.removeFirst()
}
}
extension Node {
func push(var stack:[Node<Character>], element:Node<Character>){
stack.insert(element, atIndex: 0)
}
}

func parseExpression(input: String) -> [Node<Character>?] {

let operators: Set<Character> = ["+", "-", "*", "/"]
var stack: [Node<Character>] = []

for character in input.characters {

let node = Node (value: character)

if stack.isEmpty{
stack.first?.push(stack, element: node)
} else {
if !operators.contains(character){
stack.append(node)
} else {
stack.first?.pop(stack)
stack.append(node)
}

}
}
stack.first?.printInOrder()
return stack
}


//check
let input = "ab+cde+**"

parseExpression(input)





//Bonus1


//Bonus2
//Bonus2
38 changes: 35 additions & 3 deletions HWfrom1-09-16(SwiftIntro).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,45 @@ Use the link here to get the questions. Then code your solutions below. If it
https://docs.google.com/document/d/1DQ2aCJ_yUZtazzCfb0PaS81bg61V2ZOSxpABh981xSo/edit


1)
1)Given an integer N, there is a list of size N-1 that is missing one number from 1 - N(inclusive). Find that number.

func findMissingNumber (N:Int


2)Given a list of size N containing numbers 1 - N (inclusive). return true if there are duplicates, false if not

func hasDuplicates(arr:[Int]) -> Bool{
let set = Set(arr)

if set.count == arr.count{
return false //no duplicates
} else{
return true //there are duplicates
}
}

2)

3)
func smallestValue(arr1:[Int], arr2:[Int2]){
var smallestNum = 0;
for i in 0..arr1.count{
if i
}

}

4)func isPalindrome(word: String) -> Bool {

let wordLength = word.characters.count

for i in 0...wordLength/2{
if(Array(word.characters)[i] != Array(word.characters)[wordLength - i - 1]){
return false
}
}
return true
}

4)


*/
Loading