aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-079/abigail/input-1-11
-rw-r--r--challenge-079/abigail/input-1-21
-rw-r--r--challenge-079/abigail/input-1-31
-rw-r--r--challenge-079/abigail/input-2-11
-rw-r--r--challenge-079/abigail/input-2-21
-rw-r--r--challenge-079/abigail/input-2-31
-rw-r--r--challenge-079/abigail/node/ch-1.js53
-rw-r--r--challenge-079/abigail/node/ch-2.js77
-rw-r--r--challenge-079/abigail/output-1-1.exp1
-rw-r--r--challenge-079/abigail/output-1-2.exp1
-rw-r--r--challenge-079/abigail/output-1-3.exp1
-rw-r--r--challenge-079/abigail/output-2-1.exp7
-rw-r--r--challenge-079/abigail/output-2-2.exp7
-rw-r--r--challenge-079/abigail/output-2-3.exp13
-rw-r--r--challenge-079/abigail/perl/ch-1.pl51
-rw-r--r--challenge-079/abigail/perl/ch-2.pl55
-rwxr-xr-xchallenge-079/abigail/test.pl49
17 files changed, 321 insertions, 0 deletions
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/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/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];
+}
+
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);
+}
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
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
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__
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__
diff --git a/challenge-079/abigail/test.pl b/challenge-079/abigail/test.pl
new file mode 100755
index 0000000000..222475a99b
--- /dev/null
+++ b/challenge-079/abigail/test.pl
@@ -0,0 +1,49 @@
+#!/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 = <input-$challenge-*> 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`;
+ s/\h+$//gm for $exp, $got;
+ is $got, $exp, $input;
+ }
+ }
+ }
+ }
+}
+
+done_testing;
+
+
+__END__