blib/lib/Math/Cryptarithm.pm

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