Created
May 3, 2023 01:14
-
-
Save smartwatermelon/50907e191ab0a499a638f8614cd5bf14 to your computer and use it in GitHub Desktop.
@cassidoo's interview question from April 30, 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 | |
removeZeroes () { | |
IFS=" " read -ra array <<< "$@" | |
declare -a ltrim=("") | |
declare -a rtrim=("") | |
tmp="" | |
length="${#array[@]}" | |
echo -e "begin:\t${array[@]}" | |
for (( j=0; j<length; j++ )); do | |
if [[ "${array[j]}" == '0' ]]; then | |
continue | |
else | |
tmp=${array[@]:j} | |
break | |
fi | |
done | |
if [ -z "$tmp" ]; then | |
echo -e "done:\t[]" | |
return | |
fi | |
IFS=" " read -ra ltrim <<< "$tmp" | |
unset tmp | |
length="${#ltrim[@]}" | |
for (( k=length-1; k>=0; k-- )); do | |
# echo "k is ${k}; ltrim[k] is ${ltrim[k]}" | |
if [[ "${ltrim[k]}" == '0' ]]; then | |
continue | |
else | |
tmp=${ltrim[@]::k+1} | |
break | |
fi | |
done | |
IFS=" " read -ra rtrim <<< "$tmp" | |
echo -e "done:\t${rtrim[@]}" | |
} | |
declare -a array1=( 0 0 0 3 1 4 1 5 9 0 0 0 0 ) | |
declare -a array2=( 0 0 0 ) | |
declare -a array3=( 8 ) | |
removeZeroes "${array1[@]}" | |
removeZeroes "${array2[@]}" | |
removeZeroes "${array3[@]}" |
Author
smartwatermelon
commented
May 3, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment