#!/usr/bin/perl use strict; use warnings; my $alphabet = uc('ADEFGMSTV'); my $N = length($alphabet); my $L = $N - 1; my @ltr_lkup = $alphabet =~ /./g; my %idx_lkup = map { substr($alphabet, $_, 1) => $_ } 0..$L; sub tupple { return join '', @ltr_lkup[ sort { $a <=> $b } @_ ]; } sub find { my ($counts) = @_; my %solutions; local *try_straight_tupple = sub { my ($counts, $i, $solution) = @_; my $instances = $counts->[$i]; if ($instances == 0) { process($counts, $i+1, $solution); return; } return if $i+3 > $L || $counts->[$i+1] < $instances || $counts->[$i+2] < $instances || $counts->[$i+3] < $instances; my @counts = @$counts; my @solution = ( @$solution, tupple( $i+0 .. $i+3 ) ); $counts[$i+$_] -= $instances for 0..3; process(\@counts, $i+1, \@solution); }; local *try_quad_tupple = sub { my ($counts, $i, $solution) = @_; try_straight_tupple($counts, $i, $solution); if ($counts->[$i] < 4) { return; } my @counts = @$counts; my @solution = ( @$solution, tupple( ($i) x 4 ) ); while ($counts[$i] >= 4) { $counts[$i] -= 4; try_straight_tupple(\@counts, $i, \@solution); } }; local *process = sub { my ($counts, $i, $solution) = @_; if (!grep $_, @$counts) { ++$solutions{ join(';', @$solution) }; return; } if ($i > $L) { return; } try_quad_tupple($counts, $i, $solution); }; try_quad_tupple($counts, 0, []); return [ sort keys %solutions ]; } { my ($input) = @ARGV or die("usage: $0 string\n"); $input = uc($input); my @counts; while ($input =~ /(.)/g) { defined($idx_lkup{$1}) or die("'$1' not in alphabet\n"); ++$counts[ $idx_lkup{$1} ]; } $counts[$_] ||= 0 for 0..$L; # Avoid warnings. my $solutions = find(\@counts); die "No solution\n" if !@$solutions; print("$_\n") for @$solutions; }
$ perl all.pl AAAAADDDDDEFFGMMSSTVVVVV AAAA;ADEF;DDDD;FGMS;MSTV;VVVV perl all.pl AADDDEEEEFFFFGGMMMMMMMMMMSTV ADEF;DEFG;EFGM;MMMM;MSTV $ perl all.pl AAAADDDDEEEEFFFFGGGG AAAA;DDDD;EEEE;FFFF;GGGG AAAA;DEFG ADEF;GGGG

I think this can be optimised for strings with a lot of repetition, but I haven't found out how yet.


In reply to Re: finding tuples (all solutions) by ikegami
in thread finding tuples by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.