blib/lib/Math/Cryptarithm.pm

filter: Statementstmt 100% 98 / 98 Branchbran 81.2% 13 / 16 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 13 / 13 Podpod 50.0% 2 / 4 totaltotal 96.5% 140 / 145 CC 23 SCAR 17.7
CC
23
Cov
98%
solve_alphabetvalue_hash
10.0
_replacement
8.0
_list_alphabets
4.0
CRAP 5.9
↑ Summary
1 package Math::Cryptarithm;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Algorithm::Permute;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use v5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 require Exporter;
10  
11 2 our @ISA = qw(Exporter);
12  
13 2 our %EXPORT_TAGS = ( 'all' => [ qw(
14
15 ) ] );
16  
17 2 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
18  
19 2 our @EXPORT = qw(
20
21 );
22  
23 2 our $VERSION = qv('v0.20.2');
24  
25  
26 # Preloaded methods go here.
27  
28 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
29 8 my ($class) = @_;
30 8 bless {
31 _equations => $_[1]
32 }, $class;
33 }
34  
35 10 ▶sub _check_syntax_of_an_equation {
Subroutine _check_syntax_of_an_equation: called CC=1 SCAR=0.0
36 10 my $equation = $_[0];
37 # to be written
38 10 return 1;
39 }
40  
41 10 ▶sub _seperate_lhs_rhs {
Subroutine _seperate_lhs_rhs: called CC=1 SCAR=0.0
42 10 my $eq_text = $_[0];
43 10 my $i = index($eq_text, "=");
44 10 return [ substr($eq_text, 0, $i ), substr($eq_text, $i+1) ];
45 }
46  
47 84016 ▶sub _replacement {
Subroutine _replacement: called CC=8 SCAR=20.8
48 84016 my $original_text = $_[0];
49 84016 my $text = $_[0];
50 84016 my @arr_ab = @{$_[1]};
51 84016 my @arr_digits = @{$_[2]};
52 84016 for (0..$#arr_digits) {
53 392884 $text =~ s/$arr_ab[$_]/$arr_digits[$_]/g;
54 }
55  
56 #BEGIN: cut leading zeros
57 84016 ▶ substr($text,0,1) = " "
Branch: 1 branch
BranchTrueFalseTotal
if substr($text, 0, 1) eq 0 and substr($text, 1, 1) =~ /[0-9]/ 4041 79975 84016
Condition100%3/3
substr($text, 0, 1) eq 0 and substr($text, 1, 1) =~ /[0-9]/
!ll&&!rl&&r
793046714041
MC/DC100%2/2
substr($text, 0, 1) eq 0 and substr($text, 1, 1) =~ /[0-9]/
substr($text, 0, 1) eq 0 substr($text, 1, 1) =~ /[0-9]/
Truth table100%3/3
A and B
A= substr($text, 0, 1) eq 0B= substr($text, 1, 1) =~ /[0-9]/
ABresult
0X0
100
111
58 if substr($text,0,1) eq '0' && substr($text,1,1) =~ m/[0-9]/;
59 84016 for my $i (1..(length($text) - 2) ) {
60 729722 ▶ if ( substr($text,$i,1) eq '0'
Branch: 1 branch
BranchTrueFalseTotal
if (substr($text, $i, 1) eq 0 and not substr($text, $i - 1, 1) =~ /[1-9]/ and substr($text, $i + 1, 1) =~ /\d/) 26856 702866 729722
Condition100%6/6
substr($text, $i, 1) eq 0 and not substr($text, $i - 1, 1) =~ /[1-9]/ and substr($text, $i + 1, 1) =~ /\d/
!ll&&!rl&&r
695917694926856
substr($text, $i, 1) eq 0 and not substr($text, $i - 1, 1) =~ /[1-9]/
!ll&&!rl&&r
6725892332833805
MC/DC100%3/3
substr($text, $i, 1) eq 0 and not substr($text, $i - 1, 1) =~ /[1-9]/ and substr($text, $i + 1, 1) =~ /\d/
substr($text, $i, 1) eq 0 not substr($text, $i - 1, 1) =~ /[1-9]/ substr($text, $i + 1, 1) =~ /\d/
Truth table100%4/4
A and not(B) and C
A= substr($text, $i, 1) eq 0B= not substr($text, $i - 1, 1) =~ /[1-9]/C= substr($text, $i + 1, 1) =~ /\d/
ABCresult
0XX0
10X0
1100
1111
61 && substr($text,$i-1,1) !~ m/[1-9]/
62 && substr($text,$i+1,1) =~ m/\d/ )
63 {
64 26856 substr($text,$i,1) = " ";
65 }
66 }
67 #END cut leading zeros
68 84016 return $text;
69 }
70  
71 8 ▶sub _list_alphabets {
Subroutine _list_alphabets: called CC=4 SCAR=13.9
72 8 my @eq = @{$_[0]};
73 8 my %abcdz;
74 8 for my $symbol ('A'..'Z') {
75 208 for my $e (@eq) {
76 260 ▶ $abcdz{$symbol} = 1 if $e =~ m/$symbol/;
Branch: 1 branch
BranchTrueFalseTotal
if $e =~ /$symbol/ 27 233 260
77 }
78 }
79 8 return [sort keys %abcdz];
80 }
81  
82 10 ▶sub equations {
Subroutine equations: called CC=1 SCAR=0.0
Pod undocumented
83 10 $_[0]->{_equations};
84 }
85  
86 8 ▶sub solve_alphabetvalue_hash {
Subroutine solve_alphabetvalue_hash: called CC=10 SCAR=23.1
Pod documented
87 8 my ($self) = @_;
88 8 my @eqs = @{$self->equations};
89 8 my @answers = ();
90  
91 8 _check_syntax_of_an_equation($_) foreach @eqs;
92  
93 8 my @eqs_lhs, my @eqs_rhs;
94 8 foreach (@eqs) {
95 10 my $temp_l, my $temp_r;
96 10 ($temp_l, $temp_r) = _seperate_lhs_rhs($_)->@*;
97 10 push @eqs_lhs, $temp_l;
98 10 push @eqs_rhs, $temp_r;
99 }
100  
101 8 my @arr_alphabets = _list_alphabets(\@eqs)->@*;
102 8 my $num_of_alphabets = scalar @arr_alphabets;
103  
104 8 my $iter = Algorithm::Permute->new([0..9], $num_of_alphabets);
105 8 ▶ COMBIN_TEST: while (my @res = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $iter->next) 41950 8 41958
106 41950 my $ok = undef;
107 41950 for my $i (0..$#eqs) {
108 42008 $ok = undef;
109 42008 my $str_lhs =
110 _replacement( $eqs_lhs[$i] , \@arr_alphabets, \@res );
111 42008 my $str_rhs =
112 _replacement( $eqs_rhs[$i] , \@arr_alphabets, \@res );
113 42008 my $val_lhs = eval $str_lhs;
114 42008 my $val_rhs = eval $str_rhs;
115 42008 ▶ die "LHS is not numeric:\n $eqs_lhs[$i]\n\"$str_lhs\"\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $val_lhs =~ /^[0-9]+$/ 0 42008 42008
116 if $val_lhs !~ m/^[0-9]+$/;
117 42008 ▶ die "RHS is not numeric:\n $eqs_rhs[$i]\n\"$str_rhs\"\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $val_rhs =~ /^[0-9]+$/ 0 42008 42008
118 if $val_rhs !~ m/^[0-9]+$/;
119 42008 ▶ next COMBIN_TEST unless $val_lhs == $val_rhs;
Branch: 1 branch
BranchTrueFalseTotal
unless $val_lhs == $val_rhs 41937 71 42008
120 71 $ok = 1;
121 }
122 13 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 13 0 13
123 13 my %temp_hash;
124 13 for my $i (0..$num_of_alphabets-1) {
125 37 $temp_hash{$arr_alphabets[$i]} = $res[$i];
126 }
127 13 push @answers, \%temp_hash;
128 }
129 }
130 8 return \@answers;
131 }
132  
133 2 ▶sub solve_ans_in_equations {
Subroutine solve_ans_in_equations: called CC=4 SCAR=13.9
Pod documented
134 2 my ($self) = @_;
135 2 my @eqs = @{$self->equations};
136 2 my @answers_of_hashes = $self->solve_alphabetvalue_hash()->@*;
137 2 my @answers_in_eq;
138 2 for my $my_hash (@answers_of_hashes) {
139 2 my @a_set_of_answer_in_eq;
140 2 for my $crypt_eq (@eqs) {
141 2 my $numeric_eq = $crypt_eq;
142 2 foreach my $k (keys %{$my_hash}) {
143 6 my $digit = $$my_hash{$k};
144 6 $numeric_eq =~ s/$k/$digit/g;
145 }
146 2 push @a_set_of_answer_in_eq, $numeric_eq;
147 }
148 2 push @answers_in_eq, \@a_set_of_answer_in_eq;
149 }
150 2 return \@answers_in_eq;
151 }
152  
153 2 1;
154 __END__
↑ Summary