From d61e52113cd9222ba8c876002997ba4bc1a0367c Mon Sep 17 00:00:00 2001 From: WmMonty Date: Sun, 30 Aug 2020 15:00:54 -0700 Subject: Happy that I got this one done. Well, I couldn't quite finish the first challenge, but I did try my hand at it. The second one was a pleasure to accomplish. --- challenge-075/will-west/perl/75.2.pl | 78 ++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100755 challenge-075/will-west/perl/75.2.pl diff --git a/challenge-075/will-west/perl/75.2.pl b/challenge-075/will-west/perl/75.2.pl new file mode 100755 index 0000000000..460415aff1 --- /dev/null +++ b/challenge-075/will-west/perl/75.2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/perl +use 5.28.1; +use warnings; +use List::Util 'max'; +use Data::Dumper; + + + +#TASK #2 › Largest Rectangle Histogram +#Submitted by: Mohammad S Anwar +# +#You are given an array of positive numbers @A. +# +#Write a script to find the largest rectangle histogram created by the given array. + + +#my solution takes advantage of the fact that a chart like what we're working with never has vertical gaps. So, we just need to find contiguous horizontal lines and square them with the distance from the chart's base. + +#I promise to try to do proper testing for the next one! + + +my @chart = getchart(); +my @rows = makerowsfromchart(@chart); +my @contiguous = contiguousinrows(@rows); +my @squares = square(@contiguous); +my $biggestsquare = max(@squares); +say $biggestsquare; + + + + + +sub getchart{ + (1,4,6,4,2,2,4,5,5) +} + + + + + + + +sub makerowsfromchart{ + my $mx = max @_; + my @rows; + for my $row (1..$mx){ + for my $col (@_){ + push @{$rows[$row]}, int($col>=$row) + } + } + @rows +} + +sub contiguousinrows{ + my @longests; + for my $row (1..$#_){ + my $str = join '',@{$_[$row]}; + my @contigs = ($str=~/(1+)/g); + my @lengths =map{length($_)}@contigs; + push @longests, [$row, max(@lengths)]; + } + @longests +} + + +sub square{ + map{ $_->[0] * $_->[1] } @_ +} + + + + + +sub DEPRECATEDsquare{ + my @sqrd; + push @sqrd, $_->[0] * $_->[1] for @_; + @sqrd +} -- cgit