aboutsummaryrefslogtreecommitdiff
path: root/challenge-255/lubos-kolouch/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-255/lubos-kolouch/python')
-rw-r--r--challenge-255/lubos-kolouch/python/ch-1.py24
-rw-r--r--challenge-255/lubos-kolouch/python/ch-2.py35
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-255/lubos-kolouch/python/ch-1.py b/challenge-255/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..b09f8ab050
--- /dev/null
+++ b/challenge-255/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,24 @@
+def find_odd_character(s: str, t: str) -> str:
+ """
+ Finds the additional character in string t.
+
+ :param s: Original string.
+ :param t: Modified string with one extra character.
+ :return: The additional character.
+ """
+ from collections import Counter
+
+ count_s = Counter(s)
+ count_t = Counter(t)
+
+ for char in count_t:
+ if count_t[char] > count_s.get(char, 0):
+ return char
+
+ return ""
+
+
+# Tests
+assert find_odd_character("Perl", "Preel") == "e"
+assert find_odd_character("Weekly", "Weeakly") == "a"
+assert find_odd_character("Box", "Boxy") == "y"
diff --git a/challenge-255/lubos-kolouch/python/ch-2.py b/challenge-255/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..2c2f1d6807
--- /dev/null
+++ b/challenge-255/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,35 @@
+def most_frequent_word(p: str, w: str) -> str:
+ """
+ Find the most frequent word in a paragraph excluding a specific word.
+
+ :param p: Paragraph as a string.
+ :param w: Banned word.
+ :return: Most frequent word.
+ """
+ import re
+ from collections import Counter
+
+ # Clean and split the paragraph into words
+ words = re.findall(r"\w+", p.lower())
+
+ # Count the frequency of each word, excluding the banned word
+ word_count = Counter(words)
+ if w in word_count:
+ del word_count[w]
+
+ # Find the most frequent word
+ most_frequent = word_count.most_common(1)[0][0]
+
+ return most_frequent
+
+
+# Test cases
+print(
+ most_frequent_word("Joe hit a ball, the hit ball flew far after it was hit.", "hit")
+)
+print(
+ most_frequent_word(
+ "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.",
+ "the",
+ )
+)