aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2021-06-28 14:59:22 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2021-07-02 15:50:55 +0200
commit7227c3e91b9989b187085de5a565fad8d0779715 (patch)
tree6ee2b27d80593b1ade211346e84aa815758ddbb1
parent77625ea9400c81e01f661efa8c55ab09b5452359 (diff)
downloadperlweeklychallenge-club-7227c3e91b9989b187085de5a565fad8d0779715.tar.gz
perlweeklychallenge-club-7227c3e91b9989b187085de5a565fad8d0779715.tar.bz2
perlweeklychallenge-club-7227c3e91b9989b187085de5a565fad8d0779715.zip
Solution to task 2
-rwxr-xr-xchallenge-119/jo-37/perl/ch-2.pl73
1 files changed, 73 insertions, 0 deletions
diff --git a/challenge-119/jo-37/perl/ch-2.pl b/challenge-119/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..fc3ac4c238
--- /dev/null
+++ b/challenge-119/jo-37/perl/ch-2.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use Math::Cartesian::Product;
+use experimental qw(signatures postderef);
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [N ...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N ...
+ Find the N-th element of the "CY sequence"
+
+EOS
+
+
+### Input and Output
+
+say cy($_) for @ARGV;
+
+
+### Implementation
+
+# Find the n-th element of the CY sequence.
+sub cy ($n) {
+ # CY sequence known so far.
+ state @cy;
+ # Current number of digits.
+ state $digits;
+
+ # Augment the calculated CY sequence by blocks having an increasing
+ # number of digits if the requested element isn't in the list yet.
+ local $" = '';
+ push @cy,
+ cartesian {"@_" !~ /11/} ([1 .. 3]) x ++$digits
+ while $n > @cy;
+
+ # Join the digits of the n-th element.
+ "$cy[$n - 1]->@*";
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is cy(5), 13, 'example 1';
+ is cy(10), 32, 'example 2';
+ is cy(60), 2223, 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is cy(11), 33, 'last 2-digit element';
+ is cy(12), 121, 'first 3-digit element';
+ }
+
+ done_testing;
+ exit;
+}