aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Wilson <steven1170@zoho.eu>2019-10-14 14:37:29 +0100
committerSteven Wilson <steven1170@zoho.eu>2019-10-14 14:37:29 +0100
commit4cdc285ea33ba29ad70b31326c8deb0f68d40df0 (patch)
tree9f41b06667d9383ab4b684b92a7338a8b060cca7
parent45c4b2f1edb37ad26bf8ac1f14b0c170ea21b2b6 (diff)
downloadperlweeklychallenge-club-4cdc285ea33ba29ad70b31326c8deb0f68d40df0.tar.gz
perlweeklychallenge-club-4cdc285ea33ba29ad70b31326c8deb0f68d40df0.tar.bz2
perlweeklychallenge-club-4cdc285ea33ba29ad70b31326c8deb0f68d40df0.zip
add solution to week 30 task 2
-rw-r--r--challenge-030/steven-wilson/perl5/ch-2.pl41
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-030/steven-wilson/perl5/ch-2.pl b/challenge-030/steven-wilson/perl5/ch-2.pl
new file mode 100644
index 0000000000..22d4822601
--- /dev/null
+++ b/challenge-030/steven-wilson/perl5/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+# Author: Steven Wilson
+# Date: 2019-10-14
+# Week: 030
+
+# Task #2
+# Write a script to print all possible series of 3 positive numbers,
+# where in each series at least one of the number is even and sum of the
+# three numbers is always 12. For example, 3,4,5.
+
+# Assumptions: 3,4,5 and 4,3,5 are distinct 'series'.
+
+use strict;
+use warnings;
+use feature qw/ say /;
+use List::Util qw/ sum any /;
+
+my %series = ();
+
+for my $i ( 1 .. 10 ) {
+ for my $j ( 1 .. 10 ) {
+ my $k = 12 - sum( $i, $j );
+ if ( $k > 0 ) {
+ if ( !exists $series{"$i, $j, $k"} ) {
+ my $bool = any { $_ % 2 == 0 } ( $i, $j, $k );
+ if ($bool) {
+ $series{"$i, $j, $k"} = 1;
+ $series{"$i, $k, $j"} = 1;
+ $series{"$j, $k, $i"} = 1;
+ $series{"$j, $i, $k"} = 1;
+ $series{"$k, $i, $j"} = 1;
+ $series{"$k, $j, $i"} = 1;
+ }
+ }
+ }
+ }
+}
+
+say join "\n", sort keys %series;
+my $series_size = keys %series;
+say "\nThere were $series_size possible series of 3 positive numbers.";