Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Ceil solution in Clear category for Split List by nomis517
from math import ceil
def split_list(items: list) -> list:
"""
Splits a list in two lists that have equal size except if the number
of items is odd then the first list has one more element than the second
one
"""
len1 = ceil(len(items) / 2)
return [items[:len1], items[len1:]]
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!")
March 30, 2020