aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-09 10:50:45 +0100
committerGitHub <noreply@github.com>2024-04-09 10:50:45 +0100
commit5d1f1e6450ee752545f081f2dcbf64b47a999860 (patch)
tree520bfa8c3cc3760b97cc9af08be5c2d8616772b6
parentf379ca7e2858abaed96aed3e2b0066e8df0152b0 (diff)
parent96996fba85966eeee31a1276a7c3b7cd7db704fa (diff)
downloadperlweeklychallenge-club-5d1f1e6450ee752545f081f2dcbf64b47a999860.tar.gz
perlweeklychallenge-club-5d1f1e6450ee752545f081f2dcbf64b47a999860.tar.bz2
perlweeklychallenge-club-5d1f1e6450ee752545f081f2dcbf64b47a999860.zip
Merge pull request #9896 from jeanluc2020/jeanluc-264
Add solution 264
-rw-r--r--challenge-264/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-264/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-264/jeanluc2020/perl/ch-1.pl74
-rwxr-xr-xchallenge-264/jeanluc2020/perl/ch-2.pl73
-rwxr-xr-xchallenge-264/jeanluc2020/python/ch-1.py73
-rwxr-xr-xchallenge-264/jeanluc2020/python/ch-2.py64
6 files changed, 286 insertions, 0 deletions
diff --git a/challenge-264/jeanluc2020/blog-1.txt b/challenge-264/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..b9c7d45562
--- /dev/null
+++ b/challenge-264/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-264-1.html
diff --git a/challenge-264/jeanluc2020/blog-2.txt b/challenge-264/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..6c10f0498d
--- /dev/null
+++ b/challenge-264/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-264-2.html
diff --git a/challenge-264/jeanluc2020/perl/ch-1.pl b/challenge-264/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..4768b28dd7
--- /dev/null
+++ b/challenge-264/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-264/#TASK1
+#
+# Task 1: Greatest English Letter
+# ===============================
+#
+# You are given a string, $str, made up of only alphabetic characters [a..zA..Z].
+#
+# Write a script to return the greatest english letter in the given string.
+#
+### A letter is greatest if it occurs as lower and upper case. Also letter
+### ‘b’ is greater than ‘a’ if ‘b’ appears after ‘a’ in the English alphabet.
+#
+## Example 1
+##
+## Input: $str = 'PeRlwEeKLy'
+## Output: L
+##
+## There are two letters E and L that appears as lower and upper.
+## The letter L appears after E, so the L is the greatest english letter.
+#
+## Example 2
+##
+## Input: $str = 'ChaLlenge'
+## Output: L
+#
+## Example 3
+##
+## Input: $str = 'The'
+## Output: ''
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# First, we split the input string into its characters. We then
+# calculate the lowercase version of the character. We fill some
+# data into a hash of hashes:
+# - The key of the outermost hash is the lowercase version of the
+# character
+# - The key of the inner hash is the character in its original form
+# - The value of the inner hash is just a true value
+# This way, if both the upper- and lowercase form of a character are
+# in the input string, the corresponding inner hash for the character
+# will have two keys (the upper- and lowercase version).
+# Now we simply walk through the reverse sorted keys of the outer hash.
+# As soon as we find a character that has both upper- and lowercase
+# versions in its inner hash, we have found the Greatest English Letter
+# and can finish the loop.
+
+use strict;
+use warnings;
+
+greatest_english_letter('PeRlwEeKLy');
+greatest_english_letter('ChaLlenge');
+greatest_english_letter('The');
+
+sub greatest_english_letter {
+ my $str = shift;
+ print "Input: '$str'\n";
+ my $all = {};
+ my $output = "''";
+ foreach my $c (split(//, $str)) {
+ $all->{lc($c)}->{$c} = 1;
+ }
+ foreach my $c (reverse sort keys %$all) {
+ next unless scalar(keys %{ $all->{$c} }) == 2;
+ $output = uc($c);
+ last;
+ }
+ print "Output: $output\n";
+}
diff --git a/challenge-264/jeanluc2020/perl/ch-2.pl b/challenge-264/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..0db40fa201
--- /dev/null
+++ b/challenge-264/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-264/#TASK2
+#
+# Task 2: Target Array
+# ====================
+#
+# You are given two arrays of integers, @source and @indices. The @indices can
+# only contains integers 0 <= i < size of @source.
+#
+# Write a script to create target array by insert at index $indices[i] the
+# value $source[i].
+#
+## Example 1
+##
+## Input: @source = (0, 1, 2, 3, 4)
+## @indices = (0, 1, 2, 2, 1)
+## Output: (0, 4, 1, 3, 2)
+##
+## @source @indices @target
+## 0 0 (0)
+## 1 1 (0, 1)
+## 2 2 (0, 1, 2)
+## 3 2 (0, 1, 3, 2)
+## 4 1 (0, 4, 1, 3, 2)
+#
+## Example 2
+##
+## Input: @source = (1, 2, 3, 4, 0)
+## @indices = (0, 1, 2, 3, 0)
+## Output: (0, 1, 2, 3, 4)
+##
+## @source @indices @target
+## 1 0 (1)
+## 2 1 (1, 2)
+## 3 2 (1, 2, 3)
+## 4 3 (1, 2, 3, 4)
+## 0 0 (0, 1, 2, 3, 4)
+#
+## Example 3
+##
+## Input: @source = (1)
+## @indices = (0)
+## Output: (1)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Walk the arrays in parallel using an index $i; keep all
+# elements < $indices[$i] in your target array, and move
+# any elements >= $indices[$i] up by one element, creating
+# space for $source->[$i].
+# By slicing properly, we can do all of that in a single step.
+
+use strict;
+use warnings;
+
+target_array( [0, 1, 2, 3, 4], [0, 1, 2, 2, 1] );
+target_array( [1, 2, 3, 4, 0], [0, 1, 2, 3, 0] );
+target_array( [1], [0] );
+
+sub target_array {
+ my ($source, $indices) = @_;
+ print "Input: (", join(", ", @$source), "), (", join(", ", @$indices), ")\n";
+ my @target = ();
+ foreach my $i (0..scalar(@$indices)-1) {
+ my $j = $indices->[$i];
+ @target = (@target[0..$j-1], $source->[$i], @target[$j..scalar(@target)-1]);
+ }
+ print "Output: (", join(", ", @target), ")\n";
+}
diff --git a/challenge-264/jeanluc2020/python/ch-1.py b/challenge-264/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..7b2d21c5d8
--- /dev/null
+++ b/challenge-264/jeanluc2020/python/ch-1.py
@@ -0,0 +1,73 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-264/#TASK1
+#
+# Task 1: Greatest English Letter
+# ===============================
+#
+# You are given a string, $str, made up of only alphabetic characters [a..zA..Z].
+#
+# Write a script to return the greatest english letter in the given string.
+#
+### A letter is greatest if it occurs as lower and upper case. Also letter
+### ‘b’ is greater than ‘a’ if ‘b’ appears after ‘a’ in the English alphabet.
+#
+## Example 1
+##
+## Input: $str = 'PeRlwEeKLy'
+## Output: L
+##
+## There are two letters E and L that appears as lower and upper.
+## The letter L appears after E, so the L is the greatest english letter.
+#
+## Example 2
+##
+## Input: $str = 'ChaLlenge'
+## Output: L
+#
+## Example 3
+##
+## Input: $str = 'The'
+## Output: ''
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# First, we split the input string into its characters. We then
+# calculate the lowercase version of the character. We fill some
+# data into a hash of hashes:
+# - The key of the outermost hash is the lowercase version of the
+# character
+# - The key of the inner hash is the character in its original form
+# - The value of the inner hash is just a true value
+# This way, if both the upper- and lowercase form of a character are
+# in the input string, the corresponding inner hash for the character
+# will have two keys (the upper- and lowercase version).
+# Now we simply walk through the reverse sorted keys of the outer hash.
+# As soon as we find a character that has both upper- and lowercase
+# versions in its inner hash, we have found the Greatest English Letter
+# and can finish the loop.
+
+def greatest_english_letter(str: str) -> None:
+ all = {}
+ output = "''"
+ print(f"Input: {str}")
+ for c in list(str):
+ c_l = c.lower()
+ if c_l not in all:
+ all[c_l] = {c: 1}
+ else:
+ if c not in all[c_l]:
+ all[c_l][c] = 1
+ for c in reversed(sorted(list(all.keys()))):
+ if len(all[c].keys()) == 2:
+ output = c.upper()
+ break
+ print(f"Output: {output}")
+
+greatest_english_letter('PeRlwEeKLy');
+greatest_english_letter('ChaLlenge');
+greatest_english_letter('The');
+
diff --git a/challenge-264/jeanluc2020/python/ch-2.py b/challenge-264/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..0d8c2928cf
--- /dev/null
+++ b/challenge-264/jeanluc2020/python/ch-2.py
@@ -0,0 +1,64 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-264/#TASK2
+#
+# Task 2: Target Array
+# ====================
+#
+# You are given two arrays of integers, @source and @indices. The @indices can
+# only contains integers 0 <= i < size of @source.
+#
+# Write a script to create target array by insert at index $indices[i] the
+# value $source[i].
+#
+## Example 1
+##
+## Input: @source = (0, 1, 2, 3, 4)
+## @indices = (0, 1, 2, 2, 1)
+## Output: (0, 4, 1, 3, 2)
+##
+## @source @indices @target
+## 0 0 (0)
+## 1 1 (0, 1)
+## 2 2 (0, 1, 2)
+## 3 2 (0, 1, 3, 2)
+## 4 1 (0, 4, 1, 3, 2)
+#
+## Example 2
+##
+## Input: @source = (1, 2, 3, 4, 0)
+## @indices = (0, 1, 2, 3, 0)
+## Output: (0, 1, 2, 3, 4)
+##
+## @source @indices @target
+## 1 0 (1)
+## 2 1 (1, 2)
+## 3 2 (1, 2, 3)
+## 4 3 (1, 2, 3, 4)
+## 0 0 (0, 1, 2, 3, 4)
+#
+## Example 3
+##
+## Input: @source = (1)
+## @indices = (0)
+## Output: (1)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Walk the arrays in parallel using an index $i; insert
+# source[i] at the index indices[i] in the target array
+
+def target_array(source: list, indices: list) -> None:
+ print("Input: (", ", ".join([str(x) for x in source]), "), (", ", ".join([str(x) for x in indices]), ")", sep="")
+ target = []
+ for i in range(len(indices)):
+ target.insert(indices[i], source[i])
+ print("Output: (", ", ".join([str(x) for x in target]), ")", sep="")
+
+target_array( [0, 1, 2, 3, 4], [0, 1, 2, 2, 1] );
+target_array( [1, 2, 3, 4, 0], [0, 1, 2, 3, 0] );
+target_array( [1], [0] );
+