D
danielj
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)
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)