Skip to content

Commit 4524fa6

Browse files
committed
Solution for array rotate problem.
A small array rotate solution with passing tests. Solution uses only Array#unshift and Array#pop.
1 parent faf57d9 commit 4524fa6

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

array_rotate/solutions/keppy.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class StringAutoma
2+
3+
def self.solve(set, n)
4+
# set is an array, n is an integer.
5+
n.times do
6+
set.unshift(set.pop())
7+
end
8+
return set
9+
end
10+
11+
end

array_rotate/solutions/keppy_test.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'minitest/autorun'
2+
3+
require_relative './keppy.rb'
4+
5+
class StringAutomaTest < MiniTest::Unit::TestCase
6+
def test_one_step
7+
set = [1, 2, 3, 4, 5, 6]
8+
expected = [6, 1, 2, 3, 4, 5]
9+
n = 1
10+
assert_equal(expected, StringAutoma.solve(set,n))
11+
end
12+
13+
def test_odd_split
14+
set = [1, 2, 3, 4, 5, 6, 1, 2, 3]
15+
expected = [1, 2, 3, 1, 2, 3, 4, 5, 6]
16+
n = 3
17+
assert_equal(expected, StringAutoma.solve(set,n))
18+
end
19+
20+
def test_over_step
21+
set = [1, 2, 3]
22+
expected = [3, 1, 2]
23+
n = 4
24+
assert_equal(expected, StringAutoma.solve(set,n))
25+
end
26+
end

0 commit comments

Comments
 (0)