Skip to content

Instantly share code, notes, and snippets.

@smartwatermelon
Last active August 19, 2023 23:02
Show Gist options
  • Save smartwatermelon/9b1a2a445c2a7e5df236f5b428f33ded to your computer and use it in GitHub Desktop.
Save smartwatermelon/9b1a2a445c2a7e5df236f5b428f33ded to your computer and use it in GitHub Desktop.
@cassidoo's interview question from August 13, 2023
#!/usr/bin/env bash
set -eu -o pipefail
isVowel(){
CHAR=${1:0:1}
if [[ "$CHAR" == *[AaEeIiOoUu]* ]]; then
echo 0
else
echo 1
fi
}
# check input
ARGC="$#"
if [[ "$ARGC" -lt 1 ]]; then
echo "Please enter a string"
exit 1
fi
# grab entire arg group as a single string
STR="${*}"
OUT=""
# iterate over string
for (( i=0; i<${#STR}; i++ )); do
CHAR="${STR:$i:1}"
if [ $( isVowel "${CHAR}" ) -eq 0 ]; then
if (( i != 0 )); then
# vowel is not first char
# reverse string up to position i
OUT="$( rev <<< "${OUT}")"
fi
else
# char is not vowel
# concat char onto output string
OUT="${OUT}${STR:$i:1}"
fi
done
echo "${OUT}"
@smartwatermelon
Copy link
Author

MONTASIO:cassidoo andrewrich$ ./faultyKeeb.sh string
rtsng
MONTASIO:cassidoo andrewrich$ ./faultyKeeb.sh hello world!
w hllrld!
MONTASIO:cassidoo andrewrich$ ./faultyKeeb.sh cassidoo
sscd

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