Skip to content

Instantly share code, notes, and snippets.

@smartwatermelon
Created July 3, 2023 18:34
Show Gist options
  • Save smartwatermelon/dcebee4ecdb231af7e736602fc08a6c5 to your computer and use it in GitHub Desktop.
Save smartwatermelon/dcebee4ecdb231af7e736602fc08a6c5 to your computer and use it in GitHub Desktop.
@cassidoo's interview question from July 2, 2023
#!/usr/bin/env bash
set -eu -o pipefail
isPerfect() {
NUM=$1
SQRT=$( bc -l <<< "sqrt ($NUM)" )
INT_SQRT=$( cut -d '.' -f 1 <<< "$SQRT" )
SQ_INT_SQRT=$( bc -l <<< "$INT_SQRT ^ 2" )
test $NUM -eq $SQ_INT_SQRT; echo $?
}
# check input
ARGC="$#"
if [[ "$ARGC" -ne 1 ]]; then
echo "Please enter an integer number, e.g. 123"
exit 1
fi
NUM="$1"
re='^[0-9]+$'
if ! [[ $NUM =~ $re ]] ; then
echo "error: Not a number" >&2; exit 1
fi
if [ $( isPerfect $NUM ) -eq 0 ] && [ $( isPerfect $( rev <<< $NUM ) ) -eq 0 ]; then
echo "$NUM is Perfectly Perfect!"
else
echo "$NUM is not Perfectly Perfect."
fi
@smartwatermelon
Copy link
Author

MONTASIO:cassidoo andrewrich$ ./reversedSquares.sh 9
9 is Perfectly Perfect!
MONTASIO:cassidoo andrewrich$ ./reversedSquares.sh 441
441 is Perfectly Perfect!
MONTASIO:cassidoo andrewrich$ ./reversedSquares.sh 25
25 is not Perfectly Perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment