diff options
| -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 |
6 files changed, 188 insertions, 0 deletions
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'; |
