Ruby programming challenges and riddles?


#1

I recently fell in love with programming riddles and was told that
they’re the best way to learn a language. I agree with this statement
because a good riddle challenges you to think outside the box, and
that’s a great skill to have when programming. Plus getting too cozy
can lead to flawed and automated code because you forget WHY it worked
before and didn’t think about why it wouldn’t work here.

So: Ruby programming riddles . . . I must admit I didn’t start this
discussion because I know any.

But just to get the ball rolling:
Print 1…100 on the screen in 4 or less lines of code.

(And note that I will consider these by the most lines you can make
out of your answer so writing it all on one line is useless.)

I know it’s not the hardest, but it’s just to get the ball rolling.
I’m betting this isn’t even the right place to make this thread.


#2

removed_email_address@domain.invalid wrote:

I recently fell in love with programming riddles and was told that
they’re the best way to learn a language. I agree with this statement
because a good riddle challenges you to think outside the box, and
that’s a great skill to have when programming. Plus getting too cozy
can lead to flawed and automated code because you forget WHY it worked
before and didn’t think about why it wouldn’t work here.

So: Ruby programming riddles . . . I must admit I didn’t start this
discussion because I know any.

But just to get the ball rolling:
Print 1…100 on the screen in 4 or less lines of code.

(And note that I will consider these by the most lines you can make
out of your answer so writing it all on one line is useless.)

I know it’s not the hardest, but it’s just to get the ball rolling.
I’m betting this isn’t even the right place to make this thread.

You may want to check out http://www.rubyquiz.com

Also, in response to your question: (1…100).each{|i| puts i}


#3

On Fri, 25 May 2007, removed_email_address@domain.invalid wrote:

But just to get the ball rolling:
Print 1…100 on the screen in 4 or less lines of code.

puts “1…100”

hahaha

Actually:

puts (1…100).to_a

Kirk H.


#4

removed_email_address@domain.invalid wrote:

But just to get the ball rolling:
Print 1…100 on the screen in 4 or less lines of code.

puts *1…100 #Â will print each on a new line

And rubyquiz.com - as Drew already pointed out :slight_smile:

Regards
Stefan


#5

Stefan R. wrote:

puts *1…100 # will print each on a new line

Cool! How does this work? What’s going on behind the scenes?


#6

On Fri, 25 May 2007, Ari B. wrote:

puts (1…100).to_a

Careful with .to_a, they’re getting rid of it. Break the habit while you can!

Someday.

No point in not using a construct of the current language just because
is
may not be there at some point in the far-off.

Kirk H.


#7

On May 24, 2007, at 8:11 PM, removed_email_address@domain.invalid wrote:

puts (1…100).to_a

Careful with .to_a, they’re getting rid of it. Break the habit while
you can!

To make this a little longer than one line, and to avoid repetition
of ideas…
100.downto(1) do |yourmom|
puts yourmom
end

Nothing quite like a little bit of immaturity.
heh. do yourmom.

I like these. They’re easier than RubyQuiz, so I can do them, and
they don’t take a lot of time. Which I am lacking of right now.

immaturity ftw
-------------------------------------------------------|
~ Ari
crap my sig won’t fit


#8

On 5/24/07, Ari B. removed_email_address@domain.invalid wrote:

hahaha
100.downto(1) do |yourmom|
-------------------------------------------------------|
~ Ari
crap my sig won’t fit

I was under the impression they are removing Object#to_a, not
Enumerable#to_a…


#9

Drew O. wrote:

Stefan R. wrote:

puts *1…100 # will print each on a new line

Cool! How does this work? What’s going on behind the scenes?

  • is the splash operator, it expands an array to a list, if the object
    is not an array it will call .to_a on it
    so the above is virtually the same as doing:
    puts 1, 2, 3, …, 100

Regards
Stefan


#10

Chris C. wrote:

On 5/24/07, Ari B. removed_email_address@domain.invalid wrote:

hahaha
100.downto(1) do |yourmom|
-------------------------------------------------------|
~ Ari
crap my sig won’t fit

I was under the impression they are removing Object#to_a, not
Enumerable#to_a…

As far as I know your impression is correct. IRB backs that up:
irb(main):001:0> 1.to_a
(irb):1: warning: default `to_a’ will be obsolete
=> [1]
irb(main):002:0> (1…2).to_a
=> [1, 2]

Regards
Stefan


#11

http://www.rubyquiz.com

Hm. Everything I’ve looked at on this site is time consuming, geared
towards experience programmers, and most of all: not making me learn
about Ruby specific things. My favorite riddles are the kind that can
be written and solved quickly if you know your stuff, and can teach
you something by other’s solutions if you don’t. Any site that does
that? (I’m not looking for difficulty, I’m looking for outside-the-
box.)


#12

On May 24, 2007, at 8:40 PM, Chris C. wrote:

I was under the impression they are removing Object#to_a, not
Enumerable#to_a…

You’re probably right. I was criticized for using to_a in that
context, so you’re probably right.
What would be an example of Object#to_a?

---------------------------------------------------------------|
~Ari
“I don’t suffer from insanity. I enjoy every minute of it” --1337est
man alive


#13

On 5/24/07, removed_email_address@domain.invalid removed_email_address@domain.invalid wrote:

http://www.rubyquiz.com

Hm. Everything I’ve looked at on this site is time consuming, geared
towards experience programmers, and most of all: not making me learn
about Ruby specific things. My favorite riddles are the kind that can
be written and solved quickly if you know your stuff, and can teach
you something by other’s solutions if you don’t. Any site that does
that? (I’m not looking for difficulty, I’m looking for outside-the-
box.)

I hate reading code. I mean absolutely hate with a passion. I hate
reading my own code, even. I also hate when people are smarter or
more clever than me. I’ve almost always been able to pick up the
subject material from a textbook, and jump write into the application
of it. In college, I never went to recitations, and rarely even
lectures.

With that being said, Ruby quiz and ruby-talk have become very
powerful humbling and mind-expanding learning tools for me – in not
only Ruby, but programming concepts in general. Better than taking a
class, by far, because it continuously gives you the chance to bite
off more than you can chew, which is, IMO, the key to good learning in
or outside of whatever you see as a box :slight_smile:

Read some more code! Download stuff from rubyforge. Read through
some of the quiz solutions. I think you’ll find just what you’re
looking for. In fact, if I was going to teach an Intro to Ruby class,
James’ ruby quiz book would be required reading material.


#14

I’m obviously biased, since I run the Ruby Q., but…

On May 24, 2007, at 7:55 PM, removed_email_address@domain.invalid wrote:

http://www.rubyquiz.com

Hm. Everything I’ve looked at on this site is time consuming,

Quizzes run the gamut. We’ve had too hard problems and we’ve had
some very easy ones. See the recent Checking Credit Cards or the
older pp Pascal problem for some pretty easy stuff. In general, the
quizzes with a lot of solutions are easier.

More importantly, a large percentage of the quizzes are user
submitted. If you want easier problems, please send them in:

removed_email_address@domain.invalid

geared towards experience programmers,

I hope we’ve appealed to everyone at some point, but maybe that’s
just wishful thinking.

and most of all: not making me learn about Ruby specific things.

I really hope that’s not true. This is the reason I built the Ruby
Quiz: to teach myself and others Ruby. Hopefully we’ve managed to
do at least a little of that.

James Edward G. II


#15

On 5/25/07, Ari B. removed_email_address@domain.invalid wrote:

---------------------------------------------------------------|
~Ari
“I don’t suffer from insanity. I enjoy every minute of it” --1337est
man alive

Object.new.to_a
(irb):1: warning: default `to_a’ will be obsolete
=> [#Object:0x15b8d70]

vs.

(1…10).to_a # Ranges mixin Enumerable for to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


#16

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Maybe you should try http://projecteuler.net

On 25/05/07, removed_email_address@domain.invalid wrote:I recently fell in love with
programming
riddles and was told that
they’re the best way to learn a language. I agree with this statement
because a good riddle challenges you to think outside the box, and
that’s a great skill to have when programming. Plus getting too cozy
can lead to flawed and automated code because you forget WHY it worked
before and didn’t think about why it wouldn’t work here.

So: Ruby programming riddles . . . I must admit I didn’t start this
discussion because I know any.

But just to get the ball rolling:
Print 1…100 on the screen in 4 or less lines of code.

(And note that I will consider these by the most lines you can make
out of your answer so writing it all on one line is useless.)

I know it’s not the hardest, but it’s just to get the ball rolling.
I’m betting this isn’t even the right place to make this thread.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFGVtU02vy7v+d+psQRAr4qAKC3unbjQ6v22pVwpHII2cujj/VUeACg5PSA
dIylkDJvf12VfljvA0j0Tpk=
=eQMn
-----END PGP SIGNATURE-----


#17

On May 25, 12:25 am, James Edward G. II removed_email_address@domain.invalid
wrote:

older pp Pascal problem for some pretty easy stuff. In general, the
just wishful thinking.

and most of all: not making me learn about Ruby specific things.

I really hope that’s not true. This is the reason I built the Ruby
Quiz: to teach myself and others Ruby. Hopefully we’ve managed to
do at least a little of that.

James Edward G. II

OK, then it’s by luck of the draw of what I read, and I’ve read the
wrong things. But I learned more about Ruby by reading people’s
answers to my printing quiz then I did reading through the card game
quiz–which to be honest confused me greatly. I think the best
riddles, as I’ve said, are just a few lines of code that exploit a
languages quirks. Reading code in general will teach you them and is a
practice I should temp myself to keep up with, but I’d have to hunt
and peck through the source code to find it. Short riddles have little
too find so the quirks are more apparent.

But I think I’ll browse your selection anyway. It’s not exactly what
I’d prefer in challenges, but it probably is a much better resource
than I’m giving it credit for.

Better than taking a
class, by far, because it continuously gives you the chance to bite
off more than you can chew, which is, IMO, the key to good learning in
or outside of whatever you see as a box :slight_smile:

Hmm. Very good point. I do hate it when my school teachers protect me
from knowing what’s in the next lesson or how the (subject) relates to
(corollary). I guess attempting a harder one is better that protecting
myself by saying it’s hard.