aboutsummaryrefslogtreecommitdiff
path: root/challenge-126/belmark-caday
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-08-21 18:11:11 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-08-21 18:11:11 +0100
commita773d7982932342b8b93510419859cdceb22a980 (patch)
treeaa0193e217fd51962e3a97ff621e4aa9a6d0c0a6 /challenge-126/belmark-caday
parent1dd7de3e719483009105438314e713c41b076a4d (diff)
downloadperlweeklychallenge-club-a773d7982932342b8b93510419859cdceb22a980.tar.gz
perlweeklychallenge-club-a773d7982932342b8b93510419859cdceb22a980.tar.bz2
perlweeklychallenge-club-a773d7982932342b8b93510419859cdceb22a980.zip
- Added solutions by Belmark Caday.
Diffstat (limited to 'challenge-126/belmark-caday')
-rw-r--r--challenge-126/belmark-caday/perl/ch-1.pl18
-rw-r--r--challenge-126/belmark-caday/perl/ch-2.pl44
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-126/belmark-caday/perl/ch-1.pl b/challenge-126/belmark-caday/perl/ch-1.pl
new file mode 100644
index 0000000000..1ae4724034
--- /dev/null
+++ b/challenge-126/belmark-caday/perl/ch-1.pl
@@ -0,0 +1,18 @@
+sub countNumbers {
+ my $n = shift;
+ my $count = 0;
+
+ for ( $i = 1; $i <= $n; $i++ ) {
+ my @stringSplit = split(//, "$i");
+
+ my $j = 0;
+ foreach my $s ( @stringSplit ) {
+ $s == 1 ? last : $j++;
+ }
+ $count++ if $j == scalar @stringSplit; # We arrived at the end of the list without finding 1
+ }
+ print "Count is : $count \n";
+}
+
+countNumbers(25);
+
diff --git a/challenge-126/belmark-caday/perl/ch-2.pl b/challenge-126/belmark-caday/perl/ch-2.pl
new file mode 100644
index 0000000000..b89c70266a
--- /dev/null
+++ b/challenge-126/belmark-caday/perl/ch-2.pl
@@ -0,0 +1,44 @@
+sub mineSweeperMatrix {
+ my $matrix = shift;
+
+ for ( my $i = 0; $i < scalar @$matrix; $i++ ) {
+ for ( my $j =0; $j < scalar @{$matrix->[$i]}; $j++ ) {
+ if ( $matrix->[$i]->[$j] eq '*') {
+ my @loopList1 = ($i -1, $i, $i + 1);
+ my @loopList2 = ($j -1, $j, $j + 1);
+ my $mineCount = 0;
+
+ foreach my $lpLst1 ( @loopList1 ) {
+ next if $lpLst1 < 0;
+ if (defined $matrix->[$lpLst1]) {
+ foreach my $lpLst2 ( @loopList2 ) {
+ next if $lpLst2 < 0;
+ next unless defined $matrix->[$lpLst1]->[$lpLst2];
+ next if $lpLst2 == $j && $lpLst1 == $i;
+ $mineCount++ if $matrix->[$lpLst1]->[$lpLst2] eq 'x';
+ }
+ }
+ else {
+ next;
+ }
+ }
+
+ print $mineCount . ", ";
+ }
+ else {
+ print 'x, ';
+ }
+ }
+ print "\n";
+ }
+}
+
+my $testData = [
+ ['x', '*', '*', '*', 'x', '*', 'x', 'x', 'x', 'x'],
+ ['*', '*', '*', '*', '*', '*', '*', '*', '*', 'x'],
+ ['*', '*', '*', '*', 'x', '*', 'x', '*', 'x', '*'],
+ ['*', '*', '*', 'x', 'x', '*', '*', '*', '*', '*'],
+ ['x', '*', '*', '*', 'x', '*', '*', '*', '*', 'x']
+];
+
+mineSweeperMatrix($testData);