diff --git a/ar.bash b/ar.bash index c1d2fd5..5b40375 100644 --- a/ar.bash +++ b/ar.bash @@ -303,13 +303,13 @@ ar::reverse() { ## @param arr The array to setify ## @param arr_set The array to write the result into ar::set() { - local -n arr="$1" - local -n arr_set="$2" - local -A assoc - for val in "${arr[@]}"; do - assoc[$val]=1 + local -n _set_arr="$1" + local -n _set_arr_set="$2" + local -A _set_assoc + for val in "${_set_arr[@]}"; do + _set_assoc[$val]=1 done - arr_set=("${!assoc[@]}") + _set_arr_set=("${!_set_assoc[@]}") } ## @fn ar::in_set() @@ -501,7 +501,7 @@ ar::is_proper_subset() { } ## @fn ar::is_proper_superset -## @brief Return true if _proper_superset_arr1 is a superset of _proper_superset_arr2 +## @brief Return 0 if _proper_superset_arr1 is a superset of _proper_superset_arr2 ## @param _proper_superset_arr1 The first array name ## @param _proper_superset_arr2 The second array name ## @retval 0 if _proper_superset_arr1 is a superset of _proper_superset_arr2, non-zero otherwise @@ -519,6 +519,23 @@ ar::is_proper_superset() { return 0 } +## @fn ar::is_disjoint +## @brief Return 0 if _disjoint_arr1 is a superset of _disjoint_arr2 +## @param _disjoint_arr1 The first array name +## @param _disjoint_arr2 The second array name +## @retval 0 if _disjoint_arr1 and _disjoint_arr2 are disjoint sets, non-zero otherwise +ar::is_disjoint() { + local -n _disjoint_arr1="$1" + local -n _disjoint_arr2="$2" + for i in "${_disjoint_arr1[@]}"; do + if ar::in_set _disjoint_arr2 "$i"; then + return 1 + fi + done + return 0 +} + + ## @fn array_to_string ## @brief Turn an array into a string with seperator ## @param vname_of_array The variable name of the array diff --git a/examples/longest_consecutive.bash b/examples/longest_consecutive.bash new file mode 100644 index 0000000..47695f6 --- /dev/null +++ b/examples/longest_consecutive.bash @@ -0,0 +1,45 @@ +# Source the library +. "../ar.bash" + +max() { + local -i _first="$1" + local -i _second="$2" + if (( _second > _first)); then + echo "$_second" + else + echo "$_first" + fi +} + +## @fn longest_consecutive() +## @brief Return the count of the longest consecuritve string of numbers +longest_consecutive() { + local -a _arr=("$@") + local -a _arr_set + local -i cur + local -i cnt=0 + local -i res=0 + # Setify array + ar::set _arr _arr_set + for val in "${_arr[@]}"; do + if ar::in_set _arr_set "$val" && ! ar::in_set _arr_set $(( val - 1 )); then + cur="$val" + cnt=0 + while ar::in_set _arr_set "$cur"; do + # Remove number to avoid recomputation + ar::remove _arr_set "$cur" + (( cur++ )) + (( cnt++ )) + done + # Update optimal length + res="$(max "$res" "$cnt")" + fi + done + echo "$res" +} + +main() { + echo "$(longest_consecutive 2 6 1 9 4 5 3)" +} + +main "$@" diff --git a/tests/test_ar.bats b/tests/test_ar.bats index 549d686..ee4e0f5 100644 --- a/tests/test_ar.bats +++ b/tests/test_ar.bats @@ -233,3 +233,17 @@ setup() { run ar::is_proper_superset first_arr second_arr assert_failure } + +@test "ar::is_disjoint returns 0 if set1 and set2 are disjoint" { + local -a first_arr=(rice beans sausage) + local -a second_arr=(beef pasta cheese) + run ar::is_disjoint first_arr second_arr + assert_success +} + +@test "ar::is_disjoint returns non-zero if set1 and set2 are not disjoint" { + local -a first_arr=(rice beans sausage) + local -a second_arr=(rice beef beans) + run ar::is_disjoint first_arr second_arr + assert_failure +}