I believe it handles all the cases correctly. Here are the tests I used:sub mu { for(@s=($_[0],$c=0);$c<$_[2];){($_=shift@s or$c++,$s[@s]=0,next )eq$_[1]&&return$c;$s[@s]="$_$1"if/^M/;/I$/and$s[@s]=$_.U;$s[ @s]="$`$'"while/UU/g;$s[@s]=$`.U.substr$',2while/I(?=II)/g}0 }
The second to last line tests rule 3 where there are four Is in a row, and the last line tests rule 1.print mu( "MUUUU", "M", 20 ); # returns 2 print mu( "MI", "MU", 7 ); # returns 0 print mu( "MI", "MI", 20 ); # returns 0 print mu( "MI", "MUIUI", 20); # returns 4 print mu( "MI", "MUIUI", 3 ); # returns 0 print mu( "MIIII", "MIU", 4); # returns 1 print mu( "MI", "MIIU", 4); # returns 2
I switched the limit on the second test from 20 to 7 because I didn't have the patience to wait for the sub to run through all the possibilities to 20 steps. :)
In reply to Re: (Golf) Gödel, Escher, and Bach, Oh My!
by chipmunk
in thread (Golf) Gödel, Escher, and Bach, Oh My!
by Masem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |