Avatar of binaryphile

binaryphile's solution

to Armstrong Numbers in the Bash Track

Published at Sep 22 2018 · 0 comments
Instructions
Test suite
Solution

An 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.

Run the tests with:

bats armstrong_numbers_test.sh

After the first test(s) pass, continue by commenting out or removing the skip annotations prepending other tests.

Source

Wikipedia https://en.wikipedia.org/wiki/Narcissistic_number

External utilities

Bash is a language to write scripts that works closely with various system utilities, like sed, awk, date and even other programming languages, like Python. This track does not restrict the usage of these utilities, and as long as your solution is portable between systems and does not require installing 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.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

armstrong_numbers_test.sh

#!/usr/bin/env bash

@test 'Single digits are Armstrong numbers' {
  # skip
  run bash armstrong_numbers.sh 5

  [ "$status" -eq 0 ]
  [ "$output" = "true" ]
}

@test 'There are no two digit Armstrong numbers' {
  skip
  run bash armstrong_numbers.sh 10

  [ "$status" -eq 0 ]
  [ "$output" = "false" ]
}

@test 'A three digit number that is an Armstrong number' {
  skip
  run bash armstrong_numbers.sh 153

  [ "$status" -eq 0 ]
  [ "$output" = "true" ]
}

@test 'A three digit number that is not an Armstrong number' {
  skip
  run bash armstrong_numbers.sh 100

  [ "$status" -eq 0 ]
  [ "$output" = "false" ]
}

@test 'A four digit number that is an Armstrong number' {
  skip
  run bash armstrong_numbers.sh 9474

  [ "$status" -eq 0 ]
  [ "$output" = "true" ]
}

@test 'A four digit number that is not an Armstrong number' {
  skip
  run bash armstrong_numbers.sh 9475

  [ "$status" -eq 0 ]
  [ "$output" = "false" ]
}

@test 'A seven digit number that is an Armstrong number' {
  skip
  run bash armstrong_numbers.sh 9926315

  [ "$status" -eq 0 ]
  [ "$output" = "true" ]
}

@test 'A seven digit number that is not an Armstrong number' {
  skip
  run bash armstrong_numbers.sh 9926314

  [ "$status" -eq 0 ]
  [ "$output" = "false" ]
}
#!/usr/bin/env bash

# http://www.binaryphile.com/bash/2018/07/26/approach-bash-like-a-developer-part-1-intro.html

IFS=$'\n'
set -o noglob
shopt -s expand_aliases
alias args?='(( $# ))'
alias args_include?='include? "$*"'
alias fewer_args_than?='fewer_than? $#'

get () {
  local ref_=$1 indent_

  ! read -rd '' $ref_
  indent_=${!ref_%%[^[:space:]]*}
  printf -v $ref_ %s "${!ref_#$indent_}"
  printf -v $ref_ %s "${!ref_//$'\n'$indent_/$'\n'}"
}

Prog=${0##*/}

get Usage <<END
  $Prog:

    Determine whether a number is an Armstrong number

  Usage:

    $Prog NUMBER

  An Armstrong number is equal to the sum of each of its digits
  raised to the power equal to the total number of digits.
END

main () {
  armstrong? $1 && echo true || echo false
}

armstrong? () {
  local length=${#1}

  power () {
    echo $(( $1**length ))
  }

  [[ $1 == "$(split $1 | map power | reduce +)" ]]
}

die () {
  local rc=$?

  present? ${2:-}   && rc=$2
  present? "${1:-}" && echo "$1"
  exit $rc
}

fewer_than? () {
  (( $1 < $2 ))
}

include? () {
  [[ $IFS$1$IFS == *"$IFS$2$IFS"* ]]
}

integer? () {
  printf %d $1 &>/dev/null
}

map () {
  local item

  while read -r item; do
    $1 $item
  done
}

present? () {
  [[ -n ${1:-} ]]
}

reduce () {
  local -i item result

  read -r result
  while read -r item; do
    result=$result$1$item
  done
  echo $result
}

split () {
  local i

  for (( i = 0; i < ${#1}; i++ )); do
    echo ${1:i:1}
  done
}

sourced? () {
  [[ ${FUNCNAME[1]} == source ]]
}

strict_mode () {
  case $1 in
    on  ) set -euo pipefail;;
    off ) set +euo pipefail;;
  esac
}

usage () {
  local rc=0

  present? ${1:-} && {
    echo "$1$IFS"
    rc=2
  }
  die "$Usage" $rc
}

sourced? && return
strict_mode on

args_include? --help  && usage
args?                 || usage "Error: argument required"
fewer_args_than? 2    || usage "Error: wrong number of arguments"
integer? $1           || usage "Error: argument must be an integer"

main $*

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?