Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
TRIE solution. 50 seconds too slow though
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| use v6; | ||
|
|
||
| my Int $node = 1; | ||
|
|
||
| sub trie(@string is copy, $root = $node) { | ||
| @string .= grep: *.chars; | ||
| return {} if not @string; | ||
| hash gather for @string.classify(*.substr: 0, 1).kv -> $k, $v { | ||
| my @value = map *.substr(1), grep *.chars > 1, $v[]; | ||
| say "$root {++$node} $k"; | ||
| take $k => &?ROUTINE( @value, $node ) if @value; | ||
| } | ||
| } | ||
|
|
||
| trie <ATAGA ATC GAT>; | ||
|
|
||
| =END | ||
| use strict; | ||
| use warnings; | ||
| no warnings qw(recursion); | ||
|
|
||
| my $node = 1; | ||
|
|
||
| sub classify { | ||
| my $f = shift; | ||
| my %classif; | ||
| for (@_) { | ||
| $classif{$f->($_)} //= []; | ||
| push $classif{$f->($_)}, $_; | ||
| } | ||
| \%classif; | ||
| } | ||
|
|
||
| sub trie { | ||
| my @string = @{shift()}; | ||
| my $root = shift // $node; | ||
| return +{} if not @string; | ||
| my %trie; | ||
| my %classify = %{ classify sub { substr shift, 0, 1 }, @string }; | ||
| for my $key (keys %classify) { | ||
| my @value = map { substr $_, 1 } grep { length > 1 } @{$classify{$key}}; | ||
| printf "%i %i %s\n", $root, ++$node, $key; | ||
| $trie{$key} = trie( [ @value ], $node ) if @value; | ||
| } | ||
| return \%trie; | ||
| } | ||
|
|
||
| trie [ map { chomp; $_ } <> ]; | ||
| __END__ | ||
| trie [ qw(ATAGA ATC GAT) ]; | ||
|
|
||
| __END__ | ||
|
|
||
| # vim: ft=perl6 |