Last active
August 19, 2023 23:02
-
-
Save smartwatermelon/9b1a2a445c2a7e5df236f5b428f33ded to your computer and use it in GitHub Desktop.
@cassidoo's interview question from August 13, 2023
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 | |
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}" |
Author
smartwatermelon
commented
Aug 19, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment