Forum: Ruby Ruby programming challenges and riddles?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
79941ab5294a74685db85edfe0db8b7f?d=identicon&s=25 Hakusa@gmail.com (Guest)
on 2007-05-25 01:57
(Received via mailing list)
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.
94cc3e46cfc5bc361e409e2e884ecfa4?d=identicon&s=25 Drew Olson (dfg59)
on 2007-05-25 02:09
Hakusa@gmail.com 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}
1b5341b64f7ce0244366eae17f06c801?d=identicon&s=25 unknown (Guest)
on 2007-05-25 02:12
(Received via mailing list)
On Fri, 25 May 2007, Hakusa@gmail.com 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 Haines
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2007-05-25 02:14
Hakusa@gmail.com 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 :)

Regards
Stefan
94cc3e46cfc5bc361e409e2e884ecfa4?d=identicon&s=25 Drew Olson (dfg59)
on 2007-05-25 02:17
Stefan Rusterholz wrote:
> puts *1..100 # will print each on a new line

Cool! How does this work? What's going on behind the scenes?
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-05-25 02:29
(Received via mailing list)
On May 24, 2007, at 8:11 PM, khaines@enigo.com 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
1b5341b64f7ce0244366eae17f06c801?d=identicon&s=25 unknown (Guest)
on 2007-05-25 02:34
(Received via mailing list)
On Fri, 25 May 2007, Ari Brown 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 Haines
96931bfe0c2948f47a98e15ae52e5637?d=identicon&s=25 Chris Carter (cdcarter)
on 2007-05-25 02:40
(Received via mailing list)
On 5/24/07, Ari Brown <ari@aribrown.com> 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...
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2007-05-25 02:42
Drew Olson wrote:
> Stefan Rusterholz 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
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2007-05-25 02:45
Chris Carter wrote:
> On 5/24/07, Ari Brown <ari@aribrown.com> 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
79941ab5294a74685db85edfe0db8b7f?d=identicon&s=25 Hakusa@gmail.com (Guest)
on 2007-05-25 02:56
(Received via mailing list)
> 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.)
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-05-25 06:27
(Received via mailing list)
I'm obviously biased, since I run the Ruby Quiz, but...

On May 24, 2007, at 7:55 PM, Hakusa@gmail.com 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:

   suggestion@rubyquiz.com

> 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
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-05-25 13:39
(Received via mailing list)
On May 24, 2007, at 8:40 PM, Chris Carter wrote:
<snip>
>
> 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
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2007-05-25 13:43
(Received via mailing list)
On 5/24/07, Hakusa@gmail.com <Hakusa@gmail.com> 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 :)

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.
96931bfe0c2948f47a98e15ae52e5637?d=identicon&s=25 Chris Carter (cdcarter)
on 2007-05-25 13:58
(Received via mailing list)
On 5/25/07, Ari Brown <ari@aribrown.com> 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]
Ef53c4abeae8cc46327030bae489a33c?d=identicon&s=25 Fred Phillips (Guest)
on 2007-05-25 14:24
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Maybe you should try http://projecteuler.net

On 25/05/07, Hakusa@gmail.com 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-----
79941ab5294a74685db85edfe0db8b7f?d=identicon&s=25 Hakusa@gmail.com (Guest)
on 2007-05-25 14:50
(Received via mailing list)
On May 25, 12:25 am, James Edward Gray II <j...@grayproductions.net>
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 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.
This topic is locked and can not be replied to.