From 8a8d9cfdd8a4ba17d3f73db47c54057cd12a9eef Mon Sep 17 00:00:00 2001 From: Artem Darius Weber Date: Fri, 29 Nov 2024 15:42:30 +0300 Subject: [PATCH] feat: implement task1 function to return indices of array elements in decreasing order --- lab3_task1_funcation_block_arg/main.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 lab3_task1_funcation_block_arg/main.rb diff --git a/lab3_task1_funcation_block_arg/main.rb b/lab3_task1_funcation_block_arg/main.rb new file mode 100644 index 0000000..fac45b4 --- /dev/null +++ b/lab3_task1_funcation_block_arg/main.rb @@ -0,0 +1,11 @@ +def task1(array) + indices = array.each_with_index.to_a + sorted_indices = indices.sort_by { |(element, index)| -element } + result = sorted_indices.map { |(element, index)| index } + result +end + +# Ex +array = [5, 3, 8, 1, 7] +puts "Indices in order of decreasing elements:" +puts task1(array).join(", ")