diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-04-14 19:05:41 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-04-19 18:48:49 +0200 |
| commit | 9010da0854b11ba07bcc8e11d9b6e47746f6629e (patch) | |
| tree | 8a33cd288a50ddc267e8debadc38b9c61f943e0e /challenge-054 | |
| parent | 587e0d7537f41e9c1e380085c57a965d4cb0f6fa (diff) | |
| download | perlweeklychallenge-club-9010da0854b11ba07bcc8e11d9b6e47746f6629e.tar.gz perlweeklychallenge-club-9010da0854b11ba07bcc8e11d9b6e47746f6629e.tar.bz2 perlweeklychallenge-club-9010da0854b11ba07bcc8e11d9b6e47746f6629e.zip | |
Challenge 054 task 2
Diffstat (limited to 'challenge-054')
| -rwxr-xr-x | challenge-054/jo-37/perl/ch-2.pl | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/challenge-054/jo-37/perl/ch-2.pl b/challenge-054/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..abc880dd15 --- /dev/null +++ b/challenge-054/jo-37/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use List::Gen; +use experimental 'signatures'; + +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 + print N's Collatz sequence + +EOS + + +### Input and Output + +# Append the final "one". +(gen_collatz(shift) + [1])->say; + + +### Implementation + +sub gen_collatz ($n) { + # Generator for N's Collatz sequence. + iterate {$_ % 2 ? $_ * 3 + 1 : $_ / 2}->from($n)->until('== 1'); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is gen_collatz(23)->apply, [23, 70, 35, 106, 53, 160, 80, 40, + 20, 10, 5, 16, 8, 4, 2], 'example'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
