diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-08-21 00:29:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-21 00:29:43 +0100 |
| commit | 4ca2a513a349f848e49e2ac99c4a6d694ef9fc47 (patch) | |
| tree | 69b458d5dff29600e4bb6ed8e4429ddc8c57065d | |
| parent | 636e35fa1cf2af5d808dee8ee67c99105212f8b6 (diff) | |
| parent | 8bebe6826d5e14b3931945fd5a1184a57f2605af (diff) | |
| download | perlweeklychallenge-club-4ca2a513a349f848e49e2ac99c4a6d694ef9fc47.tar.gz perlweeklychallenge-club-4ca2a513a349f848e49e2ac99c4a6d694ef9fc47.tar.bz2 perlweeklychallenge-club-4ca2a513a349f848e49e2ac99c4a6d694ef9fc47.zip | |
Merge pull request #4748 from konstantinos-giannakakis/challenge126
the solution for challenge 126 in perl
| -rw-r--r-- | challenge-126/kostas-giannakakis/README | 1 | ||||
| -rw-r--r-- | challenge-126/kostas-giannakakis/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-126/kostas-giannakakis/perl/ch-2.pl | 100 |
3 files changed, 127 insertions, 0 deletions
diff --git a/challenge-126/kostas-giannakakis/README b/challenge-126/kostas-giannakakis/README new file mode 100644 index 0000000000..5d795508c9 --- /dev/null +++ b/challenge-126/kostas-giannakakis/README @@ -0,0 +1 @@ +Solution by Konstantinos Giannakakis diff --git a/challenge-126/kostas-giannakakis/perl/ch-1.pl b/challenge-126/kostas-giannakakis/perl/ch-1.pl new file mode 100644 index 0000000000..d81d6f73d3 --- /dev/null +++ b/challenge-126/kostas-giannakakis/perl/ch-1.pl @@ -0,0 +1,26 @@ +use strict; +use warnings; +use List::Util qw(any); + +# Give the integer for the upper limit of 1 - $N +my $N = 25; + +# This array contains the range of numbers we want to check +my @list = (1..$N); + +my @count = (); + +# find all the numbers that dont contain 1 +@count = @count = grep { $_ if $_ !~ /1/g } @list; + +# print them +my $count = 0; +print "There are " . scalar(@count) . " numbers between 1 and $N that don't contain digit 1.\n"; +foreach (@count) { + if ($count != $#count){ + print("$_, "); + } else { + print ("$_.\n"); + } +$count++; +}
\ No newline at end of file diff --git a/challenge-126/kostas-giannakakis/perl/ch-2.pl b/challenge-126/kostas-giannakakis/perl/ch-2.pl new file mode 100644 index 0000000000..307193ca52 --- /dev/null +++ b/challenge-126/kostas-giannakakis/perl/ch-2.pl @@ -0,0 +1,100 @@ +use strict; +use warnings; + +# my input data +# an assumption is made here that the size of the +# array will always be MxN, which means no row or col +# will have extra element in comparison with the others +my @board = ( + ['x', '*', '*', '*', 'x', '*', 'x', 'x', 'x', 'x'], + ['*', '*', '*', '*', '*', '*', '*', '*', '*', 'x'], + ['*', '*', '*', '*', 'x', '*', 'x', '*', 'x', '*'], + ['*', '*', '*', 'x', 'x', '*', '*', '*', '*', '*'], + ['x', '*', '*', '*', 'x', '*', '*', '*', '*', 'x'] +); + +sub _get_rows_cols { + my @arr = @_; + + my $rows; + my $cols; + my @inner; + + $rows = $#arr; + # deref an inner array and get the length + @inner = @{$board[$rows]}; + $cols = $#inner; + + return +{rows => $rows, cols => $cols}; +} + +sub _traverse_array { + my $cords = shift; + my $board = shift; + + my $rows = $cords->{rows}; + my $cols = $cords->{cols}; + + foreach my $r (0..$rows) { + foreach my $c (0..$cols) { + if ($board[$r][$c] eq "*") { + $board[$r][$c] = 0; + } + } + } + + foreach my $r (0..$rows) { + foreach my $c (0..$cols) { + if ($board[$r][$c] eq "x") { + _visit_neighbours_apply_numbers($r, $c, @board, $rows, $cols); + _print_array($cords, @board); + print "\n\n"; + } + } + } +} + +sub _visit_neighbours_apply_numbers { + my $row = shift; + my $col = shift; + my $board = shift; + my $rows = shift; + my $cols = shift; + + $board[$row-1][$col-1]++ unless !defined($board[$row-1][$col-1]) || $board[$row-1][$col-1] eq "x" || $row-1 < 0 || $col-1 < 0; + $board[$row-1][$col]++ unless !defined($board[$row-1][$col]) || $board[$row-1][$col] eq "x" || $row-1 < 0; + $board[$row-1][$col+1]++ unless !defined($board[$row-1][$col+1]) || $board[$row-1][$col+1] eq "x" || $row-1 < 0; + + $board[$row][$col-1]++ unless !defined($board[$row][$col-1]) || $board[$row][$col-1] eq "x" || $col-1 < 0; + $board[$row][$col+1]++ unless !defined($board[$row][$col+1]) || $board[$row][$col+1] eq "x"; + + $board[$row+1][$col-1]++ unless !defined($board[$row+1][$col-1]) || $board[$row+1][$col-1] eq "x" || $col-1 < 0; + $board[$row+1][$col]++ unless !defined($board[$row+1][$col]) || $board[$row+1][$col] eq "x"; + $board[$row+1][$col+1]++ unless !defined($board[$row+1][$col+1]) || $board[$row+1][$col+1] eq "x"; + +} + +sub _print_array { + my $cords = shift; + my $board = shift; + + my $rows = $cords->{rows}; + my $cols = $cords->{cols}; + + foreach my $r (0..$rows) { + foreach my $c (0..$cols) { + print "$board[$r][$c] "; + } + print "\n"; + } +} + +# main code + +# get the rows and cols of the array +my $cords = _get_rows_cols(@board); + +# traverse the array +_traverse_array($cords, @board); + +_print_array($cords, @board); |
