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

In progress #13

Open
wants to merge 9 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
Binary file added .DS_Store
Binary file not shown.
24 changes: 20 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,10 +13,26 @@ Link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3lQZQiFUOxZ

1)



2)

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

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
}


3)
Expand Down
48 changes: 39 additions & 9 deletions HWFrom1-24(Recursion).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
/*


Expand All @@ -11,15 +12,44 @@ Homework link: https://docs.google.com/document/d/1INvOynuggw69yLRNg3y-TPwBiYb3l

//Question 1





func fib(n: Int) -> Int {

var a = 1
var b = 1

for _ in 0..<n {
let t = a
a = b
b = t + b
}
return b
}

//Question 2





//Question 3
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 -1:
stepUp()
stepUp()
default:
stepUp()
}
}

//Question 3
216 changes: 215 additions & 1 deletion HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,230 @@

import Foundation
//: Playground - noun: a place where people can play

//Answer the questions in the homework below
//https://docs.google.com/document/d/1KlK3PmRMybmHS5db_AP11uHXIcbIeM5vHuuAZOV3xME/edit#

//1)
/*
bubble sort Time: O(n^2), Space: O(1)
insertion sort Time: O(n^2), Space: O(1)
selction sort Time: O(n^2), Space: O(1)
merge sort Time: O(n log n), Space: O(n)
quick sort Time: O(n log n), Space: O(log n)
*/

//2)

// Smaller space complexity

//3)

func median(inout array: [Int], first: Int, last: Int) {

if last - first > 4 {

var medianIndex: Int
var candidates: [Int] = [array.first!, array[(last+first) / 2], array.last!]

if (candidates[0] >= candidates[1] && candidates[0] <= candidates[2]) ||
(candidates[0] >= candidates[2] && candidates[0] <= candidates[1]) {

medianIndex = first

} else if (candidates[1] >= candidates[2] && candidates[1] <= candidates[0]) ||
(candidates[1] >= candidates[0] && candidates[1] <= candidates[2]) {

medianIndex = array.count / 2

} else {

medianIndex = last
}

if medianIndex != first {

swap(&array[first], &array[medianIndex])
}
}
}

func partition(inout array: [Int], first: Int, last: Int) -> Int {

let pivotIndex = first
var rightIndex = last
var leftIndex = pivotIndex + 1

while leftIndex <= rightIndex {

if array[leftIndex] > array[pivotIndex] {

if array[rightIndex] < array[pivotIndex] {

swap(&array[rightIndex], &array[leftIndex])

} else {

rightIndex--
}

} else {

leftIndex++
}
}

if pivotIndex != rightIndex {

swap(&array[pivotIndex], &array[rightIndex])
}

return rightIndex
}

func quickSort(inout array: [Int], first: Int, last: Int) {

if last - first <= 0 {
return
}
median(&array, first: first, last: last)
let splitPoint = partition(&array, first: first, last: last)
quickSort(&array, first: 0, last: splitPoint - 1)
quickSort(&array, first: splitPoint + 1, last: last)
}


//4)

func generateArray(size: Int, valueUpperBound: Int) -> [Int] {

var array = Array<Int>(count: size, repeatedValue: 0)

for i in 0..<array.count {

array[i] = Int(arc4random_uniform(UInt32(valueUpperBound)))
}
return array
}



//5)
/*
Quick sort uses a pivot and sorts the two parts with the pivot as a reference point. Merge sort recursively splits the array by dividing them ito halves and merging them back together
*/
//6)

struct Stack<T> {

var items: [T]

init() {
items = [T]()
}


mutating func push(element: T) {

items.append(element)
}

mutating func pop() -> T? {

if items.count > 0 {

return items.removeLast()
}
return nil
}

func peek() -> T? {

return items.last
}

func size() -> Int {

return items.count
}
}

struct Bracket {

var orientation: String
var name: String

init(symbol: String) {

switch symbol {
case "(":
orientation = "open"
name = "parenthesis"
case ")":
orientation = "close"
name = "parenthesis"
case "[":
orientation = "open"
name = "bracket"
case "]":
orientation = "close"
name = "bracket"
case "{":
orientation = "open"
name = "brace"
case "}":
orientation = "close"
name = "brace"
default:
orientation = "invalid"
name = "invalid"
}
}
}


func isBalanced(array: [String]) -> Bool {
if array.count % 2 != 0 { return false }

var stack = Stack<Bracket>()

for i in 0..<array.count {

let bracket = Bracket(symbol: array[i])
print(bracket)

if i == 0 {

if bracket.orientation == "close" { return false }

stack.push(bracket)

} else {

if bracket.orientation == "close" &&
bracket.name != stack.peek()?.name {

return false

} else if bracket.orientation == "close" &&
bracket.name == stack.peek()?.name {

stack.pop()

} else {

stack.push(bracket)
}
}
}

if stack.items.count == 0 { return true }

return false
}

//6)
print(isBalanced(["(", ")", "[", "]", "(", ")", "(", "[", "]", "(", ")", "[", "]", ")"]))
print(isBalanced(["(", "(", "]", "(", "[", ")", "]"]))
print(isBalanced(["]", "(", "]", "(", "[", ")", "]"]))
print(isBalanced(["(", ")", ")", ")", "(", ")", "(", "[", "]", "(", ")", "[", "]", ")"]))
print(isBalanced(["[", "(", "[", "(", "[", "[", "["]))
66 changes: 61 additions & 5 deletions HWfrom1-09-16(SwiftIntro).playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,70 @@ 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.

2)
func findMissingNumber(input: Int, list: [Int]) -> Int {

var sumArr = 0
var sumList = 0

for i in 1...input {

sumArr += i
}

for i in list {

sumList += i
}

return sumArr - sumList

}

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

4)
func isDuplicate(numArray: [Int]) -> Bool {
let set = Set(numArray)

if set.count == numArray.count {
return false
}
return true
}

let arrTest = [2, 3, 4, 5, 6, 7]
isDuplicate(arrTest)


*/
//3) Given two lists, find the smallest value that exists in both lists.
//L1 = [1,2,5,9]
//L2 = [9, 20 , 5]
var firstList = [1, 2, 5, 9]
var secondList = [9, 20, 5]

func smallestValue(theFirstList: [Int], theSecondList: [Int]) -> Int? {
let list1 = Set(theFirstList)
let list2 = Set(theSecondList)

return list1.intersect(list2).minElement()

}

smallestValue(firstList, theSecondList: secondList)


//4) Check to see if an integer is a palindrome don’t use casting

func Palindrome(var num: Int) -> Bool {
let originalNum = num
var finalNum = 0
while(num > 0) {
finalNum *= 10
finalNum += num % 10
num /= 10
}
return finalNum == originalNum
}
Loading