aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZapwai <zapwai@gmail.com>2024-02-12 21:34:08 -0500
committerGitHub <noreply@github.com>2024-02-12 21:34:08 -0500
commit49b818982b37f86d2b1efdce96ed7a858e7e851e (patch)
tree95e5b2f40d25e468dd9036baf2ef8661ed5d4358
parent94d2ed24dcd44638355ac7c5084e05d8b8ca9f41 (diff)
downloadperlweeklychallenge-club-49b818982b37f86d2b1efdce96ed7a858e7e851e.tar.gz
perlweeklychallenge-club-49b818982b37f86d2b1efdce96ed7a858e7e851e.tar.bz2
perlweeklychallenge-club-49b818982b37f86d2b1efdce96ed7a858e7e851e.zip
Week 256
-rw-r--r--challenge-256/zapwai/c/ch-1.c53
-rw-r--r--challenge-256/zapwai/c/ch-2.c42
-rw-r--r--challenge-256/zapwai/perl/ch-1.pl27
-rw-r--r--challenge-256/zapwai/perl/ch-2.pl32
-rw-r--r--challenge-256/zapwai/python/ch-1.py14
-rw-r--r--challenge-256/zapwai/python/ch-2.py20
-rw-r--r--challenge-256/zapwai/rust/ch-1.rs36
-rw-r--r--challenge-256/zapwai/rust/ch-2.rs34
8 files changed, 258 insertions, 0 deletions
diff --git a/challenge-256/zapwai/c/ch-1.c b/challenge-256/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..18615d1e5f
--- /dev/null
+++ b/challenge-256/zapwai/c/ch-1.c
@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void proc(char**, int);
+char* rev_word(char*);
+
+int main() {
+ char* words[] = {"ab", "de", "ed", "bc"};
+ int len = sizeof(words) / sizeof(words[0]); /* Assuming that each word is the same length. */
+ proc(words, len);
+
+ char* words2[] = {"aa", "ba", "cd", "ed"};
+ int len2 = sizeof(words2) / sizeof(words2[0]);
+ proc(words2, len2);
+
+ char* words3[] = {"uv", "qp", "st", "vu", "mn", "pq"};
+ int len3 = sizeof(words3) / sizeof(words3[0]);
+ proc(words3, len3);
+}
+
+void proc(char* words[], int len) {
+ int count = 0;
+ for (int i = 0; i < len - 1; i++) {
+ for (int j = i + 1; j < len; j++) {
+ if (!strcmp(words[i], rev_word(words[j]))) {
+ count++;
+ }
+ }
+ }
+ printf("Input: {");
+ for (int i = 0; i < len - 1; i++) {
+ printf("%s, ", words[i]);
+ }
+ printf("%s}\n", words[len - 1]);
+ printf("Output: %d\n\n", count);
+}
+
+char* rev_word(char* stringy) {
+ int e = strlen(stringy);
+ char* word = (char*) malloc( (e + 1)*sizeof(char) ); /* Did not bother to free this */
+ strcpy(word, stringy);
+ e--;
+ int s = 0;
+ while (e > s) {
+ char temp = word[s];
+ word[s] = word[e];
+ word[e] = temp;
+ e--;
+ s++;
+ }
+ return word;
+}
diff --git a/challenge-256/zapwai/c/ch-2.c b/challenge-256/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..4f2bd7be84
--- /dev/null
+++ b/challenge-256/zapwai/c/ch-2.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+void proc(char*, char*);
+int main() {
+ char s1[] = "abcd";
+ char s2[] = "1234";
+ proc(s1, s2);
+
+ char r1[] = "abc";
+ char r2[] = "12345";
+ proc(r1, r2);
+
+ char q1[] = "abcde";
+ char q2[] = "123";
+ proc(q1, q2);
+}
+
+void proc(char* str1, char* str2) {
+ printf("Input: str1 = %s, str2 = %s\n", str1, str2);
+ int len1 = strlen(str1);
+ int len2 = strlen(str2);
+ char* merged = malloc( sizeof(char) * (1 + len1 + len2) );
+ int N = (len1 < len2) ? len1 : len2;
+ int i;
+ for (i = 0; i < N; i++) {
+ merged[2*i] = str1[i];
+ merged[2*i + 1] = str2[i];
+ }
+ if (len1 < len2) {
+ for (int s = 2*i; i < len2; s++, i++) {
+ merged[s] = str2[i];
+ }
+ }
+ else {
+ for (int s = 2*i; i < len1; s++, i++) {
+ merged[s] = str1[i];
+ }
+ }
+ printf("Output: %s\n\n", merged);
+ free(merged);
+}
diff --git a/challenge-256/zapwai/perl/ch-1.pl b/challenge-256/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..531b3c6b78
--- /dev/null
+++ b/challenge-256/zapwai/perl/ch-1.pl
@@ -0,0 +1,27 @@
+use v5.30;
+
+my @words = ("ab", "de", "ed", "bc");
+my @words2 = ("aa", "ba", "cd", "ed");
+my @words3 = ("uv", "qp", "st", "vu", "mn", "pq");
+
+proc($_) foreach (\@words, \@words2, \@words3);
+
+sub proc {
+ my $ref = shift;
+ my @words = @{$ref};
+ say "Input: \@words = (" . join(", ", @words) . ")";
+ my $cnt_of_pairs = 0;
+ count_pairs(\$cnt_of_pairs, \@words);
+ say "Output: $cnt_of_pairs";
+}
+
+sub count_pairs {
+ my @words = @{$_[1]};
+ foreach my $i1 (0 .. $#words - 1) {
+ for my $i2 ($i1 + 1 .. $#words) {
+ if ($words[$i1] eq reverse $words[$i2]) {
+ ${$_[0]}++;
+ }
+ }
+ }
+}
diff --git a/challenge-256/zapwai/perl/ch-2.pl b/challenge-256/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..f20d1f88d7
--- /dev/null
+++ b/challenge-256/zapwai/perl/ch-2.pl
@@ -0,0 +1,32 @@
+use v5.30;
+
+my $str1a = "abcd"; my $str2a = "1234";
+my $str1b = "abc"; my $str2b = "12345";
+my $str1c = "abcde"; my $str2c = "123";
+
+proc($str1a,$str2a);
+proc($str1b,$str2b);
+proc($str1c,$str2c);
+
+sub proc {
+ my $s1 = shift;
+ my $s2 = shift;
+ say "Input: \$s1 = $s1, \$s2 = $s2";
+ print "Output: ";
+ say merge($s1, $s2);
+}
+
+sub merge {
+ my $str1 = shift;
+ my $str2 = shift;
+
+ my $s;
+ my @a = split("", $str1);
+ my @b = split("", $str2);
+
+ my $M = ($#a > $#b) ? $#a : $#b;
+ for (0 .. $M) {
+ $s .= $a[$_].$b[$_];
+ }
+ return $s;
+}
diff --git a/challenge-256/zapwai/python/ch-1.py b/challenge-256/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..7117690a82
--- /dev/null
+++ b/challenge-256/zapwai/python/ch-1.py
@@ -0,0 +1,14 @@
+def proc(words):
+ print("Input:", words)
+ cnt = 0
+ for i in range(len(words)-1):
+ for j in range(i+1, len(words)):
+ if words[i] == words[j][::-1]:
+ cnt += 1
+ print("Output:", cnt)
+words = ['ab', 'de', 'ed', 'bc']
+proc(words)
+words = ['aa', 'ba', 'ed', 'cd']
+proc(words)
+words = ['uv', 'qp', 'st', 'vu', 'mn', 'pq']
+proc(words)
diff --git a/challenge-256/zapwai/python/ch-2.py b/challenge-256/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..3296f37bbe
--- /dev/null
+++ b/challenge-256/zapwai/python/ch-2.py
@@ -0,0 +1,20 @@
+def proc(str1, str2):
+ print(f"Input: str1 = {str1}, str2 = {str2}")
+ n1 = len(str1)
+ n2 = len(str2)
+ if n1 < n2:
+ m = n1
+ ending = str2[m:]
+ else:
+ m = n2
+ ending = str1[m:]
+ print("Output: ", end='')
+ for i in range(m):
+ print(str1[i]+str2[i], end='')
+ print(ending)
+str1 = "abcd"; str2 = "1234"
+proc(str1, str2)
+str1 = "abc"; str2 = "12345"
+proc(str1, str2)
+str1 = "abcde"; str2 = "123"
+proc(str1, str2)
diff --git a/challenge-256/zapwai/rust/ch-1.rs b/challenge-256/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..6bc86c8379
--- /dev/null
+++ b/challenge-256/zapwai/rust/ch-1.rs
@@ -0,0 +1,36 @@
+fn main() {
+ let words = vec!("ab", "de", "ed", "bc");
+ proc(words);
+ let words2 = vec!("aa", "ba", "cd", "ed");
+ proc(words2);
+ let words3 = vec!("uv", "qp", "st", "vu", "mn", "pq");
+ proc(words3);
+}
+
+fn proc(words: Vec<&str>) {
+ println!("Input: words = {:?}",words);
+ let mut cnt = 0;
+ count(&mut cnt, &words);
+ println!("Output: {cnt}");
+}
+
+fn count(cnt: &mut i32, words: &Vec<&str>) {
+ let mut i = 0;
+ loop {
+ let mut j = i + 1;
+ loop {
+ if words[i] == reverse_string(words[j]) {
+ *cnt += 1;
+ }
+ j += 1;
+ if j >= words.len() { break; }
+ }
+ i += 1;
+ if i >= words.len() - 1 { break; }
+ }
+}
+
+fn reverse_string(input: &str) -> String {
+ let reversed: String = input.chars().rev().collect();
+ reversed
+}
diff --git a/challenge-256/zapwai/rust/ch-2.rs b/challenge-256/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..47775bd5c3
--- /dev/null
+++ b/challenge-256/zapwai/rust/ch-2.rs
@@ -0,0 +1,34 @@
+fn main() {
+ let str1a = "abcd"; let str2a = "1234";
+ let str1b = "abc"; let str2b = "12345";
+ let str1c = "abcde"; let str2c = "123";
+ proc(str1a, str2a);
+ proc(str1b, str2b);
+ proc(str1c, str2c);
+}
+
+fn proc(str1: &str, str2: &str) {
+ let m;
+ let ending;
+ if str1.len() < str2.len() {
+ m = str1.len();
+ ending = str2[m..].to_string();
+ }
+ else {
+ m = str2.len();
+ ending = str1[m..].to_string();
+ }
+ let mut ans = "".to_string();
+ let mut i = 0;
+ loop {
+ ans = [ans,
+ str1.chars().nth(i).unwrap().to_string(),
+ str2.chars().nth(i).unwrap().to_string()
+ ].join("");
+ i += 1;
+ if i >= m { break; }
+ }
+ ans = [ans, ending].join("");
+ println!("Input: str1 = {str1}, str2 = {str2}");
+ println!("Output: {ans}");
+}