diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-08 10:52:19 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-08 10:52:19 +0100 |
| commit | 770cf74bcfb8a1f55c7aa10fd0c1f1e6c50c2f36 (patch) | |
| tree | 7987bcf04d370882d944a8dddaa134f4fde093bd /challenge-077 | |
| parent | 41baccbf1440fcd9cd88abc370bec2cb462ae278 (diff) | |
| download | perlweeklychallenge-club-770cf74bcfb8a1f55c7aa10fd0c1f1e6c50c2f36.tar.gz perlweeklychallenge-club-770cf74bcfb8a1f55c7aa10fd0c1f1e6c50c2f36.tar.bz2 perlweeklychallenge-club-770cf74bcfb8a1f55c7aa10fd0c1f1e6c50c2f36.zip | |
- Added Perl/Raku solutions to "Lonely X" task.
Diffstat (limited to 'challenge-077')
| -rw-r--r-- | challenge-077/mohammad-anwar/perl/ch-2.pl | 105 | ||||
| -rw-r--r-- | challenge-077/mohammad-anwar/perl/ch-2.t | 112 | ||||
| -rw-r--r-- | challenge-077/mohammad-anwar/raku/ch-2.raku | 89 | ||||
| -rw-r--r-- | challenge-077/mohammad-anwar/raku/ch-2.t | 99 |
4 files changed, 405 insertions, 0 deletions
diff --git a/challenge-077/mohammad-anwar/perl/ch-2.pl b/challenge-077/mohammad-anwar/perl/ch-2.pl new file mode 100644 index 0000000000..ef6f597b43 --- /dev/null +++ b/challenge-077/mohammad-anwar/perl/ch-2.pl @@ -0,0 +1,105 @@ +#!/usr/bin/perl + +# +# Perl Weekly Challenge - 077 +# +# Task #2: Lonely X +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/ +# + +use strict; +use warnings; + +print "USAGE: perl $0 <row> <col>\n" and exit + if (defined $ARGV[0] && ($ARGV[0] eq '-h')); + +my $R = $ARGV[0] || 3; +my $C = $ARGV[1] || 3; + +die "ERROR: Invalid rows [$R].\n" unless ($R =~ /^\d+$/ && $R >=2 ); +die "ERROR: Invalid cols [$C].\n" unless ($C =~ /^\d+$/ && $C >=2 ); + +p(find_lonely_x(get_matrix($R, $C))); + +# +# +# METHODS + +sub find_lonely_x { + my ($matrix) = @_; + + my $rows = $#$matrix; + my $cols = $#{$matrix->[0]}; + + my @lonely_x = (); + foreach my $row (0..$rows) { + foreach my $col (0..$cols) { + next unless $matrix->[$row][$col] eq 'X'; + + my @neighbours = (); + # top + push @neighbours, $matrix->[$row-1][$col] if $row > 0; + + # bottom + push @neighbours, $matrix->[$row+1][$col] if $row < $rows; + + # left + push @neighbours, $matrix->[$row][$col-1] if $col > 0; + # diagonal top left + push @neighbours, $matrix->[$row-1][$col-1] if $row > 0 && $col > 0; + # diagonal bottom left + push @neighbours, $matrix->[$row+1][$col-1] if $row < $rows && $col > 0; + + # right + push @neighbours, $matrix->[$row][$col+1] if $col < $cols; + # diagonal top right + push @neighbours, $matrix->[$row-1][$col+1] if $row > 0 && $col < $cols; + # diagonal bottom right + push @neighbours, $matrix->[$row+1][$col+1] if $row < $rows && $col < $cols; + + push @lonely_x, [$row+1, $col+1] + unless (grep /X/, @neighbours); + } + } + + return 0 if @lonely_x == 0; + return @lonely_x; +} + +sub p { + my (@data) = @_; + + print "0\n" and return unless ref($data[0]); + foreach (@data) { + print sprintf("%s\n", join ", ", @$_); + } +} + +sub get_matrix { + my ($rows, $cols) = @_; + + my $min = 0; + my $max = 9; + my $array = [ 'X', 'O', 'X', 'X', 'X', 'O', 'O', 'O', 'X', 'O' ]; + my $matrix = []; + + foreach my $r (0..$rows) { + foreach my $c (0..$cols) { + $matrix->[$r][$c] = $array->[int($min + rand($max - $min))]; + } + } + + display($matrix); + return $matrix; +} + +sub display { + my ($matrix) = @_; + + print "Matrix:\n"; + foreach my $r (0..$#$matrix) { + print sprintf("[ %s ]\n", join ', ', @{$matrix->[$r]}); + } + print "\n"; +} diff --git a/challenge-077/mohammad-anwar/perl/ch-2.t b/challenge-077/mohammad-anwar/perl/ch-2.t new file mode 100644 index 0000000000..4d0acb685b --- /dev/null +++ b/challenge-077/mohammad-anwar/perl/ch-2.t @@ -0,0 +1,112 @@ +#!/usr/bin/perl + +# +# Perl Weekly Challenge - 077 +# +# Task #2: Lonely X +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/ +# + +use strict; +use warnings; +use Test::More; +use Test::Deep; + +my $example_1 = [ ['O', 'O', 'X'], + ['X', 'O', 'O'], + ['X', 'O', 'O'], + ]; + +my $example_2 = [ ['O', 'O', 'X', 'O'], + ['X', 'O', 'O', 'O'], + ['X', 'O', 'O', 'X'], + ['O', 'X', 'O', 'O'], + ]; + +is_deeply([find_lonely_x($example_1)], [[1, 3]], "example 1"); +is_deeply([find_lonely_x($example_2)], [[1, 3], [3,4]], "example 2"); + +done_testing; + +# +# +# METHODS + +sub find_lonely_x { + my ($matrix) = @_; + + my $rows = $#$matrix; + my $cols = $#{$matrix->[0]}; + + my @lonely_x = (); + foreach my $row (0..$rows) { + foreach my $col (0..$cols) { + next unless $matrix->[$row][$col] eq 'X'; + + my @neighbours = (); + # top + push @neighbours, $matrix->[$row-1][$col] if $row > 0; + + # bottom + push @neighbours, $matrix->[$row+1][$col] if $row < $rows; + + # left + push @neighbours, $matrix->[$row][$col-1] if $col > 0; + # diagonal top left + push @neighbours, $matrix->[$row-1][$col-1] if $row > 0 && $col > 0; + # diagonal bottom left + push @neighbours, $matrix->[$row+1][$col-1] if $row < $rows && $col > 0; + + # right + push @neighbours, $matrix->[$row][$col+1] if $col < $cols; + # diagonal top right + push @neighbours, $matrix->[$row-1][$col+1] if $row > 0 && $col < $cols; + # diagonal bottom right + push @neighbours, $matrix->[$row+1][$col+1] if $row < $rows && $col < $cols; + + push @lonely_x, [$row+1, $col+1] + unless (grep /X/, @neighbours); + } + } + + return 0 if @lonely_x == 0; + return @lonely_x; +} + +sub p { + my (@data) = @_; + + print "0\n" and return unless ref($data[0]); + foreach (@data) { + print sprintf("%s\n", join ", ", @$_); + } +} + +sub get_matrix { + my ($rows, $cols) = @_; + + my $min = 0; + my $max = 9; + my $array = [ 'X', 'O', 'X', 'X', 'X', 'O', 'O', 'O', 'X', 'O' ]; + my $matrix = []; + + foreach my $r (0..$rows) { + foreach my $c (0..$cols) { + $matrix->[$r][$c] = $array->[int($min + rand($max - $min))]; + } + } + + display($matrix); + return $matrix; +} + +sub display { + my ($matrix) = @_; + + print "Matrix:\n"; + foreach my $r (0..$#$matrix) { + print sprintf("[ %s ]\n", join ', ', @{$matrix->[$r]}); + } + print "\n"; +} diff --git a/challenge-077/mohammad-anwar/raku/ch-2.raku b/challenge-077/mohammad-anwar/raku/ch-2.raku new file mode 100644 index 0000000000..8773c02fe8 --- /dev/null +++ b/challenge-077/mohammad-anwar/raku/ch-2.raku @@ -0,0 +1,89 @@ +#!/usr/bin/env raku + +# +# Perl Weekly Challenge - 077 +# +# Task #2: Lonely X +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/ +# + +use v6.d; + +sub MAIN(Int :$R is copy where { $R >= 2 } = 3, + Int :$C is copy where { $C >= 2 } = 3) { + + my $matrix = get-matrix(--$R, --$C); + find-lonely-x($matrix).say; +} + +# +# +# METHODS + +sub find-lonely-x($matrix) { + + my $rows = $matrix.elems - 1; + my $cols = $matrix.[0].elems - 1; + + my @lonely_x = (); + for 0..$rows -> $row { + for 0..$cols -> $col { + next unless $matrix.[$row][$col] eq 'X'; + + my @neighbours = (); + + # top + @neighbours.push: $matrix.[$row-1][$col] if $row > 0; + # bottom + @neighbours.push: $matrix.[$row+1][$col] if $row < $rows; + + # left + @neighbours.push: $matrix.[$row][$col-1] if $col > 0; + # diagonal top left + @neighbours.push: $matrix.[$row-1][$col-1] if $row > 0 && $col > 0; + # diagonal bottom left + @neighbours.push: $matrix.[$row+1][$col-1] if $row < $rows && $col > 0; + + # right + @neighbours.push: $matrix.[$row][$col+1] if $col < $cols; + # diagonal top right + @neighbours.push: $matrix.[$row-1][$col+1] if $row > 0 && $col < $cols; + # diagonal bottom right + @neighbours.push: $matrix.[$row+1][$col+1] if $row < $rows && $col < $cols; + + unless @neighbours.grep({ /X/ }) { + @lonely_x.push: ($row+1, $col+1); + } + } + } + + return 0 if @lonely_x.elems == 0; + return |@lonely_x; +} + +sub get-matrix(Int $rows where { $rows >= 1 }, + Int $cols where { $cols >= 1 }) { + + my $min = 0; + my $max = 9; + my $array = [ 'X', 'O', 'X', 'X', 'X', 'O', 'O', 'O', 'X', 'O' ]; + + my $matrix = []; + for 0..$rows -> $r { + for 0..$cols -> $c { + $matrix.[$r][$c] = $array.pick; + } + } + + display-matrix('Matrix:', $matrix); + return $matrix; +} + +sub display-matrix($label, $matrix) { + + say $label; + for 0..$matrix.elems - 1 -> $r { + say sprintf("[ %s ]", $matrix.[$r].join(', ')); + } +} diff --git a/challenge-077/mohammad-anwar/raku/ch-2.t b/challenge-077/mohammad-anwar/raku/ch-2.t new file mode 100644 index 0000000000..68f6d094d0 --- /dev/null +++ b/challenge-077/mohammad-anwar/raku/ch-2.t @@ -0,0 +1,99 @@ +#!/usr/bin/env raku + +# +# Perl Weekly Challenge - 077 +# +# Task #2: Lonely X +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/ +# + +use Test; + +my $example_1 = [ ('O', 'O', 'X'), + ('X', 'O', 'O'), + ('X', 'O', 'O'), + ]; + +my $example_2 = [ ('O', 'O', 'X', 'O'), + ('X', 'O', 'O', 'O'), + ('X', 'O', 'O', 'X'), + ('O', 'X', 'O', 'O'), + ]; + +is-deeply find-lonely-x($example_1), ((1, 3)), "example 1"; +is-deeply find-lonely-x($example_2), ((1, 3), (3, 4)), "example 2"; + +done-testing; + + +# +# +# METHODS + +sub find-lonely-x($matrix) { + + my $rows = $matrix.elems - 1; + my $cols = $matrix.[0].elems - 1; + + my @lonely_x = (); + for 0..$rows -> $row { + for 0..$cols -> $col { + next unless $matrix.[$row][$col] eq 'X'; + + my @neighbours = (); + + # top + @neighbours.push: $matrix.[$row-1][$col] if $row > 0; + # bottom + @neighbours.push: $matrix.[$row+1][$col] if $row < $rows; + + # left + @neighbours.push: $matrix.[$row][$col-1] if $col > 0; + # diagonal top left + @neighbours.push: $matrix.[$row-1][$col-1] if $row > 0 && $col > 0; + # diagonal bottom left + @neighbours.push: $matrix.[$row+1][$col-1] if $row < $rows && $col > 0; + + # right + @neighbours.push: $matrix.[$row][$col+1] if $col < $cols; + # diagonal top right + @neighbours.push: $matrix.[$row-1][$col+1] if $row > 0 && $col < $cols; + # diagonal bottom right + @neighbours.push: $matrix.[$row+1][$col+1] if $row < $rows && $col < $cols; + + unless @neighbours.grep({ /X/ }) { + @lonely_x.push: ($row+1, $col+1); + } + } + } + + return 0 if @lonely_x.elems == 0; + return |@lonely_x; +} + +sub get-matrix(Int $rows where { $rows >= 1 }, + Int $cols where { $cols >= 1 }) { + + my $min = 0; + my $max = 9; + my $array = [ 'X', 'O', 'X', 'X', 'X', 'O', 'O', 'O', 'X', 'O' ]; + + my $matrix = []; + for 0..$rows -> $r { + for 0..$cols -> $c { + $matrix.[$r][$c] = $array.pick; + } + } + + return $matrix; + +} + +sub display-matrix($label, $matrix) { + + say $label; + for 0..$matrix.elems - 1 -> $r { + say sprintf("[ %s ]", $matrix.[$r].join(', ')); + } +} |
