 # Learn to program (Chris Pine) array.sort

Hey,

I’ve searched the archives and don’t see anything on this problem for
Chris’ book. From Chapter 7 on Arrays and Iterators:

“Write the program we talked about at the very beginning of this
chapter.
Hint: There’s a lovely array method which will give you a sorted
version of an array: sort. Use it!”

From the beginning of the chapter:

“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.”

Here is what I have that works, but I had to change it in order to get
the program to exit. You see I can’t seem to get the program to exit
on just entering a blank line at the prompt, so I wrote some code so
that it would quit with when I entered the word “bye.”

array_input = []
input = ‘’
while input != “bye”
input = gets.chomp
array_input << input
end
puts
array_input.delete_if { |x| x == ‘bye’ }
puts array_input.sort

Problem is I would actually like to know how to solve the problem as
written! So when I use "while input != “something goes here and I’m
not sure what to use to designate a blank line.” Then I can eliminate
the second to the last line.

Thanks!

Geoff

“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.”

Problem is I would actually like to know how to solve the problem as
written! So when I use "while input != “something goes here and I’m
not sure what to use to designate a blank line.” Then I can eliminate
the second to the last line.

I would think “\n” would designate a blank line, or you could chomp
first and check to see if its empty; here’s my solution:

wordlist = []
while line = gets
if line.chomp!.empty?
puts wordlist.sort
exit
else
wordlist << line
end
end

Mitchell

Geoff wrote:
[…]

Problem is I would actually like to know how to solve the problem as
written! So when I use "while input != “something goes here and I’m
not sure what to use to designate a blank line.” Then I can eliminate
the second to the last line.

Hi! gets returns a blank line as a string with only the line feed
character ("\n"). chomp chops it off and nothing remains, so after
input = gets.chomp
you can compare input with ‘’ to see if you have an empty line.

Delete that first spurious assignment to input (and don’t put it back)
and you’ll find the solution. Good luck.

Geoff wrote:

From the beginning of the chapter:

Problem is I would actually like to know how to solve the problem as
written! So when I use "while input != “something goes here and I’m
not sure what to use to designate a blank line.” Then I can eliminate
the second to the last line.

Thanks!

Geoff

array_input = []
while (input = gets.chomp) != “”
array_input << input
end
puts
puts array_input.sort

Or:

array_input = []
loop do
input = gets.chomp
break if “” == input
array_input << input
end
puts
puts array_input.sort

You could leave the “\n” at the end of each line. However, if you
forget about the “\n”, it may cause you lots of trouble.

array_input = []
loop do
input = gets
break if “\n” == input
array_input << input
end
puts
puts array_input.sort

On Jul 26, 2006, at 12:25 AM, Carlos wrote:

Delete that first spurious assignment to input (and don’t put it
back) and you’ll find the solution. Good luck.

There’s nothing wrong with the initial assignment in the original
message. Chris P. teaches this technique (initialize before loop,
test in loop condition, and change inside the loop) throughout his
book. I agree that it’s not the most elegant (thus the need for the
delete_if() call or similar trick), but it works just fine. You use
what you learn.

James Edward G. II

You’re very close to what is needed. The following variation on your
code will do the job.

Regards, Morton

#! /usr/bin/ruby -w

array_input = []
loop do
input = gets.chomp
break if input.empty?
array_input << input
end
puts
puts array_input.sort

I think I like this one the most. It’s closest to what I had in my head
as an ideal solution. I just didn’t know how to use the empty? method.

Also, in my while loop I needed to initialize the input variable before
the loop, but in the ‘loop do’ you use here it does not need to be.
Would you mind explaining why?

Thanks again,

Geoff

Also, in my while loop I needed to initialize the input variable before
the loop, but in the ‘loop do’ you use here it does not need to be.
Would you mind explaining why?

in ‘loop do’ input is tested after assignment
in while loop it is tested before assignment, so to do the test
reasonably, you have to provide some initial value.

notice the while (input = gets) != “\n” version - assignment and test
in one place (although I would say that this kind should be avoided if
possible, due to readability issues)

Jan S. wrote:

possible, due to readability issues)
Makes sense. Thanks for taking the time to explain!

Geoff wrote:

From the beginning of the chapter:

Problem is I would actually like to know how to solve the problem as
written! So when I use "while input != “something goes here and I’m
not sure what to use to designate a blank line.” Then I can eliminate
the second to the last line.

Thanks!

Geoff

Here is my solution:

wordlist = []
word = gets.chomp
while word != ‘’
wordlist.push word
word = gets.chomp
end
puts wordlist.sort

Randy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs