aboutsummaryrefslogtreecommitdiff
path: root/challenge-255
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2024-02-05 14:45:15 +0100
committerThomas Köhler <jean-luc@picard.franken.de>2024-02-05 14:45:15 +0100
commit42b3de759cb5a10aa49c486d32bcbdbae2bc38fa (patch)
tree8cd46bfffe290dd79157ccf9ec606fde4165ad1f /challenge-255
parentd58258463850d2949fe13a4097a83a5d17951548 (diff)
downloadperlweeklychallenge-club-42b3de759cb5a10aa49c486d32bcbdbae2bc38fa.tar.gz
perlweeklychallenge-club-42b3de759cb5a10aa49c486d32bcbdbae2bc38fa.tar.bz2
perlweeklychallenge-club-42b3de759cb5a10aa49c486d32bcbdbae2bc38fa.zip
Add solution 255.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
Diffstat (limited to 'challenge-255')
-rw-r--r--challenge-255/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-255/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-255/jeanluc2020/perl/ch-1.pl62
-rwxr-xr-xchallenge-255/jeanluc2020/perl/ch-2.pl56
-rwxr-xr-xchallenge-255/jeanluc2020/python/ch-1.py62
-rwxr-xr-xchallenge-255/jeanluc2020/python/ch-2.py60
6 files changed, 242 insertions, 0 deletions
diff --git a/challenge-255/jeanluc2020/blog-1.txt b/challenge-255/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..589a67eb25
--- /dev/null
+++ b/challenge-255/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-255-1.html
diff --git a/challenge-255/jeanluc2020/blog-2.txt b/challenge-255/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..cfa902820f
--- /dev/null
+++ b/challenge-255/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-255-2.html
diff --git a/challenge-255/jeanluc2020/perl/ch-1.pl b/challenge-255/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..6ae4cc6640
--- /dev/null
+++ b/challenge-255/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/#TASK1
+#
+# Task 1: Odd Character
+# =====================
+#
+# You are given two strings, $s and $t. The string $t is generated using the
+# shuffled characters of the string $s with an additional character.
+#
+# Write a script to find the additional character in the string $t..
+#
+## Example 1
+##
+## Input: $s = "Perl" $t = "Preel"
+## Output: "e"
+#
+## Example 2
+##
+## Input: $s = "Weekly" $t = "Weeakly"
+## Output: "a"
+#
+## Example 3
+##
+## Input: $s = "Box" $t = "Boxy"
+## Output: "y"
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Split the word into its character, store them in a hash table, then
+# count the result for each character in both the table for the original
+# word and for the new word.
+
+use strict;
+use warnings;
+
+odd_character("Perl", "Preel");
+odd_character("Weekly", "Weeakly");
+odd_character("Box", "Boxy");
+
+sub odd_character {
+ my ($s, $t) = @_;
+ print "Input: '$s', '$t'\n";
+ my $s_hash = {};
+ my $t_hash = {};
+ foreach my $char (split//,$s) {
+ $s_hash->{$char}++;
+ }
+ foreach my $char (split//,$t) {
+ $t_hash->{$char}++;
+ }
+ foreach my $found (keys %$t_hash) {
+ $s_hash->{$found} //= 0;
+ if($t_hash->{$found} > $s_hash->{$found}) {
+ print "Output: $found\n";
+ return;
+ }
+ }
+}
diff --git a/challenge-255/jeanluc2020/perl/ch-2.pl b/challenge-255/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..b3896b9710
--- /dev/null
+++ b/challenge-255/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/#TASK2
+#
+# Task 2: Most Frequent Word
+# ==========================
+#
+# You are given a paragraph $p and a banned word $w.
+#
+# Write a script to return the most frequent word that is not banned.
+#
+## Example 1
+##
+## Input: $p = "Joe hit a ball, the hit ball flew far after it was hit."
+## $w = "hit"
+## Output: "ball"
+##
+## The banned word "hit" occurs 3 times.
+## The other word "ball" occurs 2 times.
+#
+## Example 2
+##
+## Input: $p = "Perl and Raku belong to the same family. Perl is the most
+## popular language in the weekly challenge."
+## $w = "the"
+## Output: "Perl"
+##
+## The banned word "the" occurs 3 times.
+## The other word "Perl" occurs 2 times.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Split the sentence into its words, then count all the words != $w
+
+use strict;
+use warnings;
+
+most_frequent_word("Joe hit a ball, the hit ball flew far after it was hit.", "hit");
+most_frequent_word("Perl and Raku belong to the same family. Perl is the most" .
+ " popular language in the weekly challenge.", "the");
+
+sub most_frequent_word {
+ my ($p, $w) = @_;
+ print "Input: '$p', '$w'\n";
+ my $found = {};
+ foreach my $word (split/[^\w]/, $p) {
+ next if $word eq $w;
+ $found->{$word}++;
+ }
+ my @most = sort { $found->{$b} <=> $found->{$a} } keys %$found;
+ print "Found the most frequent word to be '$most[0]'\n";
+}
+
diff --git a/challenge-255/jeanluc2020/python/ch-1.py b/challenge-255/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..728f307d69
--- /dev/null
+++ b/challenge-255/jeanluc2020/python/ch-1.py
@@ -0,0 +1,62 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/#TASK1
+#
+# Task 1: Odd Character
+# =====================
+#
+# You are given two strings, $s and $t. The string $t is generated using the
+# shuffled characters of the string $s with an additional character.
+#
+# Write a script to find the additional character in the string $t..
+#
+## Example 1
+##
+## Input: $s = "Perl" $t = "Preel"
+## Output: "e"
+#
+## Example 2
+##
+## Input: $s = "Weekly" $t = "Weeakly"
+## Output: "a"
+#
+## Example 3
+##
+## Input: $s = "Box" $t = "Boxy"
+## Output: "y"
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Split the word into its character, store them in a hash table, then
+# count the result for each character in both the table for the original
+# word and for the new word.
+
+def odd_character(s: str, t: str) -> None:
+ print(f"Input: '{s}', '{t}'")
+ s_hash = {}
+ t_hash = {}
+ for char in list(s):
+ if char in s_hash:
+ s_hash[char] += 1
+ else:
+ s_hash[char] = 1
+ for char in list(t):
+ if char in t_hash:
+ t_hash[char] += 1
+ else:
+ t_hash[char] = 1
+ for found in t_hash.keys():
+ old = 0
+ if found in s_hash:
+ old = s_hash[found]
+ if t_hash[found] > old:
+ print(f"Output: {found}")
+ return
+
+
+odd_character("Perl", "Preel");
+odd_character("Weekly", "Weeakly");
+odd_character("Box", "Boxy");
diff --git a/challenge-255/jeanluc2020/python/ch-2.py b/challenge-255/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..ebc9f1edce
--- /dev/null
+++ b/challenge-255/jeanluc2020/python/ch-2.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/#TASK2
+#
+# Task 2: Most Frequent Word
+# ==========================
+#
+# You are given a paragraph $p and a banned word $w.
+#
+# Write a script to return the most frequent word that is not banned.
+#
+## Example 1
+##
+## Input: $p = "Joe hit a ball, the hit ball flew far after it was hit."
+## $w = "hit"
+## Output: "ball"
+##
+## The banned word "hit" occurs 3 times.
+## The other word "ball" occurs 2 times.
+#
+## Example 2
+##
+## Input: $p = "Perl and Raku belong to the same family. Perl is the most
+## popular language in the weekly challenge."
+## $w = "the"
+## Output: "Perl"
+##
+## The banned word "the" occurs 3 times.
+## The other word "Perl" occurs 2 times.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Split the sentence into its words, then count all the words != $w
+
+import re
+from operator import itemgetter
+
+def most_frequent_word(p: str, w: str) -> None:
+ print(f"Input: '{p}', '{w}'")
+ found = {}
+ for word in re.split('\W+', p):
+ if word != w:
+ if word in found:
+ found[word] += 1
+ else:
+ found[word] = 1
+ most = []
+ for key in found.keys():
+ most.append( (key, found[key]) )
+ most.sort(key=itemgetter(1))
+ print(most)
+ print(f"Found the most frequent word to be '{most[-1][0]}'")
+
+most_frequent_word("Joe hit a ball, the hit ball flew far after it was hit.", "hit");
+most_frequent_word("Perl and Raku belong to the same family. Perl is the most" +
+ " popular language in the weekly challenge.", "the");
+