Skip to content
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
23 changes: 21 additions & 2 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
54 changes: 54 additions & 0 deletions Sources/swift-ds-algorithms/Arrays/LongestWord.swift
Original file line number Diff line number Diff line change
@@ -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])
}
}
31 changes: 31 additions & 0 deletions Sources/swift-ds-algorithms/Arrays/MaxValue.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
20 changes: 20 additions & 0 deletions Sources/swift-ds-algorithms/Arrays/Pairs.swift
Original file line number Diff line number Diff line change
@@ -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..<elements.count {
for j in i + 1..<elements.count {
pairs.append([elements[i], elements[j]])
}
}

return pairs
}
}
15 changes: 8 additions & 7 deletions Sources/swift-ds-algorithms/Stack/Brackets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ public enum Brackets {
public static func validBrackets(_ input: String) -> Bool {
let stack = Stack<Character>()
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
}
}
Expand Down
29 changes: 29 additions & 0 deletions Sources/swift-ds-algorithms/Strings/Anagrams.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
26 changes: 26 additions & 0 deletions Tests/swift-ds-algorithms-tests/ArrayTests/LongestWord.swift
Original file line number Diff line number Diff line change
@@ -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")
}
}
25 changes: 25 additions & 0 deletions Tests/swift-ds-algorithms-tests/ArrayTests/MaxValueTest.swift
Original file line number Diff line number Diff line change
@@ -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)
}

}
37 changes: 37 additions & 0 deletions Tests/swift-ds-algorithms-tests/ArrayTests/PairsTest.swift
Original file line number Diff line number Diff line change
@@ -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"]
])
}
}
18 changes: 18 additions & 0 deletions Tests/swift-ds-algorithms-tests/StringsTests/AnagramsTest.swift
Original file line number Diff line number Diff line change
@@ -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"))
}
}