List Info

Thread: Re: Alphabetize a word




Re: Alphabetize a word
country flaguser name
United States
2007-09-05 11:15:23

Ah, thanks. Neat little trick. Well I finished my first complete working
version of the code and is a bit sluggish. Though this is expected since
the input file contains about 170,000 words stuffed into my WORDS array.
Wondering if you guys know any tricks to speed things up anywhere? Here
is a link to my source code: http://pastie.textmate.org/94246

Ryan

Paul Archer < tigger%40io.com">tiggerio.com>;
Sent by: perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com
09/05/2007 11:28 AM
Please respond to
perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com

To
perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com
cc

Subject
Re: [PBML] Alphabetize a word

The slashes delimit a regular expression. In this case, the regexp is
empty.
Using an empty regular expression for the split means that each character
will become a separate element in our array.

Paul

10:21am, Ryan J Nauman wrote:

> What does the pattern " //" break down to? I'm somewhat familiar with
>; regular expressions and don't know what a forward slash does. I would
&gt; have guessed the matching pattern would've been the ".&quot; character that
>; matches any character (except n).
>
>
&gt;
>
> Paul Archer < tigger%40io.com">tiggerio.com>;
> Sent by: perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com
> 09/05/2007 10:12 AM
> Please respond to
> perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com
>
>
> To
> perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com
> cc
>
> Subject
> Re: [PBML] Alphabetize a word
>;
>
>
>
>
&gt;
> Which gets us to the oneliner:
> perl -e ' $word=&quot;alphabetize"; print join "&quot;, (sort split ( //, $word)),
> "n&quot;'
>
> 1:41pm, greenberg.d%40gmail.com">greenberg.dgmail.com wrote:
&gt;
>&gt; As for returning it as a string instead of an array of characters, look
>; at the "join" function. With it, you can explicitly return a string.
>>
>> -David
&gt;>
&gt;> Sent via BlackBerry by AT&T
>>
>> -----Original Message-----
>&gt; From: Paul Archer < tigger%40io.com">tiggerio.com>;
>>;
>>; Date: Wed, 5 Sep 2007 08:20:52
>> To: perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com
>> Subject: Re: [PBML] Alphabetize a word
>;>
>;>
>;> If what you are looking for is a list of the letters in a word, in
>&gt; alphabetical order, then you want something like:
&gt;>
&gt;> perl -e ' $word=&quot;alphabetize"; array= sort split ( //, $word); print
&gt;> array, "n&quot;'
>>
>> And you're doing fine with the 'shift'. No reason not to do it just the
> way
>> you have it.
>>
>> Paul
>;>
>;> 9:13am, Ryan J Nauman wrote:
&gt;>
&gt;>> I wrote the following code to alphabetize a word:
&gt;>>
>>&gt; sub alphabetize
>>;> {
>&gt;> my $word = uc(shift);
>>> my $wordlen = length($word);
>;>> my letters;
>>&gt;
>&gt;> for (my $i = 0; $i < length $word; $i++)
>>&gt; {
>&gt;> push letters, substr($word, $i, 1);
>>> }
>&gt;>
&gt;>> letters = sort letters;
>>&gt; return letters;
>>&gt; }
>&gt;>
&gt;>> This function will always only receive one parameter. In this case I
> do
>&gt;> not know if shift is the best method or not. $_ was not giving me
>&gt;> anything so I went with shift. Alternatively, I think I could've used
>; _.
>&gt;> Does it matter?
>>>;
>>;> If it can be optimized in any way I would appreciate pointers. My
>&gt;> ultimate question though is I want it to return a string instead of
> the
>>> sorted character array that it is now. I can write it using a for loop
>;>> and string concatenation but I think that would be extremely
> inefficient.
>&gt;> Help appreciated!
>&gt;>
&gt;>>
>>&gt; [Non-text portions of this message have been removed]
>>&gt;
>&gt;>
&gt;>
&gt;> ---------------------------
>> 404 Error - Item Not Found
&gt;> <haiku&gt;
>&gt; You step in the stream,
>> but the water has moved on.
>> That page is not here.
&gt;> </haiku>
>> ---------------------------
>>
>> -----11004 days until retirement!-----
&gt;>
&gt;>
&gt;> Unsubscribing info is here:
&gt; http://help.yahoo.com/help/us/groups/groups-32.html
>&gt; Yahoo! Groups Links
&gt;>
&gt;>
&gt;>
&gt;>
&gt;>
&gt;
> __________________________________________________________
> "Can't you recognize bullshit? Don't you think it would be a
> useful item to add to your intellectual toolkits to be capable
> of saying, when a ton of wet steaming bullshit lands on your
>; head, 'My goodness, this appears to be bullshit'?
> _____________Neal Stephenson, "Cryptonomicon&quot;__________________
>
> -----11004 days until retirement!-----
&gt;
>
>
&gt; [Non-text portions of this message have been removed]
>
>;

---------------------------------------------------------
";You will always be lucky if you know how to make friends
with strange cats."; - Colonial American proverb
---------------------------------------------------------

-----11004 days until retirement!-----


[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re: Alphabetize a word
country flaguser name
United States
2007-09-05 11:36:55

>>&gt;>> "Ryan" == Ryan J Nauman < RJNauman%40uss.com">RJNaumanuss.com&gt; writes:

Ryan> Ah, thanks. Neat little trick. Well I finished my first complete working
Ryan>; version of the code and is a bit sluggish. Though this is expected since
Ryan>; the input file contains about 170,000 words stuffed into my WORDS array.
Ryan>; Wondering if you guys know any tricks to speed things up anywhere? Here
Ryan>; is a link to my source code: http://pastie.textmate.org/94246

Ooof, yeah. You're doing an exponential matching O(n squared)
instead of just stashing everything according to its anagram.

You need to use a "bucket" approach. As you compute each anagram,
dump the original word into a bucket keyed by anagram, using a hash
of arrayrefs...

my %buckets;
for my $word (words) {
my $alphaword = alphabetize($word);
push {$bucket{$alphaword}}, $word;
}

Now it's simply a matter of dumping each bucket:

for my $alphaword (sort keys %bucket) {
my $aref = $bucket{$alphaword};
print "$arefn&quot;;
}

That will be infinitely faster when you get above a few hundred words.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<; merlyn%40stonehenge.com">merlynstonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )