diff options
Diffstat (limited to 'challenge-278/pokgopun/python/ch-2.py')
| -rw-r--r-- | challenge-278/pokgopun/python/ch-2.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/challenge-278/pokgopun/python/ch-2.py b/challenge-278/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..a5e1e34059 --- /dev/null +++ b/challenge-278/pokgopun/python/ch-2.py @@ -0,0 +1,55 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-278/ +""" + +Task 2: Reverse Word + +Submitted by: [52]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a word, $word and a character, $char. + + Write a script to replace the substring up to and including $char with + its characters sorted alphabetically. If the $char doesn’t exist then + DON'T do anything. + +Example 1 + +Input: $str = "challenge", $char = "e" +Ouput: "acehllnge" + +Example 2 + +Input: $str = "programming", $char = "a" +Ouput: "agoprrmming" + +Example 3 + +Input: $str = "champion", $char = "b" +Ouput: "champion" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 21st July 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def reverseWord(word, char): + i = word.find(char) + if i < 0: + return word + return "".join(sorted(word[:i+1]))+word[i+1:] + +import unittest + +class TestReverseWord(unittest.TestCase): + def test(self): + for (word,char), otpt in { + ("challenge", "e"): "acehllnge", + ("programming", "a"):"agoprrmming", + ("champion", "b"): "champion", + }.items(): + self.assertEqual(reverseWord(word,char), otpt) + +unittest.main() |
