aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2020-12-27 10:14:45 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2020-12-27 10:14:45 +0000
commit6b184acf75d88f29ba25d7cf9b261ada2f54a129 (patch)
tree57a2266020a603a973cea2ffdf0a044311aaeebc
parent8b82802e962f27d327dd7c2d8bb14b96b2ceec4d (diff)
downloadperlweeklychallenge-club-6b184acf75d88f29ba25d7cf9b261ada2f54a129.tar.gz
perlweeklychallenge-club-6b184acf75d88f29ba25d7cf9b261ada2f54a129.tar.bz2
perlweeklychallenge-club-6b184acf75d88f29ba25d7cf9b261ada2f54a129.zip
Add Perl solution to challenge 079
-rw-r--r--challenge-079/paulo-custodio/README1
-rw-r--r--challenge-079/paulo-custodio/perl/ch-1.pl72
-rw-r--r--challenge-079/paulo-custodio/perl/ch-2.pl73
-rw-r--r--challenge-079/paulo-custodio/test.pl21
4 files changed, 167 insertions, 0 deletions
diff --git a/challenge-079/paulo-custodio/README b/challenge-079/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-079/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-079/paulo-custodio/perl/ch-1.pl b/challenge-079/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..857010146c
--- /dev/null
+++ b/challenge-079/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+
+# Challenge 079
+#
+# TASK #1 › Count Set Bits
+# Submitted by: Mohammad S Anwar
+# 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.
+#
+# Example 1:
+# Input: $N = 4
+#
+# Explanation: First find out the set bit counts of all numbers i.e. 1, 2, 3 and 4.
+#
+# Decimal: 1
+# Binary: 001
+# Set Bit Counts: 1
+#
+# Decimal: 2
+# Binary: 010
+# Set Bit Counts: 1
+#
+# Decimal: 3
+# Binary: 011
+# Set Bit Counts: 2
+#
+# Decimal: 4
+# Binary: 100
+# Set Bit Counts: 1
+#
+# Total set bit count: 1 + 1 + 2 + 1 = 5
+#
+# Output: Your script should print `5` as `5 % 1000000007 = 5`.
+# Example 2:
+# Input: $N = 3
+#
+# Explanation: First find out the set bit counts of all numbers i.e. 1, 2 and 3.
+#
+# Decimal: 1
+# Binary: 01
+# Set Bit Count: 1
+#
+# Decimal: 2
+# Binary: 10
+# Set Bit Count: 1
+#
+# Decimal: 3
+# Binary: 11
+# Set Bit Count: 2
+#
+# Total set bit count: 1 + 1 + 2 = 4
+#
+# Output: Your script should print `4` as `4 % 1000000007 = 4`.
+
+use strict;
+use warnings;
+use 5.030;
+use List::Util 'sum';
+
+@ARGV==1 or die "Usage: ch-1.pl N\n";
+say(sum(map {bit_count($_)} 1..$ARGV[0]) % 1000000007);
+
+sub bit_count {
+ my($n) = @_;
+ my $count = 0;
+ while ($n > 0) {
+ $count++ if $n & 1;
+ $n >>= 1;
+ }
+ return $count;
+}
diff --git a/challenge-079/paulo-custodio/perl/ch-2.pl b/challenge-079/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..9f160027f2
--- /dev/null
+++ b/challenge-079/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/perl
+
+# Challenge 079
+#
+# TASK #2 › Trapped Rain Water
+# Submitted by: Mohammad S Anwar
+# 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.
+#
+# Example 1:
+# Input: @N = (2, 1, 4, 1, 2, 5)
+# The histogram representation of the given array is as below.
+# 5 #
+# 4 # #
+# 3 # #
+# 2 # # # #
+# 1 # # # # # #
+# _ _ _ _ _ _ _
+# 2 1 4 1 2 5
+# Looking at the above histogram, we can see, it can trap 1 unit of rain water between 1st and 3rd column. Similary it can trap 5 units of rain water betweem 3rd and last column.
+#
+# Therefore your script should print 6.
+#
+# Example 2:
+# Input: @N = (3, 1, 3, 1, 1, 5)
+# The histogram representation of the given array is as below.
+# 5 #
+# 4 #
+# 3 # # #
+# 2 # # #
+# 1 # # # # # #
+# _ _ _ _ _ _ _
+# 3 1 3 1 1 5
+# Looking at the above histogram, we can see, it can trap 2 units of rain water between 1st and 3rd column. Also it can trap 4 units of rain water between 3rd and last column.
+#
+# Therefore your script should print 6.
+
+use strict;
+use warnings;
+use 5.030;
+use List::Util 'max', 'sum';
+
+@ARGV or die "Usage: ch-2.pl list\n";
+my @N = @ARGV;
+
+# draw histogram
+my @hist = draw_hist(@N);
+
+# count spaces between walls, replace them by 'x'
+for (@hist) {
+ 1 while s/#( +)#/'#'.('x' x length($1)).'#'/e;
+}
+
+# count x
+my $count = sum(map {tr/x/x/} @hist);
+
+say $count;
+
+sub draw_hist {
+ my(@n) = @_;
+ my $max = max(@n);
+ my @hist;
+ for my $row (0..$max-1) {
+ my $n = $max-$row;
+ my $line = '';
+ for my $col (0..$#n) {
+ $line .= $n[$col] >= $n ? '#' : ' ';
+ }
+ push @hist, $line;
+ }
+ return @hist;
+}
diff --git a/challenge-079/paulo-custodio/test.pl b/challenge-079/paulo-custodio/test.pl
new file mode 100644
index 0000000000..106c86d494
--- /dev/null
+++ b/challenge-079/paulo-custodio/test.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+use 5.030;
+
+is capture("perl perl/ch-1.pl 3"), "4\n";
+is capture("perl perl/ch-1.pl 4"), "5\n";
+
+is capture("perl perl/ch-2.pl 2 1 4 1 2 5"), "6\n";
+is capture("perl perl/ch-2.pl 3 1 3 1 1 5"), "6\n";
+
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \t\v\f\r]*\n/\n/g;
+ return $out;
+}