diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-17 23:04:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-17 23:04:29 +0100 |
| commit | 7fb6116d76583e09c32b53f319ed71d2d33b8183 (patch) | |
| tree | 8296312485f46c35493dfa9b5bb5215ebfdec572 /challenge-278/roger-bell-west/python/ch-1.py | |
| parent | 18572eea4648336b67cb95c2b2d01462277d8819 (diff) | |
| parent | 96abfd6c0b42e00d4f986e1f7a1114b13a406136 (diff) | |
| download | perlweeklychallenge-club-7fb6116d76583e09c32b53f319ed71d2d33b8183.tar.gz perlweeklychallenge-club-7fb6116d76583e09c32b53f319ed71d2d33b8183.tar.bz2 perlweeklychallenge-club-7fb6116d76583e09c32b53f319ed71d2d33b8183.zip | |
Merge pull request #10450 from Firedrake/rogerbw-challenge-278
RogerBW solutions for challenge no. 278
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() |
