diff options
| author | David Ferrone <zapwai@gmail.com> | 2024-05-06 12:21:40 -0400 |
|---|---|---|
| committer | David Ferrone <zapwai@gmail.com> | 2024-05-06 12:21:40 -0400 |
| commit | 8aea6becac2f3dc6e97d884959c05980b09b067b (patch) | |
| tree | c3fb50e5d363781688f6bc56934a94992ae94606 | |
| parent | 479ca4e9c71aa2ea5be2bc7b66451c21be9d8dde (diff) | |
| download | perlweeklychallenge-club-8aea6becac2f3dc6e97d884959c05980b09b067b.tar.gz perlweeklychallenge-club-8aea6becac2f3dc6e97d884959c05980b09b067b.tar.bz2 perlweeklychallenge-club-8aea6becac2f3dc6e97d884959c05980b09b067b.zip | |
Week 268
| -rw-r--r-- | challenge-268/zapwai/c/ch-1.c | 53 | ||||
| -rw-r--r-- | challenge-268/zapwai/c/ch-2.c | 44 | ||||
| -rw-r--r-- | challenge-268/zapwai/javascript/ch-1.js | 29 | ||||
| -rw-r--r-- | challenge-268/zapwai/javascript/ch-2.js | 16 | ||||
| -rw-r--r-- | challenge-268/zapwai/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-268/zapwai/perl/ch-2.pl | 16 | ||||
| -rw-r--r-- | challenge-268/zapwai/python/ch-1.py | 24 | ||||
| -rw-r--r-- | challenge-268/zapwai/python/ch-2.py | 14 | ||||
| -rw-r--r-- | challenge-268/zapwai/rust/ch-1.rs | 33 | ||||
| -rw-r--r-- | challenge-268/zapwai/rust/ch-2.rs | 23 |
10 files changed, 283 insertions, 0 deletions
diff --git a/challenge-268/zapwai/c/ch-1.c b/challenge-268/zapwai/c/ch-1.c new file mode 100644 index 0000000000..656fe4f7a9 --- /dev/null +++ b/challenge-268/zapwai/c/ch-1.c @@ -0,0 +1,53 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +bool check(int x[], int y[], int xlen) { + int cnt = 0; + for (int i = 0; i < xlen; i++) + if (x[i] == y[i]) + cnt++; + return ( cnt == xlen ); +} + +void sort(int x[], int xlen) { + int cnt = 0; + do { + cnt = 0; + for (int i = 0; i < xlen - 1; i++) { + if (x[i] > x[i+1]) { + cnt++; + int tmp = x[i]; + x[i] = x[i+1]; + x[i+1] = tmp; + } + } + } while (cnt != 0); +} + +void proc(int x[], int y[], int xlen) { + printf("Input: x = "); + for (int i = 0; i < xlen; i++) + printf("%d ", x[i]); + printf("\n\ty = "); + for (int i = 0; i < xlen; i++) + printf("%d ", y[i]); + printf("\nOutput: "); + sort(x, xlen); + sort(y, xlen); + for (int i = -99; i < 99; i++) { + int* n = malloc(xlen * sizeof(int)); + for (int j = 0; j < xlen; j++) + n[j] += x[j] + i; + if (check(n, y, xlen)) + printf("%d\n", i); + free(n); + } +} + +int main() { + int x[] = {3, 7, 5}; + int y[] = {9, 5, 7}; + int xlen = sizeof(x) / sizeof(int); + proc(x, y, xlen); +} diff --git a/challenge-268/zapwai/c/ch-2.c b/challenge-268/zapwai/c/ch-2.c new file mode 100644 index 0000000000..524b7e4bc0 --- /dev/null +++ b/challenge-268/zapwai/c/ch-2.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> + +void sort(int x[], int xlen) { + int cnt = 0; + do { + cnt = 0; + for (int i = 0; i < xlen - 1; i++) { + if (x[i] > x[i+1]) { + cnt++; + int tmp = x[i]; + x[i] = x[i+1]; + x[i+1] = tmp; + } + } + } while (cnt != 0); +} + +void proc(int ints[], int len) { + printf("Input: "); + for (int i = 0; i < len; i++) + printf("%d ", ints[i]); + int* new = malloc(len * sizeof(int)); + sort(ints, len); + int j = 0; + do { + int x = ints[j]; + int y = ints[j+1]; + new[j] = y; + new[j+1] = x; + j += 2; + } while (j < len); + printf("\nOutput: "); + for (int i = 0; i < len; i++) + printf("%d ", new[i]); + printf("\n"); + free(new); +} + +int main() { + int ints[] = {2, 5, 3, 4}; + proc(ints, sizeof(ints) / sizeof(int)); +} + diff --git a/challenge-268/zapwai/javascript/ch-1.js b/challenge-268/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..9c263c98fc --- /dev/null +++ b/challenge-268/zapwai/javascript/ch-1.js @@ -0,0 +1,29 @@ +function check(x, y) { + let cnt = 0; + for (let i = 0; i < x.length; i++) { + if (x[i] == y[i]) { + cnt++; + } + } + return ( cnt == x.length ) +} + +function proc(x, y) { + console.log("Input: x =", x, "\n\ty =", y) + x.sort(); + y.sort(); + for (let i = -99; i < 99; i++) { + let n = x.map((x) => x); + for (let j = 0; j < n.length; j++) { + n[j] += i; + } + if (check(n, y)) { + console.log("Output:", i); + break; + } + } +} + +x = [3, 7, 5]; +y = [9, 5, 7]; +proc(x, y); diff --git a/challenge-268/zapwai/javascript/ch-2.js b/challenge-268/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..384b706bab --- /dev/null +++ b/challenge-268/zapwai/javascript/ch-2.js @@ -0,0 +1,16 @@ +function proc(ints) { + newt = []; + console.log("Input:", ints); + ints.sort(); + while (ints.length > 0) { + x = ints.shift(); + y = ints.shift(); + newt.push(y) + newt.push(x) + } + console.log("Output:", newt); +} + +let ints = [2, 5, 3, 4]; +proc(ints) + diff --git a/challenge-268/zapwai/perl/ch-1.pl b/challenge-268/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..869cd9a708 --- /dev/null +++ b/challenge-268/zapwai/perl/ch-1.pl @@ -0,0 +1,31 @@ +use v5.38; +my @x = (3, 7, 5); +my @y = (9, 5, 7); +proc(\@x, \@y); + +sub check($x, $y) { + my $cnt = 0; + for (0 .. scalar @$x - 1) { + $cnt++ if ($$x[$_] == $$y[$_]); + } + return ( $cnt == scalar @$x ) +} + +sub proc($x, $y) { + my @x = @$x; + my @y = @$y; + say "Input: \@x = @x\n\t\@y = @y"; + print "Output: "; + @x = sort { $a <=> $b } @x; + @y = sort { $a <=> $b } @y; + for my $i (-99 .. 99) { + my @n = @x; + foreach (0 .. $#n) { + $n[$_] += $i; + } + if (check(\@n, \@y)) { + say "$i"; + } + } + +} diff --git a/challenge-268/zapwai/perl/ch-2.pl b/challenge-268/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..8ece79cb77 --- /dev/null +++ b/challenge-268/zapwai/perl/ch-2.pl @@ -0,0 +1,16 @@ +use v5.38; +my @ints = (2, 5, 3, 4); +proc(@ints); + +sub proc(@ints) { + my @new; + say "Input: @ints"; + @ints = sort { $a <=> $b } @ints; + do { + my $x = shift @ints; + my $y = shift @ints; + push @new, $y; + push @new, $x; + } while ($#ints > 0); + say "Output: @new"; +} diff --git a/challenge-268/zapwai/python/ch-1.py b/challenge-268/zapwai/python/ch-1.py new file mode 100644 index 0000000000..cdd754a6e0 --- /dev/null +++ b/challenge-268/zapwai/python/ch-1.py @@ -0,0 +1,24 @@ +def check(x, y): + cnt = 0 + for i in range(len(x)): + if x[i] == y[i]: + cnt += 1 + return ( cnt == len(x) ) + +def proc(x, y): + print("Input: x =", x, "\n\ty =", y) + print("Output: ", end='') + x.sort(); + y.sort(); + for i in range(-99, 99): + n = x.copy() + for j in range(len(n)): + n[j] += i + if (check(n, y)): + print(i) + break + +x = [3, 7, 5] +y = [9, 5, 7] +proc(x, y) + diff --git a/challenge-268/zapwai/python/ch-2.py b/challenge-268/zapwai/python/ch-2.py new file mode 100644 index 0000000000..45cc7126e7 --- /dev/null +++ b/challenge-268/zapwai/python/ch-2.py @@ -0,0 +1,14 @@ +def proc(ints): + new = [] + print("Input:", ints) + ints.sort() + while len(ints) > 0: + x = ints.pop(0) + y = ints.pop(0) + new.append(y) + new.append(x) + print("Output:", new) + +ints = [2, 5, 3, 4] +proc(ints) + diff --git a/challenge-268/zapwai/rust/ch-1.rs b/challenge-268/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..54bff4ef86 --- /dev/null +++ b/challenge-268/zapwai/rust/ch-1.rs @@ -0,0 +1,33 @@ +fn main() { + let x = vec![3, 7, 5]; + let y = vec![9, 5, 7]; + proc(x, y); +} + +fn check(x : Vec<i32>, y : &Vec<i32>) -> bool { + let mut cnt = 0; + for i in 0 .. x.len() { + if x[i] == y[i] { + cnt += 1; + } + } + return cnt == x.len(); +} + +fn proc(x : Vec<i32>, y : Vec<i32>) { + println!("Input: x = {:?} \n\ty = {:?}", x, y); + print!("Output: "); + let mut myx = x.clone(); + myx.sort(); + let mut myy = y.clone(); + myy.sort(); + for i in -99 .. 99 { + let mut n : Vec<i32> = Vec::new(); + for j in 0 .. x.len() { + n.push(myx[j] + i); + } + if check(n, &myy) { + println!("{i}"); + } + } +} diff --git a/challenge-268/zapwai/rust/ch-2.rs b/challenge-268/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..7953ab91c0 --- /dev/null +++ b/challenge-268/zapwai/rust/ch-2.rs @@ -0,0 +1,23 @@ +use std::collections::VecDeque; + +fn main() { + let ints = vec![2, 5, 3, 4]; + proc(ints); +} + +fn proc(mut ints : Vec<i32>) { + println!("Input: {:?}", ints); + let mut new : Vec<i32> = Vec::new(); + ints.sort(); + let mut my_ints = VecDeque::new(); + for obj in ints { + my_ints.push_back(obj); + } + while my_ints.len() > 0 { + let x = my_ints.pop_front().unwrap(); + let y = my_ints.pop_front().unwrap(); + new.push(y); + new.push(x); + } + println!("Output: {:?}", new); +} |
