/prefix_suffix.pm6 Secret
Last active
August 29, 2015 13:57
Star
You must be signed in to star a gist
factor out common prefixes and suffixes if possible.
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
| my $in = "rakudo-parrot c8ec1d, rakudo-jvm c8ec1d, rakudo-moar c8ec1d, niecza v24-109-g48a8de3"; | |
| my @pieces = $in.split(", "); | |
| class Score is rw { | |
| has $.prefix = 0 but ""; | |
| has $.postfix = 0 but ""; | |
| has @.subpieces; | |
| has @.rest; | |
| has $.output; | |
| } | |
| my %scoring{Any}; | |
| for @pieces.keys.combinations(1..*) { | |
| my @subpieces = @pieces[@$_]; | |
| my Score $score .= new(:@subpieces); | |
| for 1..([max] @subpieces>>.chars) { | |
| last unless [eq] @subpieces>>.substr(0, $_); | |
| $score.prefix = $_ but @subpieces[0].substr(0, $_); | |
| } | |
| for 1..(([max] @subpieces>>.chars) - $score.prefix) { | |
| last unless [eq] @subpieces>>.substr(* - $_); | |
| $score.postfix = $_ but @subpieces[0].substr(* - $_); | |
| } | |
| $score.rest = @subpieces>>.substr($score.prefix, * - $score.postfix); | |
| $score.output = ~$score.prefix ~ '{' ~ $score.rest.join(", ") ~ '}' ~ ~$score.postfix; | |
| %scoring{@subpieces.item} = $score; | |
| } | |
| .value.output.say for %scoring; | |
| # now that we have simple bits, we can construct bigger pieces out of them | |
| # and just use the shortest for each combination | |
| for %scoring.kv -> $pieces, $score { | |
| # @pieces is our target combination of bits. | |
| # we'll look for ways to shorten the output we've got by replacing | |
| # sub-sections with other smaller thingies. | |
| say "we're trying to improve our $pieces score"; | |
| say %scoring.keys.perl; | |
| for %scoring.keys.combinations(1..*) { | |
| # first, check if we'll reach 100% coverage of our $pieces | |
| say "we're now trying to get to: $pieces"; | |
| say $_; | |
| say; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment