diff options
| -rw-r--r-- | challenge-078/sgreen/README.md | 49 | ||||
| -rw-r--r-- | challenge-078/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-078/sgreen/perl/ch-1.pl | 31 | ||||
| -rwxr-xr-x | challenge-078/sgreen/perl/ch-2.pl | 33 |
4 files changed, 83 insertions, 31 deletions
diff --git a/challenge-078/sgreen/README.md b/challenge-078/sgreen/README.md index 9c49877d6c..f4852a4fd6 100644 --- a/challenge-078/sgreen/README.md +++ b/challenge-078/sgreen/README.md @@ -1,47 +1,34 @@ -# The Weekly Challenge 077 +# The Weekly Challenge 078 Solution by Simon Green. -## TASK #1 › Fibonacci Sum +## TASK #1 › Leader Element -One thing I noted with last week's [Prime Sum](https://perlweeklychallenge.org/blog/perl-weekly-challenge-076/#TASK1/) is a few people went straight to the logic of returning 1 if the number is a prime, 2 if it is even or 2 more than a prime or 3 otherwise. While this is correct, I tend to tackle the problem at hand without looking at the quickest way to solve a task. As was the case with this week's solution. +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. -The first part of this task was to generate a list of Fibonacci numbers up to and including the required sum. For this I started with an array of 1 and 2, and the added the last two numbers together until we reached or exceed the target figure. +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). -For the main part of the task, I created a stack with all possible solutions. For each fibonnaci number, I added the new number, and added that number to all the existing values in the stack. For example, for values between 5-7, I would add 5 in the first iteration, add 3 and (3.5) in the second, and 2, (2,5), (2,3), (2,3,5) in the third. I discarded any value in the stack that was greater than the target number or the remainder of the stack is greater than the sum of the remaining numbers. In both these cases it would not be possible to find a solution. - -Finally I counted the solutions that matched the target, and displayed the result. +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 6 - RESULT IS 2 - 5 + 1 = 6 - 3 + 2 + 1 = 6 - - - » ./ch-1.pl 1000000 - RESULT IS 263 - 832040 + 121393 + 46368 + 144 + 55 = 1000000 - 514229 + 317811 + 121393 + 46368 + 144 + 55 = 1000000 - 832040 + 121393 + 28657 + 17711 + 144 + 55 = 1000000 - 514229 + 317811 + 121393 + 28657 + 17711 + 144 + 55 = 1000000 - ... - -## TASK 2 › Lonely X + » ./ch-1.pl 9 10 7 5 6 1 + (10, 7, 6, 1) -This is very similar to the Zero Matrix task from [Challenge 068](https://perlweeklychallenge.org/blog/perl-weekly-challenge-068/), which incidentally was the first challenge I every did. This time we are using 'O' and 'X' instead of '0' and '1'. + » ./ch-1.pl 3 4 5 + (5) -Like that task, there are (at least) two ways to solve this challenge. One is to create a shadow grid that masks out the surrounding cells of a 'O' as not being lonely to a 'X'. The other is to calculate if an X is lonely as we evaluate each cell. I choose the later, purely because I did the former in challenge 068. +## TASK 2 › Left Rotation -Once I've parsed the input, I validate that all the rows are the same length. I then go through each cells (rows and then columns). For each cell that is an 'X', I find out if there are any 'X's in the (up to) eight surrounding cells. +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 "[ O O X ]" "[ X O O ]" "[ X O O ]" - Output is 1 - Matches: row 1 col 3 + » ./ch-2.pl "(10 20 30 40 50)" "(3 4)" + [40 50 10 20 30] + [50 10 20 30 40] - » ./ch-2.pl "[ O O X O ]" "[ X O O O ]" "[ X O O X ]" "[ O X O O ]" - Output is 2 - Matches: row 1 col 3, row 3 col 4 + » ./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 diff --git a/challenge-078/sgreen/blog.txt b/challenge-078/sgreen/blog.txt new file mode 100644 index 0000000000..f391de8819 --- /dev/null +++ b/challenge-078/sgreen/blog.txt @@ -0,0 +1 @@ +https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-078/sgreen/README.md diff --git a/challenge-078/sgreen/perl/ch-1.pl b/challenge-078/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..e909010d15 --- /dev/null +++ b/challenge-078/sgreen/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw(say); +use List::Util qw(max); + +sub main { + my @array = @_; + my @leader = (); + + # Sanity checks + die "You must specify one or more integers\n" unless scalar @array; + foreach my $value (@array) { + die "The value '$value' is not an integer\n" + unless $value =~ /^-?[0-9]+$/; + } + + for my $index ( 0 .. $#array - 1 ) { + # Get the current value, and the maximum of the remaining values + my $value = $array[$index]; + my $max = max @array[ $index + 1 .. $#array ]; + push @leader, $value if $value > $max; + } + + # The last value is always a maximum + push @leader, $array[-1]; + say '(', join( ', ', @leader ), ')'; +} + +main(@ARGV); diff --git a/challenge-078/sgreen/perl/ch-2.pl b/challenge-078/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..7e458d013d --- /dev/null +++ b/challenge-078/sgreen/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw(say); + +sub main { + my ( $array, $shift ) = @_; + + # Split the values into an array + my @array = ( $array =~ /(\d+(?:\.\d+)?)/gm ); + my @shift = ( $shift =~ /(\d+)/gm ); + my @results = (); + + foreach my $offset (@shift) { + if ( $offset > $#array ) { + die "$offset is >= the size of the array\n"; + } + + if ( $offset == 0 ) { + push @results, [@array]; + } + else { + push @results, [ @array[ $offset .. $#array, 0 .. $offset - 1 ] ]; + } + } + + foreach my $result (@results) { + say '[', join( ' ', @$result ), ']'; + } +} + +main(@ARGV); |
