Решение на Пет малки функции от Спасимир Нонев

Обратно към всички решения

Към профила на Спасимир Нонев

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 19 успешни тест(а)
  • 0 неуспешни тест(а)

Код

def extract_type(pairs, variety):
extracted = ''
for item in pairs:
if type(item[0]) is variety:
extracted += (str(item[0]) * item[1])
return extracted
def reversed_dict(dictionary):
return {dictionary[k]: k for k in dictionary}
def flatten_dict(dictionary):
def flatten(key, value):
if type(value) is dict:
return [(key + '.' + subkey, subvalue)
for subkey, subvalue
in flatten_dict(value).items()]
else:
return [(key, value)]
items = [item for subkey, subvalue
in dictionary.items()
for item in flatten(subkey, subvalue)]
return dict(items)
def unflatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
sub = result
for subkey in key.split('.')[:-1]:
if subkey not in sub:
sub[subkey] = {}
sub = sub[subkey]
sub[key.split('.')[-1]] = value
return result
def reps(sequence):
return tuple([i for i in sequence if not sequence.count(i) == 1])

Лог от изпълнението

...................
----------------------------------------------------------------------
Ran 19 tests in 0.172s

OK

История (1 версия и 0 коментара)

Спасимир обнови решението на 22.03.2015 03:00 (преди над 9 години)

+def extract_type(pairs, variety):
+ extracted = ''
+ for item in pairs:
+ if type(item[0]) is variety:
+ extracted += (str(item[0]) * item[1])
+ return extracted
+
+
+def reversed_dict(dictionary):
+ return {dictionary[k]: k for k in dictionary}
+
+
+def flatten_dict(dictionary):
+ def flatten(key, value):
+ if type(value) is dict:
+ return [(key + '.' + subkey, subvalue)
+ for subkey, subvalue
+ in flatten_dict(value).items()]
+ else:
+ return [(key, value)]
+
+ items = [item for subkey, subvalue
+ in dictionary.items()
+ for item in flatten(subkey, subvalue)]
+ return dict(items)
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ sub = result
+ for subkey in key.split('.')[:-1]:
+ if subkey not in sub:
+ sub[subkey] = {}
+ sub = sub[subkey]
+ sub[key.split('.')[-1]] = value
+ return result
+
+
+def reps(sequence):
+ return tuple([i for i in sequence if not sequence.count(i) == 1])