diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-26 14:27:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-26 14:27:46 +0100 |
| commit | 95443725eec48b734a933960b53432f945297d86 (patch) | |
| tree | 37ead66fa8d69f141519333286fe8edeca63b89a | |
| parent | 94b900c22a7f0595d0379de310b47c990180ef19 (diff) | |
| parent | ad0e06e36a27ea25d691a2feea9988a62ae95380 (diff) | |
| download | perlweeklychallenge-club-95443725eec48b734a933960b53432f945297d86.tar.gz perlweeklychallenge-club-95443725eec48b734a933960b53432f945297d86.tar.bz2 perlweeklychallenge-club-95443725eec48b734a933960b53432f945297d86.zip | |
Merge pull request #2379 from waltman/branch-for-challenge-079
Branch for challenge 079
| -rw-r--r-- | challenge-079/walt-mankowski/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-079/walt-mankowski/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-079/walt-mankowski/perl/ch-2.pl | 54 |
3 files changed, 78 insertions, 0 deletions
diff --git a/challenge-079/walt-mankowski/blog.txt b/challenge-079/walt-mankowski/blog.txt new file mode 100644 index 0000000000..bfdbad6361 --- /dev/null +++ b/challenge-079/walt-mankowski/blog.txt @@ -0,0 +1 @@ +http://www.mawode.com/blog/blog/2020/09/26/perl-weekly-challenge-79/ diff --git a/challenge-079/walt-mankowski/perl/ch-1.pl b/challenge-079/walt-mankowski/perl/ch-1.pl new file mode 100644 index 0000000000..10bc99650f --- /dev/null +++ b/challenge-079/walt-mankowski/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); + +# 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. + +sub bits_set($x) { + my $bin = sprintf "%b", $x; + return $bin =~ tr/1//; +} + +my $MOD = 1000000007; +my $n = $ARGV[0]; +my $sum = 0; +$sum += bits_set($_) for 1..$n; + +say $sum, " ", $sum % $MOD; diff --git a/challenge-079/walt-mankowski/perl/ch-2.pl b/challenge-079/walt-mankowski/perl/ch-2.pl new file mode 100644 index 0000000000..5a58052c37 --- /dev/null +++ b/challenge-079/walt-mankowski/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); +use List::Util qw(max); + +# 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. + +my @a = @ARGV; + +# build the histogram +my $rows = max(@a); +my $cols = @a; +my @hist; +for my $col (0..$#a) { + for my $row (0..$rows-1) { + $hist[$row][$col] = $row < $a[$col] ? 1 : 0; + } +} + +my $units = 0; +for my $row (1..$rows-1) { + for my $col (1..$cols-1) { + $units++ if trapped($row, $col, $cols, @hist); + } +} + +say $units; + +sub trapped($row, $col, $max_col, @hist) { + # check if we're at a wall + return 0 if $hist[$row][$col]; + + # look for left wall + my $left = 0; + for (my $c = $col-1; $c >= 0 && !$left; $c--) { + $left = 1 if $hist[$row][$c]; + } + + # look for right wall + my $right = 0; + for (my $c = $col+1; $c < $max_col && !$right; $c++) { + $right = 1 if $hist[$row][$c]; + } + + return $left && $right; +} |
