diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-03 10:30:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-03 10:30:19 +0100 |
| commit | 749f4e2409514122fb49f7b9f83f2c0012a04c7a (patch) | |
| tree | 49203621d772b7a70a69340015640c311bf5f781 | |
| parent | 43b27153c554a1803df441769d16149997394728 (diff) | |
| parent | 08d33d6bf0c12c4e9658ad4fefb28872d84c35ed (diff) | |
| download | perlweeklychallenge-club-749f4e2409514122fb49f7b9f83f2c0012a04c7a.tar.gz perlweeklychallenge-club-749f4e2409514122fb49f7b9f83f2c0012a04c7a.tar.bz2 perlweeklychallenge-club-749f4e2409514122fb49f7b9f83f2c0012a04c7a.zip | |
Merge pull request #4393 from jo-37/contrib
Solutions to challenge 119
| -rwxr-xr-x | challenge-119/jo-37/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-119/jo-37/perl/ch-2.pl | 73 |
2 files changed, 137 insertions, 0 deletions
diff --git a/challenge-119/jo-37/perl/ch-1.pl b/challenge-119/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..b551210f44 --- /dev/null +++ b/challenge-119/jo-37/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; + +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 ... + swap nibbles in N + +EOS + + +### Input and Output + +say swap_nibbles($_) for @ARGV; + + +### Implementation + +# This works portably for N < 2**32 and potentially up to N < 2**64. No +# need to restrict to N < 256. +sub swap_nibbles { + no warnings 'portable'; + hex unpack 'h*', pack 'J>', shift; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is swap_nibbles(101), 86, 'example 1'; + is swap_nibbles(18), 33, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is swap_nibbles(0), 0, 'all zero'; + is swap_nibbles(255), 255, 'all one'; + is swap_nibbles(129), 24, 'swap outer bits to inner bits'; + is swap_nibbles(0xfedc), 0xefcd, '16 bit'; + is swap_nibbles(0xfedcba98), 0xefcdab89, '32 bit'; + no warnings 'portable'; + is swap_nibbles(0xfedcba9876543210), 0xefcdab8967452301, '64 bit'; + } + + done_testing; + exit; +} 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; +} |
