aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Ferrone <zapwai@gmail.com>2024-04-22 10:41:58 -0400
committerDavid Ferrone <zapwai@gmail.com>2024-04-22 10:41:58 -0400
commit9e76d8d75b217afd882e2a661709b63766d058be (patch)
treeaa0637579f3ef4a6cdbbdced6da73347eb7e7ce8
parentaed41ed119210ab32e32b82af8039f2c1457726b (diff)
downloadperlweeklychallenge-club-9e76d8d75b217afd882e2a661709b63766d058be.tar.gz
perlweeklychallenge-club-9e76d8d75b217afd882e2a661709b63766d058be.tar.bz2
perlweeklychallenge-club-9e76d8d75b217afd882e2a661709b63766d058be.zip
Week 266
-rw-r--r--challenge-266/zapwai/c/ch-1.c93
-rw-r--r--challenge-266/zapwai/c/ch-2.c44
-rw-r--r--challenge-266/zapwai/javascript/ch-1.js42
-rw-r--r--challenge-266/zapwai/javascript/ch-2.js38
-rw-r--r--challenge-266/zapwai/perl/ch-1.pl35
-rw-r--r--challenge-266/zapwai/perl/ch-2.pl38
-rw-r--r--challenge-266/zapwai/python/ch-1.py35
-rw-r--r--challenge-266/zapwai/python/ch-2.py41
-rw-r--r--challenge-266/zapwai/rust/ch-1.rs44
-rw-r--r--challenge-266/zapwai/rust/ch-2.rs52
10 files changed, 462 insertions, 0 deletions
diff --git a/challenge-266/zapwai/c/ch-1.c b/challenge-266/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..c5be7d73d1
--- /dev/null
+++ b/challenge-266/zapwai/c/ch-1.c
@@ -0,0 +1,93 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#define MAX 100
+
+void proc(char* line1, char* line2) {
+ char* words[MAX];
+ int level = 0;
+ char* l1 = strdup(line1);
+ char* token = strtok(l1, " ");
+ while (token != NULL) {
+ words[level] = malloc(50*sizeof(char));
+ strcpy(words[level], token);
+ level++;
+ token = strtok(NULL, " ");
+ }
+ char* l2 = strdup(line2);
+ char* token2 = strtok(l2, " ");
+ while (token2 != NULL) {
+ words[level] = malloc(50*sizeof(char));
+ strcpy(words[level], token2);
+ level++;
+ token2 = strtok(NULL, " ");
+ }
+
+ char* sorted[level];
+ for (int i = 0; i < level; i++) {
+ sorted[i] = malloc(50*sizeof(char));
+ strcpy(sorted[i], words[i]);
+ }
+ int cnt = 0;
+ do {
+ cnt = 0;
+ for (int i = 0; i < level - 1; i++) {
+ if (strcmp(sorted[i+1], sorted[i]) > 0) {
+ char* word = sorted[i+1];
+ sorted[i+1] = sorted[i];
+ sorted[i] = word;
+ cnt++;
+ }
+ }
+ } while (cnt != 0);
+
+ bool match_flag = false;
+ char* ans[MAX] = {};
+ int ans_len = 0;
+ for (int i = 0; i < level - 1; i++) {
+ if (match_flag) {
+ if (0 != strcmp(sorted[i], sorted[i+1])) {
+ match_flag = false;
+ }
+ continue;
+ }
+ if (0 == strcmp(sorted[i], sorted[i+1])) {
+ match_flag = true;
+ continue;
+ }
+ ans[ans_len] = malloc(50*sizeof(char));
+ strcpy(ans[ans_len], sorted[i]);
+ ans_len++;
+ }
+ if (!match_flag) {
+ strcpy(ans[ans_len], sorted[level-1]);
+ ans_len++;
+ }
+ for (int i = 0; i < level; i++) {
+ free(words[i]);
+ free(sorted[i]);
+ }
+ printf("Input: line1 = %s, line2 = %s\n", line1, line2);
+ printf("Output: { ");
+ for (int i = 0; i < ans_len; i++) {
+ printf("%s ", ans[i]);
+ free(ans[i]);
+ }
+ printf("}\n");
+}
+
+int main() {
+ char* line1 = "Mango is sweet";
+ char* line2 = "Mango is sour";
+ proc(line1, line2);
+
+ line1 = "Mango Mango";
+ line2 = "Orange";
+ proc(line1, line2);
+
+ line1 = "Mango is Mango";
+ line2 = "Orange is Orange";
+ proc(line1, line2);
+}
+
diff --git a/challenge-266/zapwai/c/ch-2.c b/challenge-266/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..860ac1c451
--- /dev/null
+++ b/challenge-266/zapwai/c/ch-2.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdbool.h>
+
+void proc(int N, int matrix[N][N]) {
+ bool fail = false;
+ printf("Input: matrix = \n");
+ for (int i = 0; i < N; i++) {
+ printf("\t");
+ for (int j = 0; j < N; j++) {
+ printf("%d ", matrix[i][j]);
+ if ((i == j) || (i == N - j - 1)) {
+ if (matrix[i][j] == 0)
+ fail = true;
+ } else {
+ if (matrix[i][j] != 0)
+ fail = true;
+ }
+ }
+ printf("\n");
+ }
+ printf("Output: %s\n", (!fail) ? "True" : "False");
+}
+
+int main() {
+ int matrix[4][4] = { {1, 0, 0, 2},
+ {0, 3, 4, 0},
+ {0, 5, 6, 0},
+ {7, 0, 0, 1},
+ };
+ proc(4, matrix);
+
+ int matrix2[3][3] = { {1, 2, 3},
+ {4, 5, 6},
+ {7, 8, 9},
+ };
+ proc(3, matrix2);
+
+ int matrix3[3][3] = { {1, 0, 2},
+ {0, 3, 0},
+ {4, 0, 5},
+ };
+ proc(3, matrix3);
+}
+
diff --git a/challenge-266/zapwai/javascript/ch-1.js b/challenge-266/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..80eb3a08ed
--- /dev/null
+++ b/challenge-266/zapwai/javascript/ch-1.js
@@ -0,0 +1,42 @@
+let line1 = 'Mango is sweet';
+let line2 = 'Mango is sour';
+proc(line1, line2);
+
+line1 = 'Mango Mango';
+line2 = 'Orange';
+proc(line1, line2);
+
+line1 = 'Mango is Mango';
+line2 = 'Orange is Orange';
+proc(line1, line2);
+
+function proc(line1, line2) {
+ let words = [];
+ for (let s of line1.split(" ")) {
+ words.push(s);
+ }
+ for (let s of line2.split(" ")) {
+ words.push(s);
+ }
+ words.sort();
+ let match_flag = false;
+ let ans = [];
+ for (let i = 0; i < words.length; i++) {
+ if (match_flag) {
+ if (words[i] != words[i+1]) {
+ match_flag = false;
+ }
+ continue;
+ }
+ if (words[i] == words[i+1]) {
+ match_flag = true;
+ continue;
+ }
+ ans.push(words[i]);
+ }
+ if (match_flag) {
+ ans.push(words[words.length-1])
+ }
+ console.log("Input: l1 =", line1, "l2 =", line2);
+ console.log("Output:",ans);
+}
diff --git a/challenge-266/zapwai/javascript/ch-2.js b/challenge-266/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..abbfbddce5
--- /dev/null
+++ b/challenge-266/zapwai/javascript/ch-2.js
@@ -0,0 +1,38 @@
+function proc(N, matrix) {
+ let fail = false;
+ console.log("Input: matrix =", matrix);
+ for (let i = 0; i < N; i++) {
+ for (let j = 0; j < N; j++) {
+ if ((i == j) || (i == N - j - 1)) {
+ if (matrix[i][j] == 0){
+ fail = true;
+ }
+ } else {
+ if (matrix[i][j] != 0) {
+ fail = true;
+ }
+ }
+ }
+ }
+ let output = !fail ? "True" : "False";
+ console.log("Output:", output);
+}
+
+let matrix = [ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+ ];
+proc(4, matrix);
+
+let matrix2 = [ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+ ];
+proc(3, matrix2);
+
+let matrix3 = [ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+ ];
+proc(3, matrix3);
diff --git a/challenge-266/zapwai/perl/ch-1.pl b/challenge-266/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..851cd2fd89
--- /dev/null
+++ b/challenge-266/zapwai/perl/ch-1.pl
@@ -0,0 +1,35 @@
+use v5.38;
+
+my $line1 = 'Mango is sweet';
+my $line2 = 'Mango is sour';
+proc($line1, $line2);
+
+$line1 = 'Mango Mango';
+$line2 = 'Orange';
+proc($line1, $line2);
+
+$line1 = 'Mango is Mango';
+$line2 = 'Orange is Orange';
+proc($line1, $line2);
+
+sub proc($line1, $line2) {
+ my @words = sort (split(" ", $line1), split(" ", $line2));
+ my $match_flag = 0;
+ my @ans;
+ for my $i (0 .. $#words - 1) {
+ if ($match_flag) {
+ if ($words[$i] ne $words[$i+1]) {
+ $match_flag = 0;
+ }
+ next;
+ }
+ if ($words[$i] eq $words[$i+1]) {
+ $match_flag = 1;
+ next;
+ }
+ push @ans, $words[$i];
+ }
+ push @ans, $words[$#words] unless ($match_flag);
+ say "Input: \$line1 = $line1; \$line2 = $line2";
+ say "Output: @ans";
+}
diff --git a/challenge-266/zapwai/perl/ch-2.pl b/challenge-266/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..a81bb7e10a
--- /dev/null
+++ b/challenge-266/zapwai/perl/ch-2.pl
@@ -0,0 +1,38 @@
+use v5.38;
+my $matrix = [ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+ ];
+proc($matrix);
+
+$matrix = [ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+ ];
+proc($matrix);
+
+$matrix = [ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+ ];
+proc($matrix);
+
+sub proc($matrix) {
+ my $fail = 0;
+ my $N = $#{$matrix};
+ say "Input: matrix = ";
+ for my $i (0 .. $N) {
+ print "\t";
+ for my $j (0 .. $N) {
+ print $$matrix[$i][$j];
+ if (($i == $j) || ($i == $N - $j)) {
+ $fail = 1 unless ($$matrix[$i][$j] != 0);
+ } else {
+ $fail = 1 unless ($$matrix[$i][$j] == 0);
+ }
+ }
+ say "";
+ }
+ say "Output: ", ($fail == 0) ? "True" : "False";
+}
diff --git a/challenge-266/zapwai/python/ch-1.py b/challenge-266/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..e29bdc05c5
--- /dev/null
+++ b/challenge-266/zapwai/python/ch-1.py
@@ -0,0 +1,35 @@
+def proc(line1, line2):
+ words = []
+ for l in line1.split(" "):
+ words.append(l)
+ for l in line2.split(" "):
+ words.append(l)
+ words.sort()
+ match_flag = False
+ ans = []
+ for i in range(len(words) - 1):
+ if match_flag:
+ if words[i] != words[i+1]:
+ match_flag = False
+ continue
+ if words[i] == words[i+1]:
+ match_flag = True
+ continue
+ ans.append(words[i])
+ if not match_flag:
+ ans.append(words[-1])
+ print("Input: line1 =", line1, "line2 =",line2)
+ print("Output: ",ans)
+
+line1 = 'Mango is sweet'
+line2 = 'Mango is sour'
+proc(line1, line2)
+
+line1 = 'Mango Mango'
+line2 = 'Orange'
+proc(line1, line2)
+
+line1 = 'Mango is Mango'
+line2 = 'Orange is Orange'
+proc(line1, line2)
+
diff --git a/challenge-266/zapwai/python/ch-2.py b/challenge-266/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..ac2dce4820
--- /dev/null
+++ b/challenge-266/zapwai/python/ch-2.py
@@ -0,0 +1,41 @@
+def proc(matrix):
+ fail = False
+ N = len(matrix[0])
+ print("Input: matrix = ")
+ for i in range(N):
+ print("\t", end='')
+ for j in range(N):
+ print(matrix[i][j], end='')
+ if (i == j) or (i == N - j - 1):
+ if matrix[i][j] == 0:
+ fail = True
+ else:
+ if matrix[i][j] != 0:
+ fail = True
+ print()
+ output = ""
+ if not fail:
+ output = "True"
+ else:
+ output = "False"
+ print("Output:", output)
+
+matrix = [ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+ ]
+proc(matrix)
+
+matrix = [ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+ ]
+proc(matrix)
+
+matrix = [ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+ ]
+proc(matrix)
+
diff --git a/challenge-266/zapwai/rust/ch-1.rs b/challenge-266/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..cdba8458a5
--- /dev/null
+++ b/challenge-266/zapwai/rust/ch-1.rs
@@ -0,0 +1,44 @@
+fn main() {
+ let mut line1 = "Mango is sweet";
+ let mut line2 = "Mango is sour";
+ proc(line1, line2);
+
+ line1 = "Mango Mango";
+ line2 = "Orange";
+ proc(line1, line2);
+
+ line1 = "Mango is Mango";
+ line2 = "Orange is Orange";
+ proc(line1, line2);
+}
+
+fn proc(line1 : &str, line2 : &str) {
+ let mut words :Vec<&str> = Vec::new();
+ for w in line1.split(" ") {
+ words.push(w);
+ }
+ for w in line2.split(" ") {
+ words.push(w);
+ }
+ words.sort();
+ let mut match_flag = false;
+ let mut ans : Vec<&str> = Vec::new();
+ for i in 0 .. words.len() - 1 {
+ if match_flag {
+ if words[i] != words[i+1] {
+ match_flag = false;
+ }
+ continue;
+ }
+ if words[i] == words[i+1] {
+ match_flag = true;
+ continue;
+ }
+ ans.push(words[i]);
+ }
+ if !match_flag {
+ ans.push(words.last().unwrap());
+ }
+ println!("Input: line1 = {line1}; line2 = {line2}");
+ println!("Output: {:?}", ans);
+}
diff --git a/challenge-266/zapwai/rust/ch-2.rs b/challenge-266/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..d558e7cdc2
--- /dev/null
+++ b/challenge-266/zapwai/rust/ch-2.rs
@@ -0,0 +1,52 @@
+fn proc(n : i32, matrix : Vec<Vec<i32>>) {
+ let mut fail :bool = false;
+ println!("Input: matrix = ");
+ for i in 0 .. n {
+ print!("\t");
+ for j in 0 .. n {
+ let i_ = i as usize;
+ let j_ = j as usize;
+ print!("{} ", matrix[i_][j_]);
+ if (i == j) || (i == n - j - 1) {
+ if matrix[i_][j_] == 0 {
+ fail = true;
+ }
+ } else {
+ if matrix[i_][j_] != 0 {
+ fail = true;
+ }
+ }
+ }
+ println!();
+ }
+ let mut output : &str;
+ if !fail {
+ output = "True";
+ } else {
+ output = "False";
+ }
+ println!("Output: {}", output);
+}
+
+fn main() {
+ let matrix = vec![
+ vec![1, 0, 0, 2],
+ vec![0, 3, 4, 0],
+ vec![0, 5, 6, 0],
+ vec![7, 0, 0, 1],
+ ];
+ proc(4, matrix);
+
+ let matrix2 = vec![ vec![1, 2, 3],
+ vec![4, 5, 6],
+ vec![7, 8, 9],
+ ];
+ proc(3, matrix2);
+
+ let matrix3 = vec![ vec![1, 0, 2],
+ vec![0, 3, 0],
+ vec![4, 0, 5],
+ ];
+ proc(3, matrix3);
+}
+