Advice - Printing All Options

Hi! I’ve set a project for myself but I don’t know where to begin
(newbie). Here’s what I’m trying to do:

I’m trying to come up with a drill for my martial arts class where I can
flow from position to position, from top to bottom, and to left and
right. Here’s an example: Position 1/being on Top of opponent/Focus on
Left Side. Then go to, say, Position 2/being on Bottom of Opponent/Focus
on Right Side. In other words, I’d like a list of all different
combinations for these 3 areas (Position/Top-Bottom/Left-Right). I’ve
played around with rand with arrays but can’t get what I’m looking for.
Thanks so much!

On Wed, Dec 2, 2009 at 1:52 PM, Wood Y. [email protected] wrote:

Thanks so much!
a = [1,2,3]
b = [“a”,“b”,“c”]
c = [7,8,9]

a.each {|x| b.each {|y| c.each {|z| puts “#{x}/#{y}/#{z}”}}}

There’s probably a better way.


Posted via http://www.ruby-forum.com/.


Paul S.
http://www.nomadicfun.co.uk

[email protected]

a = [1,2,3]
b = [“a”,“b”,“c”]
c = [7,8,9]

a.each {|x| b.each {|y| c.each {|z| puts “#{x}/#{y}/#{z}”}}}

There’s probably a better way.


Paul S.
http://www.nomadicfun.co.uk

[email protected]

PERFECT!! You totally nailed it! That’s exactly what I wanted. Woo hoo!
I’ll study the code and learn from this. Thanks SO much!

Here’s what I did:

Positions = [‘Rear’, ‘Mount’, ‘Knee’, ‘Side’, ‘Turtle’, ‘Guard’]
Direction = [‘Top’, ‘Bottom’]
Side = [‘Left’, ‘Right’]

Positions.each {|x| Direction.each {|y| Side.each {|z| puts
“#{x}/#{y}/#{z}”}}}

Hey - one more thing. How can I go further and make this more random. I
ran the program and got this:

Rear/Top/Left
Rear/Top/Right
Rear/Bottom/Left
Rear/Bottom/Right
Mount/Top/Left
Mount/Top/Right
Mount/Bottom/Left
Mount/Bottom/Right
Knee/Top/Left
Knee/Top/Right
Knee/Bottom/Left
Knee/Bottom/Right
Side/Top/Left
Side/Top/Right
Side/Bottom/Left
Side/Bottom/Right
Turtle/Top/Left
Turtle/Top/Right
Turtle/Bottom/Left
Turtle/Bottom/Right
Guard/Top/Left
Guard/Top/Right
Guard/Bottom/Left
Guard/Bottom/Right

Can I drill down even further? Example - Guard/Bottom/Right, then
Turtle/Top/Right, etc? Thanks!

Wood Y. wrote:

Hey - one more thing. How can I go further and make this more random. I
ran the program and got this:

Rear/Top/Left
Rear/Top/Right
Rear/Bottom/Left
Rear/Bottom/Right
Mount/Top/Left
Mount/Top/Right
Mount/Bottom/Left
Mount/Bottom/Right
Knee/Top/Left
Knee/Top/Right
Knee/Bottom/Left
Knee/Bottom/Right
Side/Top/Left
Side/Top/Right
Side/Bottom/Left
Side/Bottom/Right
Turtle/Top/Left
Turtle/Top/Right
Turtle/Bottom/Left
Turtle/Bottom/Right
Guard/Top/Left
Guard/Top/Right
Guard/Bottom/Left
Guard/Bottom/Right

Can I drill down even further?

What do you mean?

Example - Guard/Bottom/Right, then
Turtle/Top/Right, etc? Thanks!

If you just want random order, that’s easy. Check out sort_by and rand.
Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

What do you mean?

When I ran my program, it printed out all options for each Position.
Now, I’d like to print them out in random order. For example:

Mount/Top/Left
Knee/Top/Right
Side/Bottom/Left
etc,

instead of

Mount/Top/Left
Mount/Top/Right
Mount/Bottom/Left
Mount/Bottom/Right
Knee/Top/Left
Knee/Top/Right
Knee/Bottom/Left
Knee/Bottom/Right
etc,

Thanks!

2009/12/2 Marnen Laibow-Koser [email protected]:

Mount/Bottom/Left
Turtle/Top/Right
What do you mean?

Example - Guard/Bottom/Right, then
Turtle/Top/Right, etc? Thanks!

If you just want random order, that’s easy. Check out sort_by and rand.

1.9 also has #shuffle.

Cheers

robert

On Wed, Dec 2, 2009 at 3:13 PM, Aldric G. [email protected]
wrote:

played around with rand with arrays but can’t get what I’m looking for.
Thanks so much!

This is, as others have shown, rather trivial code. While I applaud your
intent, which I presume is to not leave out any positions, your training
will benefit from you doing this work in your head and with your body
from the start.

But, still, to achieve it, change the “puts” line, which prints to
screen, to instead put the result in an array.

cobinations = []

Positions = [‘Rear’, ‘Mount’, ‘Knee’, ‘Side’, ‘Turtle’, ‘Guard’]
Direction = [‘Top’, ‘Bottom’]
Side = [‘Left’, ‘Right’]

Positions.each {|x| Direction.each {|y| Side.each {|z| combinations <<
[#{x}/#{y}/#{z}]}}}

puts combinations.shuffle
puts combinations.shuffle
puts combinations.shuffle

:slight_smile:


Posted via http://www.ruby-forum.com/.


Paul S.
http://www.nomadicfun.co.uk

[email protected]

Wood Y. wrote:

Hi! I’ve set a project for myself but I don’t know where to begin
(newbie). Here’s what I’m trying to do:

I’m trying to come up with a drill for my martial arts class where I can
flow from position to position, from top to bottom, and to left and
right. Here’s an example: Position 1/being on Top of opponent/Focus on
Left Side. Then go to, say, Position 2/being on Bottom of Opponent/Focus
on Right Side. In other words, I’d like a list of all different
combinations for these 3 areas (Position/Top-Bottom/Left-Right). I’ve
played around with rand with arrays but can’t get what I’m looking for.
Thanks so much!

This is, as others have shown, rather trivial code. While I applaud your
intent, which I presume is to not leave out any positions, your training
will benefit from you doing this work in your head and with your body
from the start.