diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-03 21:30:02 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-03 21:30:02 +0000 |
| commit | 18bdd1a6f94508781a2ce9345d3bd929de3ade1e (patch) | |
| tree | c4ddd6a7110ff9bbbc76082f769e418258278854 | |
| parent | ac34a19729fde5b6f451ef137b6c95f76f2e231d (diff) | |
| parent | 396f6af0fa102fd4869e81ec18b80f22e282679d (diff) | |
| download | perlweeklychallenge-club-18bdd1a6f94508781a2ce9345d3bd929de3ade1e.tar.gz perlweeklychallenge-club-18bdd1a6f94508781a2ce9345d3bd929de3ade1e.tar.bz2 perlweeklychallenge-club-18bdd1a6f94508781a2ce9345d3bd929de3ade1e.zip | |
Merge pull request #9511 from boblied/w254
W254 solutions from Bob Lied
| -rw-r--r-- | challenge-254/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-254/bob-lied/blog.txt | 2 | ||||
| -rw-r--r-- | challenge-254/bob-lied/perl/ch-1.pl | 67 | ||||
| -rw-r--r-- | challenge-254/bob-lied/perl/ch-2.pl | 55 |
4 files changed, 127 insertions, 3 deletions
diff --git a/challenge-254/bob-lied/README b/challenge-254/bob-lied/README index af6f9a022d..648108e446 100644 --- a/challenge-254/bob-lied/README +++ b/challenge-254/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 253 by Bob Lied +Solutions to weekly challenge 254 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-253/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-253/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-254/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-254/bob-lied diff --git a/challenge-254/bob-lied/blog.txt b/challenge-254/bob-lied/blog.txt new file mode 100644 index 0000000000..b0204b394a --- /dev/null +++ b/challenge-254/bob-lied/blog.txt @@ -0,0 +1,2 @@ +https://dev.to/boblied/pwc-254-task-2-i-too-wish-to-make-the-vowel-movement-joke-11b2 +https://dev.to/boblied/pwc-254-task-1-three-power-3i7a diff --git a/challenge-254/bob-lied/perl/ch-1.pl b/challenge-254/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..cabc8fc6b6 --- /dev/null +++ b/challenge-254/bob-lied/perl/ch-1.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-1.pl Perl Weekly Challenge 254 Task 1 Three Power +#============================================================================= +# You are given a positive integer, $n. Write a script to return true if +# the given integer is a power of three otherwise return false. +# Example 1 Input: $n = 27 Output: true +# Example 2 Input: $n = 0 Output: true +# Example 3 Input: $n = 6 Output: false +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say isPof3(shift) ? "true" : "false"; + +sub isPof3($n) +{ + if ( $n > 0 ) { $n /= 3 while ( $n % 3 == 0 ); + return $n == 1; + } + elsif ( $n == 0 ) { return true } + else { return false; } +} + +sub runTest +{ + use Test2::V0; + use builtin qw/true false/; no warnings "experimental::builtin"; + + is( isPof3(27), true, "Example 1"); + is( isPof3( 0), true, "Example 2"); + is( isPof3( 6), false, "Example 3"); + + my $p3 = Power->new(base => 3); + is( $p3->isPowerOf(27), true, "Example 1 with class"); + is( $p3->isPowerOf( 0), true, "Example 2 with class"); + is( $p3->isPowerOf( 6), false, "Example 3 with class"); + + done_testing; +} + +use feature "class"; no warnings "experimental::class"; + +class Power +{ + field $base :param = 10; + + method isPowerOf($n) { + if ( $n > 0 ) { $n /= $base while ( $n % $base == 0 ); + return $n == 1; + } + elsif ( $n == 0 ) { return true } + else { return false; } + } +} diff --git a/challenge-254/bob-lied/perl/ch-2.pl b/challenge-254/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..55e3e6ceb9 --- /dev/null +++ b/challenge-254/bob-lied/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# +# ch-2.pl Perl Weekly Challenge 254 Task 2 Reverse Vowels +#============================================================================= +# You are given a string, $s. Write a script to reverse all the vowels +# (a, e, i, o, u) in the given string. +# Example 1 Input: $s = "Raku" Output: "Ruka" +# Example 2 Input: $s = "Perl" Output: "Perl" +# Example 3 Input: $s = "Julia" Output: "Jaliu" +# Example 4 Input: $s = "Uiua" Output: "Auiu" +#============================================================================= + +use v5.38; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say revVow($_) for @ARGV; + +sub revVow($s) +{ + state $isVowel = qr/[aeiou]/i; + + my @v = $s =~ m/$isVowel/g; + my $rev; + for ( split(//, $s) ) + { + my $next = ( /$isVowel/ ? pop @v : $_ ); + $rev .= ( /\p{Uppercase}/ ? uc $next : lc $next ); + } + return $rev; +} + +sub runTest +{ + use Test2::V0; + + is( revVow("Raku"), "Ruka", "Example 1"); + is( revVow("Perl"), "Perl", "Example 2"); + is( revVow("Julia"), "Jaliu", "Example 3"); + is( revVow("Uiua"), "Auiu", "Example 4"); + is( revVow("AEIOU"), "UOIEA", "Odd length"); + is( revVow("aEiOu"), "uOiEa", "Retain casing 1"); + is( revVow("AeIo"), "OiEa", "Retain casing 2"); + + done_testing; +} |
