| 1 | package Regexp::Sudoku::Utils; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 36 | ▶ | use 5.028; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 36 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 36 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 36 | ▶ | no warnings 'syntax'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 36 | ▶ | use experimental 'signatures'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 36 | ▶ | use experimental 'lexical_subs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 36 | our $VERSION = '2022030401'; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 36 | ▶ | use Exporter (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 36 | our @ISA = qw [Exporter]; | |||||||||||||||||||
| 17 | 36 | our @EXPORT = qw [$SENTINEL $DEFAULT_SIZE $NR_OF_DIGITS $NR_OF_LETTERS | |||||||||||||||||||
| 18 | $NR_OF_LETTERS $NR_OF_SYMBOLS $ANTI_KING $ANTI_KNIGHT | ||||||||||||||||||||
| 19 | $MAIN_DIAGONAL $MINOR_DIAGONAL | ||||||||||||||||||||
| 20 | cell_name cell_row_column all_pairs semi_debruijn_seq]; | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 36 | ▶ | use Math::Sequence::DeBruijn; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 36 | our $SENTINEL = "\n"; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 36 | our $DEFAULT_SIZE = 9; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 36 | our $NR_OF_DIGITS = 9; | |||||||||||||||||||
| 29 | 36 | our $NR_OF_LETTERS = 26; | |||||||||||||||||||
| 30 | 36 | our $NR_OF_SYMBOLS = $NR_OF_DIGITS + $NR_OF_LETTERS; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 36 | our $ANTI_KNIGHT = 1; | |||||||||||||||||||
| 33 | 36 | our $ANTI_KING = 2; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 36 | our $MAIN_DIAGONAL = 1; | |||||||||||||||||||
| 36 | 36 | our $MINOR_DIAGONAL = 2; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | ################################################################################ | ||||||||||||||||||||
| 40 | # | ||||||||||||||||||||
| 41 | # sub cell_name ($row, $column) | ||||||||||||||||||||
| 42 | # | ||||||||||||||||||||
| 43 | # Given a row number and a cell number, return the name of the cell. | ||||||||||||||||||||
| 44 | # | ||||||||||||||||||||
| 45 | # TESTS: Utils/100-cell_name_row_column.t | ||||||||||||||||||||
| 46 | # | ||||||||||||||||||||
| 47 | ################################################################################ | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 31465 | ▶ | sub cell_name ($row, $column) { | ||||||||||||||||||
|
Subroutine
cell_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 50 | 31465 | "R" . $row . "C" . $column | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | ################################################################################ | ||||||||||||||||||||
| 55 | # | ||||||||||||||||||||
| 56 | # sub cell_row_column ($cell_name) | ||||||||||||||||||||
| 57 | # | ||||||||||||||||||||
| 58 | # Given the name of a cell, return its row and column. | ||||||||||||||||||||
| 59 | # | ||||||||||||||||||||
| 60 | # TESTS: Utils/100-cell_name_row_column.t | ||||||||||||||||||||
| 61 | # | ||||||||||||||||||||
| 62 | ################################################################################ | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 317520 | ▶ | sub cell_row_column ($name) { | ||||||||||||||||||
|
Subroutine
cell_row_column: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||
| 65 | 317520 | ▶ | $name =~ /R([0-9]+)C([0-9]+)/ ? ($1, $2) : (0, 0) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | ################################################################################ | ||||||||||||||||||||
| 70 | # | ||||||||||||||||||||
| 71 | # all_pairs ($set1, $set2) | ||||||||||||||||||||
| 72 | # | ||||||||||||||||||||
| 73 | # Return a string which, foreach character $x from $set1, and $y from $set2, | ||||||||||||||||||||
| 74 | # contains the substrings "$x$y" and "$y$x". Furthermore, the string will | ||||||||||||||||||||
| 75 | # not contain any substring "$w$z", with $w and $z from the same set. | ||||||||||||||||||||
| 76 | # | ||||||||||||||||||||
| 77 | # Each set is given as a string. | ||||||||||||||||||||
| 78 | # | ||||||||||||||||||||
| 79 | # Note: this is *not* a method, as it's independent of any state. | ||||||||||||||||||||
| 80 | # | ||||||||||||||||||||
| 81 | # TESTS: Utils/150-all_pairs.t | ||||||||||||||||||||
| 82 | # | ||||||||||||||||||||
| 83 | ################################################################################ | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 23 | ▶ | sub all_pairs ($set1, $set2) { | ||||||||||||||||||
|
Subroutine
all_pairs: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 86 | 23 | my @chars1 = split // => $set1; | |||||||||||||||||||
| 87 | 23 | my @chars2 = split // => $set2; | |||||||||||||||||||
| 88 | 23 | my $out = ""; | |||||||||||||||||||
| 89 | 23 | foreach my $ch1 (@chars1) { | |||||||||||||||||||
| 90 | 92 | foreach my $ch2 (@chars2) { | |||||||||||||||||||
| 91 | 460 | $out .= "$ch1$ch2"; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | 23 | $out .= $chars1 [0]; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 23 | $out; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | ################################################################################ | ||||||||||||||||||||
| 102 | # | ||||||||||||||||||||
| 103 | # semi_debruijn_seq ($values, $allow_dups = 0) | ||||||||||||||||||||
| 104 | # | ||||||||||||||||||||
| 105 | # Return, for the given values, a De Bruijn sequence of size 2 with | ||||||||||||||||||||
| 106 | # 1) Duplicates removed and | ||||||||||||||||||||
| 107 | # 2) The first character copied to the end | ||||||||||||||||||||
| 108 | # | ||||||||||||||||||||
| 109 | # TESTS: Utils/130-semi_debruijn_seq.t | ||||||||||||||||||||
| 110 | # | ||||||||||||||||||||
| 111 | ################################################################################ | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 19212 | ▶ | sub semi_debruijn_seq ($values, $allow_dups = 0) { | ||||||||||||||||||
|
Subroutine
semi_debruijn_seq: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 114 | 19212 | state $cache; | |||||||||||||||||||
| 115 | 19212 | ▶ | $$cache {$values, $allow_dups} //= do { | ||||||||||||||||||
|
Condition66%2/3
$$cache{join $;, $values, $allow_dups} //= do {
my $seq = debruijn($values, 2);
$seq .= substr($seq, 0, 1);
$seq =~ s/(.)\g{1}/$1/gu unless $allow_dups;
$seq
}
MC/DC0%0/2
$$cache{join $;, $values, $allow_dups} //= do {
my $seq = debruijn($values, 2);
$seq .= substr($seq, 0, 1);
$seq =~ s/(.)\g{1}/$1/gu unless $allow_dups;
$seq
}
$$cache{join $;, $values, $allow_dups}
do {
my $seq = debruijn($values, 2);
$seq .= substr($seq, 0, 1);
$seq =~ s/(.)\g{1}/$1/gu unless $allow_dups;
$seq
}
Truth table66%2/3
A //= B
A= $$cache{join $;, $values, $allow_dups}B= do {
my $seq = debruijn($values, 2);
$seq .= substr($seq, 0, 1);
$seq =~ s/(.)\g{1}/$1/gu unless $allow_dups;
$seq
}
| |||||||||||||||||||||
| 116 | 28 | my $seq = debruijn ($values, 2); | |||||||||||||||||||
| 117 | 28 | $seq .= substr $seq, 0, 1; # Copy first char to | |||||||||||||||||||
| 118 | # the end. | ||||||||||||||||||||
| 119 | 28 | ▶ | $seq =~ s/(.)\g{1}/$1/g unless $allow_dups; # Remove duplicates. | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 28 | $seq; | |||||||||||||||||||
| 121 | }; | ||||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 36 | 1; | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||