Skip to content

Instantly share code, notes, and snippets.

@smartwatermelon
Created June 14, 2021 20:33
Show Gist options
  • Save smartwatermelon/b09353b95feb6cfc0b5f52a12a237777 to your computer and use it in GitHub Desktop.
Save smartwatermelon/b09353b95feb6cfc0b5f52a12a237777 to your computer and use it in GitHub Desktop.
@cassidoo's Interview question of the week for June 13, 2021
#!/usr/bin/env bash
set -eu -o pipefail
# Given a direction and a number of columns, write a function that outputs
# an arrow of asterisks
if [ $# -ne 2 ] ||
! ( ( [ $( echo $1 | tr [':upper:'] [':lower:'] ) == 'left' ] || [ $( echo $1 | tr [':upper:'] [':lower:'] ) == 'right' ] ) && [ -z "$(printf '%s\n' "$2" | sed 's/[0-9]//g')" ] ); then
echo "$(basename $0): enter 'left' or 'right' and a positive integer, e.g. 'left 4' or 'right 3'"
exit 1
fi
DIRECTION=$1
NUMBER=$2
function out () {
for I in $( seq 1 $NUMBER ); do
echo "$( printf '%*s' $(( $I-1 )) )*"
done
}
function back () {
for J in $( seq $(( $NUMBER-1 )) 1 ); do
echo "$( printf '%*s' $(( $J-1 )) )*"
done
}
case $NUMBER in
0)
;;
1)
echo "*"
;;
*)
case $DIRECTION in
'left')
back; out
;;
'right')
out; back
;;
*)
;;
esac
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment