diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2025-11-08 08:00:18 -0600 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2025-11-08 08:00:18 -0600 |
| commit | e4e49ab98075e9f3d908b75fabce98c9fd76f2d6 (patch) | |
| tree | d03d0b8ca2951d1f4f3f077aafdbdec08054af83 | |
| parent | 820d8daf63f4480ab2c0ae631fdb781b33fee531 (diff) | |
| download | perlweeklychallenge-club-e4e49ab98075e9f3d908b75fabce98c9fd76f2d6.tar.gz perlweeklychallenge-club-e4e49ab98075e9f3d908b75fabce98c9fd76f2d6.tar.bz2 perlweeklychallenge-club-e4e49ab98075e9f3d908b75fabce98c9fd76f2d6.zip | |
Fix solution to ch-2
| -rwxr-xr-x | challenge-346/wlmb/perl/ch-2.pl | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/challenge-346/wlmb/perl/ch-2.pl b/challenge-346/wlmb/perl/ch-2.pl index 926411ddc9..9853470557 100755 --- a/challenge-346/wlmb/perl/ch-2.pl +++ b/challenge-346/wlmb/perl/ch-2.pl @@ -6,6 +6,7 @@ use v5.36; use feature qw(try); use Algorithm::Combinatorics qw(tuples_with_repetition); +use Scalar::Util qw(looks_like_number); die <<~"FIN" unless @ARGV && @ARGV%2==0; Usage: $0 S0 T0 S1 T1... to intercalate arithmetic operators among the digits of string Sn @@ -13,24 +14,22 @@ die <<~"FIN" unless @ARGV && @ARGV%2==0; FIN for my($string, $target)(@ARGV){ try { - die "Only digits accepted in string: $string" unless $string=~/^\d*$/; - # Deal with marginal cases - say("$string -> "), next if length $string == 0 && $string==$target; - say("$string -> $target"), next if length $string == 1 && $string==$target; - # Two or more digits - my @digits=split "", $string; - my $iterator=tuples_with_repetition(["",qw(+ - * /)], @digits-1); - my @results=(); - while(my $intercalate=$iterator->next){ - my $expression=join "", - map({($digits[$_], $intercalate->[$_])} 0..@digits-2), - $digits[-1]; - next if $expression=~/0\d/; # forbid leading zeros - my $value = eval $expression; - next unless defined $value; # ignore illegal expressions - push @results, $expression if $value==$target; - } - say "$string, $target -> @results"; + die "Empty string" unless $string ne ""; + die "Only digits accepted in string: $string" unless $string=~/^\d*$/; + die "Target doesn't looks like number: $target" unless looks_like_number($target); + my @digits=split "", $string; + my $iterator=tuples_with_repetition(["",qw(+ - * /)], @digits-1); + my @results=(); + while(my $intercalate=$iterator->next){ + my $expression=join "", + map({($digits[$_], $intercalate->[$_])} 0..@digits-2), + $digits[-1]; + next if $expression=~/(^|\D)0\d/; # forbid leading zeros + my $value = eval $expression; + next unless defined $value; # ignore illegal expressions + push @results, $expression if $value==$target; + } + say "$string, $target -> @results"; } catch($e){warn $e;} } |
