diff options
| author | wanderdoc <wanderdoc@googlemail.com> | 2020-09-27 15:28:44 +0200 |
|---|---|---|
| committer | wanderdoc <wanderdoc@googlemail.com> | 2020-09-27 15:28:44 +0200 |
| commit | 1d0f1e2d0488a265833992c6c50abf6c8ccc7465 (patch) | |
| tree | 2244ce44c934f2e794327705ab15bcd494e300ff /challenge-079 | |
| parent | 9d61e22614c244db9074f1d6ff92c81eeeb9696e (diff) | |
| download | perlweeklychallenge-club-1d0f1e2d0488a265833992c6c50abf6c8ccc7465.tar.gz perlweeklychallenge-club-1d0f1e2d0488a265833992c6c50abf6c8ccc7465.tar.bz2 perlweeklychallenge-club-1d0f1e2d0488a265833992c6c50abf6c8ccc7465.zip | |
Solutions to challenge-079.
Diffstat (limited to 'challenge-079')
| -rw-r--r-- | challenge-079/wanderdoc/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-079/wanderdoc/perl/ch-2.pl | 72 |
2 files changed, 107 insertions, 0 deletions
diff --git a/challenge-079/wanderdoc/perl/ch-1.pl b/challenge-079/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..0c3faea887 --- /dev/null +++ b/challenge-079/wanderdoc/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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 Total set bit count: 1 + 1 + 2 + 1 = 5 +Output: Your script should print `5` as `5 % 1000000007 = 5`. + + +Example 2: Input: $N = 3 Total set bit count: 1 + 1 + 2 = 4 +Output: Your script should print `4` as `4 % 1000000007 = 4`. + +=cut +use Test::More; + +sub count_set_bits +{ + my $num = $_[0]; + my $set_bits = 0; + for my $i ( 1 .. $num ) + { + + my $binary = sprintf("%b", $i); + $set_bits += $binary =~ tr/1/1/; + } + return $set_bits; +} +is(count_set_bits(4), 5, 'Example 1'); +is(count_set_bits(3), 4, 'Example 2'); + +done_testing();
\ No newline at end of file diff --git a/challenge-079/wanderdoc/perl/ch-2.pl b/challenge-079/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..7a47932b48 --- /dev/null +++ b/challenge-079/wanderdoc/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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) +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) +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. +=cut + +use List::Util qw(min max); + +sub print_histogram # Challenge 075. +{ + my @arr = @_; + my $max = max(@arr); + my $offset = length($max); + for my $high ( reverse 1 .. $max ) + { + + my @hist = map {$_ >= $high ? '#' : ' '} @arr; + print $high, ' ' x $offset, join(' ', @hist), $/; + } + + my @underl = map '_', @arr, $max; + print join(' ', @underl), $/; + print ' ' x ($offset + 1), join(' ', @arr) , $/; +} + +sub estimate_water +{ + my @arr = @_; + + + my @left; + my @right; + my $volume = 0; + + $left[0] = $arr[0]; + for my $idx ( 1 .. $#arr ) + { + $left[$idx] = max($left[$idx - 1], $arr[$idx]); + + } + + $right[$#arr] = $arr[$#arr]; + for my $idx ( reverse 0 .. $#arr - 1 ) + { + $right[$idx] = max($right[$idx + 1], $arr[$idx]); + } + + + for my $idx ( 0 .. $#arr ) + { + $volume += min($left[$idx], $right[$idx]) - $arr[$idx]; + } + + return $volume; +} + + +print_histogram(2, 1, 4, 1, 2, 5); +print estimate_water(2, 1, 4, 1, 2, 5), $/; # 6 + +print_histogram(3, 1, 3, 1, 1, 5); +print estimate_water(3, 1, 3, 1, 1, 5), $/; # 6
\ No newline at end of file |
