Решение на Пет малки функции от Стилиян Стоянов

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

Към профила на Стилиян Стоянов

Резултати

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

Код

def reversed_dict(some_dict):
return {value: key for key, value in some_dict.items()}
def reps(numbers):
return tuple([num for num in numbers if numbers.count(num) > 1])
def extract_type(zipped_text, needed_type):
extracted_symbols = ""
for symbol in zipped_text:
if(type(symbol[0]) is needed_type):
for i in range(symbol[1]):
extracted_symbols += str(symbol[0])
return extracted_symbols
def flatten_dict(some_dict):
def expand(key, value):
if type(value) is dict:
return [(key + '.' + k, v) for k, v in flatten_dict(value).items()]
else:
return [(key, value)]
items = [item for k, v in some_dict.items() for item in expand(k, v)]
return dict(items)
def unflatten_dict(some_dict):
nested_dict = {}
for key, value in some_dict.items():
parts = key.split('.')
current = nested_dict
for part in parts[:-1]:
if part not in current:
current[part] = {}
current = current[part]
current[parts[-1]] = value
return nested_dict

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.163s

OK

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

Стилиян обнови решението на 23.03.2015 03:21 (преди над 9 години)

+def reversed_dict(some_dict):
+ inversed_dict = {value: key for key, value in some_dict.items()}
+ return inversed_dict
+
+
+def reps(numbers):
+ only_repeteaed = [num for num in numbers if numbers.count(num) > 1]
+ return tuple(only_repeteaed)
+
+
+def extract_type(zipped_text, needed_type):
+ extracted_symbols = ""
+ for symbol in zipped_text:
+ if(type(symbol[0]) is needed_type):
+ for i in range(symbol[1]):
+ extracted_symbols += str(symbol[0])
+ return extracted_symbols

Стилиян обнови решението на 23.03.2015 03:33 (преди над 9 години)

def reversed_dict(some_dict):
- inversed_dict = {value: key for key, value in some_dict.items()}
- return inversed_dict
+ return {value: key for key, value in some_dict.items()}
def reps(numbers):
- only_repeteaed = [num for num in numbers if numbers.count(num) > 1]
- return tuple(only_repeteaed)
+ return tuple([num for num in numbers if numbers.count(num) > 1])
def extract_type(zipped_text, needed_type):
extracted_symbols = ""
for symbol in zipped_text:
if(type(symbol[0]) is needed_type):
for i in range(symbol[1]):
extracted_symbols += str(symbol[0])
return extracted_symbols

Стилиян обнови решението на 23.03.2015 16:09 (преди над 9 години)

def reversed_dict(some_dict):
return {value: key for key, value in some_dict.items()}
def reps(numbers):
return tuple([num for num in numbers if numbers.count(num) > 1])
def extract_type(zipped_text, needed_type):
extracted_symbols = ""
for symbol in zipped_text:
if(type(symbol[0]) is needed_type):
for i in range(symbol[1]):
extracted_symbols += str(symbol[0])
- return extracted_symbols
+ return extracted_symbols
+
+
+def flatten_dict(some_dict):
+ def expand(key, value):
+ if type(value) is dict:
+ return [(key + '.' + k, v) for k, v in flatten_dict(value).items()]
+ else:
+ return [(key, value)]
+
+ items = [item for k, v in some_dict.items() for item in expand(k, v)]
+
+ return dict(items)
+
+
+def unflatten_dict(some_dict):
+ nested_dict = {}
+ for key, value in some_dict.items():
+ parts = key.split('.')
+ current = nested_dict
+ for part in parts[:-1]:
+ if part not in current:
+ current[part] = {}
+ current = current[part]
+ current[parts[-1]] = value
+ return nested_dict