From 988d9c83dd0a6ed770c22496df461cf90c10b0ee Mon Sep 17 00:00:00 2001 From: Artem Darius Weber Date: Fri, 29 Nov 2024 15:45:18 +0300 Subject: [PATCH] feat: add elements_between_first_and_last_max function to retrieve elements between first and last maximum values --- lab3_task1_funcation_block_arg/main.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lab3_task1_funcation_block_arg/main.rb b/lab3_task1_funcation_block_arg/main.rb index ebf0a59..f2bbf90 100644 --- a/lab3_task1_funcation_block_arg/main.rb +++ b/lab3_task1_funcation_block_arg/main.rb @@ -24,6 +24,17 @@ def elements_between_first_and_second_max(array) first_max_index, second_max_index = [first_max_index, second_max_index].sort array[(first_max_index + 1)...second_max_index] end + + +def elements_between_first_and_last_max(array) + max_value = array.max + first_max_index = array.index(max_value) + last_max_index = array.rindex(max_value) + return [] if first_max_index == last_max_index + + first_max_index, last_max_index = [first_max_index, last_max_index].sort + array[(first_max_index + 1)...last_max_index] +end # Ex @@ -34,3 +45,7 @@ puts indices_sorted_by_descending_values(array).join(", ") array = [5, 1, 8, 3, 8, 7] puts "Elements between first and second maximum:" puts elements_between_first_and_second_max(array).join(", ") + +array = [5, 1, 8, 3, 7, 8] +puts "Elements between first and last maximum:" +puts elements_between_first_and_last_max(array).join(", ") \ No newline at end of file