diff options
| author | Simon Green <mail@simon.green> | 2020-09-27 23:05:24 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2020-09-27 23:05:24 +1000 |
| commit | 593bd4e927df045c4b2a1dc0e3132ffe61d34163 (patch) | |
| tree | 29c285d6cbe66b38c7751578dfd3dd6e5b9a8da4 | |
| parent | 1ac0b05188e44ca889ace85bf0e8974d83feb7b6 (diff) | |
| download | perlweeklychallenge-club-593bd4e927df045c4b2a1dc0e3132ffe61d34163.tar.gz perlweeklychallenge-club-593bd4e927df045c4b2a1dc0e3132ffe61d34163.tar.bz2 perlweeklychallenge-club-593bd4e927df045c4b2a1dc0e3132ffe61d34163.zip | |
Challenge 079 by sgreen
| -rw-r--r-- | challenge-079/sgreen/README.md | 35 | ||||
| -rw-r--r-- | challenge-079/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-079/sgreen/perl/ch1a.pl | 24 | ||||
| -rwxr-xr-x | challenge-079/sgreen/perl/ch1b.pl | 28 | ||||
| -rwxr-xr-x | challenge-079/sgreen/perl/ch2.pl | 78 |
5 files changed, 133 insertions, 33 deletions
diff --git a/challenge-079/sgreen/README.md b/challenge-079/sgreen/README.md index f4852a4fd6..356818eca9 100644 --- a/challenge-079/sgreen/README.md +++ b/challenge-079/sgreen/README.md @@ -1,34 +1,3 @@ -# The Weekly Challenge 078 +# The Weekly Challenge 079 -Solution by Simon Green. - -## TASK #1 › Leader Element - -Both of these week's tasks seemed pretty straight forward, so there's not much commentary from me this week. For the first task, I worked through the array (left to right) and displayed the number if the value is greater than all the values to the right. - -I manually added the last number as by definition (there are no values to the right), it will be greater (even if it is a negative integer). - -An alternate solution would be to work right to left keeping count of the maximum seen value so far and display values that exceeded that value. Definitely more efficient, but a little more complex. - -### Examples - - » ./ch-1.pl 9 10 7 5 6 1 - (10, 7, 6, 1) - - » ./ch-1.pl 3 4 5 - (5) - -## TASK 2 › Left Rotation - -For this task, I read in the two arrays and checked all the values in the second array were less than the number of items in the first array. I worked through the second array to display the rotated list using `@array[ $offset .. $#array, 0 .. $offset - 1 ]` to show the values in the correct order. The exception is when the offset was 0. In this case, I displayed the original array unaltered. - -### Examples - - » ./ch-2.pl "(10 20 30 40 50)" "(3 4)" - [40 50 10 20 30] - [50 10 20 30 40] - - » ./ch-2.pl "(7 4 2 6 3)" "(1 3 4)" - [4 2 6 3 7] - [6 3 7 4 2] - [3 7 4 2 6]
\ No newline at end of file +Solution by Simon Green. [Blog](https://dev.to/simongreennet/the-weekly-challenge-079-1jel) diff --git a/challenge-079/sgreen/blog.txt b/challenge-079/sgreen/blog.txt new file mode 100644 index 0000000000..3ca8d73c0a --- /dev/null +++ b/challenge-079/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/the-weekly-challenge-079-1jel diff --git a/challenge-079/sgreen/perl/ch1a.pl b/challenge-079/sgreen/perl/ch1a.pl new file mode 100755 index 0000000000..a3f6b6e817 --- /dev/null +++ b/challenge-079/sgreen/perl/ch1a.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my $N = shift; + my $count = 0; + + # Sanity check + die "Please enter a positive integer\n" unless $N; + die "The value '$N' is not a positive integer\n" + unless $N =~ /^[1-9][0-9]*$/; + + foreach my $i ( 1 .. $N ) { + my $str = sprintf '%b', $i; + $count += $str =~ tr/1//; + } + + printf qq(%d %% %d = %d\n), $count, 1000000007, $count % 1000000007; +} + +main(@ARGV); diff --git a/challenge-079/sgreen/perl/ch1b.pl b/challenge-079/sgreen/perl/ch1b.pl new file mode 100755 index 0000000000..ed0628ca60 --- /dev/null +++ b/challenge-079/sgreen/perl/ch1b.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my $N = shift; + my $count = 0; + + # Sanity check + die "Please enter a positive integer\n" unless $N; + die "The value '$N' is not a positive integer\n" + unless $N =~ /^[1-9][0-9]*$/; + + my $b = 1; + while ( $b < $N ) { + foreach my $i ( 1 .. $N ) { + $count++ if $i & $b; + } + $b *= 2; + } + + printf qq(%d %% %d = %d\n), $count, 1000000007, $count % 1000000007; +} + +main(@ARGV); + diff --git a/challenge-079/sgreen/perl/ch2.pl b/challenge-079/sgreen/perl/ch2.pl new file mode 100755 index 0000000000..670db22c6b --- /dev/null +++ b/challenge-079/sgreen/perl/ch2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use List::Util qw(min max); +use 5.10.1; + +sub _display_histogram { + my ( $array, $fills ) = @_; + + my $max_value = max @$array; + + # Build an array of arrays with the height of the block, and the + # height of the fill line + my @columns = + map { [ $array->[$_], $array->[$_] + $fills->[$_] ] } ( 0 .. $#$array ); + + my $width = length($max_value); + my $empty = ' ' x ($width); + my $fill = '·' . ( ' ' x ( $width - 1 ) ); + my $block = '#' . ( ' ' x ( $width - 1 ) ); + + for ( my $i = $max_value ; $i > 0 ; $i-- ) { + say join ' ', sprintf( "\%${width}d", $i ), + map { $_->[0] >= $i ? $block : $_->[1] >= $i ? $fill : $empty } + @columns; + } + + # The dashes row + say join ' ', ( '-' x $width ) x ( $#$array + 2 ); + + # And finally the counts for each array + say join ' ', ( ' ' x $width ), map { sprintf "\%-${width}s", $_ } @$array; +} + +sub main { + my @array = @_; + + # Sanity check + die "You must enter at least one value" if $#array == -1; + + foreach my $value (@array) { + die "Value '$value' is not an integer\n" + unless $value =~ /^[1-9][0-9]*$/; + } + + # This stores the results + my @fills = (0); # The first column cannot hold water + my $drops = 0; + + # Let's calculate the storage capcity for each column + # (except first and last) + foreach my $col ( 1 .. $#array - 1 ) { + my $height = $array[$col]; + + # The maximum height to the left and to the right + my $left = max map { $array[$_] } ( 0 .. $col - 1 ); + my $right = max map { $array[$_] } ( $col + 1 .. $#array ); + + # We can fill to the minimum of left and right, minus the + # height of this column + my $x = min( $left, $right ) - $height; + $x = 0 if $x < 0; + + $drops += $x; + push @fills, $x; + } + + # The last column cannot hold water + push @fills, 0; + + # Display the history + _display_histogram( \@array, \@fills ); + + say "\nResult is $drops"; +} + +main(@ARGV); |
