Created
December 18, 2019 17:11
-
-
Save OpakAlex/6ed3cccb45b555a9cf3a9ec92c1ab910 to your computer and use it in GitHub Desktop.
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
module ArrayUtils | |
module_function | |
def reverse(array) | |
size = array.size | |
array.each_with_index.map do |item, index| | |
array[size - 1 - index] # just take elements from end to start (size - 1) the last element, then - index, for last it's 0 | |
end | |
end | |
end | |
array = [5,4,10,20,87,0,1] | |
reverse_array = ArrayUtils.reverse(array) | |
puts "Standart Array: #{array.inspect}" | |
puts "Reverse Array: #{reverse_array.inspect}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Вот небольшая задачка от ребят:
Без использования стандартных функций написать функцию reverse, которая
принимает массив и возвращает массив в обратном порядке.
Укажите, сколько времени ушло на выполнение :)
Я не знал можно ли использовать стандартный сорт, потому выбрал простой способ, не самый оптимальный но простой.
7 минут (с пивом ;) )