Skip to content

Updating to Swift3 (enum lowercase) #335

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

Merged
merged 1 commit into from
Dec 29, 2016
Merged
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//: Playground - noun: a place where people can play

// Each time you insert something, you get back a completely new tree.
var tree = BinarySearchTree.Leaf(7)
var tree = BinarySearchTree.leaf(7)
tree = tree.insert(newValue: 2)
tree = tree.insert(newValue: 5)
tree = tree.insert(newValue: 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
The tree is immutable. Any insertions or deletions will create a new tree.
*/
public enum BinarySearchTree<T: Comparable> {
case Empty
case Leaf(T)
indirect case Node(BinarySearchTree, T, BinarySearchTree)
case empty
case leaf(T)
indirect case node(BinarySearchTree, T, BinarySearchTree)

/* How many nodes are in this subtree. Performance: O(n). */
public var count: Int {
switch self {
case .Empty: return 0
case .Leaf: return 1
case let .Node(left, _, right): return left.count + 1 + right.count
case .empty: return 0
case .leaf: return 1
case let .node(left, _, right): return left.count + 1 + right.count
}
}

/* Distance of this node to its lowest leaf. Performance: O(n). */
public var height: Int {
switch self {
case .Empty: return 0
case .Leaf: return 1
case let .Node(left, _, right): return 1 + max(left.height, right.height)
case .empty: return 0
case .leaf: return 1
case let .node(left, _, right): return 1 + max(left.height, right.height)
}
}

Expand All @@ -32,21 +32,21 @@ public enum BinarySearchTree<T: Comparable> {
*/
public func insert(newValue: T) -> BinarySearchTree {
switch self {
case .Empty:
return .Leaf(newValue)
case .empty:
return .leaf(newValue)

case .Leaf(let value):
case .leaf(let value):
if newValue < value {
return .Node(.Leaf(newValue), value, .Empty)
return .node(.leaf(newValue), value, .empty)
} else {
return .Node(.Empty, value, .Leaf(newValue))
return .node(.empty, value, .leaf(newValue))
}

case .Node(let left, let value, let right):
case .node(let left, let value, let right):
if newValue < value {
return .Node(left.insert(newValue: newValue), value, right)
return .node(left.insert(newValue: newValue), value, right)
} else {
return .Node(left, value, right.insert(newValue: newValue))
return .node(left, value, right.insert(newValue: newValue))
}
}
}
Expand All @@ -57,11 +57,11 @@ public enum BinarySearchTree<T: Comparable> {
*/
public func search(x: T) -> BinarySearchTree? {
switch self {
case .Empty:
case .empty:
return nil
case .Leaf(let y):
case .leaf(let y):
return (x == y) ? self : nil
case let .Node(left, y, right):
case let .node(left, y, right):
if x < y {
return left.search(x: x)
} else if y < x {
Expand All @@ -82,11 +82,11 @@ public enum BinarySearchTree<T: Comparable> {
public func minimum() -> BinarySearchTree {
var node = self
var prev = node
while case let .Node(next, _, _) = node {
while case let .node(next, _, _) = node {
prev = node
node = next
}
if case .Leaf = node {
if case .leaf = node {
return node
}
return prev
Expand All @@ -98,11 +98,11 @@ public enum BinarySearchTree<T: Comparable> {
public func maximum() -> BinarySearchTree {
var node = self
var prev = node
while case let .Node(_, _, next) = node {
while case let .node(_, _, next) = node {
prev = node
node = next
}
if case .Leaf = node {
if case .leaf = node {
return node
}
return prev
Expand All @@ -112,9 +112,9 @@ public enum BinarySearchTree<T: Comparable> {
extension BinarySearchTree: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
case .Empty: return "."
case .Leaf(let value): return "\(value)"
case .Node(let left, let value, let right):
case .empty: return "."
case .leaf(let value): return "\(value)"
case .node(let left, let value, let right):
return "(\(left.debugDescription) <- \(value) -> \(right.debugDescription))"
}
}
Expand Down
54 changes: 27 additions & 27 deletions Binary Search Tree/Solution 2/BinarySearchTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
The tree is immutable. Any insertions or deletions will create a new tree.
*/
public enum BinarySearchTree<T: Comparable> {
case Empty
case Leaf(T)
indirect case Node(BinarySearchTree, T, BinarySearchTree)
case empty
case leaf(T)
indirect case node(BinarySearchTree, T, BinarySearchTree)

/* How many nodes are in this subtree. Performance: O(n). */
public var count: Int {
switch self {
case .Empty: return 0
case .Leaf: return 1
case let .Node(left, _, right): return left.count + 1 + right.count
case .empty: return 0
case .leaf: return 1
case let .node(left, _, right): return left.count + 1 + right.count
}
}

/* Distance of this node to its lowest leaf. Performance: O(n). */
public var height: Int {
switch self {
case .Empty: return 0
case .Leaf: return 1
case let .Node(left, _, right): return 1 + max(left.height, right.height)
case .empty: return 0
case .leaf: return 1
case let .node(left, _, right): return 1 + max(left.height, right.height)
}
}

Expand All @@ -32,21 +32,21 @@ public enum BinarySearchTree<T: Comparable> {
*/
public func insert(newValue: T) -> BinarySearchTree {
switch self {
case .Empty:
return .Leaf(newValue)
case .empty:
return .leaf(newValue)

case .Leaf(let value):
case .leaf(let value):
if newValue < value {
return .Node(.Leaf(newValue), value, .Empty)
return .node(.leaf(newValue), value, .empty)
} else {
return .Node(.Empty, value, .Leaf(newValue))
return .node(.empty, value, .leaf(newValue))
}

case .Node(let left, let value, let right):
case .node(let left, let value, let right):
if newValue < value {
return .Node(left.insert(newValue), value, right)
return .node(left.insert(newValue), value, right)
} else {
return .Node(left, value, right.insert(newValue))
return .node(left, value, right.insert(newValue))
}
}
}
Expand All @@ -57,11 +57,11 @@ public enum BinarySearchTree<T: Comparable> {
*/
public func search(x: T) -> BinarySearchTree? {
switch self {
case .Empty:
case .empty:
return nil
case .Leaf(let y):
case .leaf(let y):
return (x == y) ? self : nil
case let .Node(left, y, right):
case let .node(left, y, right):
if x < y {
return left.search(x)
} else if y < x {
Expand All @@ -82,11 +82,11 @@ public enum BinarySearchTree<T: Comparable> {
public func minimum() -> BinarySearchTree {
var node = self
var prev = node
while case let .Node(next, _, _) = node {
while case let .node(next, _, _) = node {
prev = node
node = next
}
if case .Leaf = node {
if case .leaf = node {
return node
}
return prev
Expand All @@ -98,11 +98,11 @@ public enum BinarySearchTree<T: Comparable> {
public func maximum() -> BinarySearchTree {
var node = self
var prev = node
while case let .Node(_, _, next) = node {
while case let .node(_, _, next) = node {
prev = node
node = next
}
if case .Leaf = node {
if case .leaf = node {
return node
}
return prev
Expand All @@ -112,9 +112,9 @@ public enum BinarySearchTree<T: Comparable> {
extension BinarySearchTree: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
case .Empty: return "."
case .Leaf(let value): return "\(value)"
case .Node(let left, let value, let right):
case .empty: return "."
case .leaf(let value): return "\(value)"
case .node(let left, let value, let right):
return "(\(left.debugDescription) <- \(value) -> \(right.debugDescription))"
}
}
Expand Down
6 changes: 3 additions & 3 deletions Counting Sort/CountingSort.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//: Playground - noun: a place where people can play

enum CountingSortError: Error {
case ArrayEmpty
case arrayEmpty
}

func countingSort(array: [Int]) throws -> [Int] {
guard array.count > 0 else {
throw CountingSortError.ArrayEmpty
throw CountingSortError.arrayEmpty
}

// Step 1
Expand Down Expand Up @@ -38,4 +38,4 @@ func countingSort(array: [Int]) throws -> [Int] {
}


try countingSort(array: [10, 9, 8, 7, 1, 2, 7, 3])
try countingSort(array: [10, 9, 8, 7, 1, 2, 7, 3])
4 changes: 2 additions & 2 deletions Counting Sort/CountingSort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
//

enum CountingSortError: ErrorType {
case ArrayEmpty
case arrayEmpty
}

func countingSort(array: [Int]) throws -> [Int] {
guard array.count > 0 else {
throw CountingSortError.ArrayEmpty
throw CountingSortError.arrayEmpty
}

// Step 1
Expand Down
4 changes: 2 additions & 2 deletions Linked List/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ It is possible to implement a linked list with value semantics using an enum. Th

```swift
enum ListNode<T> {
indirect case Node(T, next: ListNode<T>)
case End
indirect case node(T, next: ListNode<T>)
case end
}
```

Expand Down
Loading