Skip to content

Instantly share code, notes, and snippets.

@nerflad
Last active June 24, 2016 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerflad/c925d295f22780bab1cbb7a17934cf94 to your computer and use it in GitHub Desktop.
Save nerflad/c925d295f22780bab1cbb7a17934cf94 to your computer and use it in GitHub Desktop.
Takes arguments of any basic data type, returning sum of all ints
#!/usr/bin/env python
def add_ints(*args):
return_sum = 0
for i in args:
if type(i) in [list, tuple]:
for list_key in i:
return_sum += add_ints(list_key)
if type(i) == dict:
for dict_key in i:
return_sum += add_ints(i[dict_key])
try:
i = int(i)
except:
continue
else:
return_sum += i
return return_sum
def main():
# tests
my_var = add_ints((['1', '2', '3', '36'],))
my_var += add_ints({'one': 1, 2: 'two', 'testlist': [-1, 100]})
my_var += add_ints(-100)
print(my_var)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment