diff options
Diffstat (limited to 'challenge-278/roger-bell-west/python/ch-1.py')
| -rwxr-xr-x | challenge-278/roger-bell-west/python/ch-1.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-278/roger-bell-west/python/ch-1.py b/challenge-278/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..fbed9dc486 --- /dev/null +++ b/challenge-278/roger-bell-west/python/ch-1.py @@ -0,0 +1,28 @@ +#! /usr/bin/python3 + +import re + +def sortstring(a): + words = a.split(" ") + out = words.copy() + pr = re.compile(r'^(.*?)([0-9]+)$') + for w in words: + c = pr.search(w) + index = int(c.group(2)) - 1 + out[index] = c.group(1) + return " ".join(out) + +import unittest + +class TestSortstring(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(sortstring("and2 Raku3 cousins5 Perl1 are4"), "Perl and Raku are cousins", 'example 1') + + def test_ex2(self): + self.assertEqual(sortstring("guest6 Python1 most4 the3 popular5 is2 language7"), "Python is the most popular guest language", 'example 2') + + def test_ex3(self): + self.assertEqual(sortstring("Challenge3 The1 Weekly2"), "The Weekly Challenge", 'example 3') + +unittest.main() |
