-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.py
37 lines (33 loc) · 1.09 KB
/
strings.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
29
30
31
32
33
34
35
36
37
def all_equal(sequence):
"""Test if all items in the sequence are equal or if the sequence is
empty.
>>> all_equal('aaaa')
True
>>> all_equal([])
True
>>> all_equal(range(5))
False
"""
# solution from http://stackoverflow.com/a/3844832/4621513
return len(set(sequence)) <= 1
def string_is_rectangular(s):
"""Test if a string occupies a rectangular area when printed, i.e.,
all lines have the same length.
>>> string_is_rectangular('')
True
>>> string_is_rectangular('asdf')
True
>>> string_is_rectangular('asdf\\nqwer')
True
>>> string_is_rectangular('asdf\\nqwert')
False
"""
return all_equal(map(len, s.splitlines()))
def string_join_horizontal(strings, between=''):
"""Join a sequence of rectangular strings horizontally, preserving the
layout of each string. All strings must have the same number of lines.
Between two strings a column of `between` is inserted.
"""
splitlines = str.splitlines
return '\n'.join(between.join(lines)
for lines in zip(*map(splitlines, strings)))