Created
January 29, 2012 16:53
-
-
Save anonymous/1699614 to your computer and use it in GitHub Desktop.
pooperz
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
require 'minitest/spec' | |
require 'minitest/autorun' | |
# Integer list generation | |
listOfInt = [1, 3, 5, 7, 9, 11] | |
left, right = 0, 1 | |
top, bottom = 2, 3 | |
def rotateFromRight(alist) | |
#Rotates alist so that the rightmost element appears at the left end of | |
#the list and the other elements are pushed toward the right end of the list | |
temp = alist.last | |
for i in ((alist.size - 2).downto(0)) | |
alist[i + 1] = alist[i] | |
end | |
alist[0] = temp | |
return alist | |
end | |
def rotateFromLeft(alist) | |
#Rotates alist so that the leftmost element appears at the right end of | |
#the list and the other elements are pushed toward the left end of the list | |
temp = alist[0] | |
(1..alist.size).each do |i| | |
alist[i - 1] = alist[i] | |
end | |
alist[alist.size - 1] = temp | |
return alist | |
end | |
def rotateRow(alist, side) | |
#Rotates alist from side where side == left or side == right | |
if side == "right" | |
rotateFromRight(alist) | |
elsif side == "left" | |
rotateFromLeft(alist) | |
end | |
end | |
def rotateAnyRow(alist, row, side) | |
#Rotates a given row of atable from side where side == left or side == right. | |
#row is given as an integer value. atable is a list of list of elements | |
alist[row] = rotateRow(alist[row], side) | |
return alist | |
end | |
def rotateFromTop(alist, col = 0) | |
#Rotates a given column (col) of atable so that the topmost element | |
#appears at the bottom of the column and the other elements are pushed toward | |
#the top of the column. col is given as an integer value. atable is a list | |
#of list of elements. | |
temp = alist[0][col] | |
(1..alist.size - 1).each do |i| | |
alist[i-1][col] = alist[i][col] | |
end | |
alist[alist.size - 1][col] = temp | |
return alist | |
end | |
def rotateFromBottom(alist, col = 0) | |
#Rotates a given column (col) of atable so that the bottommost element | |
#appears at the top of the column and the other elements are pushed toward | |
#the bottom of the column. col is given as an integer value. atable is a list | |
#of list of elements. | |
temp = alist.last[col] | |
(alist.size - 2).downto(0).each do |i| | |
alist[i + 1][col] = alist[i][col] | |
end | |
alist[0][col] = temp | |
return alist | |
end | |
def rotateAnyColumn(alist, col, side) | |
#Rotates a given column (col) of atable from side where side == top or | |
#side == bottom. col is given as an integer value. atable is a list | |
#of list of elements. | |
if side == "top" | |
rotateFromTop(alist, col) | |
elsif side == "bottom" | |
rotateFromBottom(alist, col) | |
end | |
end | |
def genListOfList() | |
#Generates a list of list of integers | |
[ [1, 3, 5, 7, 9, 11], | |
[5, 15, 25, 35, 45, 55], | |
[1, 9, 25, 49, 81, 121], | |
[-1, -3, -5, -7, -9, -11], | |
[2, 6, 10, 14, 18, 22], | |
[0, 1, 2, 3, 4, 5] ] | |
end | |
describe "Rotation Functions" do | |
before do | |
@list = [1, 3, 5, 7, 9, 11] | |
@list_of_lists = genListOfList | |
end | |
it 'is able to rotate a list left' do | |
rotateFromLeft(@list).must_equal [3, 5, 7, 9, 11, 1] | |
end | |
it 'is able to rotate a list right' do | |
rotateFromRight(@list).must_equal [11, 1, 3, 5, 7, 9] | |
end | |
it 'is able to rotate a row left' do | |
rotateRow(@list, "left").must_equal [3, 5, 7, 9, 11, 1] | |
end | |
it 'is able to rotate a row right' do | |
rotateRow(@list, "right").must_equal [11, 1, 3, 5, 7, 9] | |
end | |
it 'is able to rotate any row in a list of lists' do | |
rotateAnyRow(@list_of_lists, 5, "right").must_equal [[1, 3, 5, 7, 9, 11], [5, 15, 25, 35, 45, 55], [1, 9, 25, 49, 81, 121], [-1, -3, -5, -7, -9, -11], [2, 6, 10, 14, 18, 22], [5, 0, 1, 2, 3, 4]] | |
end | |
it 'is able to rotate a column from the top to the bottom' do | |
rotateFromTop(@list_of_lists).must_equal [[5, 3, 5, 7, 9, 11], [1, 15, 25, 35, 45, 55], [-1, 9, 25, 49, 81, 121], [2, -3, -5, -7, -9, -11], [0, 6, 10, 14, 18, 22], [1, 1, 2, 3, 4, 5]] | |
end | |
it 'is able to rotate a column from the bottom to the top' do | |
rotateFromBottom(@list_of_lists).must_equal [[0, 3, 5, 7, 9, 11], [1, 15, 25, 35, 45, 55], [5, 9, 25, 49, 81, 121], [1, -3, -5, -7, -9, -11], [-1, 6, 10, 14, 18, 22], [2, 1, 2, 3, 4, 5]] | |
end | |
it 'is able to rotate any column' do | |
rotateAnyColumn(@list_of_lists, 0, "top").must_equal [[1, 3, 5, 7, 9, 11], [5, 15, 25, 35, 45, 55], [1, 9, 25, 49, 81, 121], [-1, -3, -5, -7, -9, -11], [2, 6, 10, 14, 18, 22], [0, 1, 2, 3, 4, 5]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment