diff options
| author | Jaldhar H. Vyas <jaldhar@braincells.com> | 2022-07-24 21:11:53 -0400 |
|---|---|---|
| committer | Jaldhar H. Vyas <jaldhar@braincells.com> | 2022-07-24 21:11:53 -0400 |
| commit | 057a0f123d166b0ccbad85e31d0846f293ce8b2a (patch) | |
| tree | 7aed766ae9730898a4b4afc7414656cbde5fdbf2 | |
| parent | 492d38a462e1c494aadeb9b421f759d6d562cc1f (diff) | |
| download | perlweeklychallenge-club-057a0f123d166b0ccbad85e31d0846f293ce8b2a.tar.gz perlweeklychallenge-club-057a0f123d166b0ccbad85e31d0846f293ce8b2a.tar.bz2 perlweeklychallenge-club-057a0f123d166b0ccbad85e31d0846f293ce8b2a.zip | |
Challenge 174 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-174/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-174/jaldhar-h-vyas/perl/ch-1.pl | 27 | ||||
| -rwxr-xr-x | challenge-174/jaldhar-h-vyas/perl/ch-2.pl | 45 | ||||
| -rwxr-xr-x | challenge-174/jaldhar-h-vyas/raku/ch-1.raku | 26 | ||||
| -rwxr-xr-x | challenge-174/jaldhar-h-vyas/raku/ch-2.raku | 22 |
5 files changed, 121 insertions, 0 deletions
diff --git a/challenge-174/jaldhar-h-vyas/blog.txt b/challenge-174/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..3acab964be --- /dev/null +++ b/challenge-174/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2022/07/perl_weekly_challenge_week_174.html
\ No newline at end of file diff --git a/challenge-174/jaldhar-h-vyas/perl/ch-1.pl b/challenge-174/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..d165016ec3 --- /dev/null +++ b/challenge-174/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +sub isDisarium { + my ($n) = @_; + my @digits = split //, $n; + my $total = 0; + + while (my ($pos, $digit) = each @digits) { + $total += $digit ** ($pos + 1); + } + + return $total == $n; +} + +my @disariums; +my $n = 0; + +while (scalar @disariums < 19) { + if (isDisarium($n)) { + push @disariums, $n; + } + $n++; +} + +say join q{, }, @disariums; diff --git a/challenge-174/jaldhar-h-vyas/perl/ch-2.pl b/challenge-174/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..bfb3f72a41 --- /dev/null +++ b/challenge-174/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl +use 5.030; +use warnings; +use experimental qw/ smartmatch /; + +sub permute (&@) { + my $code = shift; + my @idx = 0..$#_; + while ( $code->(@_[@idx]) ) { + my $p = $#idx; + --$p while $idx[$p-1] > $idx[$p]; + my $q = $p or return; + push @idx, reverse splice @idx, $p; + ++$q while $idx[$p-1] > $idx[$q]; + @idx[$p-1,$q]=@idx[$q,$p-1]; + } +} + + +sub permutation2rank { + my ($args) = @_; + my @perms; + + permute { push @perms, \@_; } sort @{$args}; + + while (my ($index, $val) = each (@perms)) { + if ($val ~~ $args) { + return $index; + } + } + + return undef; +} + +sub rank2permutation { + my ($args, $rank) = @_; + my @perms; + + permute { push @perms, \@_; } @{$args}; + + return @{ $perms[$rank] }; +} + +say permutation2rank([1, 0, 2]); +say join q{, }, rank2permutation([0, 1, 2], 1); diff --git a/challenge-174/jaldhar-h-vyas/raku/ch-1.raku b/challenge-174/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..db92db5c78 --- /dev/null +++ b/challenge-174/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!/usr/bin/raku + +sub isDisarium(Int $n) { + my @digits = $n.comb; + my $total = 0; + + for @digits.kv -> $pos, $digit { + $total += $digit ** ($pos + 1); + } + + return $total == $n; +} + +sub MAIN() { + my @disariums; + my $n = 0; + + while @disariums.elems < 19 { + if isDisarium($n) { + @disariums.push($n); + } + $n++; + } + + @disariums.join(q{, }).say; +}
\ No newline at end of file diff --git a/challenge-174/jaldhar-h-vyas/raku/ch-2.raku b/challenge-174/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..7b254c5c9c --- /dev/null +++ b/challenge-174/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,22 @@ +#!/usr/bin/raku + +sub permutation2rank(@args) { + my @perms = @args.sort.permutations; + + for @perms.kv -> $index, $val { + if $val ~~ @args { + return $index; + } + } + + return Nil; +} + +sub rank2permutation(@args, $rank) { + return @args.permutations[$rank]; +} + +sub MAIN(*@args) { + say permutation2rank([1, 0, 2]); + say rank2permutation([0, 1, 2], 1); +}
\ No newline at end of file |
