diff --git a/A-Star/AStar.swift b/A-Star/AStar.swift index 41a9fac6c..2f805bac5 100644 --- a/A-Star/AStar.swift +++ b/A-Star/AStar.swift @@ -54,10 +54,7 @@ public final class AStar { /// - Precondition: both `source` and `target` belong to `graph`. public func path(start: G.Vertex, target: G.Vertex) -> [G.Vertex] { open.insert(Node(vertex: start, cost: 0, estimate: heuristic(start, target))) - while !open.isEmpty { - guard let node = open.remove() else { - break - } + while !open.isEmpty, let node = open.remove() { costs[node.vertex] = node.cost if (node.vertex == target) { diff --git a/Huffman Coding/Huffman.swift b/Huffman Coding/Huffman.swift index cad80327d..973843a5b 100644 --- a/Huffman Coding/Huffman.swift +++ b/Huffman Coding/Huffman.swift @@ -95,8 +95,7 @@ extension Huffman { while queue.count > 1 { // Find the two nodes with the smallest frequencies that do not have // a parent node yet. - let node1 = queue.dequeue()! - let node2 = queue.dequeue()! + guard let node1 = queue.dequeue(), let node2 = queue.dequeue() else { return } // Create a new intermediate node. var parentNode = Node() @@ -115,8 +114,9 @@ extension Huffman { } // The final remaining node in the queue becomes the root of the tree. - let rootNode = queue.dequeue()! - root = rootNode.index + if let rootNode = queue.dequeue() { + root = rootNode.index + } } }