Решение на Пет малки функции от Янислав Василев

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

Към профила на Янислав Василев

Резултати

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

Код

def extract_type(data, extracting_type):
return ''.join([str(element[0]) * element[1]
for element in data
if type(element[0]) == extracting_type])
def reversed_dict(data):
return dict(map(reversed, data.items()))
# ...
def flatten_dict(data, parent_key=''):
items, separator = [], '.'
for key, value in data.items():
new_key = parent_key + separator + key if parent_key else key
if isinstance(value, dict):
items.extend(flatten_dict(value, new_key).items())
else:
items.append((new_key, value))
return dict(items)
# ...
def unflatten_dict(data):
unflattened_dict = {}
for key, value in data.items():
keys = key.split(".")
value_dict = unflattened_dict
for new_key in keys[:-1]:
if new_key not in value_dict:
value_dict[new_key] = {}
value_dict = value_dict[new_key]
value_dict[keys[-1]] = value
return unflattened_dict
def reps(data):
return tuple([element for element in data if data.count(element) != 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.133s

OK

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

Янислав обнови решението на 23.03.2015 04:01 (преди над 9 години)

+from itertools import repeat
+
+
+def extract_type(data, extracting_type):
+ extracted_list = []
+ for element in data:
+ if type(element[0]) == extracting_type:
+ extracted_list.extend(repeat(element[0], element[1]))
+ return ''.join(map(str, extracted_list))
+
+
+def reversed_dict(data):
+ return dict(map(reversed, data.items()))
+
+
+# ...
+def flatten_dict(data, parent_key=''):
+ items, separator = [], '.'
+ for key, value in data.items():
+ new_key = parent_key + separator + key if parent_key else key
+ if isinstance(value, dict):
+ items.extend(flatten_dict(value, new_key).items())
+ else:
+ items.append((new_key, value))
+ return dict(items)
+
+
+# ...
+def unflatten_dict(data):
+ unflattened_dict = {}
+ for key, value in data.items():
+ keys = key.split(".")
+ value_dict = unflattened_dict
+ for new_key in keys[:-1]:
+ if new_key not in value_dict:
+ value_dict[new_key] = {}
+ value_dict = value_dict[new_key]
+ value_dict[keys[-1]] = value
+ return unflattened_dict
+
+
+def reps(data):
+ return tuple([element for element in data if data.count(element) != 1])

Янислав обнови решението на 23.03.2015 04:08 (преди над 9 години)

-from itertools import repeat
-
-
def extract_type(data, extracting_type):
- extracted_list = []
- for element in data:
- if type(element[0]) == extracting_type:
- extracted_list.extend(repeat(element[0], element[1]))
- return ''.join(map(str, extracted_list))
+ return ''.join([str(element[0]) * element[1]
+ for element in data
+ if type(element[0]) == extracting_type])
def reversed_dict(data):
return dict(map(reversed, data.items()))
# ...
def flatten_dict(data, parent_key=''):
items, separator = [], '.'
for key, value in data.items():
new_key = parent_key + separator + key if parent_key else key
if isinstance(value, dict):
items.extend(flatten_dict(value, new_key).items())
else:
items.append((new_key, value))
return dict(items)
-
# ...
+
+
def unflatten_dict(data):
unflattened_dict = {}
for key, value in data.items():
keys = key.split(".")
value_dict = unflattened_dict
for new_key in keys[:-1]:
if new_key not in value_dict:
value_dict[new_key] = {}
value_dict = value_dict[new_key]
value_dict[keys[-1]] = value
return unflattened_dict
def reps(data):
return tuple([element for element in data if data.count(element) != 1])

Янислав обнови решението на 23.03.2015 04:09 (преди над 9 години)

def extract_type(data, extracting_type):
return ''.join([str(element[0]) * element[1]
for element in data
if type(element[0]) == extracting_type])
def reversed_dict(data):
return dict(map(reversed, data.items()))
# ...
def flatten_dict(data, parent_key=''):
items, separator = [], '.'
for key, value in data.items():
new_key = parent_key + separator + key if parent_key else key
if isinstance(value, dict):
items.extend(flatten_dict(value, new_key).items())
else:
items.append((new_key, value))
return dict(items)
-# ...
-
+# ...
def unflatten_dict(data):
unflattened_dict = {}
for key, value in data.items():
keys = key.split(".")
value_dict = unflattened_dict
for new_key in keys[:-1]:
if new_key not in value_dict:
value_dict[new_key] = {}
value_dict = value_dict[new_key]
value_dict[keys[-1]] = value
return unflattened_dict
def reps(data):
- return tuple([element for element in data if data.count(element) != 1])
+ return tuple([element for element in data if data.count(element) != 1])