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

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

Към профила на Павел Димитров

Резултати

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

Код

def extract_type(items, return_type):
extracted_val = [str(k[0]) * k[1] for k in
items if type(k[0]) is return_type]
return "".join(extracted_val)
def reversed_dict(items):
reversed_result = {value: key for key, value in items.items()}
return reversed_result
def reps(items):
rep = [element for element in items if items.count(element) > 1]
return tuple(rep)
def flatten_dict(items):
flatten_reuslt = {}
def _flatten_dict(key, value):
if isinstance(value, dict):
for key_in_value in value:
_flatten_dict(key + "." + key_in_value, value[key_in_value])
else:
flatten_reuslt[key] = value
for key, value in items.items():
_flatten_dict(key, value)
return flatten_reuslt
def unflatten_dict(items):
result = {}
def _merge_dicts(dict1, dict2):
if not isinstance(dict1, dict) or not isinstance(dict2, dict):
return dict2
for key in dict2:
if key in dict1:
dict1[key] = _merge_dicts(dict1[key], dict2[key])
else:
dict1[key] = dict2[key]
return dict1
def _unflatten_dict(key, value):
if key.count(".") > 0:
split_dict = key.split(".")
new_key = ".".join(split_dict[0:len(split_dict) - 1])
new_value = {str(split_dict[-1]): value}
_unflatten_dict(new_key, new_value)
else:
if key in result.keys():
result[key] = _merge_dicts(result[key], value)
else:
result[key] = value
for key, value in items.items():
_unflatten_dict(key, value)
return result

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.162s

OK

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

Павел обнови решението на 17.03.2015 13:53 (преди над 9 години)

+def extract_type(items, return_type):
+ extracted_val = ""
+ for k in items:
+ if isinstance(k[0], return_type):
+ extracted_val += str(k[0]) * k[1]
+ return extracted_val
+
+
+def reversed_dict(items):
+ reversed_dict = {}
+ for k in items:
+ reversed_dict[items[k]] = k
+ return reversed_dict
+
+
+def reps(items):
+ rep = []
+ for k in items:
+ if items.count(k) > 1:
+ rep.append(k)
+ return tuple(rep)
+
+
+def flatten_dict(items):
+ flat_dict = {}
+
+ def _flatten_dict(key, value):
+ if isinstance(value, dict):
+ for key_in_value in value:
+ _flatten_dict(key + "." + key_in_value, value[key_in_value])
+ else:
+ flat_dict[key] = value
+
+ for key, value in items.items():
+ _flatten_dict(key, value)
+ return flat_dict

Павел обнови решението на 18.03.2015 11:04 (преди над 9 години)

def extract_type(items, return_type):
extracted_val = ""
for k in items:
if isinstance(k[0], return_type):
extracted_val += str(k[0]) * k[1]
return extracted_val
def reversed_dict(items):
reversed_dict = {}
for k in items:
reversed_dict[items[k]] = k
return reversed_dict
def reps(items):
rep = []
for k in items:
if items.count(k) > 1:
rep.append(k)
return tuple(rep)
def flatten_dict(items):
flat_dict = {}
def _flatten_dict(key, value):
if isinstance(value, dict):
for key_in_value in value:
_flatten_dict(key + "." + key_in_value, value[key_in_value])
else:
flat_dict[key] = value
for key, value in items.items():
_flatten_dict(key, value)
return flat_dict
+
+
+def unflatten_dict(items):
+ result_dict = {}
+
+ def _merge_dicts(dict1, dict2):
+ if not isinstance(dict1, dict) or not isinstance(dict2, dict):
+ return dict2
+ for key in dict2:
+ if key in dict1:
+ dict1[key] = _merge_dicts(dict1[key], dict2[key])
+ else:
+ dict1[key] = dict2[key]
+ return dict1
+
+ def _unflatten_dict(key, value):
+ if key.count(".") > 0:
+ split_dict = key.split(".")
+ new_key = ".".join(split_dict[0:len(split_dict) - 1])
+ new_value = {str(split_dict[-1]): value}
+ _unflatten_dict(new_key, new_value)
+ else:
+ if key in result_dict.keys():
+ result_dict[key] = _merge_dicts(result_dict[key], value)
+ else:
+ result_dict[key] = value
+
+ for key, value in items.items():
+ _unflatten_dict(key, value)
+ return result_dict

Павел обнови решението на 18.03.2015 21:49 (преди над 9 години)

def extract_type(items, return_type):
extracted_val = ""
for k in items:
- if isinstance(k[0], return_type):
+ if type(k[0]) is return_type:
extracted_val += str(k[0]) * k[1]
return extracted_val
def reversed_dict(items):
- reversed_dict = {}
+ reversed_result = {}
for k in items:
- reversed_dict[items[k]] = k
- return reversed_dict
+ reversed_result[items[k]] = k
+ return reversed_result
def reps(items):
rep = []
for k in items:
if items.count(k) > 1:
rep.append(k)
return tuple(rep)
def flatten_dict(items):
- flat_dict = {}
+ flatten_reuslt = {}
def _flatten_dict(key, value):
if isinstance(value, dict):
for key_in_value in value:
_flatten_dict(key + "." + key_in_value, value[key_in_value])
else:
- flat_dict[key] = value
+ flatten_reuslt[key] = value
for key, value in items.items():
_flatten_dict(key, value)
- return flat_dict
+ return flatten_reuslt
def unflatten_dict(items):
- result_dict = {}
+ result = {}
def _merge_dicts(dict1, dict2):
if not isinstance(dict1, dict) or not isinstance(dict2, dict):
return dict2
for key in dict2:
if key in dict1:
dict1[key] = _merge_dicts(dict1[key], dict2[key])
else:
dict1[key] = dict2[key]
return dict1
def _unflatten_dict(key, value):
if key.count(".") > 0:
split_dict = key.split(".")
new_key = ".".join(split_dict[0:len(split_dict) - 1])
new_value = {str(split_dict[-1]): value}
_unflatten_dict(new_key, new_value)
else:
- if key in result_dict.keys():
- result_dict[key] = _merge_dicts(result_dict[key], value)
+ if key in result.keys():
+ result[key] = _merge_dicts(result[key], value)
else:
- result_dict[key] = value
+ result[key] = value
for key, value in items.items():
_unflatten_dict(key, value)
- return result_dict
+ return result

Павел обнови решението на 19.03.2015 08:26 (преди над 9 години)

def extract_type(items, return_type):
extracted_val = ""
for k in items:
if type(k[0]) is return_type:
extracted_val += str(k[0]) * k[1]
return extracted_val
def reversed_dict(items):
- reversed_result = {}
- for k in items:
- reversed_result[items[k]] = k
+ reversed_result = {value: key for key, value in items.items()}
return reversed_result
def reps(items):
rep = []
for k in items:
if items.count(k) > 1:
rep.append(k)
return tuple(rep)
def flatten_dict(items):
flatten_reuslt = {}
def _flatten_dict(key, value):
if isinstance(value, dict):
for key_in_value in value:
_flatten_dict(key + "." + key_in_value, value[key_in_value])
else:
flatten_reuslt[key] = value
for key, value in items.items():
_flatten_dict(key, value)
return flatten_reuslt
def unflatten_dict(items):
result = {}
def _merge_dicts(dict1, dict2):
if not isinstance(dict1, dict) or not isinstance(dict2, dict):
return dict2
for key in dict2:
if key in dict1:
dict1[key] = _merge_dicts(dict1[key], dict2[key])
else:
dict1[key] = dict2[key]
return dict1
def _unflatten_dict(key, value):
if key.count(".") > 0:
split_dict = key.split(".")
new_key = ".".join(split_dict[0:len(split_dict) - 1])
new_value = {str(split_dict[-1]): value}
_unflatten_dict(new_key, new_value)
else:
if key in result.keys():
result[key] = _merge_dicts(result[key], value)
else:
result[key] = value
for key, value in items.items():
_unflatten_dict(key, value)
return result

Павел обнови решението на 19.03.2015 08:44 (преди над 9 години)

def extract_type(items, return_type):
- extracted_val = ""
- for k in items:
- if type(k[0]) is return_type:
- extracted_val += str(k[0]) * k[1]
- return extracted_val
+ extracted_val = [str(k[0]) * k[1] for k in
+ items if type(k[0]) is return_type]
+ return "".join(extracted_val)
def reversed_dict(items):
reversed_result = {value: key for key, value in items.items()}
return reversed_result
def reps(items):
- rep = []
- for k in items:
- if items.count(k) > 1:
- rep.append(k)
+ rep = [element for element in items if items.count(element) > 1]
return tuple(rep)
def flatten_dict(items):
flatten_reuslt = {}
def _flatten_dict(key, value):
if isinstance(value, dict):
for key_in_value in value:
_flatten_dict(key + "." + key_in_value, value[key_in_value])
else:
flatten_reuslt[key] = value
for key, value in items.items():
_flatten_dict(key, value)
return flatten_reuslt
def unflatten_dict(items):
result = {}
def _merge_dicts(dict1, dict2):
if not isinstance(dict1, dict) or not isinstance(dict2, dict):
return dict2
for key in dict2:
if key in dict1:
dict1[key] = _merge_dicts(dict1[key], dict2[key])
else:
dict1[key] = dict2[key]
return dict1
def _unflatten_dict(key, value):
if key.count(".") > 0:
split_dict = key.split(".")
new_key = ".".join(split_dict[0:len(split_dict) - 1])
new_value = {str(split_dict[-1]): value}
_unflatten_dict(new_key, new_value)
else:
if key in result.keys():
result[key] = _merge_dicts(result[key], value)
else:
result[key] = value
for key, value in items.items():
_unflatten_dict(key, value)
return result