[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [QUIZ] Matrix Rotator (#209

brabuhr

6/14/2009 4:56:00 PM

Hope this isn't too early, but I may be offline for a couple of days,
so I'm posting my method now:

> ## Matrix Rotator (#209)
>
> This week's quiz is write ruby method that will rotate a matrix 90
> degrees counter-clockwise (or =F0/2 radians).
>
> Ex:
> =A0 =A00 0 0 0 0 0 0 0
> =A0 =A00 X 0 0 0 0 X 0
> =A0 =A0X X X 0 0 X X 0
> =A0 =A00 0 0 0 0 0 X 0

module Matrix
def self.rotate(o)
rows, cols =3D o.size, o[0].size
Array.new(cols){|i| Array.new(rows){|j| o[j][cols - i - 1]}}
end
end

3 tests, 5 assertions, 0 failures, 0 errors:
assert_equal square_rotated, Matrix.rotate(square)
assert_equal rectangle_rotated, Matrix.rotate(rectangle)
assert_equal( [[5], [4], [3], [2], [1]], Matrix.rotate([(1..5).to_a]) )
assert_equal( [[1, 2, 3, 4, 5]], Matrix.rotate((1..5).to_a.map{|n| [n]}=
) )
assert_equal(
[ [1, 2, 3, 4, 5],
[6, 7, 8, 9, 0] ],
Matrix.rotate([ [6, 1], [7, 2], [8, 3], [9, 4], [0, 5] ])
)

7 Answers

Robert Dober

6/14/2009 5:04:00 PM

0

Spoiler
My solution does not mean that I am clever, it only means that Ruby
has such an incredible API
Maybe you should try yourself before looking at my solution ;)


http://pastie....
--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]

Todd Benson

6/15/2009 12:52:00 AM

0

On Sun, Jun 14, 2009 at 12:04 PM, Robert Dober<robert.dober@gmail.com> wrote:
> Spoiler
> My solution does not mean that I am clever, it only means that Ruby
> has such an incredible API
> Maybe you should try yourself before looking at my solution ;)

Robert, shame on you. You just allowed us all to write a program for
efficiently solving Rubik's Revenge (4x4).

Todd

Robert Dober

6/15/2009 9:23:00 AM

0

On Mon, Jun 15, 2009 at 2:52 AM, Todd Benson<caduceass@gmail.com> wrote:
> On Sun, Jun 14, 2009 at 12:04 PM, Robert Dober<robert.dober@gmail.com> wr=
ote:
>> Spoiler
>> My solution does not mean that I am clever, it only means that Ruby
>> has such an incredible API
>> Maybe you should try yourself before looking at my solution ;)
>
> Robert, shame on you. =A0You just allowed us all to write a program for
> efficiently solving Rubik's Revenge (4x4).
I should not have posted my bad, yet Todd you forget it is Ruby you
should put the blame on ;)
But I guess it is not that simple anyway, puuuh.
R.



--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]

Benoit Daloze

6/15/2009 2:51:00 PM

0

Well, I just think the easier way while not using Matrix Class is:
http://pastie....
It just transpose by the definition(i,j->j,i) and reverse :D

I don't know why I searched also to do with inject, but it seems to be
non-sense.

2009/6/15, Robert Dober <robert.dober@gmail.com>:
> On Mon, Jun 15, 2009 at 2:52 AM, Todd Benson<caduceass@gmail.com> wrote:
>> On Sun, Jun 14, 2009 at 12:04 PM, Robert Dober<robert.dober@gmail.com>
>> wrote:
>>> Spoiler
>>> My solution does not mean that I am clever, it only means that Ruby
>>> has such an incredible API
>>> Maybe you should try yourself before looking at my solution ;)
>>
>> Robert, shame on you. =A0You just allowed us all to write a program for
>> efficiently solving Rubik's Revenge (4x4).
> I should not have posted my bad, yet Todd you forget it is Ruby you
> should put the blame on ;)
> But I guess it is not that simple anyway, puuuh.
> R.
>
>
>
> --
> Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
> d=92entre elles s=92en souviennent.
>
> All adults have been children first, but not many remember.
>
> [Antoine de Saint-Exup=E9ry]
>
>

Todd Benson

6/15/2009 7:41:00 PM

0

On Mon, Jun 15, 2009 at 4:22 AM, Robert Dober<robert.dober@gmail.com> wrote:
> I should not have posted my bad, yet Todd you forget it is Ruby you
> should put the blame on ;)
> But I guess it is not that simple anyway, puuuh.
> R.

I don't want to hijack the thread, but since my venting is off topic,
it will probably get lost in the noise anyhow. Solving a cube (of any
number of pieces) is mostly working backwards from a solved state. A
good transformation algorithm is handy. Maybe that should be another
quiz :)

I tried a mathematical transformation for this quiz so that it could
be useful for something other than a quarter turn. Didn't work. For
those more inclined to "make it so", it's an inverse matrix in front
of a position matrix. What's funny about the extra dimension required
fascinates me. I get the math, but why do we need singularity (a
bunch of 1's) on that final dimension?

list. rb

6/15/2009 10:31:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

I just happened to have done this in a side project I was working on..
Here's my rotate method. It accepts negative and positive numbers..

def rotate(n=1)
(n%4).times {self.replace(self.reverse.transpose)}
end

Which supports grid.rotate(-2) for left twice, or 1 for right.. or
whatever really..

Here's the Grid class:

#!/usr/bin/env ruby -W0
SAMPLE_DATA = %w[
0 0 7 0 5 0 0 0 0
0 0 0 0 6 4 0 0 1
4 0 0 0 0 0 2 0 9
0 8 9 0 0 0 4 0 3
6 0 3 0 0 4 0 9 0
0 0 2 4 0 0 8 0 6
0 0 0 9 1 0 6 0 0
0 0 0 0 0 0 0 0 0
2 0 4 0 0 0 0 0 0
].collect {|i| i.to_i}

class Array
def /len
a = []
self.each_with_index do |x,i|
a << [] if i % len == 0
a.last << x
end
a
end
end

class Grid < Array
attr_accessor :set
def initialize(set=SAMPLE_DATA)
@set = set
self.replace(set/Math.sqrt(set.size))
end

def rotate(n=1)
(n%4).times {self.replace(self.reverse.transpose)}
end

def flip(direction=:right)
case direction
when :right then self.replace(self.transpose.rotate(1))
when :left then self.replace(self.transpose.rotate(-1))
end
end

def children ## returns array of new Grids(3x3), ordered from left to
right
return self.collect {|i| i/3}.transpose.collect {|i|
i/3}.transpose.collect {|i| i.collect {|j| Grid.new(j.flatten.compact)}}
end

def sum
Array.new(self.flatten.compact).sum
end

def columns
self.transpose
end

def rows
self
end

def inspect
self.to_s
end

def to_s
"\n" + self.collect {|i| i.inspect}.join("\n")
end

end

grid = Grid.new
grid.rotate(1) #right cw 90deg
p grid
grid.rotate(-2) #left ccw 180deg
p grid




On Mon, Jun 15, 2009 at 3:40 PM, Todd Benson <caduceass@gmail.com> wrote:

> On Mon, Jun 15, 2009 at 4:22 AM, Robert Dober<robert.dober@gmail.com>
> wrote:
> > I should not have posted my bad, yet Todd you forget it is Ruby you
> > should put the blame on ;)
> > But I guess it is not that simple anyway, puuuh.
> > R.
>
> I don't want to hijack the thread, but since my venting is off topic,
> it will probably get lost in the noise anyhow. Solving a cube (of any
> number of pieces) is mostly working backwards from a solved state. A
> good transformation algorithm is handy. Maybe that should be another
> quiz :)
>
> I tried a mathematical transformation for this quiz so that it could
> be useful for something other than a quarter turn. Didn't work. For
> those more inclined to "make it so", it's an inverse matrix in front
> of a position matrix. What's funny about the extra dimension required
> fascinates me. I get the math, but why do we need singularity (a
> bunch of 1's) on that final dimension?
>
>

Frank Fischer

6/16/2009 7:16:00 AM

0

Here's my solution:

class Matrix
def self.rotate( mat )
mat = mat.map{|r| r.reverse}
mat.first.zip(*mat[1..-1])
end
end

Again, it works by reversing the elements in each row and transposing the
matrix.