Skip to content

Instantly share code, notes, and snippets.

@mkaschenko
Last active August 22, 2022 06:40
Show Gist options
  • Save mkaschenko/68a850d3aa60c14e1cbd to your computer and use it in GitHub Desktop.
Save mkaschenko/68a850d3aa60c14e1cbd to your computer and use it in GitHub Desktop.
require 'test/unit'
class TestRecursiveReverse < Test::Unit::TestCase
def test_o_make
assert_equal [3, 2, 1], RecursiveReverse.new.o_make([1, 2, 3])
end
def test_f_make
assert_equal [3, 2, 1], RecursiveReverse.new.f_make([1, 2, 3])
end
end
class RecursiveReverse
def o_make(array)
@ts ||= array.size
if @ts.zero?
array
else
@ts -= 1
array.insert(@ts, array.shift)
o_make(array)
end
end
def f_make(array)
if array.empty?
array
else
f_make(array[1..-1]) + [array.first]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment