aboutsummaryrefslogtreecommitdiff
path: root/challenge-242
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-12 18:10:41 +0000
committerGitHub <noreply@github.com>2023-11-12 18:10:41 +0000
commitd170b5af10caf627a8cea3491459bd4338c1c41c (patch)
treeb74fe4da878c2b14b338f9345d0d4277b1a88ee7 /challenge-242
parent00ac6ae32e51a463020eb1d91474908de7aff836 (diff)
parent7cac8442e1728964d5c3f655ac4fc0dc82659b40 (diff)
downloadperlweeklychallenge-club-d170b5af10caf627a8cea3491459bd4338c1c41c.tar.gz
perlweeklychallenge-club-d170b5af10caf627a8cea3491459bd4338c1c41c.tar.bz2
perlweeklychallenge-club-d170b5af10caf627a8cea3491459bd4338c1c41c.zip
Merge pull request #9040 from adamcrussell/challenge-242
Challenge 242
Diffstat (limited to 'challenge-242')
-rw-r--r--challenge-242/adam-russell/blog.txt1
-rw-r--r--challenge-242/adam-russell/blog1.txt1
-rw-r--r--challenge-242/adam-russell/javascript/ch-1.js33
-rw-r--r--challenge-242/adam-russell/javascript/ch-2.js14
-rw-r--r--challenge-242/adam-russell/perl/ch-1.pl58
-rw-r--r--challenge-242/adam-russell/perl/ch-2.pl30
-rw-r--r--challenge-242/adam-russell/prolog/ch-1.p8
-rw-r--r--challenge-242/adam-russell/prolog/ch-2.p7
-rw-r--r--challenge-242/adam-russell/r/ch-1.r18
-rw-r--r--challenge-242/adam-russell/r/ch-2.r28
10 files changed, 198 insertions, 0 deletions
diff --git a/challenge-242/adam-russell/blog.txt b/challenge-242/adam-russell/blog.txt
new file mode 100644
index 0000000000..7778ddfedd
--- /dev/null
+++ b/challenge-242/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://rabbitfarm.com/cgi-bin/blosxom/perl/2023/11/11 \ No newline at end of file
diff --git a/challenge-242/adam-russell/blog1.txt b/challenge-242/adam-russell/blog1.txt
new file mode 100644
index 0000000000..6557439598
--- /dev/null
+++ b/challenge-242/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://rabbitfarm.com/cgi-bin/blosxom/prolog/2023/11/11 \ No newline at end of file
diff --git a/challenge-242/adam-russell/javascript/ch-1.js b/challenge-242/adam-russell/javascript/ch-1.js
new file mode 100644
index 0000000000..86f57321a5
--- /dev/null
+++ b/challenge-242/adam-russell/javascript/ch-1.js
@@ -0,0 +1,33 @@
+class Ch1{
+ missingMembers(a1, a2){
+ let r = [];
+ r.push(
+ a1.filter(function(x){
+ return a2.findIndex(function(y){
+ return x === y;
+ }) === -1;
+ })
+ );
+ r.push(
+ a2.filter(function(x){
+ return a1.findIndex(function(y){
+ return x === y;
+ }) === -1;
+ })
+ );
+ let s = [];
+ r.forEach(function(x){
+ if(x.length > 0){
+ s.push(Array.from(new Set(x)));
+ }
+ });
+ return s;
+ }
+}
+let ch1 = new Ch1();
+console.log(
+ ch1.missingMembers([1, 2, 3], [2, 4, 6])
+);
+console.log(
+ ch1.missingMembers([1, 2, 3, 3], [1, 1, 2, 2])
+); \ No newline at end of file
diff --git a/challenge-242/adam-russell/javascript/ch-2.js b/challenge-242/adam-russell/javascript/ch-2.js
new file mode 100644
index 0000000000..4a4641f9e1
--- /dev/null
+++ b/challenge-242/adam-russell/javascript/ch-2.js
@@ -0,0 +1,14 @@
+class Ch2{
+ flipMatrix(m){
+ return m.map(function(row){
+ return row.reverse().map(x => ~x & 1);
+ });
+ }
+}
+let ch2 = new Ch2();
+console.log(
+ ch2.flipMatrix([[1, 1, 0], [1, 0, 1], [0, 0, 0]])
+);
+console.log(
+ ch2.flipMatrix([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]])
+); \ No newline at end of file
diff --git a/challenge-242/adam-russell/perl/ch-1.pl b/challenge-242/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..a6ee05cfcf
--- /dev/null
+++ b/challenge-242/adam-russell/perl/ch-1.pl
@@ -0,0 +1,58 @@
+use v5.38;
+##
+# You are given two arrays of integers.
+# Write a script to find out the missing members in each other arrays.
+##
+use boolean;
+use Data::Dump q/pp/;
+sub missing_members{
+ my @r;
+ my($a0, $a1) = @_;
+ my $missing0 = [];
+ missing_members_r([@{$a0}], [@{$a1}], $missing0);
+ my $missing1 = [];
+ missing_members_r([@{$a1}], [@{$a0}], $missing1);
+ push @r, $missing0 if @{$missing0} > 0;
+ push @r, $missing1 if @{$missing1} > 0;
+ return @r;
+}
+
+sub missing_members_r{
+ my($a0, $a1, $missing, $seen) = @_;
+ $seen = [] if !defined($seen);
+ my $x = shift @{$a0};
+ push @{$missing}, $x if missing_r($x, [@{$a1}]) && !seen_r($x, $seen);
+ push @{$seen}, $x;
+ missing_members_r($a0, $a1, $missing, $seen) if @{$a0} > 0;
+}
+
+sub missing_r{
+ my($x, $a0) = @_;
+ return true if @{$a0} == 0;
+ if(@{$a0}){
+ my $y = shift @{$a0};
+ if($x == $y){
+ return false;
+ }
+ }
+ return missing_r($x, $a0);
+}
+
+sub seen_r{
+ my($x, $seen) = @_;
+ return false if @{$seen} == 0;
+ my $y = shift @{$seen};
+ if($x == $y){
+ return true;
+ }
+ return seen_r($x, $seen);
+}
+
+MAIN:{
+ my @array1 = (1, 2, 3);
+ my @array2 = (2, 4, 6);
+ say pp missing_members \@array1, \@array2;
+ @array1 = (1, 2, 3, 3);
+ @array2 = (1, 1, 2, 2);
+ say pp missing_members \@array1, \@array2;
+} \ No newline at end of file
diff --git a/challenge-242/adam-russell/perl/ch-2.pl b/challenge-242/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..63a70ca188
--- /dev/null
+++ b/challenge-242/adam-russell/perl/ch-2.pl
@@ -0,0 +1,30 @@
+use v5.38;
+##
+# You are given n x n binary matrix.
+# Write a script to flip the given matrix as below.
+# 1 1 0
+# 0 1 1
+# 0 0 1
+# a) Reverse each row
+# 0 1 1
+# 1 1 0
+# 1 0 0
+# b) Invert each member
+# 1 0 0
+# 0 0 1
+# 0 1 1
+##
+use Data::Dump q/pp/;
+sub flip_matrix{
+ return map {
+ my $row = $_;
+ [map {~$_ & 1} reverse @{$row}]
+ } @_;
+}
+
+MAIN:{
+ my @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0]);
+ say pp flip_matrix @matrix;
+ @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]);
+ say pp flip_matrix @matrix;
+} \ No newline at end of file
diff --git a/challenge-242/adam-russell/prolog/ch-1.p b/challenge-242/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..9691230ace
--- /dev/null
+++ b/challenge-242/adam-russell/prolog/ch-1.p
@@ -0,0 +1,8 @@
+missing(L, E, Member):-
+ (member(E, L), Member = nil);
+ (\+ member(E, L), Member = E).
+missing_members([List1, List2], [Missing1, Missing2]):-
+ maplist(missing(List2), List1, Missing1Nil),
+ delete(Missing1Nil, nil, Missing1),
+ maplist(missing(List1), List2, Missing2Nil),
+ delete(Missing2Nil, nil, Missing2). \ No newline at end of file
diff --git a/challenge-242/adam-russell/prolog/ch-2.p b/challenge-242/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..e472cb2953
--- /dev/null
+++ b/challenge-242/adam-russell/prolog/ch-2.p
@@ -0,0 +1,7 @@
+flip(B, F):-
+ F is \ B /\ 1.
+flip_matrix([], []).
+flip_matrix([Row|Matrix], [RowFlipped|MatrixFlipped]):-
+ reverse(Row, RowReversed),
+ maplist(flip, RowReversed, RowFlipped),
+ flip_matrix(Matrix, MatrixFlipped). \ No newline at end of file
diff --git a/challenge-242/adam-russell/r/ch-1.r b/challenge-242/adam-russell/r/ch-1.r
new file mode 100644
index 0000000000..e094ff2c90
--- /dev/null
+++ b/challenge-242/adam-russell/r/ch-1.r
@@ -0,0 +1,18 @@
+ch_1 <- function(){
+ structure(list(), class = "ch_1")
+}
+
+missing_members <- function(self, l1, l2){
+ UseMethod("missing_members", self)
+}
+
+missing_members.ch_1 <- function(self, l1, l2){
+ r <- rbind(unique(l1[!(l1 %in% l2)]), unique(l2[!(l2 %in% l1)]))
+ return(r)
+}
+
+ch_1 <- ch_1()
+missing <- missing_members(ch_1, c(1, 2, 3), c(2, 4, 6))
+print(missing)
+missing <- missing_members(ch_1, c(1, 2, 3, 3), c(1, 1, 2, 2))
+print(missing) \ No newline at end of file
diff --git a/challenge-242/adam-russell/r/ch-2.r b/challenge-242/adam-russell/r/ch-2.r
new file mode 100644
index 0000000000..1b9ce700b4
--- /dev/null
+++ b/challenge-242/adam-russell/r/ch-2.r
@@ -0,0 +1,28 @@
+ch_2 <- function() {
+ structure(list(), class = "ch_2")
+}
+
+flip_matrix <- function (self, m) {
+ UseMethod("flip_matrix", self)
+}
+
+flip_matrix.ch_2 <-
+ function (self, m) {
+ r <- t(apply(m, 1, function(row){
+ row<-rev(row)
+ sapply(row, function (x) {
+ as.integer(!x & 1)
+ })
+ }))
+ return(r)
+}
+
+ch_2 <- ch_2()
+matrix_data <- matrix(c(1, 1, 0, 1, 0, 1, 0, 0, 0), nrow = 3, byrow = TRUE)
+print(matrix_data)
+flipped_matrix <- flip_matrix(ch_2, matrix_data)
+print(flipped_matrix)
+matrix_data <- matrix(c(1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0), nrow = 4, byrow = TRUE)
+print(matrix_data)
+flipped_matrix <- flip_matrix(ch_2, matrix_data)
+print(flipped_matrix)