diff --git a/Package.swift b/Package.swift index 797d7ac..e3ea64d 100644 --- a/Package.swift +++ b/Package.swift @@ -23,9 +23,15 @@ let package = Package( targets: [ .target( name: "swift-ds-algorithms", + exclude: [ + "Arrays/README.md", + "DataStructures/README.md", + "Integers/README.md", + "Strings/README.md" + ], plugins: skipPlugins ? [] : [ .plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLintPlugins") - ], + ] ), .testTarget( name: "swift-ds-algorithms-tests", diff --git a/README.md b/README.md index eb876fa..acfbf7f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Swift Algorithms +# Swift Algorithms ![](https://github.com/ahcode0919/swift-ds-algorithms/actions/workflows/ci.yml/badge.svg?branch=main) Repository of solutions to various problems and algorithms in Swift. diff --git a/Sources/swift-ds-algorithms/Helpers.swift b/Sources/swift-ds-algorithms/Helpers.swift index db55cfa..68a7fff 100644 --- a/Sources/swift-ds-algorithms/Helpers.swift +++ b/Sources/swift-ds-algorithms/Helpers.swift @@ -9,6 +9,17 @@ import Foundation import GameKit public class Helpers { + public enum HelperError: Error, CustomStringConvertible { + case invalidInput(message: String) + + public var description: String { + switch self { + case .invalidInput(message: let message): + return message + } + } + } + public static func generateRandomString(ofLength length: Int = 1000000) -> String { let characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 " let charCount = characters.count @@ -17,9 +28,60 @@ public class Helpers { for index in 0.. SinglyLinkedListNode { + let dummyHead = SinglyLinkedListNode(value: 0) + var last = dummyHead + + for index in 0..(from array: [T]) throws -> SinglyLinkedListNode { + var head: SinglyLinkedListNode? + var last: SinglyLinkedListNode? + + for value in array { + let node = SinglyLinkedListNode(value: value) + + if head == nil { + head = SinglyLinkedListNode(value: value) + last = head + } else { + last?.next = node + last = node + } + } + + guard let head else { + throw HelperError.invalidInput(message: "Failed to generate a linked list") + } + + return head + } + + public static func linkedListToArray(head: SinglyLinkedListNode?) -> [T] { + var result: [T] = [] + var current = head + + while let node = current { + result.append(node.value) + current = node.next + } + return result + } } diff --git a/Sources/swift-ds-algorithms/LinkedList/MergeLists.swift b/Sources/swift-ds-algorithms/LinkedList/MergeLists.swift new file mode 100644 index 0000000..118ba48 --- /dev/null +++ b/Sources/swift-ds-algorithms/LinkedList/MergeLists.swift @@ -0,0 +1,39 @@ +// MergeLists.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +public enum MergeLists { + public static func merge( + head1: SinglyLinkedListNode?, + head2: SinglyLinkedListNode? + ) -> SinglyLinkedListNode? { + let dummyHead = SinglyLinkedListNode(value: 0) + var current: SinglyLinkedListNode? = dummyHead + var currentHead1 = head1 + var currentHead2 = head2 + + while let node1 = currentHead1, let node2 = currentHead2 { + if node1.value < node2.value { + current?.next = node1 + currentHead1 = node1.next + } else { + current?.next = node2 + currentHead2 = node2.next + } + current = current?.next + } + + if let node = currentHead1 { + current?.next = node + } + + if let node = currentHead2 { + current?.next = node + } + + return dummyHead.next + } +} diff --git a/Sources/swift-ds-algorithms/LinkedList/ReverseLinkedList.swift b/Sources/swift-ds-algorithms/LinkedList/ReverseLinkedList.swift new file mode 100644 index 0000000..c197bcc --- /dev/null +++ b/Sources/swift-ds-algorithms/LinkedList/ReverseLinkedList.swift @@ -0,0 +1,35 @@ +// ReverseLinkedList.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +public enum ReverseLinkedList { + public static func reverse(head: SinglyLinkedListNode?) -> SinglyLinkedListNode? { + var prev: SinglyLinkedListNode? + var current: SinglyLinkedListNode? = head + + while current != nil { + let temp = current?.next + current?.next = prev + prev = current + current = temp + } + + return prev + } + + public static func reverseRecursive( + head: SinglyLinkedListNode?, + prev: SinglyLinkedListNode? = nil + ) -> SinglyLinkedListNode? { + if head == nil { + return prev + } + + let temp = head?.next + head?.next = prev + return reverseRecursive(head: temp, prev: head) + } +} diff --git a/Sources/swift-ds-algorithms/LinkedList/ZipperLists.swift b/Sources/swift-ds-algorithms/LinkedList/ZipperLists.swift new file mode 100644 index 0000000..7f3bd45 --- /dev/null +++ b/Sources/swift-ds-algorithms/LinkedList/ZipperLists.swift @@ -0,0 +1,38 @@ +// ZipperLists.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +public enum ZipperLists { + public static func zipper( + head1: SinglyLinkedListNode?, + head2: SinglyLinkedListNode? + ) -> SinglyLinkedListNode? { + let dummyHead = SinglyLinkedListNode(value: "") + var current1: SinglyLinkedListNode? = head1 + var current2: SinglyLinkedListNode? = head2 + var current: SinglyLinkedListNode? = dummyHead + + while let node1 = current1, let node2 = current2 { + current?.next = node1 + current1 = node1.next + current = current?.next + + current?.next = node2 + current2 = node2.next + current = current?.next + } + + if let node = current1 { + current?.next = node + } + + if let node = current2 { + current?.next = node + } + + return dummyHead.next + } +} diff --git a/Tests/swift-ds-algorithms-tests/LinkedListTests/MergeListsTests.swift b/Tests/swift-ds-algorithms-tests/LinkedListTests/MergeListsTests.swift new file mode 100644 index 0000000..4542889 --- /dev/null +++ b/Tests/swift-ds-algorithms-tests/LinkedListTests/MergeListsTests.swift @@ -0,0 +1,31 @@ +// Test.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +import swift_ds_algorithms +import Testing + +struct MergeListsTests { + + @Test func testMergeLists() throws { + var list1: SinglyLinkedListNode? = try Helpers.generateLinkedList(from: [1, 4, 5, 7, 10]) + var list2: SinglyLinkedListNode? = try Helpers.generateLinkedList(from: [0, 3, 6, 8, 10, 11]) + var result = MergeLists.merge(head1: list1, head2: list2) + var values = Helpers.linkedListToArray(head: result) + #expect(values == [0, 1, 3, 4, 5, 6, 7, 8, 10, 10, 11]) + + list1 = nil + list2 = nil + result = MergeLists.merge(head1: list1, head2: list2) + #expect(result == nil) + + list1 = try Helpers.generateLinkedList(from: [1, 2, 3]) + list2 = nil + result = MergeLists.merge(head1: list1, head2: list2) + values = Helpers.linkedListToArray(head: result) + #expect(values == [1, 2, 3]) + } +} diff --git a/Tests/swift-ds-algorithms-tests/LinkedListTests/ReverseLinkedListTests.swift b/Tests/swift-ds-algorithms-tests/LinkedListTests/ReverseLinkedListTests.swift new file mode 100644 index 0000000..8219ccc --- /dev/null +++ b/Tests/swift-ds-algorithms-tests/LinkedListTests/ReverseLinkedListTests.swift @@ -0,0 +1,46 @@ +// Test.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +import swift_ds_algorithms +import Testing + +struct ReverseLinkedListTest { + + @Test func testReverseLinkedList() throws { + var head: SinglyLinkedListNode? + #expect(ReverseLinkedList.reverse(head: head) == nil) + + head = SinglyLinkedListNode(value: 0) + var result = ReverseLinkedList.reverse(head: head) + #expect(result?.value == 0) + #expect(result?.next == nil) + + head = try Helpers.generateLinkedList(ofLength: 10) + result = ReverseLinkedList.reverse(head: head) + #expect(result != nil) + + let values = Helpers.linkedListToArray(head: result) + #expect(values == [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) + } + + @Test func testReverseLinkedListRecursive() throws { + var head: SinglyLinkedListNode? + #expect(ReverseLinkedList.reverseRecursive(head: head) == nil) + + head = SinglyLinkedListNode(value: 0) + var result = ReverseLinkedList.reverseRecursive(head: head) + #expect(result?.value == 0) + #expect(result?.next == nil) + + head = try Helpers.generateLinkedList(ofLength: 10) + result = ReverseLinkedList.reverseRecursive(head: head) + #expect(result != nil) + + let values = Helpers.linkedListToArray(head: result) + #expect(values == [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) + } +} diff --git a/Tests/swift-ds-algorithms-tests/LinkedListTests/ZipperListsTests.swift b/Tests/swift-ds-algorithms-tests/LinkedListTests/ZipperListsTests.swift new file mode 100644 index 0000000..5d0240a --- /dev/null +++ b/Tests/swift-ds-algorithms-tests/LinkedListTests/ZipperListsTests.swift @@ -0,0 +1,44 @@ +// Test.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +import swift_ds_algorithms +import Testing + +struct ZipperListsTests { + + @Test func zipperLists() throws { + var result = ZipperLists.zipper(head1: nil, head2: nil) + #expect(result == nil) + + var list1 = try Helpers.generateLinkedList(from: ["a", "b", "c"]) + var list2 = try Helpers.generateLinkedList(from: ["x", "y", "z"]) + result = ZipperLists.zipper(head1: list1, head2: list2) + + var values = Helpers.linkedListToArray(head: result) + #expect(values == ["a", "x", "b", "y", "c", "z"]) + + list1 = try Helpers.generateLinkedList(from: ["a", "b", "c"]) + list2 = try Helpers.generateLinkedList(from: ["x", "y"]) + result = ZipperLists.zipper(head1: list1, head2: list2) + + values = Helpers.linkedListToArray(head: result) + #expect(values == ["a", "x", "b", "y", "c"]) + + list1 = try Helpers.generateLinkedList(from: ["a", "b"]) + list2 = try Helpers.generateLinkedList(from: ["x", "y", "z"]) + result = ZipperLists.zipper(head1: list1, head2: list2) + + values = Helpers.linkedListToArray(head: result) + #expect(values == ["a", "x", "b", "y", "z"]) + + list1 = try Helpers.generateLinkedList(from: ["a", "b", "c"]) + result = ZipperLists.zipper(head1: list1, head2: nil) + + values = Helpers.linkedListToArray(head: result) + #expect(values == ["a", "b", "c"]) + } +} diff --git a/Tests/swift-ds-algorithms-tests/Stack/BracketsTests.swift b/Tests/swift-ds-algorithms-tests/StackTests/BracketsTests.swift similarity index 100% rename from Tests/swift-ds-algorithms-tests/Stack/BracketsTests.swift rename to Tests/swift-ds-algorithms-tests/StackTests/BracketsTests.swift diff --git a/Tests/swift-ds-algorithms-tests/Stack/ReverseSomeCharsTests.swift b/Tests/swift-ds-algorithms-tests/StackTests/ReverseSomeCharsTests.swift similarity index 100% rename from Tests/swift-ds-algorithms-tests/Stack/ReverseSomeCharsTests.swift rename to Tests/swift-ds-algorithms-tests/StackTests/ReverseSomeCharsTests.swift