diff options
| author | rir <rirans@comcast.net> | 2024-02-03 21:04:52 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-03 21:04:52 -0500 |
| commit | a3f78f2b0fec122fe2272e1bbe1febbebac4fc36 (patch) | |
| tree | efc9f25b23d90907e8ca7d32eae5e90477665019 /challenge-254 | |
| parent | 7b687542caa8eae5659709a8ca6e63779d9b5197 (diff) | |
| parent | 246fff16e6e480b6e2c031b92b46c52d3e5ce0df (diff) | |
| download | perlweeklychallenge-club-a3f78f2b0fec122fe2272e1bbe1febbebac4fc36.tar.gz perlweeklychallenge-club-a3f78f2b0fec122fe2272e1bbe1febbebac4fc36.tar.bz2 perlweeklychallenge-club-a3f78f2b0fec122fe2272e1bbe1febbebac4fc36.zip | |
Merge branch 'manwar:master' into 254
Diffstat (limited to 'challenge-254')
| -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 | ||||
| -rw-r--r-- | challenge-254/lubos-kolouch/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-254/lubos-kolouch/perl/ch-2.pl | 34 | ||||
| -rw-r--r-- | challenge-254/lubos-kolouch/python/ch-1.py | 35 | ||||
| -rw-r--r-- | challenge-254/lubos-kolouch/python/ch-2.py | 45 | ||||
| -rw-r--r-- | challenge-254/lubos-kolouch/raku/ch-1.raku | 20 | ||||
| -rw-r--r-- | challenge-254/lubos-kolouch/raku/ch-2.raku | 26 | ||||
| -rw-r--r-- | challenge-254/massa/raku/ch-1.raku | 68 | ||||
| -rw-r--r-- | challenge-254/massa/raku/ch-2.raku | 67 | ||||
| -rw-r--r-- | challenge-254/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-254/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-254/sgreen/perl/ch-1.pl | 19 | ||||
| -rwxr-xr-x | challenge-254/sgreen/perl/ch-2.pl | 26 | ||||
| -rwxr-xr-x | challenge-254/sgreen/python/ch-1.py | 23 | ||||
| -rwxr-xr-x | challenge-254/sgreen/python/ch-2.py | 31 | ||||
| -rwxr-xr-x | challenge-254/sgreen/python/test.py | 22 |
19 files changed, 574 insertions, 5 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; +} diff --git a/challenge-254/lubos-kolouch/perl/ch-1.pl b/challenge-254/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..74eecdd393 --- /dev/null +++ b/challenge-254/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; +use feature 'say'; + +sub is_power_of_three { + my ($n) = @_; + + # Documentation + # Determines if the given number is a power of three. + # Params: + # n (Integer): A positive integer + # Returns: + # A boolean value: True if n is a power of three, False otherwise + + if ( $n == 0 ) { + return 1; + } + while ( $n % 3 == 0 ) { + $n /= 3; + } + return $n == 1; +} + +# Tests +die "Test failed!" unless is_power_of_three(27) == 1; +die "Test failed!" unless is_power_of_three(0) == 1; +die "Test failed!" unless is_power_of_three(6) == 0; +say "All tests passed!"; diff --git a/challenge-254/lubos-kolouch/perl/ch-2.pl b/challenge-254/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..09c4472e84 --- /dev/null +++ b/challenge-254/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,34 @@ +use strict; +use warnings; + +sub reverse_vowels { + my ($s) = @_; + my @vowels = reverse( $s =~ /[aeiou]/ig ); + my @chars = split //, $s; + + # Replace vowels in the string with reversed vowels + for my $i ( 0 .. $#chars ) { + if ( $chars[$i] =~ /[aeiouAEIOU]/ ) { + my $replacement = shift @vowels; + + # Adjusting case of the replacement vowel to match original + if ( $chars[$i] =~ /[AEIOU]/ ) { + $replacement = uc($replacement); + } + else { + $replacement = lc($replacement); + } + $chars[$i] = $replacement; + } + } + + return join '', @chars; +} + +# Tests +use Test::More; +ok( reverse_vowels("Raku") eq "Ruka", 'Test Raku' ); +ok( reverse_vowels("Perl") eq "Perl", 'Test Perl' ); +ok( reverse_vowels("Julia") eq "Jaliu", 'Test Julia' ); +ok( reverse_vowels("Uiua") eq "Auiu", 'Test Uiua' ); +done_testing(); diff --git a/challenge-254/lubos-kolouch/python/ch-1.py b/challenge-254/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..8a3cb00ca7 --- /dev/null +++ b/challenge-254/lubos-kolouch/python/ch-1.py @@ -0,0 +1,35 @@ +def is_power_of_three(n: int) -> bool: + """ + Checks if a positive integer is a power of three. + + Args: + n: The positive integer to check. + + Returns: + True if n is a power of three, False otherwise. + + Raises: + ValueError: If n is not a positive integer. + """ + + if n <= 0: + raise ValueError("Input must be a positive integer.") + + # If n is 1, it's trivially a power of three + if n == 1: + return True + + # Repeatedly divide n by 3 until it reaches 1 or becomes negative + while n % 3 == 0: + n //= 3 + + # If n is 1 after the division, it was a power of three + return n == 1 + + +# Assert tests (corrected) +assert is_power_of_three(27) is True +assert is_power_of_three(81) is True +assert is_power_of_three(6) is False + +print("All tests passed!") diff --git a/challenge-254/lubos-kolouch/python/ch-2.py b/challenge-254/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..917c60787a --- /dev/null +++ b/challenge-254/lubos-kolouch/python/ch-2.py @@ -0,0 +1,45 @@ +def reverse_vowels(s: str) -> str: + """ + Reverse the vowels in a given string while preserving the case. + + Args: + s (str): The input string. + + Returns: + str: The string with vowels reversed. + + Example: + >>> reverse_vowels("Raku") + 'Ruka' + """ + vowels = "aeiouAEIOU" + # Extract vowels from the string in reverse order + reverse_vowel_list = [char for char in s if char in vowels][::-1] + + # Replace vowels in the string with reversed vowels + result = [] + reversed_vowels_iter = iter(reverse_vowel_list) + for char in s: + if char in vowels: + # Ensure the case of the vowel is preserved + next_vowel = next(reversed_vowels_iter, char) + result.append( + next_vowel.swapcase() + if char.isupper() != next_vowel.isupper() + else next_vowel + ) + else: + result.append(char) + return "".join(result) + + +# Tests +assert reverse_vowels("Raku") == "Ruka" +assert reverse_vowels("Perl") == "Perl" +assert reverse_vowels("Julia") == "Jaliu" +assert reverse_vowels("Uiua") == "Auiu" + +# Run the function with the test cases +if __name__ == "__main__": + for test in ["Raku", "Perl", "Julia", "Uiua"]: + print(f"{test} -> {reverse_vowels(test)}") diff --git a/challenge-254/lubos-kolouch/raku/ch-1.raku b/challenge-254/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..b338046330 --- /dev/null +++ b/challenge-254/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,20 @@ +sub is-power-of-three(Int $n --> Bool) { + # Documentation + # Determines if the given number is a power of three. + # Params: + # n (Int): A positive integer + # Returns: + # Bool: True if n is a power of three, False otherwise + + return True if $n == 0; + while $n %% 3 { + $n div= 3; + } + return $n == 1; +} + +# Tests +die "Test failed!" unless is-power-of-three(27) == True; +die "Test failed!" unless is-power-of-three(0) == True; +die "Test failed!" unless is-power-of-three(6) == False; +say "All tests passed!"; diff --git a/challenge-254/lubos-kolouch/raku/ch-2.raku b/challenge-254/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..49399b4a53 --- /dev/null +++ b/challenge-254/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,26 @@ +sub reverse-vowels(Str $s) returns Str { + my @vowels = $s.comb(/<[aeiouAEIOU]>/).reverse; + my $vowel-index = 0; + + # Replace vowels in the string with reversed vowels + $s ~~ s:g/<[aeiouAEIOU]>/ + { + my $replacement = @vowels[$vowel-index++]; + # Adjust the case of the replacement to match the original + if $0 ~~ /<:Lu>/ { + $replacement.tc; + } else { + $replacement.lc; + } + }; + + return $s; +} + +# Tests +use Test; +plan 4; +is reverse-vowels("Raku"), "Ruka", 'Test Raku'; +is reverse-vowels("Perl"), "Perl", 'Test Perl'; +is reverse-vowels("Julia"), "Jaliu", 'Test Julia'; +is reverse-vowels("Uiua"), "Auiu", 'Test Uiua'; diff --git a/challenge-254/massa/raku/ch-1.raku b/challenge-254/massa/raku/ch-1.raku new file mode 100644 index 0000000000..2cd6a3f23f --- /dev/null +++ b/challenge-254/massa/raku/ch-1.raku @@ -0,0 +1,68 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 1: Three Power + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +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. + +=head3 Example 1: + + Input: $n = 27 + Output: true + + 27 = 3 ^ 3 + +=head3 Example 2: + + Input: $n = 0 + Output: true + + 0 = 0 ^ 3 + +=head3 Example 3: + + Input: $n = 6 + Output: false + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION($n) { + (^∞).map(3 ** *).first(* >= $n) == $n +} + +multi MAIN (Bool :$test!) { + use Testo; + + my @tests = + %{ input => 27, + output => True }, + %{ input => 0, + output => False }, + %{ input => 6, + output => False }, + ; + + .<input>.&SOLUTION.&is-eqv: .<output>, .<text> for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-254/massa/raku/ch-2.raku b/challenge-254/massa/raku/ch-2.raku new file mode 100644 index 0000000000..0aaf2ad34c --- /dev/null +++ b/challenge-254/massa/raku/ch-2.raku @@ -0,0 +1,67 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 2: Reverse Vowels + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given a string, $s. + +Write a script to reverse all the vowels (a, e, i, o, u) in the given string. + +=head3 Example 1: + + Input => "Raku" + Output => "Ruka" + +=head3 Example 2: + + Input => "Perl" + Output => "Perl" + +=head3 Example 3: + + Input => "Julia" + Output => "Jaliu" + +=head3 Example 4: + + Input => "Uiua" + Output => "Auiu" + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION($_) { + my @reversed-vowels = .lc.flip.comb.grep: /<[aeiou]>/; + .lc.comb.map({ /<[aeiou]>/ ?? @reversed-vowels.shift !! $_ }).join.samecase($_) +} + +multi MAIN (Bool :$test!) { + use Testo; + + my @tests = + %{ input => "Raku", output => "Ruka" }, + %{ input => "Perl", output => "Perl" }, + %{ input => "Julia", output => "Jaliu" }, + %{ input => "Uiua", output => "Auiu" }, + ; + + .<input>.&SOLUTION.&is-eqv: .<output>, .<text> for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-254/sgreen/README.md b/challenge-254/sgreen/README.md index 4f9fe5e116..8963a142f3 100644 --- a/challenge-254/sgreen/README.md +++ b/challenge-254/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 253 +# The Weekly Challenge 254 -Blog: [The weakest split](https://dev.to/simongreennet/the-weakest-split-5ad7) +Blog: [Power to the vowels](https://dev.to/simongreennet/power-to-the-vowels-5cdp) diff --git a/challenge-254/sgreen/blog.txt b/challenge-254/sgreen/blog.txt new file mode 100644 index 0000000000..d436040f1b --- /dev/null +++ b/challenge-254/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/power-to-the-vowels-5cdp
\ No newline at end of file diff --git a/challenge-254/sgreen/perl/ch-1.pl b/challenge-254/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..2b51cd8203 --- /dev/null +++ b/challenge-254/sgreen/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use Math::Round 'round'; + +sub three_power ($n) { + # Get the closest integer to a cube root + my $i = round(abs($n) ** (1/3)); + + # Return true if this number is indeed a cube root + return $i ** 3 == abs($n); +} + +my $r = three_power($ARGV[0]); +say $r ? 'true' : 'false';
\ No newline at end of file diff --git a/challenge-254/sgreen/perl/ch-2.pl b/challenge-254/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..78feee1f3d --- /dev/null +++ b/challenge-254/sgreen/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub reverse_vowels ($s) { + # Convert it to lower case + $s = lc $s; + + # Extract the vowels + my @vowel_list = grep { /[aeiou]/ } split //, $s; + + for my $pos ( 0 .. length($s) - 1 ) { + # If the character here is a vowel + if ( substr( $s, $pos, 1 ) =~ /[aeiou]/ ) { + # ... get the last vowel + substr( $s, $pos, 1 ) = pop(@vowel_list); + } + } + + return ucfirst $s; +} + +say reverse_vowels($ARGV[0]);
\ No newline at end of file diff --git a/challenge-254/sgreen/python/ch-1.py b/challenge-254/sgreen/python/ch-1.py new file mode 100755 index 0000000000..37c492575d --- /dev/null +++ b/challenge-254/sgreen/python/ch-1.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import sys + + +def three_power(n: int) -> bool: + # Get the closest integer to a cube root + i = round(abs(n) ** (1/3)) + + # Return true if this number is indeed a cube root + return i ** 3 == abs(n) + + +def main(): + # Convert input into integers + n = int(sys.argv[1]) + result = three_power(n) + print('true' if result else 'false') + + +if __name__ == '__main__': + # Convert input into integers + main() diff --git a/challenge-254/sgreen/python/ch-2.py b/challenge-254/sgreen/python/ch-2.py new file mode 100755 index 0000000000..eed9768157 --- /dev/null +++ b/challenge-254/sgreen/python/ch-2.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import sys + + +def reverse_vowels(s: str) -> str: + # Convert it to lower case + s = s.lower() + + # Extract the vowels + vowel_list = [c for c in s if c in 'aeiou'] + + new_string = '' + for c in s: + # If the character here is a vowel + if c in 'aeiou': + # ... get the last vowel + new_string += vowel_list.pop() + else: + new_string += c + + return new_string.capitalize() + + +def main(): + s = sys.argv[1] + print(reverse_vowels(s)) + + +if __name__ == '__main__': + main() diff --git a/challenge-254/sgreen/python/test.py b/challenge-254/sgreen/python/test.py new file mode 100755 index 0000000000..f9d9c15650 --- /dev/null +++ b/challenge-254/sgreen/python/test.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertTrue(ch_1.three_power(27)) + self.assertTrue(ch_1.three_power(0)) + self.assertFalse(ch_1.three_power(6)) + + def test_ch_2(self): + self.assertEqual(ch_2.reverse_vowels('Raku'), 'Ruka') + self.assertEqual(ch_2.reverse_vowels('Perl'), 'Perl') + self.assertEqual(ch_2.reverse_vowels('Julia'), 'Jaliu') + self.assertEqual(ch_2.reverse_vowels('Uiua'), 'Auiu') + + +if __name__ == '__main__': + unittest.main() |
