-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.py
28 lines (23 loc) · 778 Bytes
/
t.py
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
import unittest
def replaced(s, old, new):
out = []
i = 0
while i < len(s):
j = s.find(old, i)
if j == -1:
break
out.append(s[i:j])
out.append(new)
i = j + 1
if i < len(s):
out.append(s[i:])
return "".join(out)
class Test_Replaced(unittest.TestCase):
def test_replaced(self):
self.assertEqual(replaced("abc", "a", "d"), "dbc")
self.assertEqual(replaced("abc", "c", "d"), "abd")
self.assertEqual(replaced("abc", "d", ""), "abc")
self.assertEqual(replaced("aa", "a", "b"), "bb")
self.assertEqual(replaced("aa", "a", "ab"), "abab")
self.assertEqual(replaced("aa", "a", "bc"), "bcbc")
self.assertEqual(replaced("aa", "a", "ba"), "baba")