Last active
June 14, 2016 19:14
-
-
Save rytoj/40f2a7205fe6cb075fe5dcd37bd65ecb to your computer and use it in GitHub Desktop.
nested_sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
T = ["abc",1, 10, [3,2,5], 20, [5,5], "50" ] | |
newList=[] | |
def nested_sum(newstedList): | |
''' | |
newstedList: list composed of nested lists containing int. | |
newlist: New flat list composed of list elements. | |
''' | |
for i in range(len(newstedList)): | |
if type(newstedList[i]) == int: | |
newList.append(newstedList[i]) | |
elif type(newstedList[i]) == str: | |
continue | |
else: | |
nested_sum(newstedList[i]) | |
# print(newList, "Sum:", sum(newList)) | |
return sum(newList) | |
print(nested_sum(T)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment