Created
July 3, 2023 18:34
-
-
Save smartwatermelon/dcebee4ecdb231af7e736602fc08a6c5 to your computer and use it in GitHub Desktop.
@cassidoo's interview question from July 2, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Author
smartwatermelon
commented
Jul 3, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment