aboutsummaryrefslogtreecommitdiff
path: root/challenge-262
diff options
context:
space:
mode:
authorDavid Ferrone <zapwai@gmail.com>2024-03-25 09:49:00 -0400
committerDavid Ferrone <zapwai@gmail.com>2024-03-25 09:49:00 -0400
commit19f63fc58c320d068d069868282a77f14989b2cc (patch)
treecc55185deabd008ed48b894a6ba7335069ba7fae /challenge-262
parent041fe9129e3ef4d86df461a0feeee1b3740d5758 (diff)
downloadperlweeklychallenge-club-19f63fc58c320d068d069868282a77f14989b2cc.tar.gz
perlweeklychallenge-club-19f63fc58c320d068d069868282a77f14989b2cc.tar.bz2
perlweeklychallenge-club-19f63fc58c320d068d069868282a77f14989b2cc.zip
Week 262
Diffstat (limited to 'challenge-262')
-rw-r--r--challenge-262/zapwai/c/ch-1.c23
-rw-r--r--challenge-262/zapwai/c/ch-2.c21
-rw-r--r--challenge-262/zapwai/javascript/ch-1.js15
-rw-r--r--challenge-262/zapwai/javascript/ch-2.js15
-rw-r--r--challenge-262/zapwai/perl/ch-1.pl14
-rw-r--r--challenge-262/zapwai/perl/ch-2.pl12
-rw-r--r--challenge-262/zapwai/python/ch-1.py13
-rw-r--r--challenge-262/zapwai/python/ch-2.py11
-rw-r--r--challenge-262/zapwai/rust/ch-1.rs24
-rw-r--r--challenge-262/zapwai/rust/ch-2.rs17
10 files changed, 165 insertions, 0 deletions
diff --git a/challenge-262/zapwai/c/ch-1.c b/challenge-262/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..f76a76555f
--- /dev/null
+++ b/challenge-262/zapwai/c/ch-1.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int main() {
+ int ints[] = {-3, 1, 2, -1, 3, -2, 4};
+ printf("Input: ints = { ");
+ for (int i = 0; i < sizeof(ints)/sizeof(int); i++)
+ printf("%d ", ints[i]);
+ printf("}\n");
+ int neg = 0;
+ int pos = 0;
+ for (int i = 0; i < sizeof(ints)/sizeof(int); i++) {
+ if (ints[i] < 0) {
+ if (neg > ints[i])
+ neg = ints[i];
+ } else {
+ if (pos < ints[i])
+ pos = ints[i];
+ }
+ }
+ int ans = (abs(neg) > pos) ? abs(neg) : pos;
+ printf( "Output: %d\n", ans);
+}
diff --git a/challenge-262/zapwai/c/ch-2.c b/challenge-262/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..1eedbf8b16
--- /dev/null
+++ b/challenge-262/zapwai/c/ch-2.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+
+int main() {
+ int ints[] = {3, 1, 2, 2, 2, 1, 3};
+ int k = 2;
+ int intslen = sizeof(ints)/sizeof(int);
+ printf("Input: ints = { ");
+ for (int i = 0; i < intslen; i++)
+ printf("%d ", ints[i]);
+ printf("}, k = %d\n", k);
+ int cnt = 0;
+ for (int i = 0; i < intslen - 1; i++) {
+ for (int j = i + 1; j < intslen; j++) {
+ if (ints[i] != ints[j])
+ continue;
+ if (i * j % k == 0)
+ cnt++;
+ }
+ }
+ printf("Output: %d\n", cnt);
+}
diff --git a/challenge-262/zapwai/javascript/ch-1.js b/challenge-262/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..836fc78f7c
--- /dev/null
+++ b/challenge-262/zapwai/javascript/ch-1.js
@@ -0,0 +1,15 @@
+let ints = [-3, 1, 2, -1, 3, -2, 4];
+console.log("Input:", ints);
+let neg = 0, pos = 0;
+for (let num of ints) {
+ if (num < 0) {
+ if (neg > num)
+ neg = num;
+ }
+ else {
+ if (pos < num)
+ pos = num;
+ }
+}
+ans = Math.max(Math.abs(neg), pos);
+console.log("Output:", ans);
diff --git a/challenge-262/zapwai/javascript/ch-2.js b/challenge-262/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..cbfd30ac84
--- /dev/null
+++ b/challenge-262/zapwai/javascript/ch-2.js
@@ -0,0 +1,15 @@
+let ints = [3, 1, 2, 2, 2, 1, 3];
+let k = 2;
+console.log("Input: ints:", ints, "k:", k);
+let cnt = 0;
+for (let i = 0; i < ints.length - 1; i++) {
+ for (let j = i + 1; j < ints.length; j++) {
+ if (ints[i] != ints[j]) {
+ continue;
+ }
+ if (i * j % k == 0) {
+ cnt++;
+ }
+ }
+}
+console.log("Output:", cnt);
diff --git a/challenge-262/zapwai/perl/ch-1.pl b/challenge-262/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..a02ff043c0
--- /dev/null
+++ b/challenge-262/zapwai/perl/ch-1.pl
@@ -0,0 +1,14 @@
+use v5.38;
+use List::Util qw( max );
+my @ints = (-3, 1, 2, -1, 3, -2, 4);
+say "Input: (" . join(", ", @ints) . ")";
+my ($neg, $pos) = (0, 0);
+for my $num (@ints) {
+ if ($num < 0) {
+ $neg = $num if ($neg > $num);
+ } else {
+ $pos = $num if ($pos < $num);
+ }
+}
+my $ans = max(abs($neg), $pos);
+say "Output: $ans";
diff --git a/challenge-262/zapwai/perl/ch-2.pl b/challenge-262/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..ca1bdfe375
--- /dev/null
+++ b/challenge-262/zapwai/perl/ch-2.pl
@@ -0,0 +1,12 @@
+use v5.38;
+my @ints = (3, 1, 2, 2, 2, 1, 3);
+my $k = 2;
+say "Input: \@ints = (".join(",", @ints)."), \$k = $k";
+my $cnt = 0;
+for my $i (0 .. $#ints - 1) {
+ for my $j ($i + 1 .. $#ints) {
+ next unless ($ints[$i] == $ints[$j]);
+ $cnt++ if ($i * $j % $k == 0);
+ }
+}
+say "Output: $cnt";
diff --git a/challenge-262/zapwai/python/ch-1.py b/challenge-262/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..d3354b4a69
--- /dev/null
+++ b/challenge-262/zapwai/python/ch-1.py
@@ -0,0 +1,13 @@
+ints = [-3, 1, 2, -1, 3, -2, 4]
+print("Input:", ints)
+neg = 0
+pos = 0
+for num in ints:
+ if num < 0:
+ if neg > num:
+ neg = num
+ else:
+ if pos < num:
+ pos = num
+ans = max(abs(neg), pos)
+print("Output:", ans)
diff --git a/challenge-262/zapwai/python/ch-2.py b/challenge-262/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..a78b532467
--- /dev/null
+++ b/challenge-262/zapwai/python/ch-2.py
@@ -0,0 +1,11 @@
+ints = [3, 1, 2, 2, 2, 1, 3]
+k = 2
+print("Input: ints:", ints, "k:", k)
+cnt = 0
+for i in range(len(ints) - 1):
+ for j in range(i + 1, len(ints)):
+ if ints[i] != ints[j]:
+ continue
+ if i * j % k == 0:
+ cnt += 1
+print("Output:", cnt)
diff --git a/challenge-262/zapwai/rust/ch-1.rs b/challenge-262/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..6f7a956c73
--- /dev/null
+++ b/challenge-262/zapwai/rust/ch-1.rs
@@ -0,0 +1,24 @@
+fn main() {
+ let ints = vec![-3, 1, 2, -1, 3, -2, 4];
+ println!("Input: {:?}", ints);
+ let mut neg : i32 = 0;
+ let mut pos : i32 = 0;
+ for i in 0 .. ints.len() {
+ if ints[i] < 0 {
+ if neg > ints[i] {
+ neg = ints[i];
+ }
+ } else {
+ if pos < ints[i] {
+ pos = ints[i];
+ }
+ }
+ }
+ let mut ans = 0;
+ if neg.abs() > pos {
+ ans = neg.abs();
+ } else {
+ ans = pos;
+ }
+ println!("Output: {ans}");
+}
diff --git a/challenge-262/zapwai/rust/ch-2.rs b/challenge-262/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..f639dc4220
--- /dev/null
+++ b/challenge-262/zapwai/rust/ch-2.rs
@@ -0,0 +1,17 @@
+fn main() {
+ let ints = vec![3, 1, 2, 2, 2, 1, 3];
+ let k = 2;
+ println!("Input: ints = {:?}, k = {k}", ints);
+ let mut cnt = 0;
+ for i in 0 .. ints.len() - 1 {
+ for j in i + 1 .. ints.len() {
+ if ints[i] != ints[j] {
+ continue;
+ }
+ if i * j % k == 0 {
+ cnt += 1;
+ }
+ }
+ }
+ println!("Output: {cnt}");
+}