Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
(rosalind) EUBT
  • Loading branch information
grondilu committed Apr 28, 2013
1 parent d5f05a4 commit 1c124a8
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions rosalind/eubt-grondilu.pl
@@ -0,0 +1,36 @@
proto combine (Int, @) {*}
multi combine (0, @) { [] }
multi combine ($, []) { () }
multi combine ($n, [$head, *@tail]) {
map(
{ [$head, @^others] },
combine($n-1, @tail)
),
combine($n, @tail);
}

sub ncombine($n, $k) { (state %){$n}{$k} //= combine($k, [^$n]) }
sub eubt(@species) {
if @species == 1 { return [ @species ] }
elsif @species == 2 { return [ sprintf "(%s,%s)", @species ] }
elsif @species >= 3 {
gather for 1 .. +@species div 2 -> $k {
my %seen;
for ncombine(+@species, $k)[].map( { [ @species[@$_] ] } ) -> @picked {
%seen{join ':', @picked}++;
my @left = grep none(@picked), @species;
next if %seen{join ':', @left};
for (eubt(@picked) »~» ',') X~ eubt(@left) {
take [ "($_)" ]
}
}
}
}
else { !!! 'unexpected number of species' }
}

my @data = get.words; #<dog cat mouse elephant>;
my $first = @data.shift;
printf "(%s)%s\n", $_, $first for eubt @data;

# vim: ft=perl6

0 comments on commit 1c124a8

Please sign in to comment.