Skip to content

Commit

Permalink
Add armstrong-numbers (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Oct 31, 2024
1 parent ab2357c commit 1b70d59
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@
"map"
]
},
{
"slug": "armstrong-numbers",
"name": "Armstrong Numbers",
"uuid": "a78ccf72-e16d-441f-b2b0-6374e69a77c4",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "legacy-service-client",
"name": "Legacy Service Client",
Expand Down
14 changes: 14 additions & 0 deletions exercises/practice/armstrong-numbers/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions exercises/practice/armstrong-numbers/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"armstrong_numbers.bal"
],
"test": [
"tests/armstrong_numbers_test.bal"
],
"example": [
".meta/reference/armstrong_numbers.bal"
]
},
"blurb": "Determine if a number is an Armstrong number.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Narcissistic_number"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public function isArmstrongNumber(int number) returns boolean {
int sum = 0;
int working = number;
float length = <float> number.toString().length();
while (working > 0) {
float digit = <float> working % 10;
sum += <int> float:pow(digit, length);
working /= 10;
}
return sum == number;
}
45 changes: 45 additions & 0 deletions exercises/practice/armstrong-numbers/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions exercises/practice/armstrong-numbers/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "ballerina_exercism"
name = "armstrong_numbers"
version = "0.1.0"
distribution = "2201.5.0"
38 changes: 38 additions & 0 deletions exercises/practice/armstrong-numbers/Dependencies.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# AUTO-GENERATED FILE. DO NOT MODIFY.

# This file is auto-generated by Ballerina for managing dependency versions.
# It should not be modified by hand.

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.5.0"

[[package]]
org = "ballerina"
name = "jballerina.java"
version = "0.0.0"
scope = "testOnly"

[[package]]
org = "ballerina"
name = "test"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "test", moduleName = "test"}
]

[[package]]
org = "ballerina_exercism"
name = "armstrong_numbers"
version = "0.1.0"
dependencies = [
{org = "ballerina", name = "test"}
]
modules = [
{org = "ballerina_exercism", packageName = "armstrong_numbers", moduleName = "armstrong_numbers"}
]

3 changes: 3 additions & 0 deletions exercises/practice/armstrong-numbers/armstrong_numbers.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public function isArmstrongNumber(int number) returns boolean {
// TODO: implement this function
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

import ballerina/test;

@test:Config
function testZeroIsArmstrongNumber() {
test:assertTrue(isArmstrongNumber(0));
}

@test:Config {
enable: false
}
function testSingleDigitIsArmstrongNumber() {
test:assertTrue(isArmstrongNumber(5));
}

@test:Config {
enable: false
}
function testTwoDigitIsNotArmstrongNumber() {
test:assertFalse(isArmstrongNumber(10));
}

@test:Config {
enable: false
}
function testThreeDigitIsArmstrongNumber() {
test:assertTrue(isArmstrongNumber(153));
}

@test:Config {
enable: false
}
function testThreeDigitIsNotArmstrongNumber() {
test:assertFalse(isArmstrongNumber(100));
}

@test:Config {
enable: false
}
function testFourDigitIsArmstrongNumber() {
test:assertTrue(isArmstrongNumber(9474));
}

@test:Config {
enable: false
}
function testFourDigitIsNotArmstrongNumber() {
test:assertFalse(isArmstrongNumber(9475));
}

@test:Config {
enable: false
}
function testSevenDigitIsArmstrongNumber() {
test:assertTrue(isArmstrongNumber(9926315));
}

@test:Config {
enable: false
}
function testSevenDigitIsNotArmstrongNumber() {
test:assertFalse(isArmstrongNumber(9926314));
}

0 comments on commit 1b70d59

Please sign in to comment.