Решение на Пет малки функции от Георги Любенов

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

Към профила на Георги Любенов

Резултати

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

Код

def extract_type(lst, given_type):
result = ""
for element in lst:
if type(element[0]) is given_type:
result += element[0] * element[1]
return result
def reversed_dict(given_dict):
result = {}
for key in given_dict:
result[given_dict[key]] = key
return result
def flatten_dict(given_dict):
separator = '.'
result = {}
def helper(dct, parent_keys=[]):
for key, value in dct.items():
if isinstance(value, dict):
helper(value, parent_keys + [key])
else:
sub_key = separator.join(parent_keys + [key])
result[sub_key] = value
helper(given_dict)
return result
def unflatten_dict(given_dict):
result = {}
for key, value in given_dict.items():
context = result
for sub_key in key.split('.')[:-1]:
if sub_key not in context:
context[sub_key] = {}
context = context[sub_key]
context[key.split('.')[-1]] = value
return result
def reps(lst):
result = []
for elem in lst:
if lst.count(elem) > 1:
result.append(elem)
return tuple(result)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.134s

OK

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

Георги обнови решението на 18.03.2015 02:00 (преди над 9 години)

+def extract_type(lst, given_type):
+ result = ""
+ for elem in lst:
+ if type(elem[0]) == given_type:
+ result += elem[0] * elem[1]
+ return result
+
+
+def reversed_dict(given_dict):
+ result = {}
+ for key in given_dict:
+ result[given_dict[key]] = key
+ return result
+
+
+def flatten_dict(given_dict):
+ separator = '.'
+ res = {}
+
+ def helper(dct, parent_keys=[]):
+ for key, value in dct.items():
+ if isinstance(value, dict):
+ helper(value, parent_keys + [key])
+ else:
+ sub_key = separator.join(parent_keys + [key])
+ res[sub_key] = value
+ helper(given_dict)
+ return res
+
+
+def unflatten_dict(given_dict):
+ res = {}
+ for key, value in given_dict.items():
+ context = res
+ for sub_key in key.split('.')[:-1]:
+ if sub_key not in context:
+ context[sub_key] = {}
+ context = context[sub_key]
+ context[key.split('.')[-1]] = value
+ return res
+
+
+def reps(lst):
+ result = []
+ for elem in lst:
+ if lst.count(elem) > 1:
+ result.append(elem)
+ return tuple(result)

Георги обнови решението на 20.03.2015 21:17 (преди над 9 години)

def extract_type(lst, given_type):
result = ""
for elem in lst:
if type(elem[0]) == given_type:
- result += elem[0] * elem[1]
+ result += str(elem[0]) * elem[1]
return result
def reversed_dict(given_dict):
result = {}
for key in given_dict:
result[given_dict[key]] = key
return result
def flatten_dict(given_dict):
separator = '.'
res = {}
def helper(dct, parent_keys=[]):
for key, value in dct.items():
if isinstance(value, dict):
helper(value, parent_keys + [key])
else:
sub_key = separator.join(parent_keys + [key])
res[sub_key] = value
helper(given_dict)
return res
def unflatten_dict(given_dict):
res = {}
for key, value in given_dict.items():
context = res
for sub_key in key.split('.')[:-1]:
if sub_key not in context:
context[sub_key] = {}
context = context[sub_key]
context[key.split('.')[-1]] = value
return res
def reps(lst):
result = []
for elem in lst:
if lst.count(elem) > 1:
result.append(elem)
return tuple(result)

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

def extract_type(lst, given_type):
result = ""
- for elem in lst:
- if type(elem[0]) == given_type:
- result += str(elem[0]) * elem[1]
+ for element in lst:
+ if type(element[0]) is given_type:
+ result += element[0] * element[1]
return result
def reversed_dict(given_dict):
result = {}
for key in given_dict:
result[given_dict[key]] = key
return result
def flatten_dict(given_dict):
separator = '.'
- res = {}
+ result = {}
def helper(dct, parent_keys=[]):
for key, value in dct.items():
if isinstance(value, dict):
helper(value, parent_keys + [key])
else:
sub_key = separator.join(parent_keys + [key])
- res[sub_key] = value
+ result[sub_key] = value
helper(given_dict)
- return res
+ return result
def unflatten_dict(given_dict):
- res = {}
+ result = {}
for key, value in given_dict.items():
- context = res
+ context = result
for sub_key in key.split('.')[:-1]:
if sub_key not in context:
context[sub_key] = {}
context = context[sub_key]
context[key.split('.')[-1]] = value
- return res
+ return result
def reps(lst):
result = []
for elem in lst:
if lst.count(elem) > 1:
result.append(elem)
return tuple(result)