Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Split List by COLDPLAY
def split_list(items: list) -> list:
"""I create 3 variable, result for stocking the result and 2 others
different variables in list format.
I check the longer of list to verify if there are elements and i check also
if the list is divisible by 2.I put every element of list in 2 differents lists
even for the empty list"""
result = list()
liste = list()
lista = list()
if len(items) > 0:
if len(items) % 2 == 0:
for i in range(0, len(items)//2):
liste.append(items[i])
for i in range(len(items)//2, len(items)):
lista.append(items[i])
else:
for i in range(0, (len(items)//2) + 1):
liste.append(items[i])
for i in range((len(items)//2) + 1, len(items)):
lista.append(items[i])
result.append(liste)
result.append(lista)
return result
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!")
June 11, 2021
Comments: