From 53fb95224422df596fc95c32b93875c2f05a8c81 Mon Sep 17 00:00:00 2001 From: Carl Date: Sun, 19 Nov 2023 11:19:28 +0100 Subject: [PATCH 1/2] Add Armstrong Numbers exercise --- config.json | 8 +++ .../armstrong-numbers/.docs/instructions.md | 14 +++++ .../ArmstrongNumbersExample.swift | 8 +++ .../armstrong-numbers/.meta/config.json | 17 +++++++ .../armstrong-numbers/.meta/template.swift | 20 ++++++++ .../armstrong-numbers/.meta/tests.toml | 45 ++++++++++++++++ .../practice/armstrong-numbers/Package.swift | 21 ++++++++ .../ArmstrongNumbers/ArmstrongNumbers.swift | 3 ++ .../ArmstrongNumbersTests.swift | 51 +++++++++++++++++++ 9 files changed, 187 insertions(+) create mode 100644 exercises/practice/armstrong-numbers/.docs/instructions.md create mode 100644 exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift create mode 100644 exercises/practice/armstrong-numbers/.meta/config.json create mode 100644 exercises/practice/armstrong-numbers/.meta/template.swift create mode 100644 exercises/practice/armstrong-numbers/.meta/tests.toml create mode 100644 exercises/practice/armstrong-numbers/Package.swift create mode 100644 exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift create mode 100644 exercises/practice/armstrong-numbers/Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift diff --git a/config.json b/config.json index 9566e9ab0..37451a125 100644 --- a/config.json +++ b/config.json @@ -325,6 +325,14 @@ "difficulty": 2, "topics": ["maps", "transforming"] }, + { + "slug": "armstrong-numbers", + "name": "Armstrong Numbers", + "uuid": "6bd76515-cc17-4238-bc08-7fbdfea14131", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "grade-school", "name": "Grade School", diff --git a/exercises/practice/armstrong-numbers/.docs/instructions.md b/exercises/practice/armstrong-numbers/.docs/instructions.md new file mode 100644 index 000000000..5e56bbe46 --- /dev/null +++ b/exercises/practice/armstrong-numbers/.docs/instructions.md @@ -0,0 +1,14 @@ +# Instructions + +An [Armstrong number][armstrong-number] is a number that is the sum of its own digits each raised to the power of the number of digits. + +For example: + +- 9 is an Armstrong number, because `9 = 9^1 = 9` +- 10 is _not_ an Armstrong number, because `10 != 1^2 + 0^2 = 1` +- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153` +- 154 is _not_ an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190` + +Write some code to determine whether a number is an Armstrong number. + +[armstrong-number]: https://en.wikipedia.org/wiki/Narcissistic_number diff --git a/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift b/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift new file mode 100644 index 000000000..df0d8576a --- /dev/null +++ b/exercises/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift @@ -0,0 +1,8 @@ +import Foundation + +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))) } + return sum == number +} diff --git a/exercises/practice/armstrong-numbers/.meta/config.json b/exercises/practice/armstrong-numbers/.meta/config.json new file mode 100644 index 000000000..5a8ec9e1f --- /dev/null +++ b/exercises/practice/armstrong-numbers/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": [], + "files": { + "solution": [ + "Sources/ArmstrongNumbers/ArmstrongNumbers.swift" + ], + "test": [ + "Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift" + ], + "example": [ + ".meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift" + ] + }, + "blurb": "Determine if a number is an Armstrong number.", + "source": "Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/Narcissistic_number" +} diff --git a/exercises/practice/armstrong-numbers/.meta/template.swift b/exercises/practice/armstrong-numbers/.meta/template.swift new file mode 100644 index 000000000..0ae784d07 --- /dev/null +++ b/exercises/practice/armstrong-numbers/.meta/template.swift @@ -0,0 +1,20 @@ +import XCTest +@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 -%} +} diff --git a/exercises/practice/armstrong-numbers/.meta/tests.toml b/exercises/practice/armstrong-numbers/.meta/tests.toml new file mode 100644 index 000000000..b956bdfd4 --- /dev/null +++ b/exercises/practice/armstrong-numbers/.meta/tests.toml @@ -0,0 +1,45 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[c1ed103c-258d-45b2-be73-d8c6d9580c7b] +description = "Zero is an Armstrong number" + +[579e8f03-9659-4b85-a1a2-d64350f6b17a] +description = "Single-digit numbers are Armstrong numbers" + +[2d6db9dc-5bf8-4976-a90b-b2c2b9feba60] +description = "There are no two-digit Armstrong numbers" + +[509c087f-e327-4113-a7d2-26a4e9d18283] +description = "Three-digit number that is an Armstrong number" + +[7154547d-c2ce-468d-b214-4cb953b870cf] +description = "Three-digit number that is not an Armstrong number" + +[6bac5b7b-42e9-4ecb-a8b0-4832229aa103] +description = "Four-digit number that is an Armstrong number" + +[eed4b331-af80-45b5-a80b-19c9ea444b2e] +description = "Four-digit number that is not an Armstrong number" + +[f971ced7-8d68-4758-aea1-d4194900b864] +description = "Seven-digit number that is an Armstrong number" + +[7ee45d52-5d35-4fbd-b6f1-5c8cd8a67f18] +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 new file mode 100644 index 000000000..9cc117728 --- /dev/null +++ b/exercises/practice/armstrong-numbers/Package.swift @@ -0,0 +1,21 @@ +// swift-tools-version:5.3 + +import PackageDescription + +let package = Package( + name: "ArmstrongNumbers", + products: [ + .library( + name: "ArmstrongNumbers", + targets: ["ArmstrongNumbers"]) + ], + dependencies: [], + targets: [ + .target( + name: "ArmstrongNumbers", + dependencies: []), + .testTarget( + name: "ArmstrongNumbersTests", + dependencies: ["ArmstrongNumbers"]), + ] +) diff --git a/exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift b/exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift new file mode 100644 index 000000000..63b122561 --- /dev/null +++ b/exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift @@ -0,0 +1,3 @@ +func isArmstrongNumber(_ number: Int) -> 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 new file mode 100644 index 000000000..e99683b94 --- /dev/null +++ b/exercises/practice/armstrong-numbers/Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift @@ -0,0 +1,51 @@ +import XCTest + +@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)) + } +} From 95ab180d4fc04a9903f411b60be774bee54e7956 Mon Sep 17 00:00:00 2001 From: Carl Date: Sun, 19 Nov 2023 11:20:08 +0100 Subject: [PATCH 2/2] Add author --- exercises/practice/armstrong-numbers/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/armstrong-numbers/.meta/config.json b/exercises/practice/armstrong-numbers/.meta/config.json index 5a8ec9e1f..e90322e97 100644 --- a/exercises/practice/armstrong-numbers/.meta/config.json +++ b/exercises/practice/armstrong-numbers/.meta/config.json @@ -1,5 +1,5 @@ { - "authors": [], + "authors": ["meatball133"], "files": { "solution": [ "Sources/ArmstrongNumbers/ArmstrongNumbers.swift"