Skip to content

Async for Pythagorean Triplet #709

Open
@Maartz

Description

@Maartz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions