aboutsummaryrefslogtreecommitdiff
path: root/challenge-275/roger-bell-west/python/ch-2.py
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-25 22:27:26 +0100
committerGitHub <noreply@github.com>2024-06-25 22:27:26 +0100
commit9a8f0b8043c806ea406fdaddc718ccd263fbc093 (patch)
tree6084b8d9d56b7370d12c2de83e952ae23bb4eb90 /challenge-275/roger-bell-west/python/ch-2.py
parent815048f1183179a10ebb61c46e2c4160925b837b (diff)
parent508b49f4cef1ec360e5a9ec738c46f60a92c3c0b (diff)
downloadperlweeklychallenge-club-9a8f0b8043c806ea406fdaddc718ccd263fbc093.tar.gz
perlweeklychallenge-club-9a8f0b8043c806ea406fdaddc718ccd263fbc093.tar.bz2
perlweeklychallenge-club-9a8f0b8043c806ea406fdaddc718ccd263fbc093.zip
Merge pull request #10325 from Firedrake/rogerbw-challenge-275
RogerBW solutions for challenge no. 275
Diffstat (limited to 'challenge-275/roger-bell-west/python/ch-2.py')
-rwxr-xr-xchallenge-275/roger-bell-west/python/ch-2.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-275/roger-bell-west/python/ch-2.py b/challenge-275/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..fe1a9c1eee
--- /dev/null
+++ b/challenge-275/roger-bell-west/python/ch-2.py
@@ -0,0 +1,32 @@
+#! /usr/bin/python3
+
+def replacedigits(a):
+ out = ""
+ prev = 0
+ digits = {chr(asc) for asc in list(range(ord('0'), ord('9')+1))}
+ for c in a:
+ if c >= '0' and c <= '9':
+ out += chr(prev + int(c))
+ else:
+ prev = ord(c)
+ out += c
+ return out
+
+
+import unittest
+
+class TestReplacedigits(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(replacedigits("a1c1e1"), "abcdef", 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(replacedigits("a1b2c3d4"), "abbdcfdh", 'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(replacedigits("b2b"), "bdb", 'example 3')
+
+ def test_ex4(self):
+ self.assertEqual(replacedigits("a16z"), "abgz", 'example 4')
+
+unittest.main()