blib/lib/Math/Cryptarithm.pm

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