initial commit
This commit is contained in:
58
bash/armstrong-numbers/README.md
Normal file
58
bash/armstrong-numbers/README.md
Normal file
@@ -0,0 +1,58 @@
|
||||
# Armstrong Numbers
|
||||
|
||||
An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_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.
|
||||
|
||||
|
||||
Run the tests with:
|
||||
|
||||
```bash
|
||||
bats armstrong_numbers_test.sh
|
||||
```
|
||||
|
||||
After the first test(s) pass, continue by commenting out or removing the
|
||||
`[[ $BATS_RUN_SKIPPED == true ]] || skip`
|
||||
annotations prepending other tests.
|
||||
|
||||
To run all tests, including the ones with `skip` annotations, run:
|
||||
|
||||
```bash
|
||||
BATS_RUN_SKIPPED=true bats armstrong_numbers_test.sh
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number)
|
||||
|
||||
|
||||
## External utilities
|
||||
`Bash` is a language to write "scripts" -- programs that can call
|
||||
external tools, such as
|
||||
[`sed`](https://www.gnu.org/software/sed/),
|
||||
[`awk`](https://www.gnu.org/software/gawk/),
|
||||
[`date`](https://www.gnu.org/software/coreutils/manual/html_node/date-invocation.html)
|
||||
and even programs written in other programming languages,
|
||||
like [`Python`](https://www.python.org/).
|
||||
This track does not restrict the usage of these utilities, and as long
|
||||
as your solution is portable between systems and does not require
|
||||
installation of third party applications, feel free to use them to solve
|
||||
the exercise.
|
||||
|
||||
For an extra challenge, if you would like to have a better understanding
|
||||
of the language, try to re-implement the solution in pure `Bash`,
|
||||
without using any external tools. Note that there are some types of
|
||||
problems that bash cannot solve, such as performing floating point
|
||||
arithmetic and manipulating dates: for those, you must call out to an
|
||||
external tool.
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others
|
||||
have completed the exercise.
|
44
bash/armstrong-numbers/armstrong_numbers.sh
Executable file
44
bash/armstrong-numbers/armstrong_numbers.sh
Executable file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# set to mask to enable logs. 0: none, 255: all
|
||||
(( DEBUG=2#00000000 ))
|
||||
|
||||
# $1: log level (mask), then strings to display.
|
||||
debug () {
|
||||
(( DEBUG & $1 )) && shift && echo "${@}" >&2
|
||||
}
|
||||
|
||||
usage () {
|
||||
echo "usage: ./armstrong_numbers.sh <number>" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# basic args checks: 1 arg, digits (at least 1) only
|
||||
(( $# != 1)) || [[ ! "$1" =~ ^[[:digit:]]+$ ]] && usage
|
||||
|
||||
number="$1"
|
||||
digits=${#number}
|
||||
armstrong=0
|
||||
result="false"
|
||||
|
||||
debug 1 d=$digits n=$number
|
||||
|
||||
# armstrong number calculation
|
||||
for (( i=0; i<digits; ++i )); do
|
||||
((armstrong += ${number:$i:1} ** digits ))
|
||||
done
|
||||
|
||||
debug 2 "armstrong=$armstrong"
|
||||
|
||||
((number == armstrong)) && result="true"
|
||||
|
||||
echo "$result"
|
||||
|
||||
exit 0
|
||||
|
||||
# Indent style for emacs
|
||||
# Local Variables:
|
||||
# sh-basic-offset: 4
|
||||
# indent-tabs-mode: nil
|
||||
# comment-column: 60
|
||||
# End:
|
Reference in New Issue
Block a user