Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for Sum by Type by olevoniuk
from typing import Tuple
def sum_by_types(items: list) -> Tuple[str, int]:
def go(items: list, acc: Tuple[str, int]) -> Tuple[str, int]:
if items == []:
return acc
if type(items[0]) is str:
return go(items[1:], (acc[0] + items[0], acc[1]))
return go(items[1:], (acc[0], acc[1] + items[0]))
return go(items, ('', 0))
if __name__ == '__main__':
print("Example:")
print(sum_by_types([]))
# These "asserts" are used for self-checking and not for an auto-testing
assert sum_by_types([]) == ('', 0)
assert sum_by_types([1, 2, 3]) == ('', 6)
assert sum_by_types(['1', 2, 3]) == ('1', 5)
assert sum_by_types(['1', '2', 3]) == ('12', 3)
assert sum_by_types(['1', '2', '3']) == ('123', 0)
assert sum_by_types(['size', 12, 'in', 45, 0]) == ('sizein', 57)
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 17, 2020
Comments: