in reply to Golf: Arbitrary Alphabetical Sorting
Two subtle details. The /s is needed if returns are in the alphabet. And you cannot save a character by replacing string with numerical comparison without breaking on fairly small words.sub o{ ($c,$s)=@_;@m{@$c}=map{10*@$c+$_}0..@$c;sub t{$_=pop;s/./$m{$&}/gs;$_} +sort{t($a)cmp t($b)}@$s }
UPDATE
MeowChow is right. :-( But the following 91 character
version will handle alphabets of up to 90,000 characters.
Given that this is bigger than Unicode's space, that is sufficient for any alphabet that any version of Perl will accept:
(And so we create more legacy code with a stupid hard-coded limit that is bound to come back to bite us...)sub o{ ($c,$s)=@_;@m{@$c}=map{$_+9999}1..@$c;sub t{$_=pop;s/./$m{$&}/gs;$_}so +rt{t($a)cmp t($b)}@$s }
UPDATE 2
MeowChow told me to lose parens gaining 2, and to make the range several hundred million by rewriting the number. Also I commified a map. This falls to 88. Or change to "99" to make it 86 if you are willing to accept one-byte alphabets.
sub o{ ($c,$s)=@_;@m{@$c}=map$_+9**9,1..@$c;sub t{$_=pop;s/./$m{$&}/gs;$_}sor +t{t($a)cmp t$b}@$s }
UPDATE 3
ChemBoy dropped to 87 by rewriting the number again:
sub o{ ($c,$s)=@_;@m{@$c}=map$_+1e9,1..@$c;sub t{$_=pop;s/./$m{$&}/gs;$_}sort +{t($a)cmp t$b}@$s }
UPDATE 4
tye made it 86 with:
sub o{ ($c,$s)=@_;@m{@$c}=map$_+~0,1..@$c;sub t{$_=pop;s/./$m{$&}/gs;$_}sort{ +t($a)cmp t$b}@$s }
UPDATE 5
And I finish the round of golf by making it 83 with:
(The ~0 trick doesn't work with ranges.)sub o{ ($c,$s)=@_;@m{@$c}=1e8..@$c+1e8;sub t{$_=pop;s/./$m{$&}/gs;$_}sort{t($ +a)cmp t$b}@$s }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re (tilly) 1: Golf: Arbitrary Alphabetical Sorting
by MeowChow (Vicar) on May 09, 2001 at 22:03 UTC | |
by tilly (Archbishop) on May 10, 2001 at 00:40 UTC | |
by tadman (Prior) on May 10, 2001 at 01:39 UTC | |
by tadman (Prior) on May 10, 2001 at 03:04 UTC | |
by chipmunk (Parson) on May 10, 2001 at 05:18 UTC | |
by tilly (Archbishop) on May 10, 2001 at 11:26 UTC |