diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index e278018..d2508b9 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -12,13 +12,32 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Build 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 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/Arrays/LongestWord.swift b/Sources/swift-ds-algorithms/Arrays/LongestWord.swift new file mode 100644 index 0000000..efaacaa --- /dev/null +++ b/Sources/swift-ds-algorithms/Arrays/LongestWord.swift @@ -0,0 +1,54 @@ +// 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? + + 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 + let 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..562ec0e --- /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..2abd8e4 --- /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 { 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 } } diff --git a/Sources/swift-ds-algorithms/Strings/Anagrams.swift b/Sources/swift-ds-algorithms/Strings/Anagrams.swift new file mode 100644 index 0000000..3979f58 --- /dev/null +++ b/Sources/swift-ds-algorithms/Strings/Anagrams.swift @@ -0,0 +1,29 @@ +// Anagrams.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/15/26. +// Copyright © 2026. All rights reserved. +// + +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 new file mode 100644 index 0000000..06d8dae --- /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..4bcf70a --- /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")) + } +}