aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-31 13:12:49 +0000
committerGitHub <noreply@github.com>2024-01-31 13:12:49 +0000
commitb4fbbc5811af8f7c8799bd4f5542a83f02750550 (patch)
treea4cb96129022f499ae1c4fecb08075fd16dc553a
parent82a33a5ff469bac85006be43a48eee3cad38f684 (diff)
parent7f665027cfb52ecbab78999e054aa9cbb2214589 (diff)
downloadperlweeklychallenge-club-b4fbbc5811af8f7c8799bd4f5542a83f02750550.tar.gz
perlweeklychallenge-club-b4fbbc5811af8f7c8799bd4f5542a83f02750550.tar.bz2
perlweeklychallenge-club-b4fbbc5811af8f7c8799bd4f5542a83f02750550.zip
Merge pull request #9487 from jeanluc2020/jeanluc-254
Add solution 254
-rw-r--r--challenge-254/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-254/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-254/jeanluc2020/perl/ch-1.pl59
-rwxr-xr-xchallenge-254/jeanluc2020/perl/ch-2.pl88
-rwxr-xr-xchallenge-254/jeanluc2020/python/ch-1.py55
-rwxr-xr-xchallenge-254/jeanluc2020/python/ch-2.py84
6 files changed, 288 insertions, 0 deletions
diff --git a/challenge-254/jeanluc2020/blog-1.txt b/challenge-254/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..62379a7741
--- /dev/null
+++ b/challenge-254/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-254-1.html
diff --git a/challenge-254/jeanluc2020/blog-2.txt b/challenge-254/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..422b30e1fc
--- /dev/null
+++ b/challenge-254/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-254-2.html
diff --git a/challenge-254/jeanluc2020/perl/ch-1.pl b/challenge-254/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..6076b8b237
--- /dev/null
+++ b/challenge-254/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-254/#TASK1
+#
+# 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
+##
+## 27 = 3 ^ 3
+#
+## Example 2
+##
+## Input: $n = 0
+## Output: true
+##
+## 0 = 0 ^ 3
+#
+## Example 3
+##
+## Input: $n = 6
+## Output: false
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Starting with $i at 0, count up to $n. If $i**3 == $n we have
+# a third power, so return true. If $i**3 > $n we don't have a
+# third power, so return false.
+#
+use strict;
+use warnings;
+
+three_power(27);
+three_power(0);
+three_power(6);
+
+sub three_power {
+ my $n = shift;
+ print "Input: $n\n";
+ foreach my $i (0..$n) {
+ last if $i**3 > $n;
+ if($i**3 == $n) {
+ print "Output: true\n";
+ return;
+ }
+ }
+ print "Output: false\n";
+}
diff --git a/challenge-254/jeanluc2020/perl/ch-2.pl b/challenge-254/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..2d78bff247
--- /dev/null
+++ b/challenge-254/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,88 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-254/#TASK2
+#
+# 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"
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Let's first turn the string into an array of characters. Then
+# let's collect the position of all vowels (and whether or not
+# they were uppercase). Then we can walk through all vowels and
+# write them to their new location in the correct (uppwer/lower)
+# case.
+
+use strict;
+use warnings;
+
+reverse_vowels("Raku");
+reverse_vowels("Perl");
+reverse_vowels("Julia");
+reverse_vowels("Uiua");
+
+sub reverse_vowels {
+ my $s = shift;
+ print "Input: \"$s\"\n";
+ my @chars = split //, $s;
+ my @vowels = ();
+ my @indices = ();
+ foreach my $i (0..$#chars) {
+ if(is_vowel($chars[$i])) {
+ push @vowels, [ $i, $chars[$i] ];
+ unshift @indices, [ $i, is_upper($chars[$i]) ];
+ }
+ }
+ foreach my $j (0..$#vowels) {
+ my ($old_index, $char, $is_upper) = @{ $vowels[$j] };
+ my ($new_index, $is_upper) = @{ $indices[$j] };
+ my $new_char = $char;
+ if($is_upper) {
+ $new_char = uc($new_char);
+ } else {
+ $new_char = lc($new_char);
+ }
+ $chars[$new_index] = $new_char;
+ }
+ $s = join("", @chars);
+ print "Output: \"$s\"\n";
+
+}
+
+sub is_vowel {
+ my $char = shift;
+ my $vowels = { "a" => 1, "e" => 1, "i" => 1, "o" => 1, "u" => 1 };
+ return $vowels->{lc($char)};
+}
+
+sub is_upper {
+ my $char = shift;
+ return $char eq uc($char);
+}
diff --git a/challenge-254/jeanluc2020/python/ch-1.py b/challenge-254/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..cf41e8df6f
--- /dev/null
+++ b/challenge-254/jeanluc2020/python/ch-1.py
@@ -0,0 +1,55 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-254/#TASK1
+#
+# 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
+##
+## 27 = 3 ^ 3
+#
+## Example 2
+##
+## Input: $n = 0
+## Output: true
+##
+## 0 = 0 ^ 3
+#
+## Example 3
+##
+## Input: $n = 6
+## Output: false
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Starting with $i at 0, count up to $n. If $i**3 == $n we have
+# a third power, so return true. If $i**3 > $n we don't have a
+# third power, so return false.
+#
+
+def three_power(n: int) -> None:
+ print(f"Input: {n}")
+ for i in range(n+1):
+ if i**3 == n:
+ print("Output: true")
+ return
+ if i**3 > n:
+ print("Output: false")
+ return
+
+three_power(27);
+three_power(0);
+three_power(6);
+
diff --git a/challenge-254/jeanluc2020/python/ch-2.py b/challenge-254/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..d87fb5f0f1
--- /dev/null
+++ b/challenge-254/jeanluc2020/python/ch-2.py
@@ -0,0 +1,84 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-254/#TASK2
+#
+# 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"
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Let's first turn the string into an array of characters. Then
+# let's collect the position of all vowels (and whether or not
+# they were uppercase). Then we can walk through all vowels and
+# write them to their new location in the correct (uppwer/lower)
+# case.
+
+def is_upper(char: str) -> bool:
+ upper = char.upper()
+ if upper == char:
+ return True
+ return False
+
+
+def is_vowel(char: str) -> bool:
+ vowels = { "a": True, "e": True, "i": True, "o": True, "u": True }
+ if char.lower() in vowels:
+ return True
+ return False
+
+def reverse_vowels(s: str) -> str:
+ print(f"Input: {s}")
+ chars = list(s)
+ vowels = []
+ indices = []
+ for i in range(len(chars)):
+ if is_vowel(chars[i]):
+ vowels.append( (i, chars[i] ) )
+ indices.append( (i, is_upper(chars[i]) ) )
+ indices.reverse()
+ for j in range(len(vowels)):
+ ( old_index, char ) = vowels[j]
+ ( new_index, is_up ) = indices[j]
+ new_char = char
+ if is_up:
+ new_char = char.upper()
+ else:
+ new_char = char.lower()
+ chars[new_index] = new_char
+ s = "".join(chars)
+ print(f"Output: {s}")
+ return s
+
+
+reverse_vowels("Raku")
+reverse_vowels("Perl")
+reverse_vowels("Julia")
+reverse_vowels("Uiua")
+