Skip to content

Instantly share code, notes, and snippets.

@smartwatermelon
Created May 3, 2023 01:14
Show Gist options
  • Save smartwatermelon/50907e191ab0a499a638f8614cd5bf14 to your computer and use it in GitHub Desktop.
Save smartwatermelon/50907e191ab0a499a638f8614cd5bf14 to your computer and use it in GitHub Desktop.
@cassidoo's interview question from April 30, 2023
#!/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[@]}"
@smartwatermelon
Copy link
Author

begin:	0 0 0 3 1 4 1 5 9 0 0 0 0
done:	3 1 4 1 5 9
begin:	0 0 0
done:	[]
begin:	8
done:	8

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