From a2ee0611aa7172cf6ad3c52d2ef4b4ae4b02e702 Mon Sep 17 00:00:00 2001 From: Jesse Wattenbarger Date: Sat, 17 May 2025 23:02:36 -0400 Subject: [PATCH 1/2] Add is_disjoint and tests --- ar.bash | 31 ++++++++++++++++++++++++------- tests/test_ar.bats | 14 ++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) 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/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 +} From 231a40c236825d3b198c1541abe58621bc71ed21 Mon Sep 17 00:00:00 2001 From: Jesse Wattenbarger Date: Sun, 18 May 2025 07:43:55 -0400 Subject: [PATCH 2/2] Add longest_consecutive.bash to examples/ --- examples/longest_consecutive.bash | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 examples/longest_consecutive.bash 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 "$@"