aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Ferrone <zapwai@gmail.com>2024-04-29 10:08:42 -0400
committerDavid Ferrone <zapwai@gmail.com>2024-04-29 10:08:42 -0400
commite20f80af6de0712ace72e4680a7dfc829fb94f58 (patch)
tree5fe2ac439cb99b7fe35ac540752727ef6bbb06b2
parent8af4e17dc115ce2e7a19f8fa11e70c799d2f6fb9 (diff)
downloadperlweeklychallenge-club-e20f80af6de0712ace72e4680a7dfc829fb94f58.tar.gz
perlweeklychallenge-club-e20f80af6de0712ace72e4680a7dfc829fb94f58.tar.bz2
perlweeklychallenge-club-e20f80af6de0712ace72e4680a7dfc829fb94f58.zip
Week 267
-rw-r--r--challenge-267/zapwai/c/ch-1.c30
-rw-r--r--challenge-267/zapwai/c/ch-2.c27
-rw-r--r--challenge-267/zapwai/javascript/ch-1.js23
-rw-r--r--challenge-267/zapwai/javascript/ch-2.js25
-rw-r--r--challenge-267/zapwai/perl/ch-1.pl24
-rw-r--r--challenge-267/zapwai/perl/ch-2.pl26
-rw-r--r--challenge-267/zapwai/python/ch-1.py23
-rw-r--r--challenge-267/zapwai/python/ch-2.py22
-rw-r--r--challenge-267/zapwai/rust/ch-1.rs29
-rw-r--r--challenge-267/zapwai/rust/ch-2.rs25
10 files changed, 254 insertions, 0 deletions
diff --git a/challenge-267/zapwai/c/ch-1.c b/challenge-267/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..da91c3251f
--- /dev/null
+++ b/challenge-267/zapwai/c/ch-1.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+
+int sign_of_prod(int ints[], int intlen) {
+ int num_of_negs = 0;
+ for (int i = 0; i < intlen; i++) {
+ if (ints[i] < 0) {
+ num_of_negs++;
+ } else if (ints[i] == 0) {
+ return 0;
+ }
+ }
+ return (num_of_negs % 2 == 0) ? 1 : -1;
+}
+
+void proc(int ints[], int intlen) {
+ printf("Input: ints = { ");
+ for (int i = 0; i < intlen; i++)
+ printf("%d ", ints[i]);
+ printf("}\n");
+ printf("Output: %d\n", sign_of_prod(ints, intlen));
+}
+
+int main() {
+ int ints[] = {-1, -2, -3, -4, 3, 2, 1};
+ proc(ints, sizeof(ints) / sizeof(int));
+ int ints2[] = {1, 2, 0, -2, -1};
+ proc(ints2, sizeof(ints2) / sizeof(int));
+ int ints3[] = {-1, -1, 1, -1, 2};
+ proc(ints3, sizeof(ints3) / sizeof(int));
+}
diff --git a/challenge-267/zapwai/c/ch-2.c b/challenge-267/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..6563346fb2
--- /dev/null
+++ b/challenge-267/zapwai/c/ch-2.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <string.h>
+
+void proc(char* stringy, int widths[]) {
+ printf("Input: string = %s\n", stringy);
+ int pixwidth = 100;
+ int width = pixwidth;
+ int lines = 1;
+ for (int i = 0; i < strlen(stringy); i++) {
+ int w = widths[stringy[i]-97];
+ if (width - w < 0) {
+ lines++;
+ width = pixwidth;
+ }
+ width -= w;
+ }
+ printf("Output: %d, %d\n", lines, pixwidth - width);
+}
+
+int main() {
+ char* stringy = "abcdefghijklmnopqrstuvwxyz";
+ int widths[] = {10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10};
+ proc(stringy, widths);
+ char* stringy2 = "bbbcccdddaaa";
+ int widths2[] = {4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10};
+ proc(stringy2, widths2);
+}
diff --git a/challenge-267/zapwai/javascript/ch-1.js b/challenge-267/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..5bcbe4888d
--- /dev/null
+++ b/challenge-267/zapwai/javascript/ch-1.js
@@ -0,0 +1,23 @@
+let ints = [-1, -2, -3, -4, 3, 2, 1];
+proc(ints);
+ints = [1, 2, 0, -2, -1];
+proc(ints);
+ints = [-1, -1, 1, -1, 2];
+proc(ints);
+
+function proc(ints) {
+ console.log("Input: ints =",ints);
+ console.log("Output: ", sign_of_prod(ints));
+}
+
+function sign_of_prod(ints) {
+ let num_of_negs = 0;
+ for (let i of ints) {
+ if (i < 0) {
+ num_of_negs++;
+ } else if (i == 0) {
+ return 0;
+ }
+ }
+ return (num_of_negs % 2 == 0) ? 1 : -1;
+}
diff --git a/challenge-267/zapwai/javascript/ch-2.js b/challenge-267/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..d17af8d606
--- /dev/null
+++ b/challenge-267/zapwai/javascript/ch-2.js
@@ -0,0 +1,25 @@
+let alph = "abcdefghijklmnopqrstuvwxyz";
+
+function proc(string, widths) {
+ console.log("Input: string =", string,"\n widths =", widths);
+ let pixwidth = 100;
+ let width = pixwidth;
+ let lines = 1;
+ for (let s of string.split('')) {
+ w = widths[alph.indexOf(s)]
+ if (width - w < 0) {
+ lines++;
+ width = pixwidth;
+ }
+ width -= w
+ }
+ console.log("Output:", lines, pixwidth - width);
+}
+
+let string = "abcdefghijklmnopqrstuvwxyz";
+let widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10];
+proc(string, widths);
+
+string = "bbbcccdddaaa";
+widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10];
+proc(string, widths);
diff --git a/challenge-267/zapwai/perl/ch-1.pl b/challenge-267/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..2cf63e5ebc
--- /dev/null
+++ b/challenge-267/zapwai/perl/ch-1.pl
@@ -0,0 +1,24 @@
+use v5.38;
+my @ints = (-1, -2, -3, -4, 3, 2, 1);
+proc(@ints);
+@ints = (1, 2, 0, -2, -1);
+proc(@ints);
+@ints = (-1, -1, 1, -1, 2);
+proc(@ints);
+
+sub proc(@ints) {
+ say "Input: \@ints = @ints";
+ say "Output: ", sign_of_prod(@ints);
+}
+
+sub sign_of_prod(@ints) {
+ my $num_of_negs = 0;
+ for my $int (@ints) {
+ if ($int < 0) {
+ $num_of_negs++;
+ } elsif ($int == 0) {
+ return 0;
+ }
+ }
+ return ($num_of_negs % 2 == 0) ? 1 : -1;
+}
diff --git a/challenge-267/zapwai/perl/ch-2.pl b/challenge-267/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..007e5ba77c
--- /dev/null
+++ b/challenge-267/zapwai/perl/ch-2.pl
@@ -0,0 +1,26 @@
+use v5.38;
+my $alph = "abcdefghijklmnopqrstuvwxyz";
+
+my $str = "abcdefghijklmnopqrstuvwxyz";
+my @widths = (10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10);
+proc($str, @widths);
+
+$str = "bbbcccdddaaa";
+@widths = (4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10);
+proc($str, @widths);
+
+sub proc($str, @widths) {
+ say "Input: $str";
+ my $pixwidth = 100;
+ my $width = $pixwidth;
+ my $lines = 1;
+ for my $s (split "", $str) {
+ my $w = $widths[index $alph, $s];
+ if ($width - $w < 0) {
+ $lines++;
+ $width = $pixwidth;
+ }
+ $width -= $w;
+ }
+ say "Output: ($lines, ", ($pixwidth - $width), ")";
+}
diff --git a/challenge-267/zapwai/python/ch-1.py b/challenge-267/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..1e71a1e53d
--- /dev/null
+++ b/challenge-267/zapwai/python/ch-1.py
@@ -0,0 +1,23 @@
+def sign_of_prod(ints):
+ num_of_negs = 0
+ for i in ints:
+ if i < 0:
+ num_of_negs += 1
+ elif i == 0:
+ return 0
+ if num_of_negs % 2 == 0:
+ return 1
+ else:
+ return -1
+
+def proc(ints):
+ print("Input: ints = ", ints)
+ print("Output: ", sign_of_prod(ints))
+
+ints = [-1, -2, -3, -4, 3, 2, 1]
+proc(ints)
+ints = [1, 2, 0, -2, -1]
+proc(ints)
+ints = [-1, -1, 1, -1, 2]
+proc(ints)
+
diff --git a/challenge-267/zapwai/python/ch-2.py b/challenge-267/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..5a93f254dc
--- /dev/null
+++ b/challenge-267/zapwai/python/ch-2.py
@@ -0,0 +1,22 @@
+alph = "abcdefghijklmnopqrstuvwxyz"
+
+def proc(string, widths):
+ print("Input: string =", string,"\n widths =", widths)
+ pixwidth = 100
+ width = pixwidth
+ lines = 1
+ for s in list(string):
+ w = widths[alph.index(s)]
+ if width - w < 0:
+ lines += 1
+ width = pixwidth
+ width -= w
+ print("Output:", lines, (pixwidth - width))
+
+string = "abcdefghijklmnopqrstuvwxyz"
+widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
+proc(string, widths)
+
+string = "bbbcccdddaaa"
+widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
+proc(string, widths)
diff --git a/challenge-267/zapwai/rust/ch-1.rs b/challenge-267/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..ee8283bab2
--- /dev/null
+++ b/challenge-267/zapwai/rust/ch-1.rs
@@ -0,0 +1,29 @@
+fn main() {
+ let ints = vec![-1, -2, -3, -4, 3, 2, 1];
+ proc(ints);
+ let ints2 = vec![1, 2, 0, -2, -1];
+ proc(ints2);
+ let ints3 = vec![-1, -1, 1, -1, 2];
+ proc(ints3);
+}
+
+fn proc(ints : Vec<i32>) {
+ println!("Input: ints = {:?}", ints);
+ println!("Output: {}", sign_of_prod(ints));
+}
+
+fn sign_of_prod(ints : Vec<i32>) -> i32 {
+ let mut num_of_negs = 0;
+ for i in ints {
+ if i < 0 {
+ num_of_negs += 1;
+ } else if i == 0 {
+ return 0;
+ }
+ }
+ if num_of_negs % 2 == 0 {
+ return 1;
+ } else {
+ return -1;
+ }
+}
diff --git a/challenge-267/zapwai/rust/ch-2.rs b/challenge-267/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..756a497bf8
--- /dev/null
+++ b/challenge-267/zapwai/rust/ch-2.rs
@@ -0,0 +1,25 @@
+fn proc(stringy : &str, widths : Vec<i32>) {
+ let alph = "abcdefghijklmnopqrstuvwxyz";
+ println!("Input: string = {stringy}");
+ let pixwidth = 100;
+ let mut width = pixwidth;
+ let mut lines = 1;
+ for s in stringy.chars() {
+ let w = widths[alph.chars().position(|c| c == s).unwrap()];
+ if width - w < 0 {
+ lines += 1;
+ width = pixwidth;
+ }
+ width -= w;
+ }
+ println!("Output: {}, {}", lines, pixwidth - width);
+}
+
+fn main() {
+ let stringy = "abcdefghijklmnopqrstuvwxyz";
+ let widths = vec![10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10];
+ proc(stringy, widths);
+ let stringy2 = "bbbcccdddaaa";
+ let widths2 = vec![4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10];
+ proc(stringy2, widths2);
+}