diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-04-06 11:01:49 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-04-06 11:01:49 +0100 |
| commit | b37f0ec54ec1d6f4953a2fdba7822d588ca4127c (patch) | |
| tree | 3105b446d186b376340231c96790e34235810de3 /challenge-107 | |
| parent | f3e5ea7842e75a0d8e3beb5908f6a57c9c430f5b (diff) | |
| download | perlweeklychallenge-club-b37f0ec54ec1d6f4953a2fdba7822d588ca4127c.tar.gz perlweeklychallenge-club-b37f0ec54ec1d6f4953a2fdba7822d588ca4127c.tar.bz2 perlweeklychallenge-club-b37f0ec54ec1d6f4953a2fdba7822d588ca4127c.zip | |
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-107')
| -rw-r--r-- | challenge-107/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-107/laurent-rosenfeld/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-107/laurent-rosenfeld/perl/ch-2.pl | 7 | ||||
| -rw-r--r-- | challenge-107/laurent-rosenfeld/raku/ch-1.raku | 32 | ||||
| -rw-r--r-- | challenge-107/laurent-rosenfeld/raku/ch-2.raku | 7 |
5 files changed, 94 insertions, 0 deletions
diff --git a/challenge-107/laurent-rosenfeld/blog.txt b/challenge-107/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..a92a0befe6 --- /dev/null +++ b/challenge-107/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2021/04/perl-weekly-challenge-107-self-descripting-numbers-and-list-methods.html diff --git a/challenge-107/laurent-rosenfeld/perl/ch-1.pl b/challenge-107/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..054e4f5d18 --- /dev/null +++ b/challenge-107/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,47 @@ +use strict; +use warnings; +use feature qw /say/; +use constant DIGITS => ('0'..'9', 'A'..'Z'); +use constant MAX => 3; +my $count = 0; + +sub to_base_b { # Converts decimal number to base b string + my($dec, $base) = @_; + my @digits; + while ($dec) { + unshift @digits, (DIGITS)[$dec % $base]; + $dec = int($dec/$base); + } + return join "", @digits; +} + +sub check_self_desc { + my $base = shift; + for my $num ($base ** ($base -1) .. $base ** $base -1) { + my $num_in_b = to_base_b ($num, $base); + next unless $num_in_b =~ /0$/; + my @digits = split //, $num_in_b; + my $sum = 0; + $sum += $_ for split //, $num_in_b; + next if $sum != $base; + my $success = 1; + for my $rank (0..$base - 1) { + my $nb_digits = $digits[$rank]; + my $num_occ = $num_in_b =~ s/$rank/$rank/g; + if ($num_occ != $nb_digits) { + $success = 0; + last; + } + } + if ($success) { + say "Number in base $base: $num_in_b; decimal: $num" ; + $count++; + return if $count >= MAX; + } + } +} + +for my $base (2..10) { + check_self_desc($base); + last if $count >= MAX; +} diff --git a/challenge-107/laurent-rosenfeld/perl/ch-2.pl b/challenge-107/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..5f5098ed06 --- /dev/null +++ b/challenge-107/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,7 @@ +use strict; +use warnings; +use feature qw /say/; + +while (<>) { + say $1 if /^\s*sub\s+(\w+)/; +} diff --git a/challenge-107/laurent-rosenfeld/raku/ch-1.raku b/challenge-107/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..b25167da72 --- /dev/null +++ b/challenge-107/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,32 @@ +use v6; +constant MAX = 4; + +my $*count = 0; +for 2..Inf -> $base { + check-self-desc($base); + last if $*count >= MAX; +} + +sub check-self-desc (Int $base) { + my $found = False; + for $base ** ($base -1) .. $base ** $base -1 -> $num { + my $num-in-b = $num.base($base); + next unless $num-in-b ~~ /0$/; + my @digits = $num-in-b.comb; + next if $base != [+] @digits; + my $success = True; + for 0..$base - 1 -> $rank { + if (+ $num-in-b.indices($rank) != @digits[$rank]) { + $success = False; + last; + } + } + if $success { + say "Number in base $base: $num-in-b; decimal: $num"; + $*count++; + $found = True; + return if $*count >= MAX; + } + } + say "No self-descriptive number for base $base" unless $found; +} diff --git a/challenge-107/laurent-rosenfeld/raku/ch-2.raku b/challenge-107/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..44ca49644d --- /dev/null +++ b/challenge-107/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,7 @@ +use v6; + +sub MAIN (Str $file-name) { + for $file-name.IO.lines -> $line { + say ~$0 if $line ~~ /^\s* method \s+ (<[-'\w]>+)/; + } +} |
