From 8bebe6826d5e14b3931945fd5a1184a57f2605af Mon Sep 17 00:00:00 2001 From: Kostas Date: Fri, 20 Aug 2021 18:13:50 +0200 Subject: the solution for challenge 126 in perl --- challenge-126/kostas-giannakakis/README | 1 + challenge-126/kostas-giannakakis/perl/ch-1.pl | 26 +++++++ challenge-126/kostas-giannakakis/perl/ch-2.pl | 100 ++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 challenge-126/kostas-giannakakis/README create mode 100644 challenge-126/kostas-giannakakis/perl/ch-1.pl create mode 100644 challenge-126/kostas-giannakakis/perl/ch-2.pl 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); -- cgit