Last active
April 21, 2022 12:39
-
-
Save foxweb/a93c6aa40e793a85b775c1febf888258 to your computer and use it in GitHub Desktop.
This file contains 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
a = [0, 1, 2, 3, 4, 5] | |
# example 1 | |
def reverse(array) | |
array.each_with_index do |val, index| | |
break if index >= array.count / 2 | |
temp = array[index] | |
array[index], array[-index-1] = array[-index-1], temp | |
end | |
return array | |
end | |
reverse(a) | |
# example 2 | |
def reverse(array) | |
array.reduce([]) do |memo, item| | |
memo.unshift(item) | |
end | |
end | |
reverse(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment