From 70046d7c3f11dd6e6af060a3e74021a07771c718 Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcode0919@users.noreply.github.com> Date: Sat, 15 Aug 2026 13:46:32 -0700 Subject: [PATCH 1/4] Fix syntax --- .github/workflows/swift.yml | 2 +- README.md | 2 +- .../swift-ds-algorithms/Stack/Brackets.swift | 17 +++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index e278018..4fd79f3 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -12,7 +12,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Build run: SKIP_PLUGINS=1 swift build diff --git a/README.md b/README.md index 174cdc4..eb876fa 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,5 @@ Repository of solutions to various problems and algorithms in Swift. ## Commands -- Run unit tests: `swift test` +- Run unit tests: `swift test` - Run formatting/linting: `swift package plugin swiftlint --fix` diff --git a/Sources/swift-ds-algorithms/Stack/Brackets.swift b/Sources/swift-ds-algorithms/Stack/Brackets.swift index f61ba6b..4870bed 100644 --- a/Sources/swift-ds-algorithms/Stack/Brackets.swift +++ b/Sources/swift-ds-algorithms/Stack/Brackets.swift @@ -9,23 +9,24 @@ public enum Brackets { public static func validBrackets(_ input: String) -> Bool { let stack = Stack() let brackets: [Character: Character] = [ - ")": "(", - "]": "[", - "}": "{" + "(": ")", + "[": "]", + "{": "}" ] for char in input { - if brackets[char] == nil { + if brackets[char] != nil { stack.push(value: char) } else { - if stack.peek() == brackets[char] { - _ = stack.pop() - } else { + guard let openBracket = stack.pop() else { + return false + } + if brackets[openBracket] != char { return false } } } - return stack.empty() + return stack.empty() } } From bc8ae2f799d25f4273eef3ac01861381ec7cf160 Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcode0919@users.noreply.github.com> Date: Sat, 15 Aug 2026 13:54:21 -0700 Subject: [PATCH 2/4] Check Code coverage --- .github/workflows/swift.yml | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 4fd79f3..d2508b9 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -18,7 +18,26 @@ jobs: run: SKIP_PLUGINS=1 swift build - name: Run tests - run: SKIP_PLUGINS=1 swift test + run: SKIP_PLUGINS=1 swift test --enable-code-coverage + + - name: Enforce coverage threshold + run: | + THRESHOLD=95 + BINARY=.build/debug/swift-ds-algorithmsPackageTests.xctest/Contents/MacOS/swift-ds-algorithmsPackageTests + PROFDATA=.build/debug/codecov/default.profdata + + xcrun llvm-cov export "$BINARY" \ + -instr-profile "$PROFDATA" \ + -ignore-filename-regex='Tests|\.build' \ + -format=text > coverage.json + + COVERAGE=$(jq '.data[0].totals.lines.percent' coverage.json) + echo "Line coverage: ${COVERAGE}% (threshold: ${THRESHOLD}%)" + + if (( $(echo "$COVERAGE < $THRESHOLD" | bc -l) )); then + echo "::error::Coverage ${COVERAGE}% is below the ${THRESHOLD}% threshold" + exit 1 + fi - name: Run Linter run: swift package plugin --allow-writing-to-package-directory swiftlint --strict From 6ee05922c33b98d6432de4d12064fdd41486dff1 Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcode0919@users.noreply.github.com> Date: Sat, 15 Aug 2026 21:04:44 -0700 Subject: [PATCH 3/4] Add additional algorithms --- .../Arrays/LongestWord.swift | 55 +++++++++++++++++++ .../swift-ds-algorithms/Arrays/MaxValue.swift | 31 +++++++++++ .../swift-ds-algorithms/Arrays/Pairs.swift | 20 +++++++ .../Strings/Anagrams.swift | 29 ++++++++++ .../ArrayTests/LongestWord.swift | 26 +++++++++ .../ArrayTests/MaxValueTest.swift | 25 +++++++++ .../ArrayTests/PairsTest.swift | 37 +++++++++++++ .../StringsTests/AnagramsTest.swift | 18 ++++++ 8 files changed, 241 insertions(+) create mode 100644 Sources/swift-ds-algorithms/Arrays/LongestWord.swift create mode 100644 Sources/swift-ds-algorithms/Arrays/MaxValue.swift create mode 100644 Sources/swift-ds-algorithms/Arrays/Pairs.swift create mode 100644 Sources/swift-ds-algorithms/Strings/Anagrams.swift create mode 100644 Tests/swift-ds-algorithms-tests/ArrayTests/LongestWord.swift create mode 100644 Tests/swift-ds-algorithms-tests/ArrayTests/MaxValueTest.swift create mode 100644 Tests/swift-ds-algorithms-tests/ArrayTests/PairsTest.swift create mode 100644 Tests/swift-ds-algorithms-tests/StringsTests/AnagramsTest.swift diff --git a/Sources/swift-ds-algorithms/Arrays/LongestWord.swift b/Sources/swift-ds-algorithms/Arrays/LongestWord.swift new file mode 100644 index 0000000..0715a00 --- /dev/null +++ b/Sources/swift-ds-algorithms/Arrays/LongestWord.swift @@ -0,0 +1,55 @@ +// LongestWord.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/15/26. +// Copyright © 2026. All rights reserved. +// + + +public enum LongestWord { + public static func longestWord(in sentence: String) -> String? { + guard sentence.count > 0 else { + return nil + } + + let words = sentence.split(separator: " ") + var longestWord: Substring? = nil + + for word in words { + if word.count > longestWord?.count ?? 0 { + longestWord = word + } + } + + if let longestWord = longestWord { + return String(longestWord) + } + return nil + } + + public static func longestWordOnePass(in sentence: String) -> String? { + guard sentence.count > 0 else { + return nil + } + let arr = Array(sentence) + + var start = 0 + var end = 0 + var longest = 0 + var longestWord = (0, 0) + + for (index, char) in arr.enumerated() { + if char == " " { + end = index - 1 + var length = end - start + 1 + if length > longest { + longest = length + longestWord = (start, end) + } + start = index + 1 + end = index + 1 + } + } + return String(arr[longestWord.0...longestWord.1]) + } +} diff --git a/Sources/swift-ds-algorithms/Arrays/MaxValue.swift b/Sources/swift-ds-algorithms/Arrays/MaxValue.swift new file mode 100644 index 0000000..166531d --- /dev/null +++ b/Sources/swift-ds-algorithms/Arrays/MaxValue.swift @@ -0,0 +1,31 @@ +// MaxValue.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/15/26. +// Copyright © 2026. All rights reserved. +// + +public enum MaxValue { + public static func max_value_builtin(nums: [Int]) -> Int? { + guard !nums.isEmpty else { + return nil + } + + return nums.max() + } + + public static func max_value(nums: [Int]) -> Int? { + guard nums.count > 0 else { + return nil + } + + var max = Int.min + + for num in nums { + if num > max { + max = num + } + } + return max + } +} diff --git a/Sources/swift-ds-algorithms/Arrays/Pairs.swift b/Sources/swift-ds-algorithms/Arrays/Pairs.swift new file mode 100644 index 0000000..6394575 --- /dev/null +++ b/Sources/swift-ds-algorithms/Arrays/Pairs.swift @@ -0,0 +1,20 @@ +// Pairs.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/15/26. +// Copyright © 2026. All rights reserved. +// + +public enum Pairs { + public static func pairs(for elements: [String]) -> [[String]] { + var pairs = [[String]]() + + for i in 0.. Bool { + var dictA = [Character: Int]() + var dictB = [Character: Int]() + + for char in a { + dictA[char, default: 0] += 1 + } + + for char in b { + dictB[char, default: 0] += 1 + } + + for (key, value) in dictA { + if dictB[key] != value { + return false + } + } + + return true + } +} diff --git a/Tests/swift-ds-algorithms-tests/ArrayTests/LongestWord.swift b/Tests/swift-ds-algorithms-tests/ArrayTests/LongestWord.swift new file mode 100644 index 0000000..2937b35 --- /dev/null +++ b/Tests/swift-ds-algorithms-tests/ArrayTests/LongestWord.swift @@ -0,0 +1,26 @@ +// Test.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/15/26. +// Copyright © 2026. All rights reserved. +// + +import swift_ds_algorithms +import Testing + +struct LongestWordTests { + + @Test func longestWord() throws { + #expect(LongestWord.longestWord(in: "the quick brown fox jumped over the lazy dog") == "jumped") + #expect(LongestWord.longestWord(in: "") == nil) + #expect(LongestWord.longestWord(in: "a") == "a") + #expect(LongestWord.longestWord(in: " a ") == "a") + } + + @Test func longestWordOnePass() throws { + #expect(LongestWord.longestWordOnePass(in: "the quick brown fox jumped over the lazy dog") == "jumped") + #expect(LongestWord.longestWordOnePass(in: "") == nil) + #expect(LongestWord.longestWordOnePass(in: "a") == "a") + #expect(LongestWord.longestWordOnePass(in: " a ") == "a") + } +} diff --git a/Tests/swift-ds-algorithms-tests/ArrayTests/MaxValueTest.swift b/Tests/swift-ds-algorithms-tests/ArrayTests/MaxValueTest.swift new file mode 100644 index 0000000..47431cc --- /dev/null +++ b/Tests/swift-ds-algorithms-tests/ArrayTests/MaxValueTest.swift @@ -0,0 +1,25 @@ +// Test.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/15/26. +// Copyright © 2026. All rights reserved. +// + +import swift_ds_algorithms +import Testing + +struct MaxValueTest { + + @Test func testMaxValue() throws { + #expect(MaxValue.max_value(nums: [1, 2, 3, 4, 5]) == 5) + #expect(MaxValue.max_value(nums: []) == nil) + #expect(MaxValue.max_value(nums: [1]) == 1) + } + + @Test func testMaxValueBuiltin() throws { + #expect(MaxValue.max_value_builtin(nums: [1, 2, 3, 4, 5]) == 5) + #expect(MaxValue.max_value_builtin(nums: []) == nil) + #expect(MaxValue.max_value_builtin(nums: [1]) == 1) + } + +} diff --git a/Tests/swift-ds-algorithms-tests/ArrayTests/PairsTest.swift b/Tests/swift-ds-algorithms-tests/ArrayTests/PairsTest.swift new file mode 100644 index 0000000..18a92d2 --- /dev/null +++ b/Tests/swift-ds-algorithms-tests/ArrayTests/PairsTest.swift @@ -0,0 +1,37 @@ +// Test.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/15/26. +// Copyright © 2026. All rights reserved. +// + +import swift_ds_algorithms +import Testing + +struct Test { + + @Test func testPairs() throws { + var pairs = Pairs.pairs(for: ["a", "b", "c"]) + #expect(pairs.count == 3) + #expect(pairs == [ + ["a", "b"], + ["a", "c"], + ["b", "c"] + ]) + + pairs = Pairs.pairs(for: []) + #expect(pairs.isEmpty) + #expect(Pairs.pairs(for: ["a"]).isEmpty) + + pairs = Pairs.pairs(for: ["a", "b", "c", "d"]) + #expect(pairs.count == 6) + #expect(pairs == [ + ["a", "b"], + ["a", "c"], + ["a", "d"], + ["b", "c"], + ["b", "d"], + ["c", "d"] + ]) + } +} diff --git a/Tests/swift-ds-algorithms-tests/StringsTests/AnagramsTest.swift b/Tests/swift-ds-algorithms-tests/StringsTests/AnagramsTest.swift new file mode 100644 index 0000000..7ab931e --- /dev/null +++ b/Tests/swift-ds-algorithms-tests/StringsTests/AnagramsTest.swift @@ -0,0 +1,18 @@ +// Test.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/15/26. +// Copyright © 2026. All rights reserved. +// + +import swift_ds_algorithms +import Testing + +struct AnagramTests { + + @Test func testAnagrams() async throws { + #expect(Anagrams.isAnagram("hello", "olleh")) + #expect(Anagrams.isAnagram("hello", "world") == false) + #expect(Anagrams.isAnagram("hello world", "world hello")) + } +} From b1035ec99ebb34e156d1069ad29218578e095651 Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcode0919@users.noreply.github.com> Date: Sat, 15 Aug 2026 21:08:40 -0700 Subject: [PATCH 4/4] Fix linter errors --- .../swift-ds-algorithms/Arrays/LongestWord.swift | 15 +++++++-------- Sources/swift-ds-algorithms/Arrays/MaxValue.swift | 8 ++++---- Sources/swift-ds-algorithms/Arrays/Pairs.swift | 2 +- Sources/swift-ds-algorithms/Stack/Brackets.swift | 2 +- .../swift-ds-algorithms/Strings/Anagrams.swift | 8 ++++---- .../ArrayTests/LongestWord.swift | 2 +- .../ArrayTests/MaxValueTest.swift | 2 +- 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Sources/swift-ds-algorithms/Arrays/LongestWord.swift b/Sources/swift-ds-algorithms/Arrays/LongestWord.swift index 0715a00..efaacaa 100644 --- a/Sources/swift-ds-algorithms/Arrays/LongestWord.swift +++ b/Sources/swift-ds-algorithms/Arrays/LongestWord.swift @@ -5,28 +5,27 @@ // Copyright © 2026. All rights reserved. // - public enum LongestWord { public static func longestWord(in sentence: String) -> String? { guard sentence.count > 0 else { return nil } - + let words = sentence.split(separator: " ") - var longestWord: Substring? = nil - + var longestWord: Substring? + for word in words { if word.count > longestWord?.count ?? 0 { longestWord = word } } - + if let longestWord = longestWord { return String(longestWord) } return nil } - + public static func longestWordOnePass(in sentence: String) -> String? { guard sentence.count > 0 else { return nil @@ -37,11 +36,11 @@ public enum LongestWord { var end = 0 var longest = 0 var longestWord = (0, 0) - + for (index, char) in arr.enumerated() { if char == " " { end = index - 1 - var length = end - start + 1 + let length = end - start + 1 if length > longest { longest = length longestWord = (start, end) diff --git a/Sources/swift-ds-algorithms/Arrays/MaxValue.swift b/Sources/swift-ds-algorithms/Arrays/MaxValue.swift index 166531d..562ec0e 100644 --- a/Sources/swift-ds-algorithms/Arrays/MaxValue.swift +++ b/Sources/swift-ds-algorithms/Arrays/MaxValue.swift @@ -10,17 +10,17 @@ public enum MaxValue { guard !nums.isEmpty else { return nil } - + return nums.max() } - + public static func max_value(nums: [Int]) -> Int? { guard nums.count > 0 else { return nil } - + var max = Int.min - + for num in nums { if num > max { max = num diff --git a/Sources/swift-ds-algorithms/Arrays/Pairs.swift b/Sources/swift-ds-algorithms/Arrays/Pairs.swift index 6394575..2abd8e4 100644 --- a/Sources/swift-ds-algorithms/Arrays/Pairs.swift +++ b/Sources/swift-ds-algorithms/Arrays/Pairs.swift @@ -14,7 +14,7 @@ public enum Pairs { pairs.append([elements[i], elements[j]]) } } - + return pairs } } diff --git a/Sources/swift-ds-algorithms/Stack/Brackets.swift b/Sources/swift-ds-algorithms/Stack/Brackets.swift index 4870bed..81a1590 100644 --- a/Sources/swift-ds-algorithms/Stack/Brackets.swift +++ b/Sources/swift-ds-algorithms/Stack/Brackets.swift @@ -27,6 +27,6 @@ public enum Brackets { } } - return stack.empty() + return stack.empty() } } diff --git a/Sources/swift-ds-algorithms/Strings/Anagrams.swift b/Sources/swift-ds-algorithms/Strings/Anagrams.swift index e928e85..3979f58 100644 --- a/Sources/swift-ds-algorithms/Strings/Anagrams.swift +++ b/Sources/swift-ds-algorithms/Strings/Anagrams.swift @@ -9,21 +9,21 @@ public enum Anagrams { public static func isAnagram(_ a: String, _ b: String) -> Bool { var dictA = [Character: Int]() var dictB = [Character: Int]() - + for char in a { dictA[char, default: 0] += 1 } - + for char in b { dictB[char, default: 0] += 1 } - + for (key, value) in dictA { if dictB[key] != value { return false } } - + return true } } diff --git a/Tests/swift-ds-algorithms-tests/ArrayTests/LongestWord.swift b/Tests/swift-ds-algorithms-tests/ArrayTests/LongestWord.swift index 2937b35..06d8dae 100644 --- a/Tests/swift-ds-algorithms-tests/ArrayTests/LongestWord.swift +++ b/Tests/swift-ds-algorithms-tests/ArrayTests/LongestWord.swift @@ -16,7 +16,7 @@ struct LongestWordTests { #expect(LongestWord.longestWord(in: "a") == "a") #expect(LongestWord.longestWord(in: " a ") == "a") } - + @Test func longestWordOnePass() throws { #expect(LongestWord.longestWordOnePass(in: "the quick brown fox jumped over the lazy dog") == "jumped") #expect(LongestWord.longestWordOnePass(in: "") == nil) diff --git a/Tests/swift-ds-algorithms-tests/ArrayTests/MaxValueTest.swift b/Tests/swift-ds-algorithms-tests/ArrayTests/MaxValueTest.swift index 47431cc..4bcf70a 100644 --- a/Tests/swift-ds-algorithms-tests/ArrayTests/MaxValueTest.swift +++ b/Tests/swift-ds-algorithms-tests/ArrayTests/MaxValueTest.swift @@ -15,7 +15,7 @@ struct MaxValueTest { #expect(MaxValue.max_value(nums: []) == nil) #expect(MaxValue.max_value(nums: [1]) == 1) } - + @Test func testMaxValueBuiltin() throws { #expect(MaxValue.max_value_builtin(nums: [1, 2, 3, 4, 5]) == 5) #expect(MaxValue.max_value_builtin(nums: []) == nil)