my-psb/technical_tests/reorganize_string.py

43 lines
879 B
Python
Raw Normal View History

2020-07-16 06:10:21 +00:00
#!/usr/bin/env python3
# coding: utf8
"""
Author: freezed <git@freezed.me> 2020-07-16
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
This file is part of [`free_zed/mypsb`](https://gitlab.com/free_zed/mypsb/)
"""
def main(string, n, sep):
"""
This function reorganize `string` by removing spaces & groups by `n`
characters separated by `sep`.
:Tests:
>>> main("ab c de fgh ijk", 2, "|")
'ab|cd|ef|gh|ij|k'
2020-07-16 07:26:15 +00:00
>>> main("ab c de fgh ijk", 3, "_")
'abc_def_ghi_jk'
>>> main("ab c de fgh ijk", 4, "/")
'abcd/efgh/ijk'
2020-07-16 06:10:21 +00:00
"""
2020-07-16 06:55:26 +00:00
2020-07-16 07:14:33 +00:00
strings = list()
2020-07-16 06:55:26 +00:00
stack = "".join(string.split(" "))
2020-07-16 07:14:33 +00:00
steps = round(len(stack) / n)
while steps != 0:
strings.append(stack[:n])
stack = stack[n:]
steps -= 1
2020-07-16 06:10:21 +00:00
return sep.join(strings)
2020-07-16 06:10:21 +00:00
if __name__ == "__main__":
import doctest
doctest.testmod()