Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ A wide variety of code samples taken directly from in-class lectures.
[Stopwatch](https://github.com/accesscode-2-2/code-samples/tree/master/StopWatchDemo)
[Countdown](https://github.com/accesscode-2-2/code-samples/tree/master/TimerProject)

**Algorithms & Data Structures**
[Unit4](https://github.com/accesscode-2-2/code-samples/tree/master/Unit4)
93 changes: 93 additions & 0 deletions Unit4/unit-4_cameron_week-0.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import UIKit

// Linked Lists

class ListNode {
var value: Int?
var next: ListNode?

init(value: Int, next: ListNode?) {
self.value = value
self.next = next
}

func removeHead() -> ListNode? {
let next = self.next
self.next = nil
return next
}
}

var head = ListNode(value: 5, next: ListNode(value: 6, next: ListNode(value: 7, next: nil)))

let v = head.value
head = head.removeHead()!

head.value

// Stacks

struct Stack<Element> {
var array: [Element] = []

var isEmpty: Bool {
return array.isEmpty
}

func peek() -> Element? {
return array.last
}

mutating func push(element: Element) {
array.append(element)
}

mutating func pop() -> Element {
return array.removeLast()
}
}

func isBalanced(parens: String) -> Bool {
var s = Stack<Character>()
for c in parens.characters {
if c == "(" {
s.push(c)
} else if c == ")" {
if s.isEmpty {
return false
}
s.pop()
}
}
return s.isEmpty
}

isBalanced("(())()()(())(()()())()()")

// Hash tables

func hash(key: String) -> Int {
return key.characters.count - 1
}
hash("aa")
hash("xy")

"aa".hash
"xy".hash

// Graphs

let graph: Dictionary<Int, [Int]> = [
0 : [4],
1 : [3, 4],
2 : [0, 1, 3],
3 : [4],
4 : []
]

let start = 2
let connectedNodes = graph[start]
let next = connectedNodes![0]
let nextConnectedNodes = graph[next]

// start.next....
4 changes: 4 additions & 0 deletions Unit4/unit-4_cameron_week-0.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.

6 changes: 6 additions & 0 deletions Unit4/unit-4_cameron_week-0.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>