diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2021-04-05 14:33:23 -0500 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2021-04-05 14:33:23 -0500 |
| commit | 67650a548ed89d7017cc9070c77dc9cfc83dc1d8 (patch) | |
| tree | 5fc86c906d4fd5a7d56466073e66acef71a3cb38 /challenge-107 | |
| parent | d44b2193b81c369285cf9ee9f9e5efa5104859e2 (diff) | |
| download | perlweeklychallenge-club-67650a548ed89d7017cc9070c77dc9cfc83dc1d8.tar.gz perlweeklychallenge-club-67650a548ed89d7017cc9070c77dc9cfc83dc1d8.tar.bz2 perlweeklychallenge-club-67650a548ed89d7017cc9070c77dc9cfc83dc1d8.zip | |
Add solution to challenge 107
Diffstat (limited to 'challenge-107')
| -rw-r--r-- | challenge-107/wlmb/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-107/wlmb/perl/Calc.pm | 14 | ||||
| -rwxr-xr-x | challenge-107/wlmb/perl/ch-1.pl | 53 | ||||
| -rwxr-xr-x | challenge-107/wlmb/perl/ch-1a.pl | 28 | ||||
| -rwxr-xr-x | challenge-107/wlmb/perl/ch-2.pl | 18 |
5 files changed, 114 insertions, 0 deletions
diff --git a/challenge-107/wlmb/blog.txt b/challenge-107/wlmb/blog.txt new file mode 100644 index 0000000000..1a6cd84aea --- /dev/null +++ b/challenge-107/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2021/04/05/PWC107/ diff --git a/challenge-107/wlmb/perl/Calc.pm b/challenge-107/wlmb/perl/Calc.pm new file mode 100644 index 0000000000..59eb369fc8 --- /dev/null +++ b/challenge-107/wlmb/perl/Calc.pm @@ -0,0 +1,14 @@ +package Calc; + +use strict; +use warnings; + +sub new { bless {}, shift; } +sub add { } +sub mul { } +sub div { } +our $scalar; +our @array; +our %hash; + +1; diff --git a/challenge-107/wlmb/perl/ch-1.pl b/challenge-107/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..3466ea1f98 --- /dev/null +++ b/challenge-107/wlmb/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl +# Perl weekly challenge 107 +# Task 1: Self-descriptive Numbers +# Use some properties to accelerate their generation +# Use letter to go beyond base 10 +# +# See https://wlmb.github.io/2021/04/05/PWC107/#task-1-self-descriptive-numbers +use strict; +use warnings; +use v5.12; +use List::Util qw(all); + +die 'Use: ./ch1.pl howmany' unless @ARGV; +my $howmany=shift @ARGV; +exit unless $howmany > 0; +my @results; +my $largest_base=36; #allows ten decimal digits and 26 letters +my @letter_from_digit=(0..9,'a'..'z'); +my $base=2; + + RESULT: + while($howmany>@results){ + ++$base; + last RESULT if $base>$largest_base; + for my $firstdigit(1..$base-1){ + try([$firstdigit],0,$base, $firstdigit); + last RESULT if @results==$howmany; + } +} +my $n=1; +foreach(@results){ + say sprintf("%2d- Base %2d: %s", $n++, scalar @$_, + join('', map {$letter_from_digit[$_]} @$_)); +} +sub try { + my ($current,$position,$base, $sum)=@_; + if($position==$base-1){ + push @results, $current if check($current); + return; + } + for my $digit(0..$base-$sum){ + try([(@$current,$digit)], $position+1, $base, $sum+$digit); + } +} + +sub check { + my $number=shift; + my @digits=@$number; + my @need=(0)x@digits; + my @have=(0)x@digits; + map {$need[$_]=$digits[$_]; $have[$digits[$_]]++} 0..@digits-1; + return all {$need[$_]==$have[$_]} 0..@digits-1; +} diff --git a/challenge-107/wlmb/perl/ch-1a.pl b/challenge-107/wlmb/perl/ch-1a.pl new file mode 100755 index 0000000000..b53bc293ab --- /dev/null +++ b/challenge-107/wlmb/perl/ch-1a.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +# Perl weekly challenge 107 +# Task 1: Self-descriptive Numbers +# Brute force solution. +# +# See https://wlmb.github.io/2021/04/05/PWC107/#task-1-self-descriptive-numbers +use strict; +use warnings; +use v5.12; +use List::Util qw(all); +die 'Use: ./ch1a.pl howmany' unless @ARGV; +my $howmany=shift @ARGV; +for my $n(0..10**10-1){ # number of digits <=10 + if(check($n)){ + say $n; + --$howmany; + last if $howmany<=0; + } +} + +sub check { + my $number=shift; + my @digits=split '',$number; + my @need=(0)x@digits; + my @have=(0)x@digits; + map {$need[$_]=$digits[$_]; $have[$digits[$_]]++} 0..@digits-1; + return all {$need[$_]==$have[$_]} 0..@digits-1; +} diff --git a/challenge-107/wlmb/perl/ch-2.pl b/challenge-107/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..dae450a85e --- /dev/null +++ b/challenge-107/wlmb/perl/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +# Perl weekly challenge 107 +# Task 2: List Methods +# +# See https://wlmb.github.io/2021/04/05/PWC107/#task-2-list-methods +use strict; +use warnings; +use v5.12; + +no strict "refs"; # don't be that strict +die 'Use: ./ch-2.pl package [other package] [...]' unless @ARGV; +foreach my $package(@ARGV){ + eval "require $package"; + die $@ if $@; + foreach my $key(sort keys %{"${package}::"}){ + say $key if defined &{"${package}::$key"}; + } +} |
