[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby programming challenges and riddles?

Hakusa@gmail.com

5/24/2007 11:54:00 PM

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.

15 Answers

Drew Olson

5/25/2007 12:09:00 AM

0

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

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

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

khaines

5/25/2007 12:11:00 AM

0

Stefan Rusterholz

5/25/2007 12:14:00 AM

0

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

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

Drew Olson

5/25/2007 12:18:00 AM

0

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?

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

Ari Brown

5/25/2007 12:28:00 AM

0


On May 24, 2007, at 8:11 PM, khaines@enigo.com wrote:

> 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

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


khaines

5/25/2007 12:34:00 AM

0

Chris Carter

5/25/2007 12:40:00 AM

0

On 5/24/07, Ari Brown <ari@aribrown.com> wrote:
>
> On May 24, 2007, at 8:11 PM, khaines@enigo.com wrote:
>
> > 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
>
> 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...

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

Stefan Rusterholz

5/25/2007 12:42:00 AM

0

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

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

Stefan Rusterholz

5/25/2007 12:45:00 AM

0

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

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

Hakusa@gmail.com

5/25/2007 12:53:00 AM

0

> http://www.ru...

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