aboutsummaryrefslogtreecommitdiff
path: root/challenge-278/steven-wilson/python/ch-1.py
diff options
context:
space:
mode:
authorSteven <steven1170@zoho.eu>2024-07-15 15:22:37 +0100
committerSteven <steven1170@zoho.eu>2024-07-15 15:22:37 +0100
commit8df08850ad20a07dfb2fe4c52a9aca1b5325fc6f (patch)
tree4480902558ba504271caa40c45b0fb6e11f12e08 /challenge-278/steven-wilson/python/ch-1.py
parentf1533357698083086127e85e17fd8e2a80780e76 (diff)
downloadperlweeklychallenge-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-1.py')
-rw-r--r--challenge-278/steven-wilson/python/ch-1.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-278/steven-wilson/python/ch-1.py b/challenge-278/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..8e113ff425
--- /dev/null
+++ b/challenge-278/steven-wilson/python/ch-1.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+import re
+from operator import itemgetter
+
+
+def sort_string(string):
+ """ Given a shuffle string, return the sorted string. A string is shuffled
+ by appending word position to each word.
+
+ >>> sort_string("and2 Raku3 cousins5 Perl1 are4")
+ 'Perl and Raku are cousins'
+ >>> sort_string("guest6 Python1 most4 the3 popular5 is2 language7")
+ 'Python is the most popular guest language'
+ >>> sort_string("Challenge3 The1 Weekly2")
+ 'The Weekly Challenge'
+ """
+ p = re.compile('^(\w+)(\d+)$')
+ words_sorted = sorted((p.search(word).groups() for word in string.split(" ")), key=itemgetter(1))
+ return " ".join(word[0] for word in words_sorted)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)