Skip to content

Instantly share code, notes, and snippets.

@smartwatermelon
Created December 6, 2021 23:12
Show Gist options
  • Save smartwatermelon/892fd09e8e2a907827edc7ab647ff2ac to your computer and use it in GitHub Desktop.
Save smartwatermelon/892fd09e8e2a907827edc7ab647ff2ac to your computer and use it in GitHub Desktop.
@cassidoo's interview question from December 5, 2021
#!/usr/bin/env bash
set -eu -o pipefail
# You have to order wrapping paper for presents. Given the length, width, and height of
# the boxes you need to wrap, return the number of square feet (or whatever units you
# want) of wrapping paper you need to order. Extra credit: allow for other shapes of
# presents and their dimensions!
if [ $# -ne 3 ]; then
echo "$(basename $0): enter three nonzero positive integers (length, width, depth)"
exit 1
fi
for arg in "$@"; do
if ! [[ $arg =~ ^[1-9]+$ ]]; then
echo "$(basename $0): not a nonzero positive integer: $arg"
exit 1
fi
done
SURFACE=$(( ( 2 * $1 * $2 ) + ( 2 * $1 * $3 ) + ( 2 * $2 * $3 ) ))
echo "You'll need at least $SURFACE square units of wrapping paper"
@smartwatermelon
Copy link
Author

MONTASIO:scripts andrewrich$ ./wrap.sh 
wrap.sh: enter three nonzero positive integers (length, width, depth)
MONTASIO:scripts andrewrich$ ./wrap.sh 5 6 7
You'll need at least 214 square units of wrapping paper
MONTASIO:scripts andrewrich$ ./wrap.sh 2 4 6
You'll need at least 88 square units of wrapping paper
MONTASIO:scripts andrewrich$ ./wrap.sh f 5 h
wrap.sh: not a nonzero positive integer: f

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