aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-08 01:53:01 +0100
committerGitHub <noreply@github.com>2020-09-08 01:53:01 +0100
commit99bc202217c73c40bc6345abbdce4736dcabf55e (patch)
tree24c89b37944b52664d08881a0b2640f1bd7bb2f8
parentae1ebad31c6817b3a39221d3decc0497532da24b (diff)
parentab99807ea1b969897bd307a5ccd5f54eb253b7a9 (diff)
downloadperlweeklychallenge-club-99bc202217c73c40bc6345abbdce4736dcabf55e.tar.gz
perlweeklychallenge-club-99bc202217c73c40bc6345abbdce4736dcabf55e.tar.bz2
perlweeklychallenge-club-99bc202217c73c40bc6345abbdce4736dcabf55e.zip
Merge pull request #2234 from waltman/branch-for-challenge-077
Branch for challenge 077
-rw-r--r--challenge-077/walt-mankowski/blog.txt1
-rw-r--r--challenge-077/walt-mankowski/perl/ch-1.pl65
-rw-r--r--challenge-077/walt-mankowski/perl/ch-2.pl75
-rw-r--r--challenge-077/walt-mankowski/perl/test1.txt3
-rw-r--r--challenge-077/walt-mankowski/perl/test2.txt4
5 files changed, 148 insertions, 0 deletions
diff --git a/challenge-077/walt-mankowski/blog.txt b/challenge-077/walt-mankowski/blog.txt
new file mode 100644
index 0000000000..e9fe0121e5
--- /dev/null
+++ b/challenge-077/walt-mankowski/blog.txt
@@ -0,0 +1 @@
+http://www.mawode.com/blog/blog/2020/09/07/perl-weekly-challenge-77/
diff --git a/challenge-077/walt-mankowski/perl/ch-1.pl b/challenge-077/walt-mankowski/perl/ch-1.pl
new file mode 100644
index 0000000000..fed027eea9
--- /dev/null
+++ b/challenge-077/walt-mankowski/perl/ch-1.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(signatures);
+use List::Util qw(sum);
+use Algorithm::Combinatorics qw(subsets);
+
+# TASK #1 › Fibonacci Sum
+# Submitted by: Mohammad S Anwar
+#
+# You are given a positive integer $N.
+# UPDATE: 2020-09-07 09:00:00
+#
+# Write a script to find out all possible combination of Fibonacci
+# Numbers required to get $N on addition.
+#
+# You are NOT allowed to repeat a number. Print 0 if none found.
+# Example 1:
+#
+# Input: $N = 6
+#
+# Output:
+# 1 + 2 + 3 = 6
+# 1 + 5 = 6
+#
+# Example 2:
+#
+# Input: $N = 9
+#
+# Output:
+# 1 + 8 = 9
+# 1 + 3 + 5 = 9
+
+sub fibs_upto($n) {
+ if ($n == 1) {
+ return (1);
+ } else {
+ my @fibs = (1,2);
+ while (1) {
+ my $next = $fibs[-2] + $fibs[-1];
+ if ($next <= $n) {
+ push @fibs, $next;
+ } else {
+ return @fibs;
+ }
+ }
+ }
+}
+
+my $n = $ARGV[0];
+my @fibs = fibs_upto($n);
+my $iter = subsets(\@fibs);
+my $found = 0;
+while (my $p = $iter->next) {
+ next unless $p->@*;
+ local $, = ' + ';
+ if (sum($p->@*) == $n) {
+ say $p->@*;
+ $found = 1;
+ }
+}
+
+say 0 unless $found;
+
diff --git a/challenge-077/walt-mankowski/perl/ch-2.pl b/challenge-077/walt-mankowski/perl/ch-2.pl
new file mode 100644
index 0000000000..1f31e4a986
--- /dev/null
+++ b/challenge-077/walt-mankowski/perl/ch-2.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(signatures);
+use List::Util qw(sum);
+use autodie;
+
+# TASK #2 › Lonely X
+# Submitted by: Mohammad S Anwar
+#
+# You are given m x n character matrix consists of O and X only.
+#
+# Write a script to count the total number of X surrounded by O
+# only. Print 0 if none found.
+
+# read in the matrix and add a layer of 0s around the outside
+sub parse_input($fname) {
+ my @grid;
+
+ open my $fh, '<', $fname;
+ my $row = 0;
+ my $width = 0;
+ while (my $line = <$fh>) {
+ # remove whitespace;
+ $line =~ s/\s//g;
+
+ # convert to array
+ my @c = split //, $line;
+
+ # add top row if needed
+ unless (@grid) {
+ $width = 2 + @c;
+ for my $col (0..$width-1) {
+ $grid[$row][$col] = 0;
+ }
+ $row = 1;
+ }
+ $grid[$row][0] = 0;
+ for my $i (0..$#c) {
+ $grid[$row][$i+1] = $c[$i] eq 'X' ? 1 : 0;
+ }
+ $grid[$row][$width-1] = 0;
+ $row++;
+ }
+
+ # make bottom line
+ for my $col (0..$width-1) {
+ $grid[$row][$col] = 0;
+ }
+
+ # return the grid
+ return \@grid;
+}
+
+my $fname = $ARGV[0];
+my $grid = parse_input($fname);
+
+my $rows = $grid->@*;
+my $cols = $grid->[0]->@*;
+
+my $count = 0;
+for my $row (1..$rows-2) {
+ for my $col (1..$cols-2) {
+ next unless $grid->[$row][$col] == 1;
+ if (sum($grid->[$row-1]->@[$col-1..$col+1]) +
+ sum($grid->[$row]->@[$col-1..$col+1]) +
+ sum($grid->[$row+1]->@[$col-1..$col+1]) == 1) {
+ $count++;
+ }
+ }
+}
+
+say $count;
+
diff --git a/challenge-077/walt-mankowski/perl/test1.txt b/challenge-077/walt-mankowski/perl/test1.txt
new file mode 100644
index 0000000000..f8beeb613f
--- /dev/null
+++ b/challenge-077/walt-mankowski/perl/test1.txt
@@ -0,0 +1,3 @@
+O O X
+X O O
+X O O
diff --git a/challenge-077/walt-mankowski/perl/test2.txt b/challenge-077/walt-mankowski/perl/test2.txt
new file mode 100644
index 0000000000..d81104f1b2
--- /dev/null
+++ b/challenge-077/walt-mankowski/perl/test2.txt
@@ -0,0 +1,4 @@
+O O X O
+X O O O
+X O O X
+O X O O