Ruby programming challenges and riddles?

H

Hakusa

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.
 
D

Drew Olson

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}
 
S

Stefan Rusterholz

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 :)

Regards
Stefan
 
A

Ari Brown

puts "1...100"

hahaha

Actually:

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
 
K

khaines

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 Haines
 
C

Chris Carter

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

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

Stefan Rusterholz

Drew said:
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
 
S

Stefan Rusterholz

Chris said:
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
 
H

Hakusa

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.)
 
J

James Edward Gray II

I'm obviously biased, since I run the Ruby Quiz, but...

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:

(e-mail address removed)
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 Gray II
 
A

Ari Brown

On May 24, 2007, at 8:40 PM, Chris Carter 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
 
T

Todd Benson

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 :)

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.
 
C

Chris Carter

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


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
(irb):1: warning: default `to_a' will be obsolete
=> [#<Object:0x15b8d70>]

vs.
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 
H

Hakusa

I'm obviously biased, since I run the Ruby Quiz, but...




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:

(e-mail address removed)


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


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 Gray 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 :)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top