aboutsummaryrefslogtreecommitdiff
path: root/challenge-115
diff options
context:
space:
mode:
authorSimon Proctor <simon.proctor@gmail.com>2021-06-14 16:49:18 +0100
committerSimon Proctor <simon.proctor@gmail.com>2021-06-14 16:49:18 +0100
commitd13d917ff2b3073d59ff60866e5af97a32be0a33 (patch)
treee2fe2a592ff25df5b83db95d25acd7834703b48c /challenge-115
parent33d755a877b0c514b6e900869577a3e741cc6d2b (diff)
parentdad6bcabbefc743b091695a82fcfb92342397e38 (diff)
downloadperlweeklychallenge-club-d13d917ff2b3073d59ff60866e5af97a32be0a33.tar.gz
perlweeklychallenge-club-d13d917ff2b3073d59ff60866e5af97a32be0a33.tar.bz2
perlweeklychallenge-club-d13d917ff2b3073d59ff60866e5af97a32be0a33.zip
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-115')
-rw-r--r--challenge-115/aaronreidsmith/blog.txt1
-rw-r--r--challenge-115/aaronreidsmith/raku/ch-1.raku32
-rw-r--r--challenge-115/aaronreidsmith/raku/ch-2.raku25
-rw-r--r--challenge-115/cheok-yin-fung/blog.txt2
-rw-r--r--challenge-115/paulo-custodio/perl/ch-1.pl46
-rw-r--r--challenge-115/paulo-custodio/perl/ch-2.pl43
-rw-r--r--challenge-115/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-115/paulo-custodio/t/test-2.yaml20
-rwxr-xr-xchallenge-115/paulo-custodio/test.pl7
9 files changed, 185 insertions, 1 deletions
diff --git a/challenge-115/aaronreidsmith/blog.txt b/challenge-115/aaronreidsmith/blog.txt
new file mode 100644
index 0000000000..c6fd3c9318
--- /dev/null
+++ b/challenge-115/aaronreidsmith/blog.txt
@@ -0,0 +1 @@
+https://aaronreidsmith.github.io/blog/perl-weekly-challenge-115/
diff --git a/challenge-115/aaronreidsmith/raku/ch-1.raku b/challenge-115/aaronreidsmith/raku/ch-1.raku
new file mode 100644
index 0000000000..b9a1d0813f
--- /dev/null
+++ b/challenge-115/aaronreidsmith/raku/ch-1.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+
+sub challenge(@S) returns Int {
+ my @solutions = @S.race.permutations.grep: -> @permutation {
+ my $valid = True;
+ for @permutation Z (|@permutation[1..*], @permutation.head) -> ($a, $b) {
+ if $a.comb.tail ne $b.comb.head {
+ $valid = False;
+ last;
+ }
+ }
+ $valid;
+ }
+ (@solutions.elems > 0).Int;
+}
+
+multi sub MAIN(*@S where all(@S) ~~ Str) {
+ say challenge(@S);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ (('abc', 'dea', 'cd'), 1),
+ (('ade', 'cbd', 'fgh'), 0)
+ );
+
+ for @tests -> (@S, $expected) {
+ is(challenge(@S), $expected);
+ }
+}
diff --git a/challenge-115/aaronreidsmith/raku/ch-2.raku b/challenge-115/aaronreidsmith/raku/ch-2.raku
new file mode 100644
index 0000000000..2f3dd8f59a
--- /dev/null
+++ b/challenge-115/aaronreidsmith/raku/ch-2.raku
@@ -0,0 +1,25 @@
+#!/usr/bin/env raku
+
+sub challenge(@N) returns Int {
+ @N.race.permutations.map(*.join.Int).grep(* %% 2).max;
+}
+
+multi sub MAIN(*@N where all(@N) ~~ /^<digit>$/) {
+ say challenge(@N);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ ((1, 0, 2, 6), 6210),
+ ((1, 4, 2, 8), 8412),
+ ((4, 1, 7, 6), 7614)
+ );
+
+ for @tests -> (@N, $expected) {
+ is(challenge(@N), $expected);
+ }
+
+ done-testing;
+}
diff --git a/challenge-115/cheok-yin-fung/blog.txt b/challenge-115/cheok-yin-fung/blog.txt
index 3cc4d8bb0f..6c43271e33 100644
--- a/challenge-115/cheok-yin-fung/blog.txt
+++ b/challenge-115/cheok-yin-fung/blog.txt
@@ -1 +1 @@
-file:///home/e78783/E7-87-83.github.io/coding/challenge_115.html
+https://e7-87-83.github.io/coding/challenge_115.html
diff --git a/challenge-115/paulo-custodio/perl/ch-1.pl b/challenge-115/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..6d28fbd2d6
--- /dev/null
+++ b/challenge-115/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+# Challenge 115
+#
+# TASK #1 - String Chain
+# Submitted by: Mohammad S Anwar
+# You are given an array of strings.
+#
+# Write a script to find out if the given strings can be chained to form a
+# circle. Print 1 if found otherwise 0.
+#
+# A string $S can be put before another string $T in circle if the last
+# character of $S is same as first character of $T.
+#
+# Examples:
+# Input: @S = ("abc", "dea", "cd")
+# Output: 1 as we can form circle e.g. "abc", "cd", "dea".
+#
+# Input: @S = ("ade", "cbd", "fgh")
+# Output: 0 as we can't form circle.
+
+use Modern::Perl;
+
+my @words = @ARGV;
+@words or die "Usage: ch-1.pl words...\n";
+
+say is_circle(\@words) ? 1 : 0;
+
+sub is_circle {
+ my($pending, @words) = @_;
+ my $found_solution;
+
+ if (@$pending == 0) {
+ $found_solution ||= substr($words[-1],-1,1) eq substr($words[0],0,1);
+ }
+ else {
+ for my $word (@$pending) {
+ if (@words == 0 ||
+ substr($words[-1],-1,1) eq substr($word,0,1)) {
+ my @new_pending = grep {$_ ne $word} @$pending;
+ $found_solution ||= is_circle(\@new_pending, @words, $word);
+ }
+ }
+ }
+ return $found_solution;
+}
diff --git a/challenge-115/paulo-custodio/perl/ch-2.pl b/challenge-115/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..afb6efc0ab
--- /dev/null
+++ b/challenge-115/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+
+# Challenge 115
+#
+# TASK #2 - Largest Multiple
+# Submitted by: Mohammad S Anwar
+# You are given a list of positive integers (0-9), single digit.
+#
+# Write a script to find the largest multiple of 2 that can be formed from the
+# list.
+#
+# Examples
+# Input: @N = (1, 0, 2, 6)
+# Output: 6210
+#
+# Input: @N = (1, 4, 2, 8)
+# Output: 8412
+#
+# Input: @N = (4, 1, 7, 6)
+# Output: 7614
+
+use Modern::Perl;
+
+my @nums = @ARGV;
+@nums or die "Usage: ch-1.pl words...\n";
+say largest_mult2(@nums);
+
+sub largest_mult2 {
+ my(@nums) = @_;
+
+ # select smallest even number for last element
+ my @even = sort {$a->[1] <=> $b->[1]}
+ grep {$_->[1] % 2 == 0}
+ map {[$_, $nums[$_]]} 0..$#nums;
+ return 0 if !@even; # no even numbers
+ my($index, $last) = @{$even[0]};
+ splice(@nums, $index, 1); # remove it from @nums
+
+ # sort the other elements in descending order
+ @nums = sort {$b <=> $a} @nums;
+
+ return 0+join('',@nums,$last);
+}
diff --git a/challenge-115/paulo-custodio/t/test-1.yaml b/challenge-115/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..017eb0af04
--- /dev/null
+++ b/challenge-115/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: abc dea cd
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: ade cbd fgh
+ input:
+ output: 0
diff --git a/challenge-115/paulo-custodio/t/test-2.yaml b/challenge-115/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..6acb5ad25d
--- /dev/null
+++ b/challenge-115/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 1 0 2 6
+ input:
+ output: 6210
+- setup:
+ cleanup:
+ args: 1 4 2 8
+ input:
+ output: 8412
+- setup:
+ cleanup:
+ args: 4 1 7 6
+ input:
+ output: 7614
+- setup:
+ cleanup:
+ args: 1 3 5 7
+ input:
+ output: 0
diff --git a/challenge-115/paulo-custodio/test.pl b/challenge-115/paulo-custodio/test.pl
new file mode 100755
index 0000000000..cf1ced98e0
--- /dev/null
+++ b/challenge-115/paulo-custodio/test.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.030;
+
+require '../../challenge-001/paulo-custodio/test.pl';