from collections import defaultdict
class Tree(defaultdict):
def __init__(self):
super().__init__(Tree)
t = Tree()
t['a']['b']['c'] = 10
t['a']['b']['d'] = 20
t['a']['e'] = 5
def count_leaves(d):
total = 0
for v in d.values():
if isinstance(v, dict):
total += count_leaves(v)
else:
total += v
return total
print(count_leaves(t))
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
d = {'a': 1, 'b': 2, 'c': 3}
try:
for k in d:
del d[k]
print(len(d))
except RuntimeError:
print("RuntimeError")#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
👍1
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
🔥1
Please open Telegram to view this post
VIEW IN TELEGRAM
def modify(x, y=[]):
y.append(x)
return sum(y)
result = modify(5) + modify(7) + modify(4)
print(result)
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
def modify(x, y=[]):
y.append(x)
return sum(y)
result = modify(5) + modify(7) + modify(4)
print(result)
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
from itertools import islice
def infinite_seq():
n = 1
while True:
yield n
n *= 2
gen = infinite_seq()
result = list(islice(gen, 5))
print(sum(result))
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
❤2
def modify(x, y=[]):
y.append(x)
return sum(y)
result = modify(5) + modify(7) + modify(4)
print(result)
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM