From e20f80af6de0712ace72e4680a7dfc829fb94f58 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 29 Apr 2024 10:08:42 -0400 Subject: Week 267 --- challenge-267/zapwai/c/ch-1.c | 30 ++++++++++++++++++++++++++++++ challenge-267/zapwai/c/ch-2.c | 27 +++++++++++++++++++++++++++ challenge-267/zapwai/javascript/ch-1.js | 23 +++++++++++++++++++++++ challenge-267/zapwai/javascript/ch-2.js | 25 +++++++++++++++++++++++++ challenge-267/zapwai/perl/ch-1.pl | 24 ++++++++++++++++++++++++ challenge-267/zapwai/perl/ch-2.pl | 26 ++++++++++++++++++++++++++ challenge-267/zapwai/python/ch-1.py | 23 +++++++++++++++++++++++ challenge-267/zapwai/python/ch-2.py | 22 ++++++++++++++++++++++ challenge-267/zapwai/rust/ch-1.rs | 29 +++++++++++++++++++++++++++++ challenge-267/zapwai/rust/ch-2.rs | 25 +++++++++++++++++++++++++ 10 files changed, 254 insertions(+) create mode 100644 challenge-267/zapwai/c/ch-1.c create mode 100644 challenge-267/zapwai/c/ch-2.c create mode 100644 challenge-267/zapwai/javascript/ch-1.js create mode 100644 challenge-267/zapwai/javascript/ch-2.js create mode 100644 challenge-267/zapwai/perl/ch-1.pl create mode 100644 challenge-267/zapwai/perl/ch-2.pl create mode 100644 challenge-267/zapwai/python/ch-1.py create mode 100644 challenge-267/zapwai/python/ch-2.py create mode 100644 challenge-267/zapwai/rust/ch-1.rs create mode 100644 challenge-267/zapwai/rust/ch-2.rs 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 + +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 +#include + +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) { + println!("Input: ints = {:?}", ints); + println!("Output: {}", sign_of_prod(ints)); +} + +fn sign_of_prod(ints : Vec) -> 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) { + 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); +} -- cgit