프로그래밍 문제/[Python] CheckIO
[CheckIO] Solve split list
UltraLowTemp-Physics
2021. 1. 5. 10:08
Problem: py.checkio.org/en/mission/split-list/
My solution
def split_list(items: list) -> list:
# your code here
length = len(items)
if length % 2 == 0: return [items[:length//2],items[length//2:]]
else: return [items[:length//2+1],items[length//2+1:]]
if __name__ == '__main__':
print("Example:")
print(split_list([1, 2, 3, 4, 5, 6]))
# These "asserts" are used for self-checking and not for an auto-testing
assert split_list([1, 2, 3, 4, 5, 6]) == [[1, 2, 3], [4, 5, 6]]
assert split_list([1, 2, 3]) == [[1, 2], [3]]
assert split_list([1, 2, 3, 4, 5]) == [[1, 2, 3], [4, 5]]
assert split_list([1]) == [[1], []]
assert split_list([]) == [[], []]
print("Coding complete? Click 'Check' to earn cool rewards!")
A good solution that I see
def split_list(items: list) -> list:
return [items[:(split_index := (len(items) + 1) // 2)], items[split_index:]]