diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-05-18 09:52:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-18 09:52:21 +0100 |
| commit | 90ac9ebf2f8309339a06213cbf5e676590727f5f (patch) | |
| tree | b430374ce666d87d5c22cea74dabd5e807b3bc92 | |
| parent | 527cb630011d5da9af18ef94d0ea4ecb80545150 (diff) | |
| parent | d775c92d3e2c0f982778979e31a3dc2d09800f8b (diff) | |
| download | perlweeklychallenge-club-90ac9ebf2f8309339a06213cbf5e676590727f5f.tar.gz perlweeklychallenge-club-90ac9ebf2f8309339a06213cbf5e676590727f5f.tar.bz2 perlweeklychallenge-club-90ac9ebf2f8309339a06213cbf5e676590727f5f.zip | |
Merge pull request #4098 from simongreen-net/swg-113
sgreen solution to challenge 113
| -rw-r--r-- | challenge-113/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-113/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-113/sgreen/perl/ch-1.pl | 61 | ||||
| -rwxr-xr-x | challenge-113/sgreen/perl/ch-2.pl | 19 |
4 files changed, 83 insertions, 2 deletions
diff --git a/challenge-113/sgreen/README.md b/challenge-113/sgreen/README.md index bc542f1f03..4bc164937e 100644 --- a/challenge-113/sgreen/README.md +++ b/challenge-113/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 112 +# The Weekly Challenge 113 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-112-27l0) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-113-4g2d) diff --git a/challenge-113/sgreen/blog.txt b/challenge-113/sgreen/blog.txt new file mode 100644 index 0000000000..2b0a3ed6d7 --- /dev/null +++ b/challenge-113/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-113-4g2d diff --git a/challenge-113/sgreen/perl/ch-1.pl b/challenge-113/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..a0d6eef22e --- /dev/null +++ b/challenge-113/sgreen/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub _find_numbers { + my ( $remainder, $numbers, $all_numbers ) = @_; + + # Find out what digits remain (must be <= remainder) + my @can_use = grep { $_ <= $remainder } @$all_numbers; + + foreach my $number (@can_use) { + if ( $remainder == $number ) { + # We have a solution, return that + return [ @$numbers, $number ]; + } + + # Recurse this function + my $solution = _find_numbers( $remainder - $number, [ @$numbers, $number ], $all_numbers ); + if ($solution) { + return $solution; + } + } + + # There is no solution. + return; +} + +sub main { + my ( $target, $digit ) = @_; + + # Sanity check + die "The first value must be a positive integer\n" unless $target =~ /^[1-9][0-9]*$/; + die "The second value must be a single digit\n" unless $digit =~ /^[1-9]$/; + + # Two short circuits + if ( index( $target, $digit ) != -1 ) { + say "Output: 1 ($target contains $digit)"; + return; + } + elsif ( $target % $digit == 0 ) { + my $n = $target / $digit; + say "Output: 1 ($digit × $n)"; + return; + } + + # Generate a list of all numbers that contain the digit + my @numbers = reverse grep { index( $_, $digit ) != -1 } 1 .. $target - 1; + + # Find out if there is a solution with these numbers + my $solution = _find_numbers( $target, [], \@numbers ); + if ($solution) { + say 'Output: 1 (', join( ' + ', @$solution ), ')'; + } + else { + say 'Output: 0'; + } +} + +main(@ARGV); diff --git a/challenge-113/sgreen/perl/ch-2.pl b/challenge-113/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..c816ed8245 --- /dev/null +++ b/challenge-113/sgreen/perl/ch-2.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +use List::Util 'sum'; + +sub main { + my $string = shift; + + # Calculate the sum of all digits + my $sum = sum( $string =~ /(\d+)/g ); + $string =~ s/(\d+)/$sum-$1/eg; + say $string; + +} + +main(@ARGV); |
