aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-06-02 14:25:15 +0100
committerGitHub <noreply@github.com>2021-06-02 14:25:15 +0100
commit51dc7d9ed809375ac2695386ed7bffbeaf8087b4 (patch)
treed67eed7a0a95077d75def1e90f6a8d636f349b61
parent789e98b3ca05ae16ebea4ce8b19e422d3a1b63f9 (diff)
parent713d167c054c92faff93c3b62b37887542783150 (diff)
downloadperlweeklychallenge-club-51dc7d9ed809375ac2695386ed7bffbeaf8087b4.tar.gz
perlweeklychallenge-club-51dc7d9ed809375ac2695386ed7bffbeaf8087b4.tar.bz2
perlweeklychallenge-club-51dc7d9ed809375ac2695386ed7bffbeaf8087b4.zip
Merge pull request #4191 from polettix/polettix/pwc115
Add polettix's solution to challenge-115
-rw-r--r--challenge-115/polettix/blog.txt1
-rw-r--r--challenge-115/polettix/blog1.txt1
-rw-r--r--challenge-115/polettix/perl/ch-1.pl57
-rw-r--r--challenge-115/polettix/perl/ch-2.pl19
-rw-r--r--challenge-115/polettix/raku/ch-1.raku62
-rw-r--r--challenge-115/polettix/raku/ch-2.raku17
6 files changed, 157 insertions, 0 deletions
diff --git a/challenge-115/polettix/blog.txt b/challenge-115/polettix/blog.txt
new file mode 100644
index 0000000000..ef4e04cb79
--- /dev/null
+++ b/challenge-115/polettix/blog.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2021/06/02/pwc115-string-chain/
diff --git a/challenge-115/polettix/blog1.txt b/challenge-115/polettix/blog1.txt
new file mode 100644
index 0000000000..2d8b2bdd0e
--- /dev/null
+++ b/challenge-115/polettix/blog1.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2021/06/03/pwc115-largest-multiple/
diff --git a/challenge-115/polettix/perl/ch-1.pl b/challenge-115/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..5a32d93c77
--- /dev/null
+++ b/challenge-115/polettix/perl/ch-1.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/env perl
+use 5.024;
+use warnings;
+use experimental qw< postderef signatures >;
+no warnings qw< experimental::postderef experimental::signatures >;
+
+sub string_chain (@S) {
+ my $start = shift @S;
+ my ($sf, $sl) = (substr($start, 0, 1), substr($start, -1, 1));
+
+ my %starting_with;
+ push $starting_with{substr $_, 0, 1}->@*, [$_, 0] for @S;
+
+ return unless exists $starting_with{$sl};
+ my @chain = ([$starting_with{$sl}, -1]);
+
+ LINK:
+ while ('necessary') {
+ my $top = $chain[-1];
+ if ((my $i = $top->[-1]) < $top->[0]->$#*) {
+ $top->[0][$i][1] = 0 if $i >= 0; # reset last iteration
+ ++$i; # advance at least once
+ ++$i while $i <= $top->[0]->$#* && $top->[0][$i][1];
+
+ $top->[-1] = $i;
+ redo LINK if $i > $top->[0]->$#*;
+
+ my $last_letter = substr $top->[0][$i][0], -1, 1;
+ if (@chain == @S) {
+ if ($last_letter eq $sf) {
+ return [
+ $start,
+ map {$_->[0][$_->[-1]][0]} @chain,
+ ];
+ }
+ }
+ else {
+ $top->[0][$i][1] = 1; # mark this item
+ if (my $sw = $starting_with{$last_letter}) {
+ push @chain, [$sw, -1]; # "recurse"
+ }
+ else {
+ return if $last_letter ne $sf;
+ }
+ }
+ }
+ elsif (@chain > 1) { pop @chain } # backtrack...
+ else { return } # no luck...
+ }
+}
+
+my @words = @ARGV ? @ARGV : qw< abc dea cd >;
+if (my $chain = string_chain(@words)) {
+ say 1;
+ say {*STDERR} join ' ', $chain->@*;
+}
+else { say 0 }
diff --git a/challenge-115/polettix/perl/ch-2.pl b/challenge-115/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..cbe0b35928
--- /dev/null
+++ b/challenge-115/polettix/perl/ch-2.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+use 5.024;
+use warnings;
+use experimental qw< postderef signatures >;
+no warnings qw< experimental::postderef experimental::signatures >;
+
+sub largest_multiple (@N) {
+ @N = sort { $a <=> $b } @N;
+ for my $i (0 .. $#N) {
+ if ($N[$i] % 2 == 0) {
+ my ($last) = splice @N, $i, 1;
+ return join '', reverse(@N), $last;
+ }
+ }
+ return;
+}
+
+my @inputs = @ARGV ? @ARGV : qw< 1 0 2 6 >;
+say largest_multiple(@inputs);
diff --git a/challenge-115/polettix/raku/ch-1.raku b/challenge-115/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..d14468c8a5
--- /dev/null
+++ b/challenge-115/polettix/raku/ch-1.raku
@@ -0,0 +1,62 @@
+#!/usr/bin/env raku
+use v6;
+
+sub string-chain (@S is copy) {
+ my $start = @S.shift;
+ my $sf = $start.substr(0, 1);
+ my $sl = $start.substr(*-1, 1);
+
+ my %starting-with;
+ for @S -> $s {
+ %starting-with{$s.substr(0, 1)}.push([$s, 0]);
+ }
+
+ return unless %starting-with{$sl};
+ my @chain = [%starting-with{$sl}, -1],;
+
+ LINK:
+ loop {
+ my $top = @chain[*-1];
+ if (my $i = $top[*-1]) < $top[0].elems - 1 {
+ $top[0][$i][1] = 0 if $i >= 0;
+ ++$i;
+ ++$i while $i < $top[0].elems && $top[0][$i][1];
+
+ $top[1] = $i;
+ redo LINK if $i > $top[0].elems - 1;
+
+ my $last_letter = $top[0][$i][0].substr(*-1,1);
+ if (@chain.elems == @S.elems) {
+ if ($last_letter eq $sf) {
+ return [
+ $start,
+ @chain.map: -> $x {$x[0][$x[*-1]][0]}
+ ];
+ }
+ }
+ else {
+ $top[0][$i][1] = 1;
+ if my $sw = %starting-with{$last_letter} {
+ @chain.push: [$sw, -1];
+ }
+ else {
+ return if $last_letter ne $sf;
+ }
+ }
+ }
+ elsif (@chain.elems > 1) { @chain.pop }
+ else { return }
+ }
+}
+
+sub MAIN (*@words is copy) {
+ @words = < abc dea cd > unless @words.elems;
+ my $chain = string-chain(@words);
+ if ($chain) {
+ say 1;
+ $chain.join(' ').note;
+ }
+ else {
+ say 0;
+ }
+}
diff --git a/challenge-115/polettix/raku/ch-2.raku b/challenge-115/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..5ed5755213
--- /dev/null
+++ b/challenge-115/polettix/raku/ch-2.raku
@@ -0,0 +1,17 @@
+#!/usr/bin/env raku
+use v6;
+
+sub largest-multiple (@N is copy) {
+ @N = @N.sort: {$^a leg $^b};
+ for 0 ..^ @N.elems -> $i {
+ next if @N[$i] % 2;
+ @N.unshift(@N.splice($i, 1).Slip);
+ return @N.reverse.join('');
+ }
+ return;
+}
+
+sub MAIN (*@inputs is copy) {
+ @inputs = < 1 0 2 6 > unless @inputs.elems;
+ say largest-multiple(@inputs);
+}