aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-13 01:10:55 +0000
committerGitHub <noreply@github.com>2024-02-13 01:10:55 +0000
commit72bce7105632e4966e4c22000af3c01fb045ddc2 (patch)
tree9a728b2843ce9446502df9fdd6bfd9c09ede847c
parent841101dd5b67b8788db152215749860f94940aea (diff)
parent7132bbea56bcc5f320746d7d5da8c1a1ba5fbe54 (diff)
downloadperlweeklychallenge-club-72bce7105632e4966e4c22000af3c01fb045ddc2.tar.gz
perlweeklychallenge-club-72bce7105632e4966e4c22000af3c01fb045ddc2.tar.bz2
perlweeklychallenge-club-72bce7105632e4966e4c22000af3c01fb045ddc2.zip
Merge pull request #9573 from jeanluc2020/jeanluc-256
Add solution 256.
-rw-r--r--challenge-256/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-256/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-256/jeanluc2020/perl/ch-1.pl62
-rwxr-xr-xchallenge-256/jeanluc2020/perl/ch-2.pl57
-rwxr-xr-xchallenge-256/jeanluc2020/python/ch-1.py54
-rwxr-xr-xchallenge-256/jeanluc2020/python/ch-2.py56
6 files changed, 231 insertions, 0 deletions
diff --git a/challenge-256/jeanluc2020/blog-1.txt b/challenge-256/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..e75fddf21a
--- /dev/null
+++ b/challenge-256/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-256-1.html
diff --git a/challenge-256/jeanluc2020/blog-2.txt b/challenge-256/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..bd89bdad44
--- /dev/null
+++ b/challenge-256/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-256-2.html
diff --git a/challenge-256/jeanluc2020/perl/ch-1.pl b/challenge-256/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..475f4c457f
--- /dev/null
+++ b/challenge-256/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/#TASK1
+#
+# Task 1: Maximum Pairs
+# =====================
+#
+# You are given an array of distinct words, @words.
+#
+# Write a script to find the maximum pairs in the given array. The words
+# $words[i] and $words[j] can be a pair one is reverse of the other.
+#
+## Example 1
+##
+## Input: @words = ("ab", "de", "ed", "bc")
+## Output: 1
+##
+## There is one pair in the given array: "de" and "ed"
+#
+## Example 2
+##
+## Input: @words = ("aa", "ba", "cd", "ed")
+## Output: 0
+#
+## Example 3
+##
+## Input: @words = ("uv", "qp", "st", "vu", "mn", "pq")
+## Output: 2
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Just check all possible pairs.
+
+use strict;
+use warnings;
+
+maximum_pairs("ab", "de", "ed", "bc");
+maximum_pairs("aa", "ba", "cd", "ed");
+maximum_pairs("uv", "qp", "st", "vu", "mn", "pq");
+
+sub maximum_pairs {
+ my @words = @_;
+ print "Input: (\"" . join("\", \"", @words) . "\")\n";
+ my $count = 0;
+ foreach my $i (0..$#words) {
+ foreach my $j ($i+1..$#words) {
+ if($words[$i] eq reversed($words[$j]) ) {
+ $count++;
+ last;
+ }
+ }
+ }
+ print "Output: $count\n";
+}
+
+sub reversed {
+ my $word = shift;
+ return join("", reverse split //, $word);
+}
diff --git a/challenge-256/jeanluc2020/perl/ch-2.pl b/challenge-256/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..094d42cddd
--- /dev/null
+++ b/challenge-256/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/#TASK2
+#
+# Task 2: Merge Strings
+# =====================
+#
+# You are given two strings, $str1 and $str2.
+#
+# Write a script to merge the given strings by adding in alternative order
+# starting with the first string. If a string is longer than the other then
+# append the remaining at the end.
+#
+## Example 1
+##
+## Input: $str1 = "abcd", $str2 = "1234"
+## Output: "a1b2c3d4"
+#
+## Example 2
+##
+## Input: $str1 = "abc", $str2 = "12345"
+## Output: "a1b2c345"
+#
+## Example 3
+##
+## Input: $str1 = "abcde", $str2 = "123"
+## Output: "a1b2c3de"
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Turn both strings into arrays, get elements from both arrays
+# until the first one is empty, then add the remainder of the
+# second one.
+
+use strict;
+use warnings;
+
+merge_strings("abcd", "1234");
+merge_strings("abc", "12345");
+merge_strings("abcde", "123");
+
+sub merge_strings {
+ my ($str1, $str2) = @_;
+ print "Input: str1 = \"$str1\", str2 = \"$str2\"\n";
+ my $output = "";
+ my @first = split //, $str1;
+ my @second = split //, $str2;
+ while (my $c = shift @first) {
+ $output .= $c;
+ $output .= shift @second // "";
+ }
+ $output .= join("", @second);
+ print "Output: \"$output\"\n";
+}
diff --git a/challenge-256/jeanluc2020/python/ch-1.py b/challenge-256/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..baf77a9ff7
--- /dev/null
+++ b/challenge-256/jeanluc2020/python/ch-1.py
@@ -0,0 +1,54 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/#TASK1
+#
+# Task 1: Maximum Pairs
+# =====================
+#
+# You are given an array of distinct words, @words.
+#
+# Write a script to find the maximum pairs in the given array. The words
+# $words[i] and $words[j] can be a pair one is reverse of the other.
+#
+## Example 1
+##
+## Input: @words = ("ab", "de", "ed", "bc")
+## Output: 1
+##
+## There is one pair in the given array: "de" and "ed"
+#
+## Example 2
+##
+## Input: @words = ("aa", "ba", "cd", "ed")
+## Output: 0
+#
+## Example 3
+##
+## Input: @words = ("uv", "qp", "st", "vu", "mn", "pq")
+## Output: 2
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Just check all possible pairs.
+
+def reverse_word(word: str) -> str:
+ chars = list(word)
+ return "".join(reversed(chars))
+
+def maximum_pairs(words: list) -> None:
+ print("Input: (\"", "\", \"".join(words), "\")", sep="")
+ count = 0
+ for i in range(len(words)):
+ for j in range(i+1, len(words)):
+ if words[i] == reverse_word(words[j]):
+ count += 1
+ break
+ print(f"Output: {count}")
+
+maximum_pairs(["ab", "de", "ed", "bc"]);
+maximum_pairs(["aa", "ba", "cd", "ed"]);
+maximum_pairs(["uv", "qp", "st", "vu", "mn", "pq"]);
+
diff --git a/challenge-256/jeanluc2020/python/ch-2.py b/challenge-256/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..9f7abb5e09
--- /dev/null
+++ b/challenge-256/jeanluc2020/python/ch-2.py
@@ -0,0 +1,56 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/#TASK2
+#
+# Task 2: Merge Strings
+# =====================
+#
+# You are given two strings, $str1 and $str2.
+#
+# Write a script to merge the given strings by adding in alternative order
+# starting with the first string. If a string is longer than the other then
+# append the remaining at the end.
+#
+## Example 1
+##
+## Input: $str1 = "abcd", $str2 = "1234"
+## Output: "a1b2c3d4"
+#
+## Example 2
+##
+## Input: $str1 = "abc", $str2 = "12345"
+## Output: "a1b2c345"
+#
+## Example 3
+##
+## Input: $str1 = "abcde", $str2 = "123"
+## Output: "a1b2c3de"
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Turn both strings into arrays, get elements from both arrays
+# until the first one is empty, then add the remainder of the
+# second one.
+
+def merge_strings(str1: str, str2: str) -> None:
+ print(f"Input: \"{str1}\", \"{str2}\"")
+ output = ""
+ first = list(str1)
+ second = list(str2)
+ while len(first) > 0:
+ elem = first.pop(0)
+ output += elem
+ if len(second) > 0:
+ elem = second.pop(0)
+ output += elem
+ if len(second) > 0:
+ output += "".join(second)
+ print(f"Output: \"{output}\"")
+
+merge_strings("abcd", "1234");
+merge_strings("abc", "12345");
+merge_strings("abcde", "123");
+