From 5bad8a7e7366e0e8b30d580332a026266f69053e Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 5 Feb 2024 19:23:23 +0000 Subject: - Added solutions by Eric Cheung. - Added solutions by Laurent Rosenfeld. - Added solutions by Matthew Neleigh. - Added solutions by Mark Anderson. - Added solutions by Luca Ferrari. - Added solutions by PokGoPun. - Added solutions by Robbie Hatley. - Added solutions by Thomas Kohler. - Added solutions by W. Luis Mochan. - Added solutions by Simon Proctor. - Added solutions by Steven Wilson. - Added solutions by David Ferrone. - Added solutions by Peter Meszaros. --- challenge-255/steven-wilson/python/ch-01.py | 34 -------------------------- challenge-255/steven-wilson/python/ch-02.py | 37 ----------------------------- challenge-255/steven-wilson/python/ch-1.py | 34 ++++++++++++++++++++++++++ challenge-255/steven-wilson/python/ch-2.py | 37 +++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 71 deletions(-) delete mode 100644 challenge-255/steven-wilson/python/ch-01.py delete mode 100644 challenge-255/steven-wilson/python/ch-02.py create mode 100644 challenge-255/steven-wilson/python/ch-1.py create mode 100644 challenge-255/steven-wilson/python/ch-2.py (limited to 'challenge-255/steven-wilson/python') diff --git a/challenge-255/steven-wilson/python/ch-01.py b/challenge-255/steven-wilson/python/ch-01.py deleted file mode 100644 index dac2d858ab..0000000000 --- a/challenge-255/steven-wilson/python/ch-01.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env python3 - -from collections import Counter - - -def odd_character(original, shuffled): - ''' Given two strings, the second string generated using the shuffled - characters of the first string with an additional character. Function - returns the additional character. - >>> odd_character("Perl", "Preel") - 'e' - >>> odd_character("Weekly", "Weeakly") - 'a' - >>> odd_character("Box", "Boxy") - 'y' - ''' - if not original or not shuffled: - raise ValueError("Both strings must be non-empty.") - - original_counter = Counter(x.casefold() for x in original) - shuffled_counter = Counter(x.casefold() for x in shuffled) - - difference_counter = shuffled_counter - original_counter - - if not difference_counter or sum(difference_counter.values()) > 1: - raise ValueError("No unique additional character found.") - - return next(difference_counter.elements()) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() diff --git a/challenge-255/steven-wilson/python/ch-02.py b/challenge-255/steven-wilson/python/ch-02.py deleted file mode 100644 index 2d5fc3ab9c..0000000000 --- a/challenge-255/steven-wilson/python/ch-02.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python3 - -from collections import Counter -import string - - -def most_frequent_word(paragraph, banned): - '''Given a paragraph and a banned word. Return the most frequent word that - is not banned - >>> most_frequent_word( - ... "Joe hit a ball, the hit ball flew far after it was hit.", "hit") - 'ball' - >>> most_frequent_word( - ... "Perl and Raku belong to the same family. " - ... "Perl is the most popular language in the weekly challenge.", "the") - 'Perl' - ''' - if not paragraph or not banned: - raise ValueError("Both strings must be non-empty.") - - paragraph_no_punctuation = paragraph.translate( - str.maketrans("", "", string.punctuation)) - paragraph_counter = Counter(paragraph_no_punctuation.split()) - - if banned in paragraph_counter: - del paragraph_counter[banned] - - if not paragraph_counter: - raise ValueError("No words found in the paragraph after removing punctuation and banned word.") - - return paragraph_counter.most_common()[0][0] - - -if __name__ == "__main__": - import doctest - - doctest.testmod() diff --git a/challenge-255/steven-wilson/python/ch-1.py b/challenge-255/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..dac2d858ab --- /dev/null +++ b/challenge-255/steven-wilson/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +from collections import Counter + + +def odd_character(original, shuffled): + ''' Given two strings, the second string generated using the shuffled + characters of the first string with an additional character. Function + returns the additional character. + >>> odd_character("Perl", "Preel") + 'e' + >>> odd_character("Weekly", "Weeakly") + 'a' + >>> odd_character("Box", "Boxy") + 'y' + ''' + if not original or not shuffled: + raise ValueError("Both strings must be non-empty.") + + original_counter = Counter(x.casefold() for x in original) + shuffled_counter = Counter(x.casefold() for x in shuffled) + + difference_counter = shuffled_counter - original_counter + + if not difference_counter or sum(difference_counter.values()) > 1: + raise ValueError("No unique additional character found.") + + return next(difference_counter.elements()) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/challenge-255/steven-wilson/python/ch-2.py b/challenge-255/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..2d5fc3ab9c --- /dev/null +++ b/challenge-255/steven-wilson/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +from collections import Counter +import string + + +def most_frequent_word(paragraph, banned): + '''Given a paragraph and a banned word. Return the most frequent word that + is not banned + >>> most_frequent_word( + ... "Joe hit a ball, the hit ball flew far after it was hit.", "hit") + 'ball' + >>> most_frequent_word( + ... "Perl and Raku belong to the same family. " + ... "Perl is the most popular language in the weekly challenge.", "the") + 'Perl' + ''' + if not paragraph or not banned: + raise ValueError("Both strings must be non-empty.") + + paragraph_no_punctuation = paragraph.translate( + str.maketrans("", "", string.punctuation)) + paragraph_counter = Counter(paragraph_no_punctuation.split()) + + if banned in paragraph_counter: + del paragraph_counter[banned] + + if not paragraph_counter: + raise ValueError("No words found in the paragraph after removing punctuation and banned word.") + + return paragraph_counter.most_common()[0][0] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() -- cgit