"Create a rock paper scissors program in ruby WITHOUT using conditionals"

I'm in an introductory software development class, and my homework is to
create a rock paper scissors program that takes two arguments (rock,
paper), etc, and returns the arg that wins.

Now I would make quick work of this problem if I could use conditionals,
but the assignment says everything we need to know is in the first three
chapters of the ruby textbook, and these chapters DO NOT include
conditionals! Would it be possible to create this program without them?
Or is he just expecting us to be resourceful and use the conditionals.
It's a very easy assignment with conditionals though...I'm thinking that
I might be missing something here.

···

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

David D. писал 07.06.2012 02:50:

I'm in an introductory software development class, and my homework is to
create a rock paper scissors program that takes two arguments (rock,
paper), etc, and returns the arg that wins.

Now I would make quick work of this problem if I could use conditionals,
but the assignment says everything we need to know is in the first three
chapters of the ruby textbook, and these chapters DO NOT include
conditionals! Would it be possible to create this program without them?
Or is he just expecting us to be resourceful and use the conditionals.
It's a very easy assignment with conditionals though...I'm thinking that
I might be missing something here.

Instead of this:

   if arg == "a"
     1
   elsif arg == "b"
     2
   end

... you can use:

   { "a" => 1, "b" => 2 }[arg]

I leave the rest to your imagination.

···

--
   WBR, Peter Zotov.

Have you considered asking the professor/teacher/TA what their expectations are? This can save you a lot of headaches, now and in the future.

-Justin

···

On 06/06/2012 03:50 PM, David D. wrote:

I'm in an introductory software development class, and my homework is to
create a rock paper scissors program that takes two arguments (rock,
paper), etc, and returns the arg that wins.

Now I would make quick work of this problem if I could use conditionals,
but the assignment says everything we need to know is in the first three
chapters of the ruby textbook, and these chapters DO NOT include
conditionals! Would it be possible to create this program without them?
Or is he just expecting us to be resourceful and use the conditionals.
It's a very easy assignment with conditionals though...I'm thinking that
I might be missing something here

Hi all,

For giggles, I decided to solve this in the most obscene manner possible, without using conditionals. I came up with:

print ARGV[1-((ARGV[0][0..0].downcase[0] - ARGV[1][0..0].downcase[0])%5)/3] + "\n"

It requires Ruby 1.8. It needs a few tweaks for the Ruby 1.9 series I believe. It handles upper and lower case, and only uses the first letter of each arg.

It is left as an exercise for the reader to explain why this actually works. :wink:

For the assignment though, I'd probably use hashes as per Peter's suggestion.

Garth

···

On 07/06/12 08:20, David D. wrote:

I'm in an introductory software development class, and my homework is to
create a rock paper scissors program that takes two arguments (rock,
paper), etc, and returns the arg that wins.

Now I would make quick work of this problem if I could use conditionals,
but the assignment says everything we need to know is in the first three
chapters of the ruby textbook, and these chapters DO NOT include
conditionals! Would it be possible to create this program without them?
Or is he just expecting us to be resourceful and use the conditionals.
It's a very easy assignment with conditionals though...I'm thinking that
I might be missing something here.

I think this is too limiting / not mind-expanding enough...

I suggest looking at how smalltalk does branching (conditionals, loops, etc).

···

On Jun 6, 2012, at 15:54 , Peter Zotov wrote:

Instead of this:

if arg == "a"
   1
elsif arg == "b"
   2
end

... you can use:

{ "a" => 1, "b" => 2 }[arg]

I leave the rest to your imagination.

Instead of this:

if arg == "a"
1
elsif arg == "b"
2
end

... you can use:

{ "a" => 1, "b" => 2 }[arg]

I leave the rest to your imagination.

I think this is too limiting / not mind-expanding enough...

I suggest looking at how smalltalk does branching (conditionals, loops, etc).

excellent suggestion.

Also consider how this could be done via delegation ala objective c style.

···

On Wed, Jun 6, 2012 at 8:24 PM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

On Jun 6, 2012, at 15:54 , Peter Zotov wrote:

Ryan Davis писал 07.06.2012 05:24:

Instead of this:

if arg == "a"
   1
elsif arg == "b"
   2
end

... you can use:

{ "a" => 1, "b" => 2 }[arg]

I leave the rest to your imagination.

I think this is too limiting / not mind-expanding enough...

I suggest looking at how smalltalk does branching (conditionals, loops, etc).

Oh, what an amazing idea.

def true.ex(a, b)
   a.()
end

def false.ex(a, b)
   b.()
end

(arg == "a").ex(-> {
   puts "arg is a"
}, -> {
   puts "arg isn't a"
})

I leave the task of explaining this (and its origins) to your professor to
yourself. Also, google for "church numerals" and "lambda calculus"--you won't
be disappointed! :smiley:

···

On Jun 6, 2012, at 15:54 , Peter Zotov wrote:

--
   WBR, Peter Zotov.