diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2024-02-07 18:21:37 +0100 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2024-02-07 18:21:37 +0100 |
| commit | fc24c2bc1b1f9aba2fc48fc2a1550fa853c520c1 (patch) | |
| tree | cdbb313ba7b8873b788964094823fb7d4475ad1e | |
| parent | a04122d295a6ad6a86418a4399be4af375e195d4 (diff) | |
| download | perlweeklychallenge-club-fc24c2bc1b1f9aba2fc48fc2a1550fa853c520c1.tar.gz perlweeklychallenge-club-fc24c2bc1b1f9aba2fc48fc2a1550fa853c520c1.tar.bz2 perlweeklychallenge-club-fc24c2bc1b1f9aba2fc48fc2a1550fa853c520c1.zip | |
Add PWC-255 in Python
| -rw-r--r-- | challenge-255/spadacciniweb/python/ch-1.py | 39 | ||||
| -rw-r--r-- | challenge-255/spadacciniweb/python/ch-2.py | 49 |
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-255/spadacciniweb/python/ch-1.py b/challenge-255/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..ffb284197c --- /dev/null +++ b/challenge-255/spadacciniweb/python/ch-1.py @@ -0,0 +1,39 @@ +# Task 1: Odd Character +# Submitted by: Mohammad Sajid Anwar +# +# 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" + +from collections import Counter + +def odd_character(s, t): + counts=Counter(s) + + for i in t: + counts[i] -= 1 + print("Output:", s, "|", t, "-> %s" %(list(counts.keys())[list(counts.values()).index(-1)])) + +if __name__ == "__main__": + s = 'Perl' + t = "Preel" + odd_character(s, t) + + s = "Weekly" + t = "Weeakly" + odd_character(s, t) + + s = "Box" + t = "Boxy" + odd_character(s, t) diff --git a/challenge-255/spadacciniweb/python/ch-2.py b/challenge-255/spadacciniweb/python/ch-2.py new file mode 100644 index 0000000000..ebcb2aa60b --- /dev/null +++ b/challenge-255/spadacciniweb/python/ch-2.py @@ -0,0 +1,49 @@ +# Task 2: Most Frequent Word +# Submitted by: Mohammad Sajid Anwar +# +# 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. + +def count(w, dictionary): + if w in dictionary: + dictionary[w] += 1 + else: + dictionary.update({w: 1}) + +def banned_word(paragraph, banned): + paragraph = filter(lambda x: x.isalnum() or x.isspace(), paragraph) + paragraph = "".join(paragraph) + + dictionary = {} + lst = paragraph.split() + + for w in lst: + if w != banned: + count(w, dictionary) + + print(max(dictionary, key=dictionary.get)) + +if __name__ == "__main__": + paragraph = "Joe hit a ball, the hit ball flew far after it was hit." + banned = "hit" + banned_word(paragraph, banned) + + paragraph = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge." + banned = "the" + banned_word(paragraph, banned) |
