exercism / exercism/swift

Async for Pythagorean Triplet

Open
#709 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
122
Forks
165
PR merge metrics
No merged PRs in 30d

Description

For the [Pythagorean Triplet exericse](https://exercism.org/tracks/swift/exercises/pythagorean-triplet), 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.

```swift
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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.