diff options
| -rw-r--r-- | challenge-068/sgreen/README.md | 41 | ||||
| -rwxr-xr-x | challenge-068/sgreen/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-068/sgreen/perl/ch-2.pl | 27 |
3 files changed, 132 insertions, 0 deletions
diff --git a/challenge-068/sgreen/README.md b/challenge-068/sgreen/README.md new file mode 100644 index 0000000000..c19c15e7d2 --- /dev/null +++ b/challenge-068/sgreen/README.md @@ -0,0 +1,41 @@ +Solution by Simon Green + +I've decided to jump the of Perl Weekly Challenge bandwagon. Seems like too much fun to miss out on :) + +# TASK #1 › Zero Matrix + +It wasn't clear on how the values of the matrix were to be supplied, so I assumed it was to be supplied in the program. It's been a long time since I needed to read from STDIN in Perl, nothing that a quick Internet search didn't solve. When processing the input, I strip all characters that aren't 0 or 1 so the user could specify `[1, 0, 1]` or `101` or even `1some0thing1`. + +Once we had the values, there were a few ways this could be solved. It seemed the easiest way was to note all the columns and rows that had a zero value. When displaying the matrix, it would only show '1' if the value is one, and neither the column or row was marked as a negative value. + +An alternate approach would be to do the calculations without using the negative columns and rows logic. This would however require more computation and require a bit more logic. + +## Examples + » ./ch-1.pl 3 3 + Enter values for row 1: [1, 0, 1] + Enter values for row 2: [1, 1, 1] + Enter values for row 3: [1, 1, 1] + [ 0, 0, 0 ] + [ 1, 0, 1 ] + [ 1, 0, 1 ] + + » ./ch-1.pl 3 3 + Enter values for row 1: [1, 0, 1] + Enter values for row 2: [1, 1, 1] + Enter values for row 3: [1, 0, 1] + [ 0, 0, 0 ] + [ 1, 0, 1 ] + [ 0, 0, 0 ] + + +# TASK #2 › Reorder List + +This was definitely the easier of the two tasks. Essentially I caclulated the half value of the length of the array, and then used a loop to add the Xth element and Xth from the end element. If the list is an odd value, I added the middle value as that wouldn't have been added in the loop. + +## Examples + + » ./ch-2.pl 1 2 3 4 + 1 4 2 3 + + » ./ch-2.pl 1 2 3 4 5 + 1 5 2 4 3
\ No newline at end of file diff --git a/challenge-068/sgreen/perl/ch-1.pl b/challenge-068/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..0d016b124e --- /dev/null +++ b/challenge-068/sgreen/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.10.1; + +sub _ask_input ($$) { + my ( $m, $n ) = @_; + die "\$M is not a postive number" unless $m >= 1; + die "\$N is not a postive number" unless $n >= 1; + my @rows = (); + + ROW: for my $row ( 1 .. $m ) { + while (1) { + print "Enter values for row $row: "; + my $input = <STDIN>; + + # Strip out everything that isn't a 0 or 1 + $input =~ s/[^01]//g; + if ( length($input) != $n ) { + say "You must enter $n values. Please try again."; + } + else { + push @rows, [ split //, $input ]; + next ROW; + } + } + } + + return @rows; +} + +sub main (@) { + my ( $M, $N ) = @_; + + # Get the data from the user input + my @array = _ask_input( $M, $N ); + + # Determine which rows and columns have negative values + my @negative_cols = (); + my @negative_rows = (); + for my $r ( 0 .. $M - 1 ) { + for my $c ( 0 .. $N - 1 ) { + if ( $array[$r][$c] == 0 ) { + $negative_rows[$r] = 1; + $negative_cols[$c] = 1; + } + } + } + + # Output the results + for my $r ( 0 .. $M - 1 ) { + my @row = (); + for my $c ( 0 .. $N - 1 ) { + push @row, + ( $array[$r][$c] + and not $negative_rows[$r] + and not $negative_cols[$c] ) ? 1 : 0; + } + say '[ ' . join( ', ', @row ), ' ]'; + } +} + +main(@ARGV); diff --git a/challenge-068/sgreen/perl/ch-2.pl b/challenge-068/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..3f788ab252 --- /dev/null +++ b/challenge-068/sgreen/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.10.1; + +sub main (@) { + # Get the list as specified + my @l = @_; + my @reordered_list = (); + + my $is_odd = @l % 2; + my $x = int( @l / 2 ); + + # Get the Li and L-i values from the array + for my $i ( 1 .. $x ) { + push @reordered_list, $l[ $i - 1 ], $l[ -$i ]; + } + + # If the list is odd, get the middle value + push @reordered_list, $l[$x] if $is_odd; + + # Output the list + say join( ' ', @reordered_list ); +} + +main(@ARGV); |
