Преглед на файлове

Add string replacement with regexp in python

Snow преди 8 години
родител
ревизия
764bd0f78e
променени са 1 файла, в които са добавени 14 реда и са изтрити 0 реда
  1. 14 0
      arrays-and-strings/string-replacement.py

+ 14 - 0
arrays-and-strings/string-replacement.py

@@ -0,0 +1,14 @@
+import pytest
+import re
+
+def test_string_rep():
+    cases = ['', ' ', 'a', 'a s !']
+    expected = ['', '%20', 'a', 'a%20s%20!']
+    for c, e in zip(cases, expected):
+        assert string_rep(c) == e
+
+pytest.main()
+
+def string_rep(input):
+    p = re.compile(' ')
+    return p.sub('%20', input)