Решение на Пет малки функции от Георги Мавридис

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

Към профила на Георги Мавридис

Резултати

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

Код

def extract_type(text, type_to_extr):
result = ""
for x in text:
if type(x[0]) == type_to_extr:
result += str(x[0]) * x[1]
return result
def reversed_dict(to_reverse):
return {value: key for key, value in to_reverse.items()}
def change_dict(dict_to_change, key_, value_dict):
for key, value in value_dict.items():
dict_to_change[key_ + '.' + key] = value
del dict_to_change[key_]
r = dict(dict_to_change)
return r
def flatten_dict(my_dict):
flatten_dictionary = {}
for key, value in my_dict.items():
if type(value) == dict:
for key_prim, value_prim in value.items():
if type(value_prim) == dict:
value.update(change_dict({keys: values
for keys, values
in value.items()},
key_prim, value_prim))
del value[key_prim]
del my_dict[key]
my_dict[key] = value
return flatten_dict(my_dict)
flatten_dictionary[key + '.' + key_prim] = value_prim
else:
flatten_dictionary[key] = value
return flatten_dictionary
def reps(arr):
result = ()
for num in arr:
if arr.count(num) != 1:
result += (num, )
return result

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.139s

OK

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

Георги обнови решението на 23.03.2015 15:08 (преди около 9 години)

+def extract_type(text, type_to_extr):
+ result = ""
+ for x in text:
+ if type(x[0]) == type_to_extr:
+ result += str(x[0]) * x[1]
+ return result
+
+
+def reversed_dict(to_reverse):
+ return {value: key for key, value in to_reverse.items()}
+
+
+def change_dict(dict_to_change, key_, value_dict):
+ for key, value in value_dict.items():
+ dict_to_change[key_ + '.' + key] = value
+ del dict_to_change[key_]
+ r = dict(dict_to_change)
+ return r
+
+
+def flatten_dict(my_dict):
+ flatten_dictionary = {}
+ for key, value in my_dict.items():
+ if type(value) == dict:
+ for key_prim, value_prim in value.items():
+ if type(value_prim) == dict:
+ value.update(change_dict({keys: values
+ for keys, values
+ in value.items()},
+ key_prim, value_prim))
+ del value[key_prim]
+ del my_dict[key]
+ my_dict[key] = value
+ return flatten_dict(my_dict)
+ flatten_dictionary[key + '.' + key_prim] = value_prim
+
+ else:
+ flatten_dictionary[key] = value
+ return flatten_dictionary
+
+
+def reps(arr):
+ result = ()
+ for num in arr:
+ if arr.count(num) != 1:
+ result += (num, )
+ return result