There's more than one way to do things PerlMonks

### (dws)Re: (Golf) Fragment Reassembly

by dws (Chancellor)
 on May 02, 2001 at 09:26 UTC ( #77244=note: print w/replies, xml ) Need Help??

in reply to (Golf) Fragment Reassembly

For my first round of Golf at the Monastery, here's a non-strict, non-polynomial approach.
```sub assemble {
## Begin Golf
\$w=join'',@_;t("",@_);\$w}
sub t{my\$s=shift;\$w=\$s if!@_ and length\$s<length\$w;
return if!@_;my@b=(@_,@_);
map{t(\$_,@b[0..\$#_-1])}_(\$s,shift@b)while(@b>@_)}
sub  _{my(\$a,\$b)=@_;\$l=length\$a;for(0..\$l){\$x=substr(\$a,0,\$_).\$b;
return\$x if substr(\$x,0,\$l)eq\$a}
## End Golf
}

print assemble qw(GATTACA ATTACA GATT AAGAT CCC)
emits AAGATTACACCC
That's 248 by my count (less the extra newlines that I threw in to avoid ugly wrapping). There's a 20 character test that prunes the search space back, but not enough (I think) to make it a polynomial solution.

Update: This is polynomial after all. I was too lazy to work that out at first. Bah.

Replies are listed 'Best First'.
Re: (dws)Re: (Golf) Fragment Reassembly
by dws (Chancellor) on May 02, 2001 at 21:20 UTC
A bit of tuning after reading japhy's Golf advice drops the score to 243
```sub assemble {
## Begin Golf
\$w=join'',@_;t("",@_);\$w}
sub t{my\$s=shift;\$w=\$s if!@_ and length\$s<length\$w;
return if!@_;my@b=(@_,@_);
map{t(\$_,@b[0..@_-2])}_(\$s,shift@b)while(@b>@_)}
sub  _{(\$a,\$b)=@_;\$l=length\$a;for(0..\$l){\$x=substr(\$a,0,\$_).\$b;
last if substr(\$x,0,\$l)eq\$a}\$x
## End Golf
}

print assemble qw(GATTACA ATTACA GATT AAGAT CCC)

Create A New User
Node Status?
node history
Node Type: note [id://77244]
help
Chatterbox?
 [Eily]: BTW LanX, you should try typing a few random chars at the beginning of each message. This will prevent expansion :P [LanX]: qwiud you sthink so? [LanX]: zxwqbd good idea! :) LanX embraces his new habit spqopiwjdnq [ambrus]: qQUkZTmHTuKxStGT- BzTIK9gdudif7TkTLI t3mnF144UaAZjkknXY 8nN-QM19wHBsTrp5vB lEYU_Kksa7X1RIBB4x EWLD5X7SW3jGX5ryfN OMn_yL5FTdQxzjhtyX mKN9sjUCzBNHK5Rrp0 S2WMUvIb1i9aZFgjtq VR0GH1bjPMvm1G16iz hBqc1U6toPd4FbJOFj VsOeT745AN1_pO88rD SRAYKtBZwCZedESZmN mvutrOTHiSNwflB- pRfn_k [Eily]: so far it seems to work Your Mother reminds the monks they should be grateful not to share an office, lest they be subjugated to constant inanities like, "Czech please!" [LanX]: what's strange is that the "Cowboy you said this already" message is missing #dqiwd [LanX]: YM: BTW learn to mute your humanity [Your Mother]: Cumin? Now I want tacos...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (17)
As of 2017-03-27 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (320 votes). Check out past polls.