diff options
| author | Steven <steven1170@zoho.eu> | 2024-07-15 15:22:37 +0100 |
|---|---|---|
| committer | Steven <steven1170@zoho.eu> | 2024-07-15 15:22:37 +0100 |
| commit | 8df08850ad20a07dfb2fe4c52a9aca1b5325fc6f (patch) | |
| tree | 4480902558ba504271caa40c45b0fb6e11f12e08 /challenge-278/steven-wilson/python/ch-2.py | |
| parent | f1533357698083086127e85e17fd8e2a80780e76 (diff) | |
| download | perlweeklychallenge-club-8df08850ad20a07dfb2fe4c52a9aca1b5325fc6f.tar.gz perlweeklychallenge-club-8df08850ad20a07dfb2fe4c52a9aca1b5325fc6f.tar.bz2 perlweeklychallenge-club-8df08850ad20a07dfb2fe4c52a9aca1b5325fc6f.zip | |
add solutions week 278 in python
Diffstat (limited to 'challenge-278/steven-wilson/python/ch-2.py')
| -rw-r--r-- | challenge-278/steven-wilson/python/ch-2.py | 27 |
1 files changed, 27 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..2c6f1e5423 --- /dev/null +++ b/challenge-278/steven-wilson/python/ch-2.py @@ -0,0 +1,27 @@ +#!/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' + """ + 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) |
