aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Ferrone <zapwai@gmail.com>2024-05-27 00:30:38 -0400
committerDavid Ferrone <zapwai@gmail.com>2024-05-27 00:30:38 -0400
commitcc7611c673901442ddfac5a71c691350144dcd03 (patch)
tree438179ab3f56053f12951d76701b704b4822a323
parent5ea56aa37a9f0b7098302e2acb76c73907c70bde (diff)
downloadperlweeklychallenge-club-cc7611c673901442ddfac5a71c691350144dcd03.tar.gz
perlweeklychallenge-club-cc7611c673901442ddfac5a71c691350144dcd03.tar.bz2
perlweeklychallenge-club-cc7611c673901442ddfac5a71c691350144dcd03.zip
Week 271
-rw-r--r--challenge-271/zapwai/c/ch-1.c45
-rw-r--r--challenge-271/zapwai/c/ch-2.c82
-rw-r--r--challenge-271/zapwai/javascript/ch-1.js39
-rw-r--r--challenge-271/zapwai/javascript/ch-2.js54
-rw-r--r--challenge-271/zapwai/perl/ch-1.pl38
-rw-r--r--challenge-271/zapwai/perl/ch-2.pl51
-rw-r--r--challenge-271/zapwai/python/ch-1.py32
-rw-r--r--challenge-271/zapwai/python/ch-2.py44
-rw-r--r--challenge-271/zapwai/rust/ch-1.rs44
-rw-r--r--challenge-271/zapwai/rust/ch-2.rs65
10 files changed, 494 insertions, 0 deletions
diff --git a/challenge-271/zapwai/c/ch-1.c b/challenge-271/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..1302f1d173
--- /dev/null
+++ b/challenge-271/zapwai/c/ch-1.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+
+void proc(int M, int N, int m[M][N]) {
+ printf( "Input: m = \n");
+ for (int i = 0; i < M; i++) {
+ printf("\t");
+ for (int j = 0; j < N; j++)
+ printf("%d ", m[i][j]);
+ printf("\n");
+ }
+ int cnt[M];
+ for (int i = 0; i < M; i++)
+ cnt[i] = 0;
+
+ for (int i = 0; i < M; i++)
+ for (int j = 0; j < N; j++)
+ if (m[i][j] == 1)
+ cnt[i]++;
+
+ int max = 0;
+ int max_index = 0;
+ for (int i = 0; i < M; i++) {
+ if (cnt[i] > max) {
+ max_index = i;
+ max = cnt[i];
+ }
+ }
+ printf("Output: row %d (count is %d)\n", max_index + 1, max);
+}
+
+int main(){
+ int matrix[2][2] = { {0, 1},
+ {1, 0},
+ };
+ proc(2, 2, matrix);
+ int matrix2[2][3] = { {0, 0, 0},
+ {1, 0, 1},
+ };
+ proc(2, 3, matrix2);
+ int matrix3[3][2] = { {0, 0},
+ {1, 1},
+ {0, 0},
+ };
+ proc(3, 2, matrix3);
+}
diff --git a/challenge-271/zapwai/c/ch-2.c b/challenge-271/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..78b3e1266f
--- /dev/null
+++ b/challenge-271/zapwai/c/ch-2.c
@@ -0,0 +1,82 @@
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#define POW2 10 /* biggest power of 2 in entries */
+
+int* dec_to_bin(int x, int N) {
+ int* b = malloc(sizeof(int) * (N+1));
+ for (int i = 0; i <= N; i++) {
+ if (x >= pow(2, N - i)) {
+ b[i] = 1;
+ x -= pow(2, N - i);
+ } else {
+ b[i] = 0;
+ }
+ }
+ return b;
+}
+
+void proc(int N, int ints[N]) {
+ printf("Input: { ");
+ for (int i = 0; i < N; i++)
+ printf("%d ", ints[i]);
+ printf("}\n");
+ int count[N];
+ for (int i = 0; i < N; i++)
+ count[i] = 0;
+ for (int k = 0; k < N; k++) {
+ int i = ints[k];
+ int* bin = dec_to_bin(i, POW2);
+ for (int j = 0; j <= POW2; j++) {
+ if (bin[j] == 1)
+ count[k]++;
+ }
+ free(bin);
+ }
+ int ord[N];
+ for (int i = 0; i < N; i++)
+ ord[i] = ints[i];
+ int c = 1;
+ while (c != 0) {
+ c = 0;
+ for (int i = 0; i < N-1; i++) {
+ if (count[i] > count[i+1]) {
+ c++;
+ int tmp_cnt = count[i];
+ int tmp_int = ord[i];
+ count[i] = count[i+1];
+ count[i+1] = tmp_cnt;
+ ord[i] = ord[i+1];
+ ord[i+1] = tmp_int;
+ }
+ }
+ }
+ c = 1;
+ while (c != 0) {
+ c = 0;
+ for (int i = 0; i < N-1; i++) {
+ if (count[i] != count[i+1])
+ continue;
+ if (ord[i] > ord[i+1]) {
+ c++;
+ int tmp_int = ord[i];
+ ord[i] = ord[i+1];
+ ord[i+1] = tmp_int;
+ int tmp_cnt = count[i];
+ count[i] = count[i+1];
+ count[i+1] = tmp_cnt;
+ }
+ }
+ }
+ printf("Output: { ");
+ for (int i = 0; i < N; i++)
+ printf("%d ", ord[i]);
+ printf("}\n");
+}
+
+int main() {
+ int ints[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
+ proc(sizeof(ints)/sizeof(int), ints);
+ int ints2[] = {1024, 512, 256, 128, 64};
+ proc(sizeof(ints2)/sizeof(int), ints2);
+}
diff --git a/challenge-271/zapwai/javascript/ch-1.js b/challenge-271/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..b79e0a378f
--- /dev/null
+++ b/challenge-271/zapwai/javascript/ch-1.js
@@ -0,0 +1,39 @@
+matrix = [ [0, 1],
+ [1, 0],
+ ];
+proc(matrix);
+matrix = [ [0, 0, 0],
+ [1, 0, 1],
+ ];
+proc(matrix);
+matrix = [ [0, 0],
+ [1, 1],
+ [0, 0],
+ ];
+proc(matrix);
+function proc(m) {
+ console.log( "Input: m = ", m);
+ let cnt = [];
+ for (let i = 0; i < m.length; i++) {
+ cnt.push(0);
+ }
+ let pres = 0;
+ for (let row of m) {
+ for (let entry of row) {
+ if (entry == 1) {
+ cnt[pres]++;
+ }
+ }
+ pres++;
+ }
+ let max = 0;
+ let max_index = 0;
+ for (let i = 0; i < cnt.length; i++) {
+ if (cnt[i] > max) {
+ max_index = i;
+ max = cnt[i];
+ }
+ }
+ console.log( "Output: row ", max_index + 1, " ( count is ", max, ")");
+}
+
diff --git a/challenge-271/zapwai/javascript/ch-2.js b/challenge-271/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..7f4dc63a58
--- /dev/null
+++ b/challenge-271/zapwai/javascript/ch-2.js
@@ -0,0 +1,54 @@
+let ints = [0, 1, 2, 3, 4, 5, 6, 7, 8];
+proc(ints);
+ints = [1024, 512, 256, 128, 64];
+proc(ints);
+function proc(ints) {
+ console.log( "Input: ", ints);
+ let count = [];
+ for (let i of ints) {
+ let bin = i.toString(2);
+ let dig = bin.split("");
+ let cnt = 0;
+ for (let d of dig) {
+ if (d == "1") {
+ cnt++;
+ }
+ }
+ count.push(cnt);
+ }
+ let ord = ints;
+ let c = 1;
+ while (c != 0) {
+ c = 0;
+ for (let i = 0; i < ord.length - 1; i++) {
+ if (count[i] > count[i+1]) {
+ c++;
+ let tmp_cnt = count[i];
+ let tmp_int = ord[i];
+ count[i] = count[i+1];
+ count[i+1] = tmp_cnt;
+ ord[i] = ord[i+1];
+ ord[i+1] = tmp_int;
+ }
+ }
+ }
+ c = 1;
+ while (c != 0) {
+ c = 0;
+ for (let i = 0; i < ord.length - 1; i++) {
+ if (count[i] != count[i+1]) {
+ continue;
+ }
+ if (ord[i] > ord[i+1]) {
+ c++;
+ let tmp_int = ord[i];
+ ord[i] = ord[i+1];
+ ord[i+1] = tmp_int;
+ let tmp_cnt = count[i];
+ count[i] = count[i+1];
+ count[i+1] = tmp_cnt;
+ }
+ }
+ }
+ console.log( "Output:",ord);
+}
diff --git a/challenge-271/zapwai/perl/ch-1.pl b/challenge-271/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..6b96494d2a
--- /dev/null
+++ b/challenge-271/zapwai/perl/ch-1.pl
@@ -0,0 +1,38 @@
+use v5.38;
+my $matrix = [ [0, 1],
+ [1, 0],
+ ];
+proc($matrix);
+$matrix = [ [0, 0, 0],
+ [1, 0, 1],
+ ];
+proc($matrix);
+$matrix = [ [0, 0],
+ [1, 1],
+ [0, 0],
+ ];
+proc($matrix);
+sub proc($m) {
+ say "Input: \$m = ";
+ my @cnt = (0) x scalar @$m;
+ my $pres = 0;
+ for my $row (@$m) {
+ say "\t @$row";
+ for my $entry (@$row) {
+ if ($entry == 1) {
+ $cnt[$pres]++;
+ }
+ }
+ $pres++;
+ }
+ my $max = 0;
+ my $max_index = 0;
+ for my $i (0 .. $#cnt) {
+ if ($cnt[$i] > $max) {
+ $max_index = $i;
+ $max = $cnt[$i];
+ }
+ }
+ say "Output: row ", $max_index + 1, " (count is $max)";
+}
+
diff --git a/challenge-271/zapwai/perl/ch-2.pl b/challenge-271/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..5b35478b29
--- /dev/null
+++ b/challenge-271/zapwai/perl/ch-2.pl
@@ -0,0 +1,51 @@
+use v5.38;
+my @ints = (0, 1, 2, 3, 4, 5, 6, 7, 8);
+proc(@ints);
+@ints = (1024, 512, 256, 128, 64);
+proc(@ints);
+sub proc(@ints) {
+ say "Input: @ints";
+ my @count;
+ my $j = 0;
+ for my $i (@ints) {
+ my $bin = sprintf("%b", $i);
+ my @dig = split "", $bin;
+ $count[$j] = scalar grep { $_ == 1 } @dig;
+ $j++;
+ }
+ my @ord = @ints;
+ my $c = 1;
+ while ($c != 0) {
+ $c = 0;
+ for my $i (0 .. $#ord - 1) {
+ if ($count[$i] > $count[$i+1]) {
+ $c++;
+ my $tmp_cnt = $count[$i];
+ my $tmp_int = $ord[$i];
+ $count[$i] = $count[$i+1];
+ $count[$i+1] = $tmp_cnt;
+ $ord[$i] = $ord[$i+1];
+ $ord[$i+1] = $tmp_int;
+ }
+ }
+ }
+ $c = 1;
+ while ($c != 0) {
+ $c = 0;
+ for my $i (0 .. $#ord - 1) {
+ if ($count[$i] != $count[$i+1]) {
+ next;
+ }
+ if ($ord[$i] > $ord[$i+1]) {
+ $c++;
+ my $tmp_int = $ord[$i];
+ $ord[$i] = $ord[$i+1];
+ $ord[$i+1] = $tmp_int;
+ my $tmp_cnt = $count[$i];
+ $count[$i] = $count[$i+1];
+ $count[$i+1] = $tmp_cnt;
+ }
+ }
+ }
+ say "Output: @ord";
+}
diff --git a/challenge-271/zapwai/python/ch-1.py b/challenge-271/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..5cba68f53f
--- /dev/null
+++ b/challenge-271/zapwai/python/ch-1.py
@@ -0,0 +1,32 @@
+def proc(m):
+ print("Input: m = ", m)
+ cnt = []
+ for i in range(len(m)):
+ cnt.append(0)
+ pres = 0
+ for row in m:
+ for entry in row:
+ if entry == 1:
+ cnt[pres] += 1
+ pres += 1
+ max = 0
+ max_index = 0
+ for i in range(len(cnt)):
+ if cnt[i] > max :
+ max_index = i
+ max = cnt[i]
+ print( "Output: row", max_index + 1, " ( count is", max, ")")
+
+matrix = [ [0, 1],
+ [1, 0],
+ ]
+proc(matrix)
+matrix = [ [0, 0, 0],
+ [1, 0, 1],
+ ]
+proc(matrix)
+matrix = [ [0, 0],
+ [1, 1],
+ [0, 0],
+ ]
+proc(matrix)
diff --git a/challenge-271/zapwai/python/ch-2.py b/challenge-271/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..3ba24a81ef
--- /dev/null
+++ b/challenge-271/zapwai/python/ch-2.py
@@ -0,0 +1,44 @@
+def proc(ints):
+ print( "Input:", ints)
+ count = []
+ for i in ints:
+ bina = bin(i)
+ dig = list(bina)
+ cnt = 0
+ for d in dig:
+ if d == '1':
+ cnt += 1
+ count.append(cnt)
+ ords = ints.copy()
+ c = 1
+ while c != 0:
+ c = 0
+ for i in range(len(ords) - 1):
+ if count[i] > count[i+1]:
+ c += 1
+ tmp_cnt = count[i]
+ tmp_int = ords[i]
+ count[i] = count[i+1]
+ count[i+1] = tmp_cnt
+ ords[i] = ords[i+1]
+ ords[i+1] = tmp_int
+ c = 1
+ while c != 0:
+ c = 0
+ for i in range(len(ords) - 1):
+ if count[i] != count[i+1]:
+ continue
+ if ords[i] > ords[i+1]:
+ c += 1
+ tmp_int = ords[i]
+ ords[i] = ords[i+1]
+ ords[i+1] = tmp_int
+ tmp_cnt = count[i]
+ count[i] = count[i+1]
+ count[i+1] = tmp_cnt
+ print("Output:", ords)
+
+ints = [0, 1, 2, 3, 4, 5, 6, 7, 8]
+proc(ints)
+ints = [1024, 512, 256, 128, 64]
+proc(ints)
diff --git a/challenge-271/zapwai/rust/ch-1.rs b/challenge-271/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..79ab479e82
--- /dev/null
+++ b/challenge-271/zapwai/rust/ch-1.rs
@@ -0,0 +1,44 @@
+#![allow(non_snake_case)]
+
+fn main() {
+ let matrix = vec![ vec![0, 1],
+ vec![1, 0],
+ ];
+ proc(2, 2, matrix);
+ let matrix2 = vec![ vec![0, 0, 0],
+ vec![1, 0, 1],
+ ];
+ proc(2, 3, matrix2);
+ let matrix3 = vec![ vec![0, 0],
+ vec![1, 1],
+ vec![0, 0],
+ ];
+ proc(3, 2, matrix3);
+}
+
+fn proc(M : i32, N : i32, m : Vec<Vec<i32>>) {
+ println!("Input: m = {:?}", m);
+ let mut cnt : Vec<i32> = Vec::new();
+ for _i in 0 .. M {
+ cnt.push(0);
+ }
+
+ for i in 0 .. M {
+ for j in 0 .. N {
+ if m[i as usize][j as usize] == 1 {
+ cnt[i as usize] += 1;
+ }
+ }
+ }
+
+ let mut max = 0;
+ let mut max_index = 0;
+ for i in 0 .. cnt.len() {
+ if cnt[i] > max {
+ max_index = i;
+ max = cnt[i];
+ }
+ }
+ println!( "Output: row {} (count is {})", max_index + 1, max);
+}
+
diff --git a/challenge-271/zapwai/rust/ch-2.rs b/challenge-271/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..41c0fb3ac5
--- /dev/null
+++ b/challenge-271/zapwai/rust/ch-2.rs
@@ -0,0 +1,65 @@
+#![allow(non_snake_case)]
+
+fn main() {
+ let ints = vec![0, 1, 2, 3, 4, 5, 6, 7, 8];
+ proc(ints);
+ let ints2 = vec![1024, 512, 256, 128, 64];
+ proc(ints2);
+}
+
+fn proc(ints : Vec<i32>) {
+ println!("Input: {:?}", ints);
+ let mut count : Vec<i32> = Vec::new();
+ for _i in 0 .. ints.len() {
+ count.push(0);
+ }
+
+ for i in 0 .. ints.len() {
+ let d = format!("{:b}",ints[i]);
+ let mut D : Vec<&str> = d.split("").collect();
+ D.pop();
+ D.reverse();
+ D.pop();
+ D.reverse();
+ for dig in D {
+ if dig == "1" {
+ count[i] += 1;
+ }
+ }
+ }
+ let mut ord = ints.clone();
+ let mut c = 1;
+ while c != 0 {
+ c = 0;
+ for i in 0 .. ints.len()-1 {
+ if count[i] > count[i+1] {
+ c += 1;
+ let tmp_cnt = count[i];
+ let tmp_int = ord[i];
+ count[i] = count[i+1];
+ count[i+1] = tmp_cnt;
+ ord[i] = ord[i+1];
+ ord[i+1] = tmp_int;
+ }
+ }
+ }
+ c = 1;
+ while c != 0 {
+ c = 0;
+ for i in 0 .. ints.len()-1 {
+ if count[i] != count[i+1] {
+ continue;
+ }
+ if ord[i] > ord[i+1] {
+ c += 1;
+ let tmp_int = ord[i];
+ ord[i] = ord[i+1];
+ ord[i+1] = tmp_int;
+ let tmp_cnt = count[i];
+ count[i] = count[i+1];
+ count[i+1] = tmp_cnt;
+ }
+ }
+ }
+ println!("Output: {:?}", ord);
+}