diff options
| author | David Ferrone <zapwai@gmail.com> | 2024-09-02 12:36:30 -0400 |
|---|---|---|
| committer | David Ferrone <zapwai@gmail.com> | 2024-09-02 12:36:30 -0400 |
| commit | a144bedd098858190c4b504282b667e7cbc0a37c (patch) | |
| tree | 5cc3ecadd2d9cd04f3ce2724ddeea68c935fcdce | |
| parent | a92117f754a875572d11900c871014386b7ade27 (diff) | |
| download | perlweeklychallenge-club-a144bedd098858190c4b504282b667e7cbc0a37c.tar.gz perlweeklychallenge-club-a144bedd098858190c4b504282b667e7cbc0a37c.tar.bz2 perlweeklychallenge-club-a144bedd098858190c4b504282b667e7cbc0a37c.zip | |
Week 285
| -rw-r--r-- | challenge-285/zapwai/c/ch-1.c | 40 | ||||
| -rw-r--r-- | challenge-285/zapwai/c/ch-2.c | 15 | ||||
| -rw-r--r-- | challenge-285/zapwai/javascript/ch-1.js | 32 | ||||
| -rw-r--r-- | challenge-285/zapwai/javascript/ch-2.js | 18 | ||||
| -rw-r--r-- | challenge-285/zapwai/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-285/zapwai/perl/ch-2.pl | 19 | ||||
| -rw-r--r-- | challenge-285/zapwai/python/ch-1.py | 27 | ||||
| -rw-r--r-- | challenge-285/zapwai/python/ch-2.py | 13 | ||||
| -rw-r--r-- | challenge-285/zapwai/r/ch-1.r | 32 | ||||
| -rw-r--r-- | challenge-285/zapwai/r/ch-2.r | 19 | ||||
| -rw-r--r-- | challenge-285/zapwai/rust/ch-1.rs | 34 | ||||
| -rw-r--r-- | challenge-285/zapwai/rust/ch-2.rs | 21 |
12 files changed, 302 insertions, 0 deletions
diff --git a/challenge-285/zapwai/c/ch-1.c b/challenge-285/zapwai/c/ch-1.c new file mode 100644 index 0000000000..9bfa8e37b9 --- /dev/null +++ b/challenge-285/zapwai/c/ch-1.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <string.h> + +void proc(char routes[][2][3], int len) { + printf("Input: "); + for (int i = 0; i < len; i++) { + printf("%s->%s ", routes[i][0], routes[i][1]); + } + printf("\n"); + char in[len][3]; + char out[len][3]; + int in_cnt = 0; + int out_cnt = 0; + for (int i = 0; i < len; i++) { + strcpy(in[in_cnt++], routes[i][0]); + strcpy(out[out_cnt++], routes[i][1]); + } + char ans[3] = ""; + + for (int i = 0; i < out_cnt; i++) { + int found = 0; + for (int j = 0; j < in_cnt; j++) { + if (0 == strcmp(out[i], in[j])) { + found = 1; + break; + } + } + if (found == 0) { + strcpy(ans, out[i]); + } + } + printf("Output: %s\n", ans); +} + +int main() { + char routes[3][2][3] = {{"B","C"}, {"D","B"}, {"C","A"}}; + proc(routes, 3); + char routes2[1][2][3] = {{"A","Z"}}; + proc(routes2, 1); +} diff --git a/challenge-285/zapwai/c/ch-2.c b/challenge-285/zapwai/c/ch-2.c new file mode 100644 index 0000000000..c8d7ed4327 --- /dev/null +++ b/challenge-285/zapwai/c/ch-2.c @@ -0,0 +1,15 @@ +#include <stdio.h> +int tally(int p, int n, int d, int q, int h) { return p + 5*n + 10*d + 25*q + 50*h; } +int main() { + int amt = 100; + int cnt = 0; + for (int h = 0; h <= amt/50; h++) + for (int q = 0; q <= amt/25; q++) + for (int d = 0; d <= amt/10; d++) + for (int n = 0; n <= amt/5; n++) + for (int p = 0; p <= amt; p++) + if (tally(p, n, d, q, h) == amt) + cnt++; + printf( "There are %d ways to make change for %d cents.\n", cnt, amt); +} + diff --git a/challenge-285/zapwai/javascript/ch-1.js b/challenge-285/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..427d9e27a3 --- /dev/null +++ b/challenge-285/zapwai/javascript/ch-1.js @@ -0,0 +1,32 @@ +let routes = [["B","C"], ["D","B"], ["C","A"]]; +proc(routes); +routes = [["A","Z"]]; +proc(routes); + +function proc(routes) { + console.log("Input: "); + for (let j = 0; j < routes.length; j++) { + console.log(routes[j]); + } + let inlist = []; + let outlist = []; + for (let j of routes) { + inlist.push(j[0]); + outlist.push(j[1]); + } + let ans = "a"; + for (let needle of outlist) { + let found = 0; + for (let hay of inlist) { + if (needle == hay) { + found = 1; + break; + } + } + if (found == 0) { + ans = needle; + } + } + console.log( "Output:", ans); +} + diff --git a/challenge-285/zapwai/javascript/ch-2.js b/challenge-285/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..f1a85eb1c6 --- /dev/null +++ b/challenge-285/zapwai/javascript/ch-2.js @@ -0,0 +1,18 @@ +let amt = 100; +let cnt = 0; +for (let h = 0; h <= amt/50; h++) { + for (let q = 0; q <= amt/25; q++) { + for (let d = 0; d <= amt/10; d++) { + for (let n = 0; n <= amt/5; n++) { + for (let p = 0; p <= amt; p++) { + if (tally(p, n, d, q, h) == amt) { + cnt++; + } + } + } + } + } +} +console.log( "There are", cnt,"ways to make change for", amt,"cents."); +function tally(p, n, d, q, h) { return p + 5*n + 10*d + 25*q + 50*h; } + diff --git a/challenge-285/zapwai/perl/ch-1.pl b/challenge-285/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..800ae25898 --- /dev/null +++ b/challenge-285/zapwai/perl/ch-1.pl @@ -0,0 +1,32 @@ +use v5.38; +my @routes = (["B","C"], ["D","B"], ["C","A"]); +proc(@routes); +@routes = (["A","Z"]); +proc(@routes); + +sub proc(@routes) { + print "Input: "; + print join(",", @{$routes[$_]})," " for (0 .. $#routes); + print "\n"; + my @in; + my @out; + foreach (@routes) { + push @in, ${$_}[0]; + push @out, ${$_}[1]; + } + my $ans = "a"; + for my $needle (@out) { + my $found = 0; + for my $hay (@in) { + if ($needle eq $hay) { + $found = 1; + last; + } + } + if ($found == 0) { + $ans = $needle; + } + } + say "Output: $ans"; +} + diff --git a/challenge-285/zapwai/perl/ch-2.pl b/challenge-285/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..ec554315e0 --- /dev/null +++ b/challenge-285/zapwai/perl/ch-2.pl @@ -0,0 +1,19 @@ +use v5.38; +my $amt = $ARGV[0] // 100; +my $cnt = 0; +for my $h (0 .. $amt/50) { + for my $q (0 .. $amt/25) { + for my $d (0 .. $amt/10) { + for my $n (0 .. $amt/5) { + for my $p (0 .. $amt) { + if (tally($p, $n, $d, $q, $h) == $amt) { + $cnt++; + } + } + } + } + } +} +say "There are $cnt ways to make change for $amt cents"; +sub tally($p, $n, $d, $q, $h) { $p + 5*$n + 10*$d + 25*$q + 50*$h } + diff --git a/challenge-285/zapwai/python/ch-1.py b/challenge-285/zapwai/python/ch-1.py new file mode 100644 index 0000000000..5722662b84 --- /dev/null +++ b/challenge-285/zapwai/python/ch-1.py @@ -0,0 +1,27 @@ +def proc(routes) : + print("Input: ") + for l in range(len(routes)): + print(" ",routes[l]) + inlist = [] + outlist = [] + for j in routes: + inlist.append(j[0]) + outlist.append(j[1]) + + ans = "a" + for needle in outlist : + found = 0 + for hay in inlist: + if needle == hay : + found = 1 + break + + if found == 0 : + ans = needle + + print("Output:", ans) + +routes = [["B","C"], ["D","B"], ["C","A"]] +proc(routes) +routes = [["A","Z"]] +proc(routes) diff --git a/challenge-285/zapwai/python/ch-2.py b/challenge-285/zapwai/python/ch-2.py new file mode 100644 index 0000000000..c980cee622 --- /dev/null +++ b/challenge-285/zapwai/python/ch-2.py @@ -0,0 +1,13 @@ +def tally(p, n, d, q, h) : + return p + 5*n + 10*d + 25*q + 50*h +amt = 100 +cnt = 0 +for h in range(1 + (int) (amt/50)) : + for q in range(1 + (int) (amt/25)) : + for d in range(1 + (int) (amt/10)) : + for n in range(1 + (int) (amt/5)) : + for p in range(1 + amt) : + if tally(p, n, d, q, h) == amt : + cnt += 1 + +print("There are", cnt, "ways to make change for", amt, "cents.") diff --git a/challenge-285/zapwai/r/ch-1.r b/challenge-285/zapwai/r/ch-1.r new file mode 100644 index 0000000000..d38f3dcaf6 --- /dev/null +++ b/challenge-285/zapwai/r/ch-1.r @@ -0,0 +1,32 @@ +proc <- function(routes) { + cat("Input: ") + for (j in routes) { + cat(j,", ") + } + cat("\n"); + inlist <- c() + out <- c() + for (r in routes) { + inlist <- c(inlist, r[1]) + out <- c(out, r[2]) + } + ans <- "a" + for (needle in out) { + found <- 0 + for (hay in inlist) { + if (needle == hay) { + found <- 1 + break + } + } + if (found == 0) { + ans <- needle + } + } + cat("Output:", ans,"\n") +} + +routes = list(c("B","C"), c("D","B"), c("C","A")) +proc(routes) +routes = list(c("A","Z")) +proc(routes) diff --git a/challenge-285/zapwai/r/ch-2.r b/challenge-285/zapwai/r/ch-2.r new file mode 100644 index 0000000000..87e2c46f37 --- /dev/null +++ b/challenge-285/zapwai/r/ch-2.r @@ -0,0 +1,19 @@ +tally <- function(p, n, d, q, h) { return(p + 5*n + 10*d + 25*q + 50*h); } +amt <- 100 +cnt <- 0 +for (h in 0:as.integer(amt/50)) { + for (q in 0:as.integer(amt/25)) { + for (d in 0:as.integer(amt/10)) { + for (n in 0:as.integer(amt/5)) { + for (p in 0:amt) { + if (tally(p, n, d, q, h) == amt) { + cnt <- cnt + 1 + } + } + } + } + } +} +print(paste( "There are", cnt,"ways to make change for", amt,"cents")) + + diff --git a/challenge-285/zapwai/rust/ch-1.rs b/challenge-285/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..66b667d763 --- /dev/null +++ b/challenge-285/zapwai/rust/ch-1.rs @@ -0,0 +1,34 @@ +fn main() { + let routes = vec![vec!['B','C'], vec!['D','B'], vec!['C','A']]; + proc(routes); + let routes2 = vec![vec!['A','Z']]; + proc(routes2); +} + +fn proc(routes :Vec<Vec<char>>) { + println!("Input: "); + for j in 0 .. routes.len() { + println!("{:?}", routes[j]); + } + let mut inlist :Vec<char> = Vec::new(); + let mut out :Vec<char> = Vec::new(); + for j in routes { + inlist.push(j[0]); + out.push(j[1]); + } + let mut ans = 'a'; + for needle in out { + let mut found = 0; + for hay in &inlist { + if needle == *hay { + found = 1; + break; + } + } + if found == 0 { + ans = needle; + } + } + println!( "Output: {ans}"); +} + diff --git a/challenge-285/zapwai/rust/ch-2.rs b/challenge-285/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..3b9e043637 --- /dev/null +++ b/challenge-285/zapwai/rust/ch-2.rs @@ -0,0 +1,21 @@ +fn main() { + let amt = 100; + let mut cnt = 0; + for h in 0 ..= amt/50 { + for q in 0 ..= amt/25 { + for d in 0 ..= amt/10 { + for n in 0 ..= amt/5 { + for p in 0 ..= amt { + if tally(p, n, d, q, h) == amt { + cnt += 1; + } + } + } + } + } + } + println!( "There are {cnt} ways to make change for {amt} cents"); +} + +fn tally(p: i32, n:i32, d:i32, q:i32, h:i32) -> i32 { return p + 5*n + 10*d + 25*q + 50*h; } + |
