Last active
November 8, 2023 10:27
-
-
Save mkaschenko/049e82797ee68fb926b07e2901456db6 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
def reverse_1(array) | |
index = array.size - 1 | |
result = [] | |
for i in 0..index do | |
result[index - i] = array[i] | |
end | |
result | |
end | |
def reverse_2(array) | |
if array.size == 0 | |
array | |
else | |
reverse_2(array[1..-1]) + [array[0]] | |
end | |
end | |
puts reverse_1([1, 2, 3, 3]) == [3, 3, 2, 1] | |
puts reverse_2([1, 2, 3, 3]) == [3, 3, 2, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment