[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Pine's tutorial "Word Sorter" exercise

Randy Shipp

2/19/2007 6:41:00 PM

On Apr 1 2006, 8:12 pm, Jan_K <n...@none.com> wrote:
> Solution for the 1st exercise in chapter 8:
>
> (I'm skipping the 2nd exercise - just a bunch of busywork)
>
> -----------------------------------------------------------------------
> input = []
>
> while input.last != ''
> input.push gets.chomp
> end
>
> puts input.sort
> -----------------------------------------------------------------------

----------
To reset slightly, this is an exercise that tells you to accept words
from the user until they just hit Enter, then alphabetize the list.

My problem with the exercise is that the sort method alphabetizes
capital letters before lowercase letters. Therefore, if input ==
[ant, bear, Zebra], the output of

puts input.sort

is

Zebra
ant
bear

....which is clearly not what most users will expect. Short of
changing the line in the program to read

input.push gets.chomp.downcase

(thus altering the entered words in a possibly undesirable way), how
can we get the sort to work the way we might expect? Thanks in
advance.

Randy...

7 Answers

SonOfLilit

2/19/2007 6:54:00 PM

0

#sort and #sort_by accept blocks that tell how to do the comparison, so:

puts input.sort_by{|a| a.downcase}

is the simplest solution.

PLUG: The RubyMentor project is looking for newbies to help!
http://rubymentor.rubyforge.org/wi...

On 2/19/07, Randy Shipp <randyshipp@gmail.com> wrote:
> On Apr 1 2006, 8:12 pm, Jan_K <n...@none.com> wrote:
> > Solution for the 1st exercise in chapter 8:
> >
> > (I'm skipping the 2nd exercise - just a bunch of busywork)
> >
> > -----------------------------------------------------------------------
> > input = []
> >
> > while input.last != ''
> > input.push gets.chomp
> > end
> >
> > puts input.sort
> > -----------------------------------------------------------------------
>
> ----------
> To reset slightly, this is an exercise that tells you to accept words
> from the user until they just hit Enter, then alphabetize the list.
>
> My problem with the exercise is that the sort method alphabetizes
> capital letters before lowercase letters. Therefore, if input ==
> [ant, bear, Zebra], the output of
>
> puts input.sort
>
> is
>
> Zebra
> ant
> bear
>
> ...which is clearly not what most users will expect. Short of
> changing the line in the program to read
>
> input.push gets.chomp.downcase
>
> (thus altering the entered words in a possibly undesirable way), how
> can we get the sort to work the way we might expect? Thanks in
> advance.
>
> Randy...
>
>
>

td

2/3/2014 1:28:00 PM

0

On Monday, February 3, 2014 8:20:12 AM UTC-5, O wrote:
> In article <b0ee9e18-b1ec-4427-8024-a39f346ec79c@googlegroups.com>, td
>
> <tomdedeacon@mac.com> wrote:
>
>
>
> > On Tuesday, January 21, 2014 1:06:36 PM UTC-5, td wrote:
>
> > > News has come that she will sing the National Anthem at the Super Bowl.
>
> >
>
> > And the jury is in:
>
> >
>
> >
>
> > http://bleacherreport.com/articles/1945767-twitter-reacts-to-renee-f...
>
> > 14-super-bowl-national-anthem-performance
>
> >
>
>
>
> Who elected Twitter celebrities to the jury?

Not sure about a jury. But they sure did listen.

I like the hoi polloi's take on things. It's a good antidote to the tight-assed opera queen and her endless drivel.

TD

ivanmaxim

2/3/2014 1:28:00 PM

0

On Monday, February 3, 2014 8:20:12 AM UTC-5, O wrote:
> In article <b0ee9e18-b1ec-4427-8024-a39f346ec79c@googlegroups.com>, td
>
> <tomdedeacon@mac.com> wrote:
>
>
>
> > On Tuesday, January 21, 2014 1:06:36 PM UTC-5, td wrote:
>
> > > News has come that she will sing the National Anthem at the Super Bowl.
>
> >
>
> > And the jury is in:
>
> >
>
> >
>
> > http://bleacherreport.com/articles/1945767-twitter-reacts-to-renee-f...
>
> > 14-super-bowl-national-anthem-performance
>
> >
>
>
>
> Who elected Twitter celebrities to the jury?
>
>
>
> -Owen

This is how you sing the National Anthem - not like Rogers and Hammerstein and not like a pop song going up the charts. its enough sung straight and needs no further help from "Divas" after 200 years. BTW this man was 75 years old at the time - even older than Deacon!!!!!!

http://www.youtube.com/watch?v=7...

ivanmaxim

2/3/2014 1:34:00 PM

0

On Monday, February 3, 2014 8:27:37 AM UTC-5, td wrote:
> On Monday, February 3, 2014 8:20:12 AM UTC-5, O wrote:
>
> > In article <b0ee9e18-b1ec-4427-8024-a39f346ec79c@googlegroups.com>, td
>
> >
>
> > <tomdedeacon@mac.com> wrote:
>
> >
>
> >
>
> >
>
> > > On Tuesday, January 21, 2014 1:06:36 PM UTC-5, td wrote:
>
> >
>
> > > > News has come that she will sing the National Anthem at the Super Bowl.
>
> >
>
> > >
>
> >
>
> > > And the jury is in:
>
> >
>
> > >
>
> >
>
> > >
>
> >
>
> > > http://bleacherreport.com/articles/1945767-twitter-reacts-to-renee-f...
>
> >
>
> > > 14-super-bowl-national-anthem-performance
>
> >
>
> > >
>
> >
>
> >
>
> >
>
> > Who elected Twitter celebrities to the jury?
>
>
>
> Not sure about a jury. But they sure did listen.
>
>
>
> I like the hoi polloi's take on things. It's a good antidote to the tight-assed opera queen and her endless drivel.
>
>
>
> TD

Sure - when they agree with your futile and hopeless causes. Poor thing. I have you so figured out its just become game playing.

td

2/3/2014 1:42:00 PM

0

On Monday, February 3, 2014 8:33:41 AM UTC-5, Willem Orange wrote:

> Sure - when they agree with your futile and hopeless causes. Poor thing. I have you so figured out its just become game playing.

You couldn't figure out how to take a pee.

Do it the right way, girl, and just squat and squeeze.

TD

ivanmaxim

2/3/2014 1:47:00 PM

0

On Monday, February 3, 2014 8:42:00 AM UTC-5, td wrote:
> On Monday, February 3, 2014 8:33:41 AM UTC-5, Willem Orange wrote:
>
>
>
> > Sure - when they agree with your futile and hopeless causes. Poor thing. I have you so figured out its just become game playing.
>
>
>
> You couldn't figure out how to take a pee.
>
>
>
> Do it the right way, girl, and just squat and squeeze.
>
>
>
> TD

There's the Deacon we all know and love - our groups very own Patient Zero. I'm sure Max will find this a real example of you on the high road (an oxymoron if there ever was one)

Norman Schwartz

2/3/2014 11:37:00 PM

0

Willem Orange wrote:
> On Monday, February 3, 2014 8:20:12 AM UTC-5, O wrote:
>> In article <b0ee9e18-b1ec-4427-8024-a39f346ec79c@googlegroups.com>,
>> td
>>
>> <tomdedeacon@mac.com> wrote:
>>
>>
>>
>>> On Tuesday, January 21, 2014 1:06:36 PM UTC-5, td wrote:
>>
>>>> News has come that she will sing the National Anthem at the Super
>>>> Bowl.
>>
>>>
>>
>>> And the jury is in:
>>
>>>
>>
>>>
>>
>>> http://bleacherreport.com/articles/1945767-twitter-reacts-to-renee-f...
>>
>>> 14-super-bowl-national-anthem-performance
>>
>>>
>>
>>
>>
>> Who elected Twitter celebrities to the jury?
>>
>>
>>
>> -Owen
>
> This is how you sing the National Anthem - not like Rogers and
> Hammerstein and not like a pop song going up the charts. its enough
> sung straight and needs no further help from "Divas" after 200 years.
> BTW this man was 75 years old at the time - even older than
> Deacon!!!!!!
>
> http://www.youtube.com/watch?v=7...

Here he sings it in 1944:
https://archive.org/details/Mo...

Wiki shows that he quipped about himself being known as "The
'Say-Can-You-See' guy!"
http://en.wikipedia.org/wiki/Robe...