in reply to (Golf) Cryptographer's Tool #1
(which is strict compliant, but only because at that point it wasn't any shorter being non-strict compliant. :)sub canonical { my(@a,$i,%c,$c)=pop=~/./g;$c.=$c{$_}||=$a[$i++]for pop=~/./g;$c }
However, here's a 46 character solution building off of btrott's solutions:
Can be called multiple times, of course, as in the example.sub canonical { ($_,$a,%h)=@_;s!.!$h{$&}||=($a=~/./g,$&)!ge;$_ }
Update: Well, tilly's been mumbling something about following the spec :) , so here's a solution that accepts any ASCII character (including zero and linefeed) in the word or the alphabet:
At 62, it actually beats my original solution by 1 character!sub canonical { ($_,$a,%h)=@_;s!.!{$h{$&}=~s+^\z+$a=~/./gs,$&+ge}$h{$&}!gse;$_ }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: (Golf) Cryptographer's Tool #1
by MeowChow (Vicar) on Jun 20, 2001 at 03:27 UTC | |
by chipmunk (Parson) on Jun 20, 2001 at 06:05 UTC | |
Re: Re: (Golf) Cryptographer's Tool #1
by srawls (Friar) on Jun 20, 2001 at 02:17 UTC | |
by chipmunk (Parson) on Jun 20, 2001 at 02:19 UTC | |
by sean (Beadle) on Jun 20, 2001 at 05:08 UTC | |
by MeowChow (Vicar) on Jun 20, 2001 at 03:31 UTC | |
by srawls (Friar) on Jun 20, 2001 at 04:20 UTC | |
by MeowChow (Vicar) on Jun 20, 2001 at 04:41 UTC | |
by srawls (Friar) on Jun 20, 2001 at 04:57 UTC |