diff options
| author | David Ferrone <zapwai@gmail.com> | 2024-08-19 12:13:08 -0400 |
|---|---|---|
| committer | David Ferrone <zapwai@gmail.com> | 2024-08-19 12:13:08 -0400 |
| commit | 6cea37b42d8e362001d68f485cc4145409ca7b64 (patch) | |
| tree | 02d775b656a9e63c2e02dadbac35eef752d5b282 | |
| parent | 2b1de09812b917ca880dec1a765d8f71bc23dea5 (diff) | |
| download | perlweeklychallenge-club-6cea37b42d8e362001d68f485cc4145409ca7b64.tar.gz perlweeklychallenge-club-6cea37b42d8e362001d68f485cc4145409ca7b64.tar.bz2 perlweeklychallenge-club-6cea37b42d8e362001d68f485cc4145409ca7b64.zip | |
Week 283
| -rw-r--r-- | challenge-283/zapwai/c/ch-1.c | 34 | ||||
| -rw-r--r-- | challenge-283/zapwai/c/ch-2.c | 30 | ||||
| -rw-r--r-- | challenge-283/zapwai/javascript/ch-1.js | 28 | ||||
| -rw-r--r-- | challenge-283/zapwai/javascript/ch-2.js | 22 | ||||
| -rw-r--r-- | challenge-283/zapwai/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-283/zapwai/perl/ch-2.pl | 21 | ||||
| -rw-r--r-- | challenge-283/zapwai/python/ch-1.py | 22 | ||||
| -rw-r--r-- | challenge-283/zapwai/python/ch-2.py | 17 | ||||
| -rw-r--r-- | challenge-283/zapwai/r/ch-1.r | 29 | ||||
| -rw-r--r-- | challenge-283/zapwai/r/ch-2.r | 24 | ||||
| -rw-r--r-- | challenge-283/zapwai/rust/ch-1.rs | 31 | ||||
| -rw-r--r-- | challenge-283/zapwai/rust/ch-2.rs | 25 |
12 files changed, 312 insertions, 0 deletions
diff --git a/challenge-283/zapwai/c/ch-1.c b/challenge-283/zapwai/c/ch-1.c new file mode 100644 index 0000000000..c4f17ba2e7 --- /dev/null +++ b/challenge-283/zapwai/c/ch-1.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +void proc(int ints[], int intslen) { + printf( "Input: ints = { "); + for (int i = 0; i < intslen; i++) + printf("%d ", ints[i]); + printf("}\n"); + int num = 0; + for (int i = 0; i < intslen; i++) { + int found = 0; + int elem = ints[i]; + for (int j = 0; j < intslen; j++) { + if (i == j) + continue; + int new_elem = ints[j]; + if (elem == new_elem) + found = 1; + } + if (found == 0) { + num = elem; + break; + } + } + printf( "Output: %d\n", num); +} + +int main() { + int ints[] = {3, 3, 1}; + proc(ints, sizeof(ints) / sizeof(int)); + int ints2[] = {3, 2, 4, 2, 4}; + proc(ints2, sizeof(ints2) / sizeof(int)); + int ints3[] = {1}; + proc(ints3, sizeof(ints3) / sizeof(int)); +} diff --git a/challenge-283/zapwai/c/ch-2.c b/challenge-283/zapwai/c/ch-2.c new file mode 100644 index 0000000000..74462ebfa4 --- /dev/null +++ b/challenge-283/zapwai/c/ch-2.c @@ -0,0 +1,30 @@ +#include <stdio.h> + +void proc(int ints[], int intslen) { + printf("Input: ints = { "); + for (int i = 0; i < intslen; i++) + printf("%d ", ints[i]); + printf("}\n"); + char* veracity = "true"; + for (int i = 0; i < intslen; i++) { + int freq = ints[i]; + int cnt = 0; + for (int j = 0; j < intslen; j++) { + int elem = ints[j]; + if (elem == i) + cnt++; + } + if (cnt != freq) { + veracity = "false"; + break; + } + } + printf( "Output: %s\n", veracity); +} + +int main(){ + int ints[] = {1, 2, 1, 0}; + proc(ints, sizeof(ints) / sizeof(int)); + int ints2[] = {0, 3, 0}; + proc(ints2, sizeof(ints2) / sizeof(int)); +} diff --git a/challenge-283/zapwai/javascript/ch-1.js b/challenge-283/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..a6996510b0 --- /dev/null +++ b/challenge-283/zapwai/javascript/ch-1.js @@ -0,0 +1,28 @@ +let ints = [3, 3, 1]; +proc(ints); +ints = [3, 2, 4, 2, 4]; +proc(ints); +ints = [1]; +proc(ints); +function proc(ints) { + console.log("Input: ints =", ints); + let num = 0; + for (let i = 0; i < ints.length; i++) { + let found = 0; + let elem = ints[i]; + for (let j = 0; j < ints.length; j++) { + if (i == j) { + continue; + } + let new_elem = ints[j]; + if (elem == new_elem) { + found = 1; + } + } + if (found == 0) { + num = elem; + break; + } + } + console.log("Output:", num); +} diff --git a/challenge-283/zapwai/javascript/ch-2.js b/challenge-283/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..8c2b046ea3 --- /dev/null +++ b/challenge-283/zapwai/javascript/ch-2.js @@ -0,0 +1,22 @@ +let ints = [1, 2, 1, 0]; +proc(ints); +ints = [0, 3, 0]; +proc(ints); +function proc(ints) { + console.log("Input: ints =", ints); + let veracity = "true"; + for (let i = 0; i < ints.length; i++) { + let freq = ints[i]; + let cnt = 0; + for (let elem of ints) { + if (elem == i) { + cnt++; + } + } + if (cnt != freq) { + veracity = "false"; + break; + } + } + console.log("Output:", veracity); +} diff --git a/challenge-283/zapwai/perl/ch-1.pl b/challenge-283/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..caa6e06e7c --- /dev/null +++ b/challenge-283/zapwai/perl/ch-1.pl @@ -0,0 +1,29 @@ +use v5.38; +my @ints = (3, 3, 1); +proc(@ints); +@ints = (3, 2, 4, 2, 4); +proc(@ints); +@ints = (1); +proc(@ints); +@ints = (4, 3, 1, 1, 1, 4); +proc(@ints); +sub proc(@ints) { + say "Input: \@ints = @ints"; + my $num = 0; + for my $i (0 .. $#ints) { + my $found = 0; + my $elem = $ints[$i]; + for my $j (0 .. $#ints) { + next if ($i == $j); + my $new_elem = $ints[$j]; + if ($elem == $new_elem) { + $found = 1; + } + } + if ($found == 0) { + $num = $elem; + last; + } + } + say "Output: $num"; +} diff --git a/challenge-283/zapwai/perl/ch-2.pl b/challenge-283/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..1b3457092e --- /dev/null +++ b/challenge-283/zapwai/perl/ch-2.pl @@ -0,0 +1,21 @@ +use v5.38; +my @ints = (1, 2, 1, 0); +proc(@ints); +@ints = (0, 3, 0); +proc(@ints); +sub proc(@ints) { + say "Input: \@ints = @ints"; + my $veracity = "true"; + for my $i (0 .. $#ints) { + my $freq = $ints[$i]; + my $cnt = 0; + for my $elem (@ints) { + $cnt++ if ($elem == $i); + } + if ($cnt != $freq) { + $veracity = "false"; + last; + } + } + say "Output: $veracity"; +} diff --git a/challenge-283/zapwai/python/ch-1.py b/challenge-283/zapwai/python/ch-1.py new file mode 100644 index 0000000000..7c6dc88868 --- /dev/null +++ b/challenge-283/zapwai/python/ch-1.py @@ -0,0 +1,22 @@ +def proc(ints): + print( "Input: ints =", ints) + num = 0 + for i in range(len(ints)): + found = False + elem = ints[i] + for j in range(len(ints)): + if i == j: + continue + new_elem = ints[j] + if elem == new_elem: + found = True + if not found: + num = elem + break + print("Output:", num); +ints = [3, 3, 1] +proc(ints) +ints = [3, 2, 4, 2, 4] +proc(ints) +ints = [1] +proc(ints) diff --git a/challenge-283/zapwai/python/ch-2.py b/challenge-283/zapwai/python/ch-2.py new file mode 100644 index 0000000000..acc044d88e --- /dev/null +++ b/challenge-283/zapwai/python/ch-2.py @@ -0,0 +1,17 @@ +def proc(ints): + print("Input: ints =", ints) + veracity = True + for i in range(len(ints)): + freq = ints[i] + cnt = 0 + for elem in ints: + if elem == i: + cnt += 1 + if cnt != freq: + veracity = False + break + print("Output:", veracity) +ints = [1, 2, 1, 0] +proc(ints) +ints = [0, 3, 0] +proc(ints) diff --git a/challenge-283/zapwai/r/ch-1.r b/challenge-283/zapwai/r/ch-1.r new file mode 100644 index 0000000000..541ae626ba --- /dev/null +++ b/challenge-283/zapwai/r/ch-1.r @@ -0,0 +1,29 @@ +proc <- function(ints) { + cat("Input: ints = ") + cat(ints) + num <- 0 + for (i in 1: length(ints)) { + found <- 0 + elem <- ints[i] + for (j in 1:length(ints)) { + if (i == j) { + next + } + new_elem <- ints[j] + if (elem == new_elem) { + found <- 1 + } + } + if (found == 0) { + num <- elem + break + } + } + cat(paste("\nOutput:", num,"\n")) +} +ints <- c(3, 3, 1) +proc(ints) +ints <- c(3, 2, 4, 2, 4) +proc(ints) +ints <- c(1) +proc(ints) diff --git a/challenge-283/zapwai/r/ch-2.r b/challenge-283/zapwai/r/ch-2.r new file mode 100644 index 0000000000..23ce0b0168 --- /dev/null +++ b/challenge-283/zapwai/r/ch-2.r @@ -0,0 +1,24 @@ +proc <- function(ints) { + cat("Input: ints = ") + cat(ints) + veracity <- "true" + for (i in 1 : length(ints)) { + freq <- ints[i] + cnt <- 0 + for (elem in ints) { + if (elem == i-1) { + cnt <- cnt + 1 + } + } + if (cnt != freq) { + veracity = "false" + break + } + } + cat("\nOutput: ") + cat(veracity, "\n") +} +ints = c(1, 2, 1, 0) +proc(ints) +ints = c(0, 3, 0) +proc(ints) diff --git a/challenge-283/zapwai/rust/ch-1.rs b/challenge-283/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..388ff1f867 --- /dev/null +++ b/challenge-283/zapwai/rust/ch-1.rs @@ -0,0 +1,31 @@ +fn main() { + let ints = vec![3, 3, 1]; + proc(ints); + let ints2 = vec![3, 2, 4, 2, 4]; + proc(ints2); + let ints3 = vec![1]; + proc(ints3); +} + +fn proc(ints :Vec<i32>) { + println!( "Input: ints = {:?}", ints); + let mut num = 0; + for i in 0..ints.len() { + let mut found = 0; + let elem = ints[i]; + for j in 0..ints.len() { + if i == j { + continue; + } + let new_elem = ints[j]; + if elem == new_elem { + found = 1; + } + } + if found == 0 { + num = elem; + break; + } + } + println!("Output: {num}"); +} diff --git a/challenge-283/zapwai/rust/ch-2.rs b/challenge-283/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..5126572386 --- /dev/null +++ b/challenge-283/zapwai/rust/ch-2.rs @@ -0,0 +1,25 @@ +fn main() { + let ints :Vec<i32> = vec![1, 2, 1, 0]; + proc(ints); + let ints2 :Vec<i32> = vec![0, 3, 0]; + proc(ints2); +} + +fn proc(ints : Vec<i32>) { + println!("Input: ints = {:?}", ints); + let mut veracity = "true"; + for i in 0 .. ints.len() { + let freq = ints[i]; + let mut cnt = 0; + for elem in &ints { + if *elem as usize == i { + cnt += 1; + } + } + if cnt != freq { + veracity = "false"; + break; + } + } + println!("Output: {veracity}"); +} |
