Created
June 16, 2023 15:52
-
-
Save smartwatermelon/55baddb7fa2af242179a8b48bca6fcae to your computer and use it in GitHub Desktop.
@cassidoo's interview question from June 11, 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 | |
trimArray () { | |
arrayName=$1[@] | |
n=$2 | |
m=$3 | |
array=("${!arrayName}") | |
echo -e "begin:\t[${array[@]}], $n, $m" | |
# left trim | |
array=("${array[@]:$n}") | |
# right trim | |
array=("${array[@]:0:${#array[@]} - $m}" ) | |
echo -e "done:\t[${array[@]}], $n, $m" | |
} | |
declare -a array1=( 1 2 3 4 5 6 ) | |
declare -a array2=( 6 2 4 3 7 1 3 ) | |
declare -a array3=( 1 7 ) | |
trimArray array1 2 1 # [3, 4, 5] | |
trimArray array2 5 0 # [1, 3] | |
trimArray array3 0 0 # [1, 7] |
Author
smartwatermelon
commented
Jun 16, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment