aboutsummaryrefslogtreecommitdiff
path: root/challenge-079
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-27 14:59:23 +0100
committerGitHub <noreply@github.com>2020-09-27 14:59:23 +0100
commitca8943ef2cd2eacbab3f0d21085b0cb046048368 (patch)
tree6b096fa746f2e3b312d91497a5ce8dcbe1cdcbaa /challenge-079
parent1e3ea66bc7f3dbc2226c0a2190b9b32d7f6d81f7 (diff)
parentcbfdbaee52b0553fef5a7bd28f3f98a6ff8ec276 (diff)
downloadperlweeklychallenge-club-ca8943ef2cd2eacbab3f0d21085b0cb046048368.tar.gz
perlweeklychallenge-club-ca8943ef2cd2eacbab3f0d21085b0cb046048368.tar.bz2
perlweeklychallenge-club-ca8943ef2cd2eacbab3f0d21085b0cb046048368.zip
Merge pull request #2387 from tedleahy/master
Add solutions for week 79
Diffstat (limited to 'challenge-079')
-rw-r--r--challenge-079/ted-leahy/ch-1.pl25
-rw-r--r--challenge-079/ted-leahy/ch-2.pl71
2 files changed, 96 insertions, 0 deletions
diff --git a/challenge-079/ted-leahy/ch-1.pl b/challenge-079/ted-leahy/ch-1.pl
new file mode 100644
index 0000000000..45f876c25a
--- /dev/null
+++ b/challenge-079/ted-leahy/ch-1.pl
@@ -0,0 +1,25 @@
+# # Count Set Bits # #
+# - 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_set_bit_count % 1000000007.
+# #
+
+use strict;
+use warnings;
+use feature 'say';
+
+my ($N) = @ARGV;
+my $total_set_bit_count = 0;
+
+foreach my $n (1..$N) {
+ # Convert to binary
+ my $binary_n = sprintf("%b", $n);
+ # Count the number of set bits ('1's)
+ my $set_bit_count = ($binary_n =~ tr/1//);
+
+ $total_set_bit_count += $set_bit_count;
+}
+
+say $total_set_bit_count % 1000000007;
+
+1; \ No newline at end of file
diff --git a/challenge-079/ted-leahy/ch-2.pl b/challenge-079/ted-leahy/ch-2.pl
new file mode 100644
index 0000000000..e4b511a944
--- /dev/null
+++ b/challenge-079/ted-leahy/ch-2.pl
@@ -0,0 +1,71 @@
+# # Trapped Rain Water # #
+# 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;
+use feature 'say';
+
+use List::Util qw(min max);
+
+my @N = @ARGV;
+
+print_histogram(@N);
+
+my $total_trapped_water = amount_of_trapped_water(@N);
+say "\nTotal amount of trapped water: $total_trapped_water";
+
+sub print_histogram {
+ my (@N) = @_;
+
+ my @y_axis = reverse (1..max(@N));
+
+ foreach my $y_value (@y_axis) {
+ print "$y_value ";
+
+ foreach my $x_value (@N) {
+ print $y_value <= $x_value ? '#' : ' ';
+ print ' ';
+ }
+
+ print "\n";
+ }
+
+ say '- ' x scalar @N + 1;
+ say ' ' . join(' ', @N);
+}
+
+# take the first number, then walk along the array until you find a number
+# that's greater than or equal to it.
+# then take all the numbers in between them, and for each one:
+# subtract that from the second number,
+# add that to $total_rain_units
+# keep going until you reach the end of the array
+
+sub amount_of_trapped_water {
+ my (@N) = @_;
+
+ my $total_rain_units = 0;
+ my $left_col_index = 0;
+
+ for my $i (0..$#N) {
+ my $current_col = $N[$i];
+ my $left_col = $N[$left_col_index];
+
+ if ($current_col >= $left_col) {
+ my $smaller_side = min($left_col, $current_col);
+
+ foreach ($left_col_index+1..$i-1) {
+ $total_rain_units += ($smaller_side - $N[$_]);
+ }
+
+ $left_col_index = $i;
+ }
+ }
+
+ return $total_rain_units
+}
+
+1; \ No newline at end of file