From 4bcb5f6529029f0a608b775c2d2fddbaa90a5dbf Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 24 Aug 2020 15:02:41 +0200 Subject: Task 2 done. --- challenge-075/luca-ferrari/raku/ch-2.p6 | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 challenge-075/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-075/luca-ferrari/raku/ch-2.p6 b/challenge-075/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..14cf0bef95 --- /dev/null +++ b/challenge-075/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,58 @@ +#!raku + +class Histogram { + has Int $.column; + has Int $.height; + + method Str() { "Histogram $!height Column $!column"; } +} + + +class Rectangle { + has Int $.height; + has Int $.base; + + method area() { $!height * $!base } + method Str() { "Rectangle with base $!base and height $!height ($!base x $!height)" } +} + + +sub MAIN( *@A + where { @A.grep( * ~~ Int ).elems == @A.elems && @A.grep( *.Int >= 1 ) } ) { + say "Numbers are { @A }"; + + my Histogram @histograms; + my $column = 1; + for @A { + @histograms.push: Histogram.new: column => $column, height => $_.Int; + } + + + my Rectangle @rectangles; + my ( $current-height, $current-width ) = Nil, Nil; + + for 0 ..^ @histograms.elems -> $current-index { + ( $current-height, $current-width ) = @histograms[ $current-index ].height, 0; + + + for 0 ..^ @histograms.elems { + next if $_ < $current-index && @histograms[ $_ ].height < $current-height; + if @histograms[ $_ ].height < $current-height { + last; + } + else { + $current-width++; + } + } + + @rectangles.push: Rectangle.new( height => $current-height, + base => $current-width ); + } + + + + # get the first one with the biggest area + @rectangles.sort( { $^b.area <=> $^a.area } ).first.put; + + +} -- cgit