aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-174/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-174/jaldhar-h-vyas/perl/ch-1.pl27
-rwxr-xr-xchallenge-174/jaldhar-h-vyas/perl/ch-2.pl45
-rwxr-xr-xchallenge-174/jaldhar-h-vyas/raku/ch-1.raku26
-rwxr-xr-xchallenge-174/jaldhar-h-vyas/raku/ch-2.raku22
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