Created
October 17, 2022 18:31
-
-
Save smartwatermelon/346bb41ff57da6050e7867670279ba99 to your computer and use it in GitHub Desktop.
@cassidoo's interview question from October 17, 2022
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 | |
# initialize vars | |
allDoors=() # array | |
# check input | |
ARGC="$#" | |
if [[ "$ARGC" -ne 2 ]]; then | |
echo "Please enter two parameters: number of doors, and number of passes." | |
echo "Example: $(basename $0) 7 3" | |
exit 1 | |
fi | |
for P in $@; do | |
re='^[0-9]+$' | |
if ! [[ $P =~ $re ]] ; then | |
echo "error: $P is not a number" >&2; exit 1 | |
fi | |
done | |
DOORS=$1 PASSES=$2 | |
# all doors start off closed | |
for door in $( seq 0 $(($DOORS -1)) ); do | |
allDoors+=("1") | |
done | |
echo "All doors start out closed: ${allDoors[@]}" | |
# iterate over doors for each pass | |
for pass in $( seq 1 $PASSES ); do | |
for (( door=$(($pass-1)); door<$DOORS; door+=$pass)); do | |
case ${allDoors[$door]} in | |
'0') | |
allDoors[$door]="1" | |
;; | |
'1') | |
allDoors[$door]="0" | |
;; | |
*) echo "wat"; exit 1;; | |
esac | |
done | |
echo "After pass $pass: ${allDoors[@]}" | |
done |
Author
smartwatermelon
commented
Oct 17, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment