Решение на Пет малки функции от Людмил Делчев

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

Към профила на Людмил Делчев

Резултати

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

Код

import itertools
def extract_type(symbols_list, wanted_type):
symbols = ""
for symbol_tuple in symbols_list:
if type(symbol_tuple[0]) is wanted_type:
for _ in itertools.repeat(None, symbol_tuple[1]):
symbols += str(symbol_tuple[0])
return symbols
def reversed_dict(dict):
reversed_dictionary = {}
for key in dict.keys():
if dict[key] not in reversed_dictionary:
reversed_dictionary[dict[key]] = key
return reversed_dictionary
def flatten_dict(dictionary, path=""):
items = []
for key in dictionary.keys():
if path == "":
current_key = key
else:
current_key = "{0}.{1}".format(path, key)
if type(dictionary[key]) is dict:
items.extend(flatten_dict(dictionary[key], current_key).items())
else:
items.append((current_key, dictionary[key]))
return dict(items)
def create_nested_dict_element(unnested_dict_element):
if "." not in unnested_dict_element[0]:
return dict([unnested_dict_element])
else:
current_key, rest = unnested_dict_element[0].split(".", maxsplit=1)
value = unnested_dict_element[1]
return {current_key: create_nested_dict_element((rest, value))}
def add_dicts_nested(first, second):
for key in second.keys():
if key not in first.keys():
first[key] = second[key]
else:
add_dicts_nested(first[key], second[key])
def unflatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
add_dicts_nested(result, create_nested_dict_element((key, value)))
return result
def reps(elements):
return tuple([item for item in elements if elements.count(item) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.132s

OK

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

Людмил обнови решението на 22.03.2015 16:30 (преди над 9 години)

+import itertools
+
+
+def extract_type(symbols_list, type):
+ symbols = ""
+ for symbol_tuple in symbols_list:
+ if isinstance(symbol_tuple[0], type):
+ for _ in itertools.repeat(None, symbol_tuple[1]):
+ symbols += str(symbol_tuple[0])
+ return symbols
+
+
+def reversed_dict(dict):
+ reversed_dictionary = {}
+ for key in dict.keys():
+ if dict[key] not in reversed_dictionary:
+ reversed_dictionary[dict[key]] = key
+ return reversed_dictionary
+
+
+def flatten_dict(dictionary, path=""):
+ items = []
+ for key in dictionary.keys():
+ if path == "":
+ current_key = key
+ else:
+ current_key = "{0}.{1}".format(path, key)
+ if type(dictionary[key]) is dict:
+ items.extend(flatten_dict(dictionary[key], current_key).items())
+ else:
+ items.append((current_key, dictionary[key]))
+ return dict(items)
+
+
+def create_nested_dict_element(unnested_dict_element):
+ if len(unnested_dict_element[0]) == 1:
+ return dict([unnested_dict_element])
+ else:
+ current_key, rest = unnested_dict_element[0].split(".", maxsplit=1)
+ value = unnested_dict_element[1]
+ return {current_key: create_nested_dict_element((rest, value))}
+
+
+def add_dicts_nested(first, second):
+ for key in second.keys():
+ if key not in first.keys():
+ first[key] = second[key]
+ else:
+ add_dicts_nested(first[key], second[key])
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ add_dicts_nested(result, create_nested_dict_element((key, value)))
+ return result
+
+
+def reps(elements):
+ return tuple([item for item in elements if elements.count(item) > 1])

Людмил обнови решението на 23.03.2015 01:10 (преди над 9 години)

import itertools
def extract_type(symbols_list, type):
symbols = ""
for symbol_tuple in symbols_list:
if isinstance(symbol_tuple[0], type):
for _ in itertools.repeat(None, symbol_tuple[1]):
symbols += str(symbol_tuple[0])
return symbols
def reversed_dict(dict):
reversed_dictionary = {}
for key in dict.keys():
if dict[key] not in reversed_dictionary:
reversed_dictionary[dict[key]] = key
return reversed_dictionary
def flatten_dict(dictionary, path=""):
items = []
for key in dictionary.keys():
if path == "":
current_key = key
else:
current_key = "{0}.{1}".format(path, key)
if type(dictionary[key]) is dict:
items.extend(flatten_dict(dictionary[key], current_key).items())
else:
items.append((current_key, dictionary[key]))
return dict(items)
def create_nested_dict_element(unnested_dict_element):
- if len(unnested_dict_element[0]) == 1:
+ if "." not in unnested_dict_element[0]:
return dict([unnested_dict_element])
else:
current_key, rest = unnested_dict_element[0].split(".", maxsplit=1)
value = unnested_dict_element[1]
return {current_key: create_nested_dict_element((rest, value))}
def add_dicts_nested(first, second):
for key in second.keys():
if key not in first.keys():
first[key] = second[key]
else:
add_dicts_nested(first[key], second[key])
def unflatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
add_dicts_nested(result, create_nested_dict_element((key, value)))
return result
def reps(elements):
return tuple([item for item in elements if elements.count(item) > 1])

Людмил обнови решението на 23.03.2015 01:16 (преди над 9 години)

import itertools
-def extract_type(symbols_list, type):
+def extract_type(symbols_list, wanted_type):
symbols = ""
for symbol_tuple in symbols_list:
- if isinstance(symbol_tuple[0], type):
+ if type(symbol_tuple[0]) is wanted_type:
for _ in itertools.repeat(None, symbol_tuple[1]):
symbols += str(symbol_tuple[0])
return symbols
def reversed_dict(dict):
reversed_dictionary = {}
for key in dict.keys():
if dict[key] not in reversed_dictionary:
reversed_dictionary[dict[key]] = key
return reversed_dictionary
def flatten_dict(dictionary, path=""):
items = []
for key in dictionary.keys():
if path == "":
current_key = key
else:
current_key = "{0}.{1}".format(path, key)
if type(dictionary[key]) is dict:
items.extend(flatten_dict(dictionary[key], current_key).items())
else:
items.append((current_key, dictionary[key]))
return dict(items)
def create_nested_dict_element(unnested_dict_element):
if "." not in unnested_dict_element[0]:
return dict([unnested_dict_element])
else:
current_key, rest = unnested_dict_element[0].split(".", maxsplit=1)
value = unnested_dict_element[1]
return {current_key: create_nested_dict_element((rest, value))}
def add_dicts_nested(first, second):
for key in second.keys():
if key not in first.keys():
first[key] = second[key]
else:
add_dicts_nested(first[key], second[key])
def unflatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
add_dicts_nested(result, create_nested_dict_element((key, value)))
return result
def reps(elements):
return tuple([item for item in elements if elements.count(item) > 1])