aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-07-27 01:08:05 +0100
committerGitHub <noreply@github.com>2020-07-27 01:08:05 +0100
commitec4b519701dd99a97b33164770847788ff309984 (patch)
treee962e095100f1002760443760c96f85783f0db61
parent3fdf43a6fb35869c3359512a2d0bfc6cddf607be (diff)
parent22285921bde3066852a456316381d5b8b0b47e38 (diff)
downloadperlweeklychallenge-club-ec4b519701dd99a97b33164770847788ff309984.tar.gz
perlweeklychallenge-club-ec4b519701dd99a97b33164770847788ff309984.tar.bz2
perlweeklychallenge-club-ec4b519701dd99a97b33164770847788ff309984.zip
Merge pull request #1984 from jaldhar/challenge-068
Challenge 68 by Jaldhar H. Vyas
-rw-r--r--challenge-068/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-068/jaldhar-h-vyas/perl/ch-1.pl49
-rwxr-xr-xchallenge-068/jaldhar-h-vyas/perl/ch-2.pl82
-rwxr-xr-xchallenge-068/jaldhar-h-vyas/raku/ch-1.p626
-rwxr-xr-xchallenge-068/jaldhar-h-vyas/raku/ch-2.p667
5 files changed, 225 insertions, 0 deletions
diff --git a/challenge-068/jaldhar-h-vyas/blog.txt b/challenge-068/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..63e8ddf185
--- /dev/null
+++ b/challenge-068/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2020/07/perl_weekly_challenge_week_68.html
diff --git a/challenge-068/jaldhar-h-vyas/perl/ch-1.pl b/challenge-068/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..1a09d43683
--- /dev/null
+++ b/challenge-068/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+use English qw/ -no-match-vars /;
+
+sub usage {
+ print<<"-USAGE-";
+Usage:
+ $PROGRAM_NAME <M> <N> [<matrix> ...]
+
+ <M> The number of rows in the matrix
+ <N> The number of columns in the matrix
+ [<matrix> ...] The elements of the matrix
+-USAGE-
+ exit(0);
+}
+
+my $M = shift // usage();
+my $N = shift // usage();
+if (scalar @ARGV != $M * $N) {
+ usage();
+}
+my @matrix = @ARGV;
+
+my @input = map {[ splice @matrix, 0, $N ]} (0 .. ($M - 1));
+my @output = map { [ map { $_ } @{$_} ] } @input;
+
+for my $row (0 .. ($M - 1)) {
+ if (grep { $_ == 0 } @{$input[$row]}) {
+ for my $col (0 .. ($N - 1)) {
+ $output[$row][$col] = 0;
+ }
+ }
+}
+
+for my $col (0 .. ($N - 1)) {
+ for my $row (0 .. ($M - 1)) {
+ if ($input[$row][$col] == 0) {
+ for my $zrow (0 .. ($M - 1)) {
+ $output[$zrow][$col] = 0;
+ }
+ last;
+ }
+ }
+}
+
+for my $row (@output) {
+ say q{[}, (join q{ }, @{$row}), q{]};
+}
diff --git a/challenge-068/jaldhar-h-vyas/perl/ch-2.pl b/challenge-068/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..e335d0b980
--- /dev/null
+++ b/challenge-068/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,82 @@
+#!/usr/bin/perl
+
+package Node;
+use Moo;
+use namespace::autoclean;
+
+has _value => (
+ is => 'rw',
+);
+
+has _next => (
+ is => 'rw',
+ isa => sub { return ref eq 'Node'; },
+);
+
+sub BUILDARGS {
+ my ($orig, $class, @args) = @_;
+
+ return { _value => $args[0], _next => undef };
+}
+
+sub add {
+ my ($self, $newval) = @_;
+
+ my $v = $self;
+ while ($v->{_next}) {
+ $v = $v->{_next};
+ }
+ $v->{_next} = Node->new(value => $newval);
+}
+
+sub print {
+ my ($self) = @_;
+
+ my $v = $self;
+
+ while ($v) {
+ print $v->{_value} // q{}, q{ };
+ $v = $v->{_next};
+ }
+
+ print "\n";
+}
+
+sub reorder() {
+ my ($self) = @_;
+
+ my $current = $self;
+
+ while ($current) {
+ my $last = $current;
+ my $second = $current;
+ while ($last->{_next}) {
+ $second = $last;
+ $last = $last->{_next};
+ }
+
+ $second->{_next} = undef;
+ $last->{_next} = $current->{_next};
+ $current->{_next} = $last;
+
+ if ($current->{_next}) {
+ $current = $current->{_next}->{_next};
+ } else {
+ $current = $current->{_next};
+ }
+ }
+}
+
+1;
+
+package main;
+use 5.020;
+use warnings;
+
+my $linked_list = Node->new(value => 1);
+$linked_list->add(2);
+$linked_list->add(3);
+$linked_list->add(4);
+
+$linked_list->reorder();
+$linked_list->print(); \ No newline at end of file
diff --git a/challenge-068/jaldhar-h-vyas/raku/ch-1.p6 b/challenge-068/jaldhar-h-vyas/raku/ch-1.p6
new file mode 100755
index 0000000000..5900288562
--- /dev/null
+++ b/challenge-068/jaldhar-h-vyas/raku/ch-1.p6
@@ -0,0 +1,26 @@
+#!/usr/bin/perl6
+
+multi sub MAIN(
+ Int $M, #= The number of rows in the matrix
+ Int $N, #= The number of columns in the matrix
+ *@matrix #= The elements of the matrix
+) {
+ my @input = (0 ..^ $M).map({ [@matrix.splice(0, $N)] });
+ my @output = [1 xx $N] xx $M;
+
+ for 0 ..^ $M -> $m {
+ if @input[$m;*].any == 0 {
+ @output[$m;*] = 0 xx $M;
+ }
+ }
+
+ for 0 ..^ $N -> $n {
+ if @input[*;$n].any == 0 {
+ @output[*;$n] = 0 xx $N;
+ }
+ }
+
+ for 0 ..^ $M -> $m {
+ say @output[$m];
+ }
+} \ No newline at end of file
diff --git a/challenge-068/jaldhar-h-vyas/raku/ch-2.p6 b/challenge-068/jaldhar-h-vyas/raku/ch-2.p6
new file mode 100755
index 0000000000..59591f69f4
--- /dev/null
+++ b/challenge-068/jaldhar-h-vyas/raku/ch-2.p6
@@ -0,0 +1,67 @@
+#!/usr/bin/perl6
+
+class Node {
+ has $.value is rw;
+ has Node $.next is rw;
+
+ multi submethod BUILD( :$value) {
+ $!value = $value;
+ $!next = Nil;
+ }
+
+ method add($newval) {
+ my $v = self;
+
+ while $v.next {
+ $v = $v.next;
+ }
+
+ $v.next = Node.new(value => $newval);
+ }
+
+ method print() {
+
+ my $v = self;
+
+ while $v.next {
+ print $v.value // q{}, q{ };
+ $v = $v.next;
+ }
+
+ print $v.value, "\n";
+ }
+
+ method reorder() {
+
+ my $current = self;
+
+ while $current {
+ my $last = $current;
+ my $second = $current;
+ while $last.next {
+ $second = $last;
+ $last = $last.next;
+ }
+
+ $second.next = Nil;
+ $last.next = $current.next;
+ $current.next = $last;
+
+ if $current.next {
+ $current = $current.next.next;
+ } else {
+ $current = $current.next;
+ }
+ }
+ }
+}
+
+multi sub MAIN() {
+ my $linked_list = Node.new(value => 1);
+ $linked_list.add(2);
+ $linked_list.add(3);
+ $linked_list.add(4);
+
+ $linked_list.reorder();
+ $linked_list.print();
+} \ No newline at end of file