Another Chris Pine Tutorial Question

Discussion in 'Ruby' started by danielj, Aug 30, 2008.

  1. danielj

    danielj Guest

    Let's write a program which asks us to type in as many words as we
    want (one word per line, continuing until we just press Enter on an
    empty line), and which then repeats the words back to us in
    alphabetical order. OK?

    So... first we'll—uh... um... hmmm... Well, we could—er... um...

    You know, I don't think we can do it. We need a way to store an
    unknown amount of words, and how to keep track of them all together,
    so they don't get mixed up with other variables. We need to put them
    in some sort of a list. We need arrays.
    puts "Let's make a shopping list!"

    shoplist = []

    while shoplist.last != ''

    shoplist.push(gets.downcase.chomp)

    end

    shoplist.pop


    puts shoplist.sort

    The program asks for one word per line but there is no way one can do
    that utilizing the methods the tutorial has expounded upon up to this
    point so I settled for what I got here.

    However, it asks you to then do this:

    • Try writing the above program without using the sort method. A large
    part of programming is solving problems, so get all the practice you
    can!

    Could somebody point me in the right direction, or give me an example
    of how that would be possible? I'm racking my brain but I can not
    think of a way to do it without using methods and tools that you are
    not yet supposed to use at this point in the tutorial (i.e. regex)
     
    danielj, Aug 30, 2008
    #1
    1. Advertising

  2. danielj

    danielj Guest

    testing
     
    danielj, Aug 31, 2008
    #2
    1. Advertising

  3. On 30.08.2008 22:55, danielj wrote:
    > However, it asks you to then do this:
    >
    > • Try writing the above program without using the sort method. A large
    > part of programming is solving problems, so get all the practice you
    > can!
    >
    > Could somebody point me in the right direction, or give me an example
    > of how that would be possible? I'm racking my brain but I can not
    > think of a way to do it without using methods and tools that you are
    > not yet supposed to use at this point in the tutorial (i.e. regex)


    The trick I assume Chris is targeting at is to insert words at the
    correct position. That way your list is always sorted and you do not
    need an explicit sort operation.

    Kind regards

    robert
     
    Robert Klemme, Aug 31, 2008
    #3
  4. On 2008-08-31 13:00:33 +0100, Robert Klemme <> said:

    > The trick I assume Chris is targeting at is to insert words at the
    > correct position. That way your list is always sorted and you do not
    > need an explicit sort operation.


    That's a good idea. Or I suppose you could implement your own sorting
    method and call that instead.
     
    Graham Ashton, Aug 31, 2008
    #4
  5. danielj

    timr Guest

    On Aug 30, 1:55 pm, danielj <> wrote:
    >  Let's write a program which asks us to type in as many words as we
    > want (one word per line, continuing until we just press Enter on an
    > empty line), and which then repeats the words back to us in
    > alphabetical order. OK?
    >
    > So... first we'll—uh... um... hmmm... Well, we could—er... um...
    >
    > You know, I don't think we can do it. We need a way to store an
    > unknown amount of words, and how to keep track of them all together,
    > so they don't get mixed up with other variables. We need to put them
    > in some sort of a list. We need arrays.
    > puts "Let's make a shopping list!"
    >
    > shoplist = []
    >
    > while shoplist.last != ''
    >
    > shoplist.push(gets.downcase.chomp)
    >
    > end
    >
    > shoplist.pop
    >
    > puts shoplist.sort
    >
    > The program asks for one word per line but there is no way one can do
    > that utilizing the methods the tutorial has expounded upon up to this
    > point so I settled for what I got here.
    >
    > However, it asks you to then do this:
    >
    > • Try writing the above program without using the sort method. A large
    > part of programming is solving problems, so get all the practice you
    > can!
    >
    > Could somebody point me in the right direction, or give me an example
    > of how that would be possible? I'm racking my brain but I can not
    > think of a way to do it without using methods and tools that you are
    > not yet supposed to use at this point in the tutorial (i.e. regex)


    words = []
    loop do #loop allows one to do an indefinite amount
    of something--must set an exit
    #inside of the loop.
    incoming = gets
    if incoming == "\n" then #if a blank line is entered, we are done.
    Sort and puts the list.
    puts words.sort
    exit
    else
    words << incoming #if data is entered, add it to the array of
    words.
    end
    end

    #=>
    alphabet soup
    bananas
    coca cola
    hot dogs
    kool aid
    lettuce
    milk
    potatoes
    spinach
    sugar

    Tim Rand
     
    timr, Sep 1, 2008
    #5
  6. danielj

    timr Guest

    On Aug 31, 11:21 pm, timr <> wrote:
    > On Aug 30, 1:55 pm, danielj <> wrote:
    >
    >
    >
    > >  Let's write a program which asks us to type in as many words as we
    > > want (one word per line, continuing until we just press Enter on an
    > > empty line), and which then repeats the words back to us in
    > > alphabetical order. OK?

    >
    > > So... first we'll—uh... um... hmmm... Well, we could—er... um...

    >
    > > You know, I don't think we can do it. We need a way to store an
    > > unknown amount of words, and how to keep track of them all together,
    > > so they don't get mixed up with other variables. We need to put them
    > > in some sort of a list. We need arrays.
    > > puts "Let's make a shopping list!"

    >
    > > shoplist = []

    >
    > > while shoplist.last != ''

    >
    > > shoplist.push(gets.downcase.chomp)

    >
    > > end

    >
    > > shoplist.pop

    >
    > > puts shoplist.sort

    >
    > > The program asks for one word per line but there is no way one can do
    > > that utilizing the methods the tutorial has expounded upon up to this
    > > point so I settled for what I got here.

    >
    > > However, it asks you to then do this:

    >
    > > • Try writing the above program without using the sort method. A large
    > > part of programming is solving problems, so get all the practice you
    > > can!

    >
    > > Could somebody point me in the right direction, or give me an example
    > > of how that would be possible? I'm racking my brain but I can not
    > > think of a way to do it without using methods and tools that you are
    > > not yet supposed to use at this point in the tutorial (i.e. regex)



    SORRY. AFTER FIXING THE COMMENTS TO BE MORE READABLE--

    >
    > words = []
    > loop do                   #loop allows one to do an indefinite amount
    > #of something--must set an exit
    >                           #inside of the loop.
    > incoming = gets
    > if incoming == "\n" then  #if a blank line is entered, we are done.
    > #Sort and puts the list.
    >   puts words.sort
    >   exit
    > else
    >   words << incoming       #if data is entered, add it to the array of
    > words.
    > end
    > end
    >
    > #=>
    > alphabet soup
    > bananas
    > coca cola
    > hot dogs
    > kool aid
    > lettuce
    > milk
    > potatoes
    > spinach
    > sugar
    >
    > Tim Rand
     
    timr, Sep 1, 2008
    #6
  7. danielj

    timr Guest

    words = []
    loop do #loop allows one to do an indefinite amount
    # of something--must set an exit
    #inside of the loop.
    incoming = gets
    if incoming == "\n" then #if a blank line is entered, we are done.
    #Sort and puts the list.
    puts words.sort
    exit
    else
    words << incoming #if data is entered, add it to the array of
    words.
    end
    end

    #=>
    alphabet soup
    bananas
    coca cola
    hot dogs
    kool aid
    lettuce
    milk
    potatoes
    spinach
    sugar

    Tim Rand
     
    timr, Sep 1, 2008
    #7
    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. Jan_K
    Replies:
    61
    Views:
    638
    Eleanor McHugh
    May 28, 2008
  2. danielj
    Replies:
    10
    Views:
    302
    Michael Morin
    Aug 28, 2008
  3. Replies:
    9
    Views:
    439
    whisperjim
    Nov 27, 2008
  4. Replies:
    0
    Views:
    272
  5. whisperjim
    Replies:
    9
    Views:
    179
    Sam Howes
    Aug 31, 2009
Loading...

Share This Page