From 5f659a4303e671581383240e09a23c0c754a742b Mon Sep 17 00:00:00 2001 From: meatball Date: Tue, 17 Dec 2024 22:20:13 +0100 Subject: [PATCH 1/3] Refactor Armstrong number functions to support Int128 and update test cases --- .../ArmstrongNumbersExample.swift | 2 +- .../armstrong-numbers/.meta/template.swift | 36 +++--- .../armstrong-numbers/.meta/tests.toml | 2 - .../practice/armstrong-numbers/Package.swift | 2 +- .../ArmstrongNumbers/ArmstrongNumbers.swift | 2 +- .../ArmstrongNumbersTests.swift | 104 ++++++++++-------- 6 files changed, 79 insertions(+), 69 deletions(-) diff --git a/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift b/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift index df0d8576a..4316e4857 100644 --- a/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift +++ b/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift @@ -1,6 +1,6 @@ import Foundation -func isArmstrongNumber(_ number: Int) -> Bool { +func isArmstrongNumber(_ number: Int128) -> Bool { let stringNumber = String(number) let numberLength = stringNumber.count let sum = stringNumber.compactMap { Int(String($0)) }.reduce(0) { $0 + Int(pow(Double($1), Double(numberLength))) } diff --git a/exercises/practice/armstrong-numbers/.meta/template.swift b/exercises/practice/armstrong-numbers/.meta/template.swift index 0ae784d07..200c862fb 100644 --- a/exercises/practice/armstrong-numbers/.meta/template.swift +++ b/exercises/practice/armstrong-numbers/.meta/template.swift @@ -1,20 +1,22 @@ -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 - {% for case in cases %} - {% if forloop.first -%} - func test{{case.description |camelCase }}() { - {% else -%} - func test{{case.description |camelCase }}() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - {% endif -%} - {%- if case.expected -%} - XCTAssertTrue(isArmstrongNumber({{case.input.number}})) - {%- else -%} - XCTAssertFalse(isArmstrongNumber({{case.input.number}})) - {%- endif %} - } - {% endfor -%} +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false + +{% for case in cases %} +{% if forloop.first -%} + @Test("{{case.description}}") + func test{{case.description |camelCase }}() { +{% else -%} + @Test("{{case.description}}", .enabled(if: RUNALL)) + func test{{case.description |camelCase }}() throws { +{% endif -%} + {%- if case.expected -%} + #expect(isArmstrongNumber({{case.input.number}})) + {%- else -%} + #expect(isArmstrongNumber({{case.input.number}})) + {%- endif %} } +{% endfor -%} + diff --git a/exercises/practice/armstrong-numbers/.meta/tests.toml b/exercises/practice/armstrong-numbers/.meta/tests.toml index b956bdfd4..b3f09e4cc 100644 --- a/exercises/practice/armstrong-numbers/.meta/tests.toml +++ b/exercises/practice/armstrong-numbers/.meta/tests.toml @@ -38,8 +38,6 @@ description = "Seven-digit number that is not an Armstrong number" [5ee2fdf8-334e-4a46-bb8d-e5c19c02c148] description = "Armstrong number containing seven zeroes" -include = false [12ffbf10-307a-434e-b4ad-c925680e1dd4] description = "The largest and last Armstrong number" -include = false diff --git a/exercises/practice/armstrong-numbers/Package.swift b/exercises/practice/armstrong-numbers/Package.swift index 9cc117728..bffadd5dd 100644 --- a/exercises/practice/armstrong-numbers/Package.swift +++ b/exercises/practice/armstrong-numbers/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.3 +// swift-tools-version:6.0 import PackageDescription diff --git a/exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift b/exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift index 63b122561..765387f1f 100644 --- a/exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift +++ b/exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift @@ -1,3 +1,3 @@ -func isArmstrongNumber(_ number: Int) -> Bool { +func isArmstrongNumber(_ number: Int128) -> Bool { // Write your code for the 'Armstrong Numbers' exercise here. } diff --git a/exercises/practice/armstrong-numbers/Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift b/exercises/practice/armstrong-numbers/Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift index e99683b94..eec3a62b4 100644 --- a/exercises/practice/armstrong-numbers/Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift +++ b/exercises/practice/armstrong-numbers/Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift @@ -1,51 +1,61 @@ -import XCTest +import Testing +import Foundation @testable import ArmstrongNumbers -class ArmstrongNumbersTests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false - - func testZeroIsAnArmstrongNumber() { - XCTAssertTrue(isArmstrongNumber(0)) - } - - func testSingleDigitNumbersAreArmstrongNumbers() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(isArmstrongNumber(5)) - } - - func testThereAreNoTwoDigitArmstrongNumbers() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isArmstrongNumber(10)) - } - - func testThreeDigitNumberThatIsAnArmstrongNumber() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(isArmstrongNumber(153)) - } - - func testThreeDigitNumberThatIsNotAnArmstrongNumber() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isArmstrongNumber(100)) - } - - func testFourDigitNumberThatIsAnArmstrongNumber() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(isArmstrongNumber(9474)) - } - - func testFourDigitNumberThatIsNotAnArmstrongNumber() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isArmstrongNumber(9475)) - } - - func testSevenDigitNumberThatIsAnArmstrongNumber() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertTrue(isArmstrongNumber(9_926_315)) - } - - func testSevenDigitNumberThatIsNotAnArmstrongNumber() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - XCTAssertFalse(isArmstrongNumber(9_926_314)) - } +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false + +@Test("Zero is an Armstrong number") +func testZeroIsAnArmstrongNumber() { + #expect(isArmstrongNumber(0)) +} + +@Test("Single-digit numbers are Armstrong numbers", .enabled(if: RUNALL)) +func testSingleDigitNumbersAreArmstrongNumbers() throws { + #expect(isArmstrongNumber(5)) +} + +@Test("There are no two-digit Armstrong numbers", .enabled(if: RUNALL)) +func testThereAreNoTwoDigitArmstrongNumbers() throws { + #expect(isArmstrongNumber(10)) +} + +@Test("Three-digit number that is an Armstrong number", .enabled(if: RUNALL)) +func testThreeDigitNumberThatIsAnArmstrongNumber() throws { + #expect(isArmstrongNumber(153)) +} + +@Test("Three-digit number that is not an Armstrong number", .enabled(if: RUNALL)) +func testThreeDigitNumberThatIsNotAnArmstrongNumber() throws { + #expect(isArmstrongNumber(100)) +} + +@Test("Four-digit number that is an Armstrong number", .enabled(if: RUNALL)) +func testFourDigitNumberThatIsAnArmstrongNumber() throws { + #expect(isArmstrongNumber(9474)) +} + +@Test("Four-digit number that is not an Armstrong number", .enabled(if: RUNALL)) +func testFourDigitNumberThatIsNotAnArmstrongNumber() throws { + #expect(isArmstrongNumber(9475)) +} + +@Test("Seven-digit number that is an Armstrong number", .enabled(if: RUNALL)) +func testSevenDigitNumberThatIsAnArmstrongNumber() throws { + #expect(isArmstrongNumber(9_926_315)) +} + +@Test("Seven-digit number that is not an Armstrong number", .enabled(if: RUNALL)) +func testSevenDigitNumberThatIsNotAnArmstrongNumber() throws { + #expect(isArmstrongNumber(9_926_314)) +} + +@Test("Armstrong number containing seven zeroes", .enabled(if: RUNALL)) +func testArmstrongNumberContainingSevenZeroes() throws { + #expect(isArmstrongNumber(186_709_961_001_538_790_100_634_132_976_990)) +} + +@Test("The largest and last Armstrong number", .enabled(if: RUNALL)) +func testTheLargestAndLastArmstrongNumber() throws { + #expect(isArmstrongNumber(115_132_219_018_763_992_565_095_597_973_971_522_401)) } From 8eff39b593b05e80ff1f17c1d4eef7b3055b90fc Mon Sep 17 00:00:00 2001 From: meatball Date: Tue, 17 Dec 2024 23:22:37 +0100 Subject: [PATCH 2/3] Revert new test cases --- .../ArmstrongNumbersExample.swift | 4 +- .../armstrong-numbers/.meta/template.swift | 30 ++--- .../armstrong-numbers/.meta/tests.toml | 2 + .../ArmstrongNumbersTests.swift | 105 ++++++++---------- 4 files changed, 68 insertions(+), 73 deletions(-) diff --git a/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift b/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift index 4316e4857..1637e6610 100644 --- a/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift +++ b/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift @@ -1,8 +1,8 @@ import Foundation -func isArmstrongNumber(_ number: Int128) -> Bool { +func isArmstrongNumber(_ number: Int) -> Bool { let stringNumber = String(number) let numberLength = stringNumber.count - let sum = stringNumber.compactMap { Int(String($0)) }.reduce(0) { $0 + Int(pow(Double($1), Double(numberLength))) } + let sum: Int = stringNumber.compactMap { Int(String($0)) }.reduce(0) { $0 + Int(pow(Double($1), Double(numberLength))) } return sum == number } diff --git a/exercises/practice/armstrong-numbers/.meta/template.swift b/exercises/practice/armstrong-numbers/.meta/template.swift index 200c862fb..18a21c771 100644 --- a/exercises/practice/armstrong-numbers/.meta/template.swift +++ b/exercises/practice/armstrong-numbers/.meta/template.swift @@ -4,19 +4,19 @@ import Foundation let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false -{% for case in cases %} -{% if forloop.first -%} - @Test("{{case.description}}") - func test{{case.description |camelCase }}() { -{% else -%} - @Test("{{case.description}}", .enabled(if: RUNALL)) - func test{{case.description |camelCase }}() throws { -{% endif -%} - {%- if case.expected -%} - #expect(isArmstrongNumber({{case.input.number}})) - {%- else -%} - #expect(isArmstrongNumber({{case.input.number}})) - {%- endif %} +@Suite struct {{exercise|camelCase}}Tests { + {% for case in cases %} + {% if forloop.first -%} + @Test("{{case.description}}") + {% else -%} + @Test("{{case.description}}", .enabled(if: RUNALL)) + {% endif -%} + func test{{case.description |camelCase }}() { + {%- if case.expected -%} + #expect(isArmstrongNumber({{case.input.number}})) + {%- else -%} + #expect(!isArmstrongNumber({{case.input.number}})) + {%- endif %} + } + {% endfor -%} } -{% endfor -%} - diff --git a/exercises/practice/armstrong-numbers/.meta/tests.toml b/exercises/practice/armstrong-numbers/.meta/tests.toml index b3f09e4cc..faded6487 100644 --- a/exercises/practice/armstrong-numbers/.meta/tests.toml +++ b/exercises/practice/armstrong-numbers/.meta/tests.toml @@ -38,6 +38,8 @@ description = "Seven-digit number that is not an Armstrong number" [5ee2fdf8-334e-4a46-bb8d-e5c19c02c148] description = "Armstrong number containing seven zeroes" +include = false [12ffbf10-307a-434e-b4ad-c925680e1dd4] description = "The largest and last Armstrong number" +include = false \ No newline at end of file diff --git a/exercises/practice/armstrong-numbers/Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift b/exercises/practice/armstrong-numbers/Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift index eec3a62b4..808c24850 100644 --- a/exercises/practice/armstrong-numbers/Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift +++ b/exercises/practice/armstrong-numbers/Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift @@ -1,61 +1,54 @@ -import Testing import Foundation +import Testing @testable import ArmstrongNumbers -let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false - -@Test("Zero is an Armstrong number") -func testZeroIsAnArmstrongNumber() { - #expect(isArmstrongNumber(0)) -} - -@Test("Single-digit numbers are Armstrong numbers", .enabled(if: RUNALL)) -func testSingleDigitNumbersAreArmstrongNumbers() throws { - #expect(isArmstrongNumber(5)) -} - -@Test("There are no two-digit Armstrong numbers", .enabled(if: RUNALL)) -func testThereAreNoTwoDigitArmstrongNumbers() throws { - #expect(isArmstrongNumber(10)) -} - -@Test("Three-digit number that is an Armstrong number", .enabled(if: RUNALL)) -func testThreeDigitNumberThatIsAnArmstrongNumber() throws { - #expect(isArmstrongNumber(153)) -} - -@Test("Three-digit number that is not an Armstrong number", .enabled(if: RUNALL)) -func testThreeDigitNumberThatIsNotAnArmstrongNumber() throws { - #expect(isArmstrongNumber(100)) -} - -@Test("Four-digit number that is an Armstrong number", .enabled(if: RUNALL)) -func testFourDigitNumberThatIsAnArmstrongNumber() throws { - #expect(isArmstrongNumber(9474)) -} - -@Test("Four-digit number that is not an Armstrong number", .enabled(if: RUNALL)) -func testFourDigitNumberThatIsNotAnArmstrongNumber() throws { - #expect(isArmstrongNumber(9475)) -} - -@Test("Seven-digit number that is an Armstrong number", .enabled(if: RUNALL)) -func testSevenDigitNumberThatIsAnArmstrongNumber() throws { - #expect(isArmstrongNumber(9_926_315)) -} - -@Test("Seven-digit number that is not an Armstrong number", .enabled(if: RUNALL)) -func testSevenDigitNumberThatIsNotAnArmstrongNumber() throws { - #expect(isArmstrongNumber(9_926_314)) -} - -@Test("Armstrong number containing seven zeroes", .enabled(if: RUNALL)) -func testArmstrongNumberContainingSevenZeroes() throws { - #expect(isArmstrongNumber(186_709_961_001_538_790_100_634_132_976_990)) -} - -@Test("The largest and last Armstrong number", .enabled(if: RUNALL)) -func testTheLargestAndLastArmstrongNumber() throws { - #expect(isArmstrongNumber(115_132_219_018_763_992_565_095_597_973_971_522_401)) +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "true"]) ?? false + +@Suite struct ArmstrongNumbersTests { + + @Test("Zero is an Armstrong number") + func testZeroIsAnArmstrongNumber() { + #expect(isArmstrongNumber(0)) + } + + @Test("Single-digit numbers are Armstrong numbers", .enabled(if: RUNALL)) + func testSingleDigitNumbersAreArmstrongNumbers() { + #expect(isArmstrongNumber(5)) + } + + @Test("There are no two-digit Armstrong numbers", .enabled(if: RUNALL)) + func testThereAreNoTwoDigitArmstrongNumbers() { + #expect(!isArmstrongNumber(10)) + } + + @Test("Three-digit number that is an Armstrong number", .enabled(if: RUNALL)) + func testThreeDigitNumberThatIsAnArmstrongNumber() { + #expect(isArmstrongNumber(153)) + } + + @Test("Three-digit number that is not an Armstrong number", .enabled(if: RUNALL)) + func testThreeDigitNumberThatIsNotAnArmstrongNumber() { + #expect(!isArmstrongNumber(100)) + } + + @Test("Four-digit number that is an Armstrong number", .enabled(if: RUNALL)) + func testFourDigitNumberThatIsAnArmstrongNumber() { + #expect(isArmstrongNumber(9474)) + } + + @Test("Four-digit number that is not an Armstrong number", .enabled(if: RUNALL)) + func testFourDigitNumberThatIsNotAnArmstrongNumber() { + #expect(!isArmstrongNumber(9475)) + } + + @Test("Seven-digit number that is an Armstrong number", .enabled(if: RUNALL)) + func testSevenDigitNumberThatIsAnArmstrongNumber() { + #expect(isArmstrongNumber(9_926_315)) + } + + @Test("Seven-digit number that is not an Armstrong number", .enabled(if: RUNALL)) + func testSevenDigitNumberThatIsNotAnArmstrongNumber() { + #expect(!isArmstrongNumber(9_926_314)) + } } From 750fc04c53d20ac82b903281498ea39a53c49607 Mon Sep 17 00:00:00 2001 From: meatball Date: Sun, 22 Dec 2024 22:55:27 +0100 Subject: [PATCH 3/3] Rollback changes around int128 --- .../Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift | 2 +- .../Sources/ArmstrongNumbers/ArmstrongNumbers.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift b/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift index 1637e6610..df0d8576a 100644 --- a/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift +++ b/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift @@ -3,6 +3,6 @@ import Foundation func isArmstrongNumber(_ number: Int) -> Bool { let stringNumber = String(number) let numberLength = stringNumber.count - let sum: Int = stringNumber.compactMap { Int(String($0)) }.reduce(0) { $0 + Int(pow(Double($1), Double(numberLength))) } + let sum = stringNumber.compactMap { Int(String($0)) }.reduce(0) { $0 + Int(pow(Double($1), Double(numberLength))) } return sum == number } diff --git a/exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift b/exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift index 765387f1f..63b122561 100644 --- a/exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift +++ b/exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift @@ -1,3 +1,3 @@ -func isArmstrongNumber(_ number: Int128) -> Bool { +func isArmstrongNumber(_ number: Int) -> Bool { // Write your code for the 'Armstrong Numbers' exercise here. }