Ruby Puzzle Challenge

Discussion in 'Ruby' started by Wyatt Greene, Jan 30, 2008.

  1. Wyatt Greene

    Wyatt Greene Guest

    Write a Ruby program that prints out the numbers 1 to 100, one number
    per line. The program must be less than 10 characters long.

    Good luck!
    Wyatt Greene
    Wyatt Greene, Jan 30, 2008
    #1
    1. Advertising

  2. Wyatt Greene

    Lee Jarvis Guest

    On Jan 30, 9:14 pm, Wyatt Greene <> wrote:
    > Write a Ruby program that prints out the numbers 1 to 100, one number
    > per line.  The program must be less than 10 characters long.
    >
    > Good luck!
    > Wyatt Greene


    Lol whats this all about?

    Well, just for the crack..

    p *1..100
    Lee Jarvis, Jan 30, 2008
    #2
    1. Advertising

  3. Wyatt Greene wrote:
    > Write a Ruby program that prints out the numbers 1 to 100, one number
    > per line. The program must be less than 10 characters long.


    Does this have some kind of spoiler-period like the ruby quiz? Well, since it
    didn't say so in the OP and anyone who wants to solve it on his own can just
    not read the replies until he's done, here it goes:

    p *1..100
    or one char shorter:
    p *1..?d


    --
    NP: Explosions in the Sky- With Tired Eyes, Tired Minds, Tired Souls, We Slept
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Jan 30, 2008
    #3
  4. Wyatt Greene

    Wyatt Greene Guest

    I was thinking of p *1..100

    I'm am truly amazed that this could be squeezed down even further to p
    *1..?d

    That leads me to wonder...is it possible to squeeze this program down
    into 7 characters?



    On Jan 30, 4:27 pm, Sebastian Hungerecker <>
    wrote:
    > Wyatt Greene wrote:
    > > Write a Ruby program that prints out the numbers 1 to 100, one number
    > > per line. The program must be less than 10 characters long.

    >
    > Does this have some kind of spoiler-period like the ruby quiz? Well, since it
    > didn't say so in the OP and anyone who wants to solve it on his own can just
    > not read the replies until he's done, here it goes:
    >
    > p *1..100
    > or one char shorter:
    > p *1..?d
    >
    > --
    > NP: Explosions in the Sky- With Tired Eyes, Tired Minds, Tired Souls, We Slept
    > Jabber:
    > ICQ: 205544826
    Wyatt Greene, Jan 30, 2008
    #4
  5. On [Thu, 31.01.2008 06:24], Lee Jarvis wrote:
    > On Jan 30, 9:14 pm, Wyatt Greene <> wrote:
    > > Write a Ruby program that prints out the numbers 1 to 100, one number
    > > per line.  The program must be less than 10 characters long.
    > >
    > > Good luck!
    > > Wyatt Greene

    >
    > Lol whats this all about?
    >
    > Well, just for the crack..
    >
    > p *1..100


    Would you be so gentle and explain this piece of code to me?
    I know things like p "a"*10, but I dont really understand your code.
    I know that 1..100 describes a range, but... isn't * a binary operator? I don't really see the first/second operand

    --
    Dominik Honnef
    Dominik Honnef, Jan 30, 2008
    #5
  6. Dominik Honnef wrote:
    > > p *1..100

    >
    > Would you be so gentle and explain this piece of code to me?
    > [...] isn't * a binary operator? I



    * can be a binary operator, yes, but here it is the unary, prefix
    splat-operator which takes an array or any object that responds to
    to_a and turns it into a list of arguments:
    foo(*[:la,:li,:lu]) becomes foo:)la,:li,:lu)
    p *1..5 becomes p 1,2,3,4,5


    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Jan 30, 2008
    #6
  7. Wyatt Greene

    Wyatt Greene Guest

    This is the problem with writing "clever" code...it's unreadable!

    The * operator used here is a unary operator that is used to convert
    an array into a list of arguments. For example, say you had this
    method:

    def say(a, b, c)
    puts a
    puts b
    puts c
    end

    If you had this array:

    arr = [1, 2, 3]

    As a convenience, Ruby lets you pass each element of the array as a
    separate argument into the method by using the * operator:

    say(*arr)

    In the case of p *1..100, the trick seems to work for a range, too.

    On Jan 30, 4:36 pm, Dominik Honnef <> wrote:
    > On [Thu, 31.01.2008 06:24], Lee Jarvis wrote:
    >
    > > On Jan 30, 9:14 pm, Wyatt Greene <> wrote:
    > > > Write a Ruby program that prints out the numbers 1 to 100, one number
    > > > per line. The program must be less than 10 characters long.

    >
    > > > Good luck!
    > > > Wyatt Greene

    >
    > > Lol whats this all about?

    >
    > > Well, just for the crack..

    >
    > > p *1..100

    >
    > Would you be so gentle and explain this piece of code to me?
    > I know things like p "a"*10, but I dont really understand your code.
    > I know that 1..100 describes a range, but... isn't * a binary operator? I don't really see the first/second operand
    >
    > --
    > Dominik Honnef
    Wyatt Greene, Jan 30, 2008
    #7
  8. On [Thu, 31.01.2008 06:42], Sebastian Hungerecker wrote:
    > Dominik Honnef wrote:
    > > > p *1..100

    > >
    > > Would you be so gentle and explain this piece of code to me?
    > > [...] isn't * a binary operator? I

    >
    >
    > * can be a binary operator, yes, but here it is the unary, prefix
    > splat-operator which takes an array or any object that responds to
    > to_a and turns it into a list of arguments:
    > foo(*[:la,:li,:lu]) becomes foo:)la,:li,:lu)
    > p *1..5 becomes p 1,2,3,4,5
    >
    >
    > HTH,
    > Sebastian
    > --
    > Jabber:
    > ICQ: 205544826


    Ah, yeah of course... I totally forgot about that.
    If it had been puts(*[1,2,3...]) I wouldnt have asked that question :/
    Actually, I'm using this feature a lot in my codes. But didn't know it takes any object,
    which responds to #to_a

    Thank you :)

    (Okay, I forgot, that puts can take multiple arguments, too. Sometimes, Ruby is just too easy)
    --
    Dominik Honnef
    Dominik Honnef, Jan 30, 2008
    #8
  9. Wyatt Greene

    Lee Jarvis Guest

    On Jan 30, 9:36 pm, Dominik Honnef <> wrote:
    > Would you be so gentle and explain this piece of code to me?
    > I know things like p "a"*10, but I dont really understand your code.
    > I know that 1..100 describes a range, but... isn't * a binary operator? I don't really see the first/second operand


    Looks like Sebastian Hungerecker explained it before I could get
    there. At least I got my post in second before him, hehe..
    Well said, Sebastian.

    Regards,
    Lee
    Lee Jarvis, Jan 30, 2008
    #9
  10. Wyatt Greene wrote:
    > I was thinking of p *1..100
    >
    > I'm am truly amazed that this could be squeezed down even further to p
    > *1..?d
    >
    > That leads me to wonder...is it possible to squeeze this program down
    > into 7 characters?
    >


    I tried another approach by relaxing your conditions a bit : the number
    must all be printed out on the output but not one on each line.

    p 2**975

    works :)

    ie the "output" verifies :
    !(1..100).any? { |v| output !~ /#{v}/ }

    Unfortunately there's no x**y solution to this condition where x and y
    use less that 4 characters.

    Still stuck with 8 chars :-/ I can't think of any other string or
    numeric operator which can generate lots of data to print with little
    input right now.

    Lionel
    Lionel Bouton, Jan 30, 2008
    #10
  11. Wyatt Greene

    Wyatt Greene Guest

    On Jan 30, 6:01 pm, Lionel Bouton <>
    wrote:
    > Wyatt Greene wrote:
    > > I was thinking of p *1..100

    >
    > > I'm am truly amazed that this could be squeezed down even further to p
    > > *1..?d

    >
    > > That leads me to wonder...is it possible to squeeze this program down
    > > into 7 characters?

    >
    > I tried another approach by relaxing your conditions a bit : the number
    > must all be printed out on the output but not one on each line.
    >
    > p 2**975
    >
    > works :)
    >
    > ie the "output" verifies :
    > !(1..100).any? { |v| output !~ /#{v}/ }
    >
    > Unfortunately there's no x**y solution to this condition where x and y
    > use less that 4 characters.
    >
    > Still stuck with 8 chars :-/ I can't think of any other string or
    > numeric operator which can generate lots of data to print with little
    > input right now.
    >
    > Lionel


    Wow, that's pretty creative, though!
    Wyatt Greene, Jan 31, 2008
    #11
  12. On Jan 30, 2008 1:35 PM, Wyatt Greene <> wrote:
    > I was thinking of p *1..100
    >
    > I'm am truly amazed that this could be squeezed down even further to p
    > *1..?d
    >
    > That leads me to wonder...is it possible to squeeze this program down
    > into 7 characters?


    How about 0 characters?

    >ruby -p -e '' < numbers


    The ruby program is zero characters. You just have to set up the
    'numbers' file ahead of time. :)

    Judson
    --
    Your subnet is currently 169.254.0.0/16. You are likely to be eaten by a grue.
    Judson Lester, Jan 31, 2008
    #12
  13. Wyatt Greene

    Drew Olson Guest

    Wyatt Greene wrote:
    > Write a Ruby program that prints out the numbers 1 to 100, one number
    > per line. The program must be less than 10 characters long.
    >
    > Good luck!
    > Wyatt Greene



    For more golf fun see: http://codegolf.com

    --
    Posted via http://www.ruby-forum.com/.
    Drew Olson, Jan 31, 2008
    #13
  14. Wyatt Greene

    fedzor Guest

    On Jan 30, 2008, at 4:14 PM, Wyatt Greene wrote:

    > Write a Ruby program that prints out the numbers 1 to 100, one number
    > per line. The program must be less than 10 characters long.


    p *1..100

    Tadah!
    I had to think about it for moment, though
    fedzor, Jan 31, 2008
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Newton

    Challenge: Triangles puzzle

    Jim Newton, Oct 16, 2004, in forum: Java
    Replies:
    42
    Views:
    7,741
    Dirk Thierbach
    Nov 3, 2004
  2. Tayssir John Gabbour

    Re: Challenge: Triangles puzzle

    Tayssir John Gabbour, Oct 18, 2004, in forum: C++
    Replies:
    0
    Views:
    386
    Tayssir John Gabbour
    Oct 18, 2004
  3. Paul Lutus

    Re: Challenge: Triangles puzzle

    Paul Lutus, Oct 18, 2004, in forum: C++
    Replies:
    0
    Views:
    421
    Paul Lutus
    Oct 18, 2004
  4. Frank Buss

    Re: Challenge: Triangles puzzle

    Frank Buss, Oct 25, 2004, in forum: C++
    Replies:
    13
    Views:
    823
    Dirk Thierbach
    Nov 3, 2004
  5. Woga Swoga
    Replies:
    5
    Views:
    126
    Drew Olson
    Dec 20, 2006
Loading...

Share This Page