I was reluctant to keep disclosing incorrect information, so I rewrote it. I tried to write several ways while reflecting the points I received.
def map_all(es):
return all([e == es[0] for e in es[1:]]) if es else False
def set_list(es):
return len(list(set(es))) == 1
TypeError: unhashable type: 'list'
def recursive(es):
return _rec(es, es[0], True) if es else False
def _rec(es, head, acc):
return _rec(es[1:], head, acc and head == es[0]) if es else acc
reduce
def multi_diff(es):
return es == [es[0]] * len(es) if es else False
Last time it was so good that I tested it to some extent this time ...
def tests(expected, elements):
assert map_all(elements) == expected
assert set_list(elements) == expected
assert recursive(elements) == expected
assert multi_diff(elements) == expected
T, F, N = True, False, None
tests(True, [-1])
tests(True, [0])
tests(True, [1])
tests(True, [2])
tests(True, ['T'])
tests(True, [T])
tests(True, [N])
tests(True, [-1, -1])
tests(True, [0, 0])
tests(True, [1, 1])
tests(True, [2, 2])
tests(True, ['T', 'T'])
tests(True, [T, T])
tests(True, [F, F])
tests(True, [N, N])
tests(True, [-1, -1, -1])
tests(True, [0, 0, 0])
tests(True, [1, 1, 1])
tests(True, [2, 2, 2])
tests(True, ['T', 'T', 'T'])
tests(True, [T, T, T])
tests(True, [F, F, F])
tests(True, [N, N, N])
tests(False, [])
tests(False, [0, 1])
tests(False, [1, 0])
tests(False, [0, 2])
tests(False, [1, 2])
tests(False, ['T', 'F'])
tests(False, ['F', 'T'])
tests(False, [T, F])
tests(False, [F, T])
tests(False, [T, N])
tests(False, [N, F])
tests(False, [N, 0])
tests(False, [0, N])
tests(False, [0, 0, 1])
tests(False, [0, 1, 0])
tests(False, [T, F, F])
tests(False, [F, T, F])
tests(False, [F, F, T])
tests(False, [F, T, T])
tests(False, [T, F, T])
tests(False, [T, T, F])
tests(False, [T, T, N])
tests(True, [[]])
tests(True, [[], []])
tests(True, [[], [], []])
tests(True, [[5]])
tests(True, [[5], [5]])
tests(True, [[5], [5], [5]])
tests(False, [[], [5]])
tests(False, [[3], [5]])
tests(False, [[3], [3, 3]])
Last time I said this, oh embarrassing ...
Recommended Posts