aboutsummaryrefslogtreecommitdiff
path: root/challenge-278/roger-bell-west/python/ch-1.py
blob: fbed9dc486ea65e5a3ab77a85e7cbd5359e93927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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()