Skip to content

Instantly share code, notes, and snippets.

@smartwatermelon
Created June 7, 2021 16:55
Show Gist options
  • Save smartwatermelon/8bf601290192ebd1338c6cb1f1f9bc0b to your computer and use it in GitHub Desktop.
Save smartwatermelon/8bf601290192ebd1338c6cb1f1f9bc0b to your computer and use it in GitHub Desktop.
@cassidoo's Interview question of the week for June 6, 2021
#!/usr/bin/env bash
set -eu -o pipefail
# lonelyNumber.sh
# @cassidoo 2021-06-06
# Given three numbers, return their product. But, if one of the numbers is the same as
# another, it does not count: If two numbers are similar, return the lonely number. If all
# numbers are same, return 1.
if [ $# -ne 3 ] || ! ([[ $1 =~ ^[0-9]+$ ]] && [[ $2 =~ ^[0-9]+$ ]] && [[ $3 =~ ^[0-9]+$ ]]); then
echo "$(basename $0): enter three postive integers"
exit 1
fi
PRODUCT=$(($1*$2*$3))
if [ $(($1**3)) -eq $PRODUCT ]; then
echo 1
elif [ $1 -eq $2 ]; then
echo $3
elif [ $1 -eq $3 ]; then
echo $2
elif [ $2 -eq $3 ]; then
echo $1
else
echo $PRODUCT
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment