Skip to content

Henna's HW #11

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 22 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.
102 changes: 102 additions & 0 deletions HW(Recursion)_final.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//: Playground - noun: a place where people can play

import UIKit
import Foundation


/*
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?

When n is 4, the iterative function, although running with O(n) complexity, is run 4 times while the recursive is run 9 times.
*/

//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(4)

// HW ANSWER

func fibIter(n: Int) -> Int {
//0, 1, 1, 2, 3, 5, 8, 13, 21
if n == 0{
return 0;
}

//if the number isn't 0, proceed

var x = 0, y = 1, z = 1;

for _ in 0..<n {
x = y;
y = z;
z = x + y;
}
return x;
}
(0...5).map{ i in fibIter(i) }




/*

The engineers have been hard at work on the clumsy robot project, and have released a new API with a new tryStep method (see Appendix B). Now it returns an Int, which is -1 if the robot fell down a step, 0 if the robot stayed on the same step, or 1 if the robot went to the next step. Write a new stepUp method using this new tryStep method that works the same as before.
*/

//Appendix B: New and improved clumsy robot


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
}

// HW ANSWER

func stepUp() {

switch tryStep() {
case 1:
return
case 0:
stepUp()
case -1:
stepUp()
stepUp()
default:
stepUp()

}
}


/*

Using the code in Appendix C as a starting point, create a Swift command line project to find files on your computer by name. Your solution should use recursion. Your method should return “NOT FOUND” if it couldn’t find the file, otherwise it should return the full path to that file.

You can’t use a playground for this because they don’t have filesystem access for some reason. Instead, in XCode, go to File > New > Project, select “Application” under “OS X” in the sidebar, then select “Command Line Tool”; on the next screen, choose “Swift” as the language.

*/









4 changes: 4 additions & 0 deletions HW(Recursion)_final.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.

11 changes: 11 additions & 0 deletions HW(Recursion)_final.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=10&amp;CharacterRangeLoc=999&amp;EndingColumnNumber=29&amp;EndingLineNumber=42&amp;StartingColumnNumber=19&amp;StartingLineNumber=42&amp;Timestamp=475719221.008075"
selectedRepresentationIndex = "2"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
134 changes: 134 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,134 @@
//: Playground - noun: a place where people can play

import UIKit

/*
Link: https://docs.google.com/document/d/1XioaEqk6VqUPA-ccQhkqP3eAoDthxYyOM9vSPB7fDkg/edit#heading=h.uopysoy45zmw

1) Get possible numbers in sudoku board

*/

func setValuesToFalse(inout arr:[Bool]) {
for i in 0..<arr.count {
arr[i] = false
}
}

func getValidNumbers(sudokuBoard:[[Int]], row:Int, col:Int) -> [Int] {
var numberUsed = Array(count: 9, repeatedValue: false) // map each index to a number used
var invalidNumbers = [Int]()
var validNumbers = [Int]()
// add all the numbers in that row to an array

for pos in (0..<sudokuBoard[row].count) {
let num = sudokuBoard[row][pos] // subtract one to account for offset
if num>=0{
invalidNumbers.append(num)

}
}

// add all the numbers in that column to an array

for pos in (0..<9) {
let num = sudokuBoard[pos][col] // subtract one to account for offset
if num>=0 && !invalidNumbers.contains(num){
invalidNumbers.append(num)
}
}


// check the 3x3 box that it's in

for i in(0..<3) {
for j in (0..<3) {
let num = sudokuBoard[row*3+i][col*3+j]
if num>=0 && !invalidNumbers.contains(num){
invalidNumbers.append(num)
}

}
}

setValuesToFalse(&numberUsed)
for i in 0..<invalidNumbers.count{
numberUsed[invalidNumbers[i]-1] = true
}

for j in 0..<numberUsed.count{
if !numberUsed[j]{
validNumbers.append(j+1)
}
}

return validNumbers
}


let sudokuBoard = [ [-1, 7,1,9,-1,4,6,8,3], [-1,9,3,6,2,8,1,4,7], [4,6,8,1,3,7,2,5,9], [7,3,6,4,1,5,8,9,2], [1,5,9,8,6,2,3,7,4], [8,4,2,3,7,9,5,6,1], [9,8,5,2,4,1,7,3,6], [6,1,7,5,9,3,4,2,8], [3,2,4,7,8,6,9,1,5] ]

getValidNumbers(sudokuBoard, row: 0, col: 0)




/*
2)rotate a matrix by ninety degrees
*/

let matrix = [ [1, 2, 3, 4],
[5, 6, 7, 8],
[9, 0, 1, 2],
[3, 4, 5, 6] ]
// m x n matrix
// 0,0 goes to 0,3
// 1,0 goes to 0,2
// 2,0 goes to 0,1
// 3,0 goes to 0,0

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

let columns = matrix[0].count
let rows = matrix.count
var arr = [[Int]](count: rows, repeatedValue: [Int](count: columns, repeatedValue: 0))

for row in 0..<rows{
for col in 0..<columns{
arr [ col ] [ rows - row - 1 ] = matrix [ row ] [ col ];
}
}

return arr
}

rotateMatrix(matrix)

/*
3)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?



*/

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



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

6 changes: 6 additions & 0 deletions HWFrom1-17-16(Lists and Sorts).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>
39 changes: 39 additions & 0 deletions HWFrom1-28-16(Merge Sort).playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//: Playground - noun: a place where people can play

import UIKit


func insertionSort(var numbers:[Int], index:Int) -> [Int]{

if index == numbers.count{
return numbers
}
else{

var temp = numbers[index],
iter = 0, //final index
iter2 = 0,
maxIter = index - 1

while (iter <= maxIter && numbers[iter] < temp) {

iter++
}

while iter2<=maxIter{
numbers[maxIter-iter2+1] = numbers[maxIter-iter2]
iter2++
}


numbers[iter] = temp;

return insertionSort(numbers, index: index+1)
}

}




insertionSort([9, 3, 4, 2, 1], index: 1)
4 changes: 4 additions & 0 deletions HWFrom1-28-16(Merge Sort).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.

Loading