aboutsummaryrefslogtreecommitdiff
path: root/challenge-051
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-03-10 01:05:52 +0000
committerGitHub <noreply@github.com>2020-03-10 01:05:52 +0000
commit8b6c6d1625ed1cc46d062cf609fff0ff03d882e9 (patch)
tree57ffe45f623c4d818ff19df03335dfb723f1aca2 /challenge-051
parent705ede35a8b266fab54eeacab7cc92d102fa6b22 (diff)
parent47d0403b9274a31bb7616ea88f9ee9591f07a477 (diff)
downloadperlweeklychallenge-club-8b6c6d1625ed1cc46d062cf609fff0ff03d882e9.tar.gz
perlweeklychallenge-club-8b6c6d1625ed1cc46d062cf609fff0ff03d882e9.tar.bz2
perlweeklychallenge-club-8b6c6d1625ed1cc46d062cf609fff0ff03d882e9.zip
Merge pull request #1389 from waltman/branch-for-challenge-051
Branch for challenge 051
Diffstat (limited to 'challenge-051')
-rw-r--r--challenge-051/walt-mankowski/perl/ch-1.pl39
-rw-r--r--challenge-051/walt-mankowski/perl/ch-2.pl33
2 files changed, 72 insertions, 0 deletions
diff --git a/challenge-051/walt-mankowski/perl/ch-1.pl b/challenge-051/walt-mankowski/perl/ch-1.pl
new file mode 100644
index 0000000000..2bdaf6c792
--- /dev/null
+++ b/challenge-051/walt-mankowski/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.30);
+use experimental qw(signatures);
+use Algorithm::Combinatorics qw(combinations);
+use List::Util qw(sum);
+
+# 3 Sum
+# Given an array @Lof integers. Write a script to find all unique
+# triplets such that a + b + c is same as the given target T. Also
+# make sure a <= b <= c.
+#
+# Here is wiki page for more information:
+# https://en.wikipedia.org/wiki/3SUM
+#
+# Example:
+#
+# @L = (-25, -10, -7, -3, 2, 4, 8, 10);
+#
+# One such triplet for target 0 i.e. -10 + 2 + 8 = 0.
+
+my @L = (-25, -10, -7, -3, 2, 4, 8, 10);
+@L = sort {$a <=> $b} @L;
+my $target = 0;
+my $iter = combinations(\@L, 3);
+while (my $p = $iter->next) {
+ say prettyprint($p, $target) if sum(@$p) == $target;
+}
+
+sub prettyprint($p, $target) {
+ my $s = $p->[0];
+ for my $i (1..$#$p) {
+ $s .= $p->[$i] >= 0 ? ' + ' : ' - ';
+ $s .= abs($p->[$i]);
+ }
+ $s .= " = $target";
+ return $s;
+}
diff --git a/challenge-051/walt-mankowski/perl/ch-2.pl b/challenge-051/walt-mankowski/perl/ch-2.pl
new file mode 100644
index 0000000000..756b3c2a4c
--- /dev/null
+++ b/challenge-051/walt-mankowski/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.30);
+use experimental qw(signatures);
+use List::Util qw(product);
+
+# Colourful Number
+# Write a script to display all Colorful Number with 3 digits.
+#
+# A number can be declare Colorful Number where all the products of
+# consecutive subsets of digit are different.
+#
+# For example, 263 is a Colorful Number since 2, 6, 3, 2x6, 6x3, 2x6x3 are unique.
+
+sub is_colorful($n) {
+ my %prods;
+ for my $len (1..length($n)) {
+ for my $i (0..length($n) - $len) {
+ my $p = product split //, substr($n, $i, $len);
+ if (defined $prods{$p}) {
+ return 0;
+ } else {
+ $prods{$p} = 1;
+ }
+ }
+ }
+ return 1;
+}
+
+for my $n (100..999) {
+ say $n if is_colorful($n);
+}