Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift 6]: Update Exercises batch 17 #801

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ let practiceExerciseTargets: [Target] = practiceExercises.flatMap {
.testTarget(
name:"\($0.pascalCased)Tests",
dependencies: [
.target(name:"\($0.pascalCased)")
.target(name:"\($0.pascalCased)"),
.product(name: "Numerics", package: "swift-numerics")
],
path:"./exercises/practice/\($0)/Tests")
]
Expand All @@ -68,5 +69,6 @@ let package = Package(
name: "xswift",
targets: allTargets.filter { $0.type == .regular }.map { $0.name })
],
dependencies: [.package(url: "https://github.com/apple/swift-numerics", from: "1.0.0"),],
targets: allTargets
)
17 changes: 10 additions & 7 deletions exercises/practice/space-age/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import XCTest
import Testing
import Foundation
import Numerics
@testable import {{exercise|camelCase}}
class {{exercise|camelCase}}Tests: XCTestCase {
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct {{exercise|camelCase}}Tests {
{% for case in cases %}
{% if forloop.first -%}
func test{{case.description |camelCase }}() {
@Test("{{case.description}}")
{% else -%}
func test{{case.description |camelCase }}() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("{{case.description}}", .enabled(if: RUNALL))
{% endif -%}
func test{{case.description |camelCase }}() {
let age = SpaceAge({{case.input.seconds}})
XCTAssertEqual(age.on{{case.input.planet |camelCase}}, {{case.expected | round:2 }}, accuracy: 0.02)
#expect(age.on{{case.input.planet |camelCase}}.isApproximatelyEqual(to: {{case.expected | round:2 }}, relativeTolerance: 0.03))
}
{% endfor -%}
}
10 changes: 6 additions & 4 deletions exercises/practice/space-age/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:6.0

import PackageDescription

Expand All @@ -9,13 +9,15 @@ let package = Package(
name: "SpaceAge",
targets: ["SpaceAge"])
],
dependencies: [],
dependencies: [
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.2"),
],
targets: [
.target(
name: "SpaceAge",
dependencies: []),
dependencies: [.product(name: "Numerics", package: "swift-numerics")]),
.testTarget(
name: "SpaceAgeTests",
dependencies: ["SpaceAge"]),
dependencies: [.product(name: "Numerics", package: "swift-numerics"), "SpaceAge"]),
]
)
15 changes: 14 additions & 1 deletion exercises/practice/space-age/Sources/SpaceAge/SpaceAge.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
class SpaceAge {
// Write your code for the 'SpaceAge' exercise in this file.
var seconds: Float = 0

var onMercury: Float { return ((seconds / 7_600_530.24) * 100).rounded() / 100 }
var onVenus: Float { return ((seconds / 19_413_907.2) * 100).rounded() / 100 }
var onEarth: Float { return ((seconds / 31_558_149.76) * 100).rounded() / 100 }
var onMars: Float { return ((seconds / 59_354_294.4) * 100).rounded() / 100 }
var onJupiter: Float { return ((seconds / 374_335_776.0) * 100).rounded() / 100 }
var onSaturn: Float { return ((seconds / 929_596_608.0) * 100).rounded() / 100 }
var onUranus: Float { return ((seconds / 2_661_041_808.0) * 100).rounded() / 100 }
var onNeptune: Float { return ((seconds / 5_200_418_592.0) * 100).rounded() / 100 }

init(_ input: Float) {
self.seconds = input
}
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@
import XCTest
import Foundation
import Numerics
import Testing

@testable import SpaceAge

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

@Suite struct SpaceAgeTests {

@Test("age on Earth")
func testAgeOnEarth() {
let age = SpaceAge(1_000_000_000)
XCTAssertEqual(age.onEarth, 31.69, accuracy: 0.02)
#expect(age.onEarth.isApproximatelyEqual(to: 31.69, relativeTolerance: 0.03))
}

func testAgeOnMercury() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("age on Mercury", .enabled(if: RUNALL))
func testAgeOnMercury() {
let age = SpaceAge(2_134_835_688)
XCTAssertEqual(age.onMercury, 280.88, accuracy: 0.02)
#expect(age.onMercury.isApproximatelyEqual(to: 280.88, relativeTolerance: 0.03))
}

func testAgeOnVenus() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("age on Venus", .enabled(if: RUNALL))
func testAgeOnVenus() {
let age = SpaceAge(189_839_836)
XCTAssertEqual(age.onVenus, 9.78, accuracy: 0.02)
#expect(age.onVenus.isApproximatelyEqual(to: 9.78, relativeTolerance: 0.03))
}

func testAgeOnMars() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("age on Mars", .enabled(if: RUNALL))
func testAgeOnMars() {
let age = SpaceAge(2_129_871_239)
XCTAssertEqual(age.onMars, 35.88, accuracy: 0.02)
#expect(age.onMars.isApproximatelyEqual(to: 35.88, relativeTolerance: 0.03))
}

func testAgeOnJupiter() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("age on Jupiter", .enabled(if: RUNALL))
func testAgeOnJupiter() {
let age = SpaceAge(901_876_382)
XCTAssertEqual(age.onJupiter, 2.41, accuracy: 0.02)
#expect(age.onJupiter.isApproximatelyEqual(to: 2.41, relativeTolerance: 0.03))
}

func testAgeOnSaturn() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("age on Saturn", .enabled(if: RUNALL))
func testAgeOnSaturn() {
let age = SpaceAge(2_000_000_000)
XCTAssertEqual(age.onSaturn, 2.15, accuracy: 0.02)
#expect(age.onSaturn.isApproximatelyEqual(to: 2.15, relativeTolerance: 0.03))
}

func testAgeOnUranus() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("age on Uranus", .enabled(if: RUNALL))
func testAgeOnUranus() {
let age = SpaceAge(1_210_123_456)
XCTAssertEqual(age.onUranus, 0.46, accuracy: 0.02)
#expect(age.onUranus.isApproximatelyEqual(to: 0.46, relativeTolerance: 0.03))
}

func testAgeOnNeptune() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("age on Neptune", .enabled(if: RUNALL))
func testAgeOnNeptune() {
let age = SpaceAge(1_821_023_456)
XCTAssertEqual(age.onNeptune, 0.35, accuracy: 0.02)
#expect(age.onNeptune.isApproximatelyEqual(to: 0.35, relativeTolerance: 0.03))
}
}
16 changes: 9 additions & 7 deletions exercises/practice/strain/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import XCTest
import Testing
import Foundation
@testable import {{exercise|camelCase}}
class {{exercise|camelCase}}Tests: XCTestCase {
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct {{exercise|camelCase}}Tests {
{% for case in cases %}
{% if forloop.first -%}
func test{{case.description |camelCase }}() {
@Test("{{case.description}}")
{% else -%}
func test{{case.description |camelCase }}() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("{{case.description}}", .enabled(if: RUNALL))
{% endif -%}
func test{{case.description |camelCase }}() {
{%- if case.input.list.count == 0 -%}
let input : [Int] = []
{%- else -%}
Expand All @@ -20,7 +22,7 @@ class {{exercise|camelCase}}Tests: XCTestCase {
{%- else %}
let expected = {{case.expected | toStringArray}}
{%- endif %}
XCTAssertEqual(input.{{case.property}} {{case.input.predicate | strain}}, expected)
#expect(input.{{case.property}} {{case.input.predicate | strain}} == expected)
}
{% endfor -%}
}
2 changes: 1 addition & 1 deletion exercises/practice/strain/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:6.0

import PackageDescription

Expand Down
89 changes: 46 additions & 43 deletions exercises/practice/strain/Tests/StrainTests/StrainTests.swift
Original file line number Diff line number Diff line change
@@ -1,108 +1,111 @@
import XCTest
import Foundation
import Testing

@testable import Strain

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

@Suite struct StrainTests {

@Test("keep on empty list returns empty list")
func testKeepOnEmptyListReturnsEmptyList() {
let input: [Int] = []
let expected: [Int] = []
XCTAssertEqual(input.keep { x in true }, expected)
#expect(input.keep { x in true } == expected)
}

func testKeepsEverything() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("keeps everything", .enabled(if: RUNALL))
func testKeepsEverything() {
let input = [1, 3, 5]
let expected = [1, 3, 5]
XCTAssertEqual(input.keep { x in true }, expected)
#expect(input.keep { x in true } == expected)
}

func testKeepsNothing() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("keeps nothing", .enabled(if: RUNALL))
func testKeepsNothing() {
let input = [1, 3, 5]
let expected: [Int] = []
XCTAssertEqual(input.keep { x in false }, expected)
#expect(input.keep { x in false } == expected)
}

func testKeepsFirstAndLast() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("keeps first and last", .enabled(if: RUNALL))
func testKeepsFirstAndLast() {
let input = [1, 2, 3]
let expected = [1, 3]
XCTAssertEqual(input.keep { x in x % 2 == 1 }, expected)
#expect(input.keep { x in x % 2 == 1 } == expected)
}

func testKeepsNeitherFirstNorLast() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("keeps neither first nor last", .enabled(if: RUNALL))
func testKeepsNeitherFirstNorLast() {
let input = [1, 2, 3]
let expected = [2]
XCTAssertEqual(input.keep { x in x % 2 == 0 }, expected)
#expect(input.keep { x in x % 2 == 0 } == expected)
}

func testKeepsStrings() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("keeps strings", .enabled(if: RUNALL))
func testKeepsStrings() {
let input = ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"]
let expected = ["zebra", "zombies", "zealot"]
XCTAssertEqual(input.keep { x in x.starts(with: "z") }, expected)
#expect(input.keep { x in x.starts(with: "z") } == expected)
}

func testKeepsLists() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("keeps lists", .enabled(if: RUNALL))
func testKeepsLists() {
let input = [
[1, 2, 3], [5, 5, 5], [5, 1, 2], [2, 1, 2], [1, 5, 2], [2, 2, 1], [1, 2, 5],
]
let expected = [[5, 5, 5], [5, 1, 2], [1, 5, 2], [1, 2, 5]]
XCTAssertEqual(input.keep { x in x.contains(5) }, expected)
#expect(input.keep { x in x.contains(5) } == expected)
}

func testDiscardOnEmptyListReturnsEmptyList() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("discard on empty list returns empty list", .enabled(if: RUNALL))
func testDiscardOnEmptyListReturnsEmptyList() {
let input: [Int] = []
let expected: [Int] = []
XCTAssertEqual(input.discard { x in true }, expected)
#expect(input.discard { x in true } == expected)
}

func testDiscardsEverything() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("discards everything", .enabled(if: RUNALL))
func testDiscardsEverything() {
let input = [1, 3, 5]
let expected: [Int] = []
XCTAssertEqual(input.discard { x in true }, expected)
#expect(input.discard { x in true } == expected)
}

func testDiscardsNothing() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("discards nothing", .enabled(if: RUNALL))
func testDiscardsNothing() {
let input = [1, 3, 5]
let expected = [1, 3, 5]
XCTAssertEqual(input.discard { x in false }, expected)
#expect(input.discard { x in false } == expected)
}

func testDiscardsFirstAndLast() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("discards first and last", .enabled(if: RUNALL))
func testDiscardsFirstAndLast() {
let input = [1, 2, 3]
let expected = [2]
XCTAssertEqual(input.discard { x in x % 2 == 1 }, expected)
#expect(input.discard { x in x % 2 == 1 } == expected)
}

func testDiscardsNeitherFirstNorLast() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("discards neither first nor last", .enabled(if: RUNALL))
func testDiscardsNeitherFirstNorLast() {
let input = [1, 2, 3]
let expected = [1, 3]
XCTAssertEqual(input.discard { x in x % 2 == 0 }, expected)
#expect(input.discard { x in x % 2 == 0 } == expected)
}

func testDiscardsStrings() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("discards strings", .enabled(if: RUNALL))
func testDiscardsStrings() {
let input = ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"]
let expected = ["apple", "banana", "cherimoya"]
XCTAssertEqual(input.discard { x in x.starts(with: "z") }, expected)
#expect(input.discard { x in x.starts(with: "z") } == expected)
}

func testDiscardsLists() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("discards lists", .enabled(if: RUNALL))
func testDiscardsLists() {
let input = [
[1, 2, 3], [5, 5, 5], [5, 1, 2], [2, 1, 2], [1, 5, 2], [2, 2, 1], [1, 2, 5],
]
let expected = [[1, 2, 3], [2, 1, 2], [2, 2, 1]]
XCTAssertEqual(input.discard { x in x.contains(5) }, expected)
#expect(input.discard { x in x.contains(5) } == expected)
}
}
4 changes: 2 additions & 2 deletions exercises/practice/sublist/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Given any two lists `A` and `B`, determine if:
- None of the above is true, thus lists `A` and `B` are unequal

Specifically, list `A` is equal to list `B` if both lists have the same values in the same order.
List `A` is a superlist of `B` if `A` contains a sub-sequence of values equal to `B`.
List `A` is a sublist of `B` if `B` contains a sub-sequence of values equal to `A`.
List `A` is a superlist of `B` if `A` contains a contiguous sub-sequence of values equal to `B`.
List `A` is a sublist of `B` if `B` contains a contiguous sub-sequence of values equal to `A`.

Examples:

Expand Down
Loading
Loading