(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;$_ }
In reply to Re: (Golf) Cryptographer's Tool #1
by chipmunk
in thread (Golf) Cryptographer's Tool #1
by Masem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |