From 75843f94f259023800bf1605c1414cfefeab704f Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 17:51:26 +0200 Subject: Test program for Week 79. --- challenge-079/abigail/test.pl | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 challenge-079/abigail/test.pl diff --git a/challenge-079/abigail/test.pl b/challenge-079/abigail/test.pl new file mode 100755 index 0000000000..578f69cc61 --- /dev/null +++ b/challenge-079/abigail/test.pl @@ -0,0 +1,48 @@ +#!/opt/perl/bin/perl + +# +# Test the solutions. Either call it with the directory name you +# want to test in, or call it as "../test.pl" from within the directory. +# + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +chdir ".." if -f "../test.pl"; + +use experimental 'signatures'; + +use Test::More; + +my %languages = ( + Perl => ["/opt/perl/bin/perl" => 'pl', 'perl'], + JavaScript => ["/usr/local/bin/node" => 'js', 'node'], +); + + +foreach my $challenge (1, 2) { + my @inputs = or next; + subtest "Challenge $challenge" => sub { + foreach my $language (sort keys %languages) { + my ($exe, $ext, $dir) = @{$languages {$language}}; + my $solution = "$dir/ch-$challenge.$ext"; + next unless -r $solution; + subtest $language => sub { + foreach my $input (@inputs) { + my $output_exp = ($input =~ s/input/output/r) . ".exp"; + my $exp = `cat $output_exp`; + my $got = `$exe ./$solution < $input`; + is $got, $exp, $input; + } + } + } + } +} + +done_testing; + + +__END__ -- cgit From dfdbb9d3c54c2f4da70d04186425a39f3193daa4 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 17:51:44 +0200 Subject: Test files for Week 79, challenge 1 --- challenge-079/abigail/input-1-1 | 1 + challenge-079/abigail/input-1-2 | 1 + challenge-079/abigail/input-1-3 | 1 + challenge-079/abigail/output-1-1.exp | 1 + challenge-079/abigail/output-1-2.exp | 1 + challenge-079/abigail/output-1-3.exp | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-079/abigail/input-1-1 create mode 100644 challenge-079/abigail/input-1-2 create mode 100644 challenge-079/abigail/input-1-3 create mode 100644 challenge-079/abigail/output-1-1.exp create mode 100644 challenge-079/abigail/output-1-2.exp create mode 100644 challenge-079/abigail/output-1-3.exp diff --git a/challenge-079/abigail/input-1-1 b/challenge-079/abigail/input-1-1 new file mode 100644 index 0000000000..b8626c4cff --- /dev/null +++ b/challenge-079/abigail/input-1-1 @@ -0,0 +1 @@ +4 diff --git a/challenge-079/abigail/input-1-2 b/challenge-079/abigail/input-1-2 new file mode 100644 index 0000000000..00750edc07 --- /dev/null +++ b/challenge-079/abigail/input-1-2 @@ -0,0 +1 @@ +3 diff --git a/challenge-079/abigail/input-1-3 b/challenge-079/abigail/input-1-3 new file mode 100644 index 0000000000..749fce669d --- /dev/null +++ b/challenge-079/abigail/input-1-3 @@ -0,0 +1 @@ +1000000 diff --git a/challenge-079/abigail/output-1-1.exp b/challenge-079/abigail/output-1-1.exp new file mode 100644 index 0000000000..7ed6ff82de --- /dev/null +++ b/challenge-079/abigail/output-1-1.exp @@ -0,0 +1 @@ +5 diff --git a/challenge-079/abigail/output-1-2.exp b/challenge-079/abigail/output-1-2.exp new file mode 100644 index 0000000000..b8626c4cff --- /dev/null +++ b/challenge-079/abigail/output-1-2.exp @@ -0,0 +1 @@ +4 diff --git a/challenge-079/abigail/output-1-3.exp b/challenge-079/abigail/output-1-3.exp new file mode 100644 index 0000000000..b489242255 --- /dev/null +++ b/challenge-079/abigail/output-1-3.exp @@ -0,0 +1 @@ +9884999 -- cgit From 1da0d24d7d9480bb239b1a5584d1f14460627f44 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 17:52:07 +0200 Subject: Perl solution for Week 79. --- challenge-079/abigail/perl/ch-1.pl | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 challenge-079/abigail/perl/ch-1.pl diff --git a/challenge-079/abigail/perl/ch-1.pl b/challenge-079/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..a2a8d27818 --- /dev/null +++ b/challenge-079/abigail/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +# +# Challenge: +# +# You are given a positive number $N. +# +# Write a script to count the total numbrer of set bits of the binary +# representations of all numbers from 1 to $N and return +# $total_count_set_bit % 1000000007. +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/ +# + +# +# This is A000778 (https://oeis.org/A000788). There's a recursive formala +# for the number of bits in the binary representation of 0 .. $N: +# +# bits (0) = 0 +# bits (2 * N) = bits (N) + bits (N - 1) + N +# bits (2 * N + 1) = 2 * bits (N) + N + 1 +# + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +use integer; + +my $BIG_NUM = 1_000_000_007; + +sub bits ($n) { + state $bits; + $$bits {$n} ||= + $n == 0 ? 0 + : do { + my $half = int ($n / 2); + bits ($half) + $half + ($n % 2 ? bits ($half) + 1 + : bits ($half - 1)) + } +} + +say bits (<>) % $BIG_NUM; + + +__END__ -- cgit From 172977c36c3012b79d1710cff34da2219a0f67a7 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 18:18:29 +0200 Subject: Ignore trailing whitespace. --- challenge-079/abigail/test.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/challenge-079/abigail/test.pl b/challenge-079/abigail/test.pl index 578f69cc61..222475a99b 100755 --- a/challenge-079/abigail/test.pl +++ b/challenge-079/abigail/test.pl @@ -35,6 +35,7 @@ foreach my $challenge (1, 2) { my $output_exp = ($input =~ s/input/output/r) . ".exp"; my $exp = `cat $output_exp`; my $got = `$exe ./$solution < $input`; + s/\h+$//gm for $exp, $got; is $got, $exp, $input; } } -- cgit From e922cd84165fccaf061431d8fdb6c7a36c56b0fb Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 18:42:59 +0200 Subject: Test input/output for Week 79, part 2. --- challenge-079/abigail/input-2-1 | 1 + challenge-079/abigail/input-2-2 | 1 + challenge-079/abigail/input-2-3 | 1 + challenge-079/abigail/output-2-1.exp | 7 +++++++ challenge-079/abigail/output-2-2.exp | 7 +++++++ challenge-079/abigail/output-2-3.exp | 13 +++++++++++++ 6 files changed, 30 insertions(+) create mode 100644 challenge-079/abigail/input-2-1 create mode 100644 challenge-079/abigail/input-2-2 create mode 100644 challenge-079/abigail/input-2-3 create mode 100644 challenge-079/abigail/output-2-1.exp create mode 100644 challenge-079/abigail/output-2-2.exp create mode 100644 challenge-079/abigail/output-2-3.exp diff --git a/challenge-079/abigail/input-2-1 b/challenge-079/abigail/input-2-1 new file mode 100644 index 0000000000..e7f18e258b --- /dev/null +++ b/challenge-079/abigail/input-2-1 @@ -0,0 +1 @@ +2 1 4 1 2 5 diff --git a/challenge-079/abigail/input-2-2 b/challenge-079/abigail/input-2-2 new file mode 100644 index 0000000000..bc96b17583 --- /dev/null +++ b/challenge-079/abigail/input-2-2 @@ -0,0 +1 @@ +3 1 3 1 1 5 diff --git a/challenge-079/abigail/input-2-3 b/challenge-079/abigail/input-2-3 new file mode 100644 index 0000000000..febba99da5 --- /dev/null +++ b/challenge-079/abigail/input-2-3 @@ -0,0 +1 @@ +3 10 3 1 11 5 diff --git a/challenge-079/abigail/output-2-1.exp b/challenge-079/abigail/output-2-1.exp new file mode 100644 index 0000000000..0ba8a01c66 --- /dev/null +++ b/challenge-079/abigail/output-2-1.exp @@ -0,0 +1,7 @@ +5 # +4 # # +3 # # +2 # # # # +1 # # # # # # +_ _ _ _ _ _ _ + 2 1 4 1 2 5 diff --git a/challenge-079/abigail/output-2-2.exp b/challenge-079/abigail/output-2-2.exp new file mode 100644 index 0000000000..a4f21fe319 --- /dev/null +++ b/challenge-079/abigail/output-2-2.exp @@ -0,0 +1,7 @@ +5 # +4 # +3 # # # +2 # # # +1 # # # # # # +_ _ _ _ _ _ _ + 3 1 3 1 1 5 diff --git a/challenge-079/abigail/output-2-3.exp b/challenge-079/abigail/output-2-3.exp new file mode 100644 index 0000000000..5dfd8fce2f --- /dev/null +++ b/challenge-079/abigail/output-2-3.exp @@ -0,0 +1,13 @@ +11 # +10 # # + 9 # # + 8 # # + 7 # # + 6 # # + 5 # # # + 4 # # # + 3 # # # # # + 2 # # # # # + 1 # # # # # # +__ __ __ __ __ __ __ + 3 10 3 1 11 5 -- cgit From 5b410e796c910a170f80bc40c4a91b0b6f1f2edf Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 18:43:32 +0200 Subject: Perl solution for Week 79, part 2. --- challenge-079/abigail/perl/ch-2.pl | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 challenge-079/abigail/perl/ch-2.pl diff --git a/challenge-079/abigail/perl/ch-2.pl b/challenge-079/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..3fc6b9fd7e --- /dev/null +++ b/challenge-079/abigail/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/opt/perl/bin/perl + +use 5.032; + +# +# Challenge: +# +# You are given an array of positive numbers @N. +# Write a script to represent it as Histogram Chart and find out +# how much water it can trap. +# + +use strict; +use warnings; +no warnings 'syntax'; + +use List::Util qw [max]; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# Read the input, extract the numbers, store them in @N, and find the maximum. +# +my $max = max my @N = <> =~ /[0-9]+/g; + +my $w = length $max; # Width of the largest number, so we + # we can align things properly. + +# +# Print histograms. We're counting down from the maximum volume to 1, +# and for each index, if the volume on given index equals, or exceeds +# the current volume, we print a #, else, we print a space. +# +for (my $volume = $max; $volume; $volume --) { + printf "%${w}d" => $volume; + printf " %${w}s" => $N [$_] >= $volume ? "#" : " " for keys @N; + print "\n"; +} + +# +# Print the line with bars. +# +my $bar = "_" x $w; +print $bar, " $bar" x @N, "\n"; + +# +# Print the sums (just the values in @N) +# +print " " x $w; +printf " %${w}d" => $_ for @N; +print "\n"; + + +__END__ -- cgit From fa07704e9e67658e76e89d0b00c9cfeb62d7aa42 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 18:56:57 +0200 Subject: Javascript solution for week 79, part 1 --- challenge-079/abigail/node/ch-1.js | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 challenge-079/abigail/node/ch-1.js diff --git a/challenge-079/abigail/node/ch-1.js b/challenge-079/abigail/node/ch-1.js new file mode 100644 index 0000000000..6c9ca09ace --- /dev/null +++ b/challenge-079/abigail/node/ch-1.js @@ -0,0 +1,53 @@ +// +// Challenge: +// +// You are given a positive number $N. +// +// Write a script to count the total numbrer of set bits of the binary +// representations of all numbers from 1 to $N and return +// $total_count_set_bit % 1000000007. +// +// https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/ +// + +// +// This is A000778 (https://oeis.org/A000788). There's a recursive formala +// for the number of bits in the binary representation of 0 .. $N: +// +// bits (0) = 0 +// bits (2 * N) = bits (N) + bits (N - 1) + N +// bits (2 * N + 1) = 2 * bits (N) + N + 1 +// + +// +// Create an interface to read from STDIN +// +const rl = require ('readline') . createInterface ({ + input: process . stdin, +}); + +// +// Read lines of input, calculate the result, and print it. +// +rl . on ('line', (line) => { + console . log (bits (+line)); // Unary + numifies +}); + + +// +// Recursive function to calculate the results (see above). +// +let cache = []; +function bits (n) { + if (n == 0) { + return 0; + } + + if (!cache [n]) { + let half = Math . floor (n / 2); + cache [n] = bits (half) + half + + (n % 2 ? bits (half) + 1 : bits (half - 1)); + } + return cache [n]; +} + -- cgit From 88acc407f4db3e21ac6701f49886728f2dc846b2 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Sep 2020 19:30:15 +0200 Subject: Javascript solution for Week 079, part 2. --- challenge-079/abigail/node/ch-2.js | 77 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 challenge-079/abigail/node/ch-2.js diff --git a/challenge-079/abigail/node/ch-2.js b/challenge-079/abigail/node/ch-2.js new file mode 100644 index 0000000000..a3de411e8c --- /dev/null +++ b/challenge-079/abigail/node/ch-2.js @@ -0,0 +1,77 @@ +// +// Challenge: +// +// You are given an array of positive numbers @N. +// Write a script to represent it as Histogram Chart and find out +// how much water it can trap. +// + +// +// Create an interface to read from STDIN +// +const rl = require ('readline') . createInterface ({ + input: process . stdin, +}); + +// +// Read lines of input, calculate the result, and print it. +// +rl . on ('line', (line) => { + print_histogram (line); +}); + + +// +// Node doesn't have (s)printf, so we're writing our own formatting function. +// +function ff (n, width, pad = " ") { + let s = n . toString (); + while (s . length < width) { + s = pad + s; + } + return s; +} + +function print_histogram (input) { + // + // Extract N from the input, find the maximum, + // and the width of this maximum. + // + let N = input . split (" ") . map (Number); + let max = Math . max (... N); + let w = max . toString () . length; + + // + // Count down volumes from the maximum, down to 1. + // For each volume, print the volume, and for each entry + // in N, print "#" if the volume is equal or less than + // the entry in N, else, print " ". + // + for (let volume = max; volume; volume --) { + let line = ff (volume, w); + for (let i = 0; i < N . length; i ++) { + line += " " + ff (volume <= N [i] ? "#" : " ", w); + } + console . log (line); + } + + // + // Print the bars + // + let line = ""; + line += ff ("_", w, "_"); + for (let i = 0; i < N . length; i ++) { + line += " " + ff ("_", w, "_"); + } + console . log (line); + + // + // Finally, print the totals + // + line = ""; + line += ff (" ", w); + for (let i = 0; i < N . length; i ++) { + line += " " + ff (N [i], w); + } + console . log (line); +} -- cgit