Написал такое решение, может разделить минус как показано в одном из асссертов.
from itertools import groupby
def join_segments(string: str, group_len: int, sep: str = ' '):
groups = groupby(
enumerate(reversed(string)),
lambda el: el[0] // group_len
)
string = sep.join(
''.join(char for i, char in group)
for _, group in groups
)
return string[::-1]
if __name__ == '__main__':
assert join_segments('1605', 4) == '1605'
assert join_segments('9550', 4) == '9550'
assert join_segments('12556430', 4) == '1255 6430'
assert join_segments('1', 4) == '1'
assert join_segments('-1234', 4) == '- 1234'
assert join_segments('1234', 1) == '1 2 3 4'
assert join_segments('1234', 10) == '1234'
assert join_segments('', 10) == ''