diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-02 17:00:24 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-02 17:00:24 +0000 |
| commit | da0ee0adda2ed32b3211a32213944eb8a997a2b4 (patch) | |
| tree | c93f2b91773bd290750e52a57693b1e7440047df | |
| parent | 89a7611a540473d18a9ccec182bec61c2909ce01 (diff) | |
| parent | fa8aa140a33674576b3021412c2de5aeaa2ebd08 (diff) | |
| download | perlweeklychallenge-club-da0ee0adda2ed32b3211a32213944eb8a997a2b4.tar.gz perlweeklychallenge-club-da0ee0adda2ed32b3211a32213944eb8a997a2b4.tar.bz2 perlweeklychallenge-club-da0ee0adda2ed32b3211a32213944eb8a997a2b4.zip | |
Merge pull request #9505 from jo-37/contrib
Solutions to challenge 254
| -rw-r--r-- | challenge-254/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-254/jo-37/perl/ch-1.pl | 61 | ||||
| -rwxr-xr-x | challenge-254/jo-37/perl/ch-2.pl | 69 |
3 files changed, 131 insertions, 0 deletions
diff --git a/challenge-254/jo-37/blog.txt b/challenge-254/jo-37/blog.txt new file mode 100644 index 0000000000..cc38eab30b --- /dev/null +++ b/challenge-254/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/blog/pwc/challenge-254 diff --git a/challenge-254/jo-37/perl/ch-1.pl b/challenge-254/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..d0478f94d5 --- /dev/null +++ b/challenge-254/jo-37/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV == 1; +usage: $0 [-examples] [-tests] [N] + +-examples + run the examples from the challenge + +-tests + run some tests + +N + a non-negative integer + +EOS + + +### Input and Output + +say qw(false true)[is_pow_3(shift)]; + + +### Implementation + +sub is_pow_3 ($n) { + $n /= 3 while $n > 2 && !($n % 3); + $n == 1; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + ok is_pow_3(27), 'example 1'; + ok !is_pow_3(0), 'example 2'; + ok !is_pow_3(6), 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + for (my $n = 1; $n < 1e12; $n *= 3) { + ok is_pow_3($n), $n; + ok !is_pow_3($n + 3), $n + 3; + } + } + + done_testing; + exit; +} diff --git a/challenge-254/jo-37/perl/ch-2.pl b/challenge-254/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..919efb3900 --- /dev/null +++ b/challenge-254/jo-37/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use Unicode::Normalize; +use utf8; +use experimental 'refaliasing'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV == 1; +usage: $0 [-examples] [-tests] [STR] + +-examples + run the examples from the challenge + +-tests + run some tests + +STR + a string + +EOS + + +### Input and Output + +say reverse_vowels(shift); + + +### Implementation + +sub reverse_vowels { + my @arr = NFD(shift) =~ /\X/g; + \(my @vow) = map /[aeiou]/i ? \$_ : (), @arr; + \(my @up) = map /\p{Lu}/ ? \$_ : (), @vow; + + @vow[0 .. $#vow] = map lc, reverse @vow; + @up[0 .. $#up] = map uc, @up; + + NFC(join '', @arr); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is reverse_vowels('Raku'), 'Ruka', 'example 1'; + is reverse_vowels('Perl'), 'Perl', 'example 2'; + is reverse_vowels('Julia'), 'Jaliu', 'example 3'; + is reverse_vowels('Uiua'), 'Auiu', 'example 4'; + } + + SKIP: { + skip "tests" unless $tests; + + is reverse_vowels('tAvowixUyez'), 'tEvuwixOyaz', 'multiple caps'; + is reverse_vowels('Öl Tuba Tür não Ähre Ångström Bär'), + 'Äl Töbå Ter näo Ãhrü Angstrum Bör', 'modified vowels'; + } + + done_testing; + exit; +} |
