exercism / swift

Exercism exercises in Swift.
https://exercism.org/tracks/swift
MIT License
113 stars 158 forks source link

Async for Pythagorean Triplet #709

Open Maartz opened 9 months ago

Maartz commented 9 months ago

For the Pythagorean Triplet exericse, it could be great to make it leverage async/DispatchGroup/Actors/withTaskGroup.

The idea comes from translating the Elixir code into Swift.

It was nice and easy but the test suite itself is not ready to handle this.

As the exercise is tagged as Medium, I assume asynchronous code fit in this tier of "difficulty".

Locally I've updated the code to handle async and because of this, sorting of the results.

import XCTest

@testable import PythagoreanTriplet

extension Array where Element == [Int] {
    func sortedTriplets() -> [[Int]] {
        self.sorted { ($0[0], $0[1], $0[2]) < ($1[0], $1[1], $1[2]) }
    }
}

class PythagoreanTripletTests: XCTestCase {
    let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

    func testTripletsWhoseSumIs12() async throws {
        let result = await tripletsWithSum(12).sortedTriplets()
        let expected = [[3, 4, 5]]
        XCTAssertEqual(result, expected)
    }

    func testTripletsWhoseSumIs108() async throws {
        try XCTSkipIf(true && !runAll)
        let result = await tripletsWithSum(108).sortedTriplets()
        let expected = [[27, 36, 45]]
        XCTAssertEqual(result, expected)
    }

    func testTripletsWhoseSumIs1000() async throws {
        try XCTSkipIf(true && !runAll)
        let result = await tripletsWithSum(1000).sortedTriplets()
        let expected = [[200, 375, 425]]
        XCTAssertEqual(result, expected)
    }

    func testNoMatchingTripletsFor1001() async throws {
        try XCTSkipIf(true && !runAll)
        let result = await tripletsWithSum(1001).sortedTriplets()
        XCTAssertEqual(result, [])
    }

    func testReturnsAllMatchingTriplets() async throws {
        try XCTSkipIf(true && !runAll)  
        let result = await tripletsWithSum(90).sortedTriplets()
        let expected = [[9, 40, 41], [15, 36, 39]]
        XCTAssertEqual(result, expected)
    }

    func testSeveralMatchingTriplets() async throws {
        try XCTSkipIf(true && !runAll) 
        let result = await tripletsWithSum(840).sortedTriplets()
        let expected = [
            [40, 399, 401], [56, 390, 394], [105, 360, 375], [120, 350, 370], [140, 336, 364],
            [168, 315, 357], [210, 280, 350], [240, 252, 348]
        ]
        XCTAssertEqual(result, expected)
    }

    func testTripletsForLargeNumber() async throws {
        try XCTSkipIf(true && !runAll)
        let result = await tripletsWithSum(30000).sortedTriplets()
        let expected = [
            [1200, 14375, 14425], [1875, 14000, 14125], [5000, 12000, 13000], [6000, 11250, 12750],
            [7500, 10000, 12500]
        ]
        XCTAssertEqual(result, expected)
    }
}

Cheers

Maartz commented 9 months ago
Exercism/swift/pythagorean-triplet via 🐦 v5.9.2 took 5s
❯ RUNALL=true swift test
Building for debugging...
[3/3] Linking PythagoreanTripletPackageTests
Build complete! (0.55s)
Test Suite 'All tests' started at 2023-12-31 09:26:20.774.
Test Suite 'PythagoreanTripletPackageTests.xctest' started at 2023-12-31 09:26:20.775.
Test Suite 'PythagoreanTripletTests' started at 2023-12-31 09:26:20.775.
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testNoMatchingTripletsFor1001]' started.
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testNoMatchingTripletsFor1001]' passed (0.006 seconds).
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testReturnsAllMatchingTriplets]' started.
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testReturnsAllMatchingTriplets]' passed (0.000 seconds).
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testSeveralMatchingTriplets]' started.
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testSeveralMatchingTriplets]' passed (0.004 seconds).
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testTripletsForLargeNumber]' started.
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testTripletsForLargeNumber]' passed (3.673 seconds).
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testTripletsWhoseSumIs1000]' started.
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testTripletsWhoseSumIs1000]' passed (0.006 seconds).
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testTripletsWhoseSumIs108]' started.
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testTripletsWhoseSumIs108]' passed (0.000 seconds).
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testTripletsWhoseSumIs12]' started.
Test Case '-[PythagoreanTripletTests.PythagoreanTripletTests testTripletsWhoseSumIs12]' passed (0.000 seconds).
Test Suite 'PythagoreanTripletTests' passed at 2023-12-31 09:26:24.465.
         Executed 7 tests, with 0 failures (0 unexpected) in 3.689 (3.690) seconds
Test Suite 'PythagoreanTripletPackageTests.xctest' passed at 2023-12-31 09:26:24.465.
         Executed 7 tests, with 0 failures (0 unexpected) in 3.689 (3.690) seconds
Test Suite 'All tests' passed at 2023-12-31 09:26:24.465.
         Executed 7 tests, with 0 failures (0 unexpected) in 3.689 (3.691) seconds