diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-16 11:43:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-16 11:43:44 +0100 |
| commit | 1f04d6355e6be40160565d5a29957c61c8a37e26 (patch) | |
| tree | 2b24eeaa0988426864f077c8d9a551e3482fbcfc | |
| parent | 7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2 (diff) | |
| parent | 330a2bbf9704b672fcc237c3d44e68bb2bad4109 (diff) | |
| download | perlweeklychallenge-club-1f04d6355e6be40160565d5a29957c61c8a37e26.tar.gz perlweeklychallenge-club-1f04d6355e6be40160565d5a29957c61c8a37e26.tar.bz2 perlweeklychallenge-club-1f04d6355e6be40160565d5a29957c61c8a37e26.zip | |
Merge pull request #10436 from oWnOIzRi/week278
add solutions week 278 in python
| -rw-r--r-- | challenge-278/steven-wilson/python/ch-1.py | 27 | ||||
| -rw-r--r-- | challenge-278/steven-wilson/python/ch-2.py | 29 |
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-278/steven-wilson/python/ch-1.py b/challenge-278/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..18b5904a2a --- /dev/null +++ b/challenge-278/steven-wilson/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import re +from operator import itemgetter + + +def sort_string(string): + """ Given a shuffle string, return the sorted string. A string is shuffled + by appending word position to each word. + + >>> sort_string("and2 Raku3 cousins5 Perl1 are4") + 'Perl and Raku are cousins' + >>> sort_string("guest6 Python1 most4 the3 popular5 is2 language7") + 'Python is the most popular guest language' + >>> sort_string("Challenge3 The1 Weekly2") + 'The Weekly Challenge' + """ + p = re.compile('^(\w+)(\d+)$') + words_numbers = (p.search(word).groups() for word in string.split(" ")) + words_numbers_sorted = sorted(((w[0], int(w[1])) for w in words_numbers), key=itemgetter(1)) + return " ".join(word[0] for word in words_numbers_sorted) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-278/steven-wilson/python/ch-2.py b/challenge-278/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..8cec33da3f --- /dev/null +++ b/challenge-278/steven-wilson/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + + +def reverse_word(word, char): + """ Given a word and a character, replace the substring up to and including + the character with its characters sorted alphabetically. If the character + doesn’t exist then DON'T do anything. + + >>> reverse_word("challenge", "e") + 'acehllnge' + >>> reverse_word("programming", "a") + 'agoprrmming' + >>> reverse_word("champion", "b") + 'champion' + >>> reverse_word("champion", "n") + 'achimnop' + """ + position = word.find(char) + + if position == -1: + return word + + return "".join(sorted(word[:position+1])) + word[position+1:] + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) |
