Perl Monk, Perl Meditation 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)
[download]```
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)
[download]```

Log In?
 Username: Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://77244]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2021-12-08 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
R or B?

Results (36 votes). Check out past polls.

Notices?