Skip to content

Instantly share code, notes, and snippets.

@mkaschenko
Last active November 8, 2023 10:27
Show Gist options
  • Save mkaschenko/049e82797ee68fb926b07e2901456db6 to your computer and use it in GitHub Desktop.
Save mkaschenko/049e82797ee68fb926b07e2901456db6 to your computer and use it in GitHub Desktop.
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