From 8538210b738ffb49303f627e0bcde2dc9a1838fc Mon Sep 17 00:00:00 2001 From: Cameron Spickert Date: Fri, 8 Jan 2016 11:15:07 -0500 Subject: [PATCH] Add Algorithms & Data Structures week 0 playground with code samples --- README.md | 2 + .../Contents.swift | 93 +++++++++++++++++++ .../contents.xcplayground | 4 + .../contents.xcworkspacedata | 7 ++ .../timeline.xctimeline | 6 ++ 5 files changed, 112 insertions(+) create mode 100644 Unit4/unit-4_cameron_week-0.playground/Contents.swift create mode 100644 Unit4/unit-4_cameron_week-0.playground/contents.xcplayground create mode 100644 Unit4/unit-4_cameron_week-0.playground/playground.xcworkspace/contents.xcworkspacedata create mode 100644 Unit4/unit-4_cameron_week-0.playground/timeline.xctimeline diff --git a/README.md b/README.md index 24b2697..5c7a485 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/Unit4/unit-4_cameron_week-0.playground/Contents.swift b/Unit4/unit-4_cameron_week-0.playground/Contents.swift new file mode 100644 index 0000000..ba54b97 --- /dev/null +++ b/Unit4/unit-4_cameron_week-0.playground/Contents.swift @@ -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 { + 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() + 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 = [ + 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.... diff --git a/Unit4/unit-4_cameron_week-0.playground/contents.xcplayground b/Unit4/unit-4_cameron_week-0.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/Unit4/unit-4_cameron_week-0.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Unit4/unit-4_cameron_week-0.playground/playground.xcworkspace/contents.xcworkspacedata b/Unit4/unit-4_cameron_week-0.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Unit4/unit-4_cameron_week-0.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Unit4/unit-4_cameron_week-0.playground/timeline.xctimeline b/Unit4/unit-4_cameron_week-0.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/Unit4/unit-4_cameron_week-0.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + +