Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def sum_squares(nested_list):
- """
- Write a function that sums squares of numbers in list.
- That list may contain additional lists.
- (Hint use the type() or isinstance() function)
- sum_squares([1, 2, 3]) -> 14
- sum_squares([[1, 2], 3]) -> sum_squares([1, 2]) + 9 -> 1 + 4 + 9 -> 14
- sum_squares([[[[[[[[[2]]]]]]]]]) -> 4
- :param nested_list: list of lists of lists of lists of lists ... and ints
- :return: sum of squares
- """
- assert nested_list >= 0
- if nested_list == 0:
- return 0
- else:
- return sum_squares(nested_list - 1) + nested_list * nested_list
- if __name__ == "__main__":
- print(sum_squares([1, 2, 3])) # -> 14
- print(sum_squares([[1, 2], 3])) # -> 14
- print(sum_squares([[[[[[[[[2]]]]]]]]])) # -> 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement