aboutsummaryrefslogtreecommitdiff
path: root/challenge-322/steven-wilson/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-322/steven-wilson/python/ch-1.py')
-rw-r--r--challenge-322/steven-wilson/python/ch-1.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-322/steven-wilson/python/ch-1.py b/challenge-322/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..1aa1d9b9c7
--- /dev/null
+++ b/challenge-322/steven-wilson/python/ch-1.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python3
+
+from more_itertools import chunked
+
+
+def string_format(string, size):
+ """ Given a string and a positive integer, format the string, removing any
+ dashes, in groups of size given by the integer. The first group can be
+ smaller than the integer but should have at least one character. Groups
+ should be separated by dashes.
+
+ >>> string_format("ABC-D-E-F", 3)
+ 'ABC-DEF'
+ >>> string_format("A-BC-D-E", 2)
+ 'A-BC-DE'
+ >>> string_format("-A-B-CD-E", 4)
+ 'A-BCDE'
+ """
+ characters = [c for c in reversed(string) if c.isalpha()]
+ chunks = chunked(characters, size)
+ grouped = ["".join(a) for a in chunks]
+ return "-".join(grouped)[::-1]
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)