In: Computer Science
Python HW with Jupyter Notebook
Dictionary Data
DATA = {
2: 7493945,
76: 4654320,
3: 4091979,
90: 1824881,
82: 714422,
45: 1137701,
10: 374362,
0: 326226,
-15: 417203,
-56: 333525,
67: 323451,
99: 321696,
21: 336753,
-100: 361237,
55: 1209714,
5150: 1771800,
42: 4714011,
888: 14817667,
3500: 13760234,
712: 10903322,
7: 10443792,
842: 11716264,
18584: 10559923,
666: 9275602,
70: 11901200,
153: 12074784,
8: 4337229
}
Expected Output
>>> iter_dict_funky_sum(DATA) 140166242
Code:
# d is the input dictionary
def iter_dict_funky_sum(d):
total = 0
for key,value in d.items(): # iterate through dictionary in a single loop
total = total + (value - key) # append to total value - key
return total # return total
DATA = {
2: 7493945,
76: 4654320,
3: 4091979,
90: 1824881,
82: 714422,
45: 1137701,
10: 374362,
0: 326226,
-15: 417203,
-56: 333525,
67: 323451,
99: 321696,
21: 336753,
-100: 361237,
55: 1209714,
5150: 1771800,
42: 4714011,
888: 14817667,
3500: 13760234,
712: 10903322,
7: 10443792,
842: 11716264,
18584: 10559923,
666: 9275602,
70: 11901200,
153: 12074784,
8: 4337229
}
iter_dict_funky_sum(DATA)
code screenshot:

===========
Code alongs with comment and screenshot has been added.