diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-07-16 13:05:06 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-07-16 13:05:06 -0400 |
| commit | f1ca8f7ac614d1b7cdc97a4284cfa710ab60f6c6 (patch) | |
| tree | f601757a365713812e069bd9f49d0ad05046fa41 /challenge-278/steven-wilson/python/ch-2.py | |
| parent | 99b5231db016ad59d74ae19ab4f706dab31e9207 (diff) | |
| parent | dd7b7f3344a33d4c8f1b6947e58eeb2ba02f84f3 (diff) | |
| download | perlweeklychallenge-club-f1ca8f7ac614d1b7cdc97a4284cfa710ab60f6c6.tar.gz perlweeklychallenge-club-f1ca8f7ac614d1b7cdc97a4284cfa710ab60f6c6.tar.bz2 perlweeklychallenge-club-f1ca8f7ac614d1b7cdc97a4284cfa710ab60f6c6.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-278/steven-wilson/python/ch-2.py')
| -rw-r--r-- | challenge-278/steven-wilson/python/ch-2.py | 29 |
1 files changed, 29 insertions, 0 deletions
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) |
