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

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

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

Резултати

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

Код

def extract_type(data, data_type):
return ''.join([str(element[0])*element[1]
for element in data
if type(element[0]) is data_type])
def reversed_dict(data):
result = dict()
for key, value in data.items():
result[value] = key
return result
def flatten_dict(data):
result = dict()
for key in data:
if type(data[key]) is dict:
for current_key, value in flatten_dict(data[key]).items():
result.update({"{}.{}".format(key, current_key): value})
else:
result[key] = data[key]
return result
def unflatten_dict(data):
result = dict()
for key, value in data.items():
splitted = key.split('.')
temporary_result = result
for element in splitted[:-1]:
if element not in temporary_result.keys():
temporary_result[element] = dict()
temporary_result = temporary_result[element]
temporary_result[splitted[-1]] = value
return result
def reps(data):
return tuple([element for element in data if data.count(element) > 1])

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

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

OK

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

Мартин обнови решението на 21.03.2015 21:56 (преди над 9 години)

+def extract_type(data, data_type):
+ return ''.join([str(element[0])*element[1]
+ for element in data
+ if type(element[0]) is data_type])
+
+
+def reversed_dict(data):
+ result = dict()
+ for key, value in data.items():
+ result[value] = key
+ return result
+
+
+def flatten_dict(data):
+ result = dict()
+ for key in data:
+ if type(data[key]) is dict:
+ for current_key, value in flatten_dict(data[key]).items():
+ result.update({"{}.{}".format(key, current_key): value})
+ else:
+ result[key] = data[key]
+ return result
+
+
+def unflatten_dict(data):
+ result = dict()
+ for key, value in data.items():
+ splitted = key.split('.')
+ for sequence in splitted:
+ temp = result
+ for element in splitted[:-1]:
+ if element not in temp.keys():
+ temp[element] = dict()
+
+ temp = temp[element]
+ temp[splitted[-1]] = value
+ return result
+
+
+def reps(data):
+ return tuple([element for element in data if data.count(element) > 1])

Мартин обнови решението на 22.03.2015 22:51 (преди над 9 години)

def extract_type(data, data_type):
return ''.join([str(element[0])*element[1]
for element in data
if type(element[0]) is data_type])
def reversed_dict(data):
result = dict()
for key, value in data.items():
result[value] = key
return result
def flatten_dict(data):
result = dict()
for key in data:
if type(data[key]) is dict:
for current_key, value in flatten_dict(data[key]).items():
result.update({"{}.{}".format(key, current_key): value})
else:
result[key] = data[key]
return result
def unflatten_dict(data):
result = dict()
for key, value in data.items():
splitted = key.split('.')
- for sequence in splitted:
- temp = result
- for element in splitted[:-1]:
- if element not in temp.keys():
- temp[element] = dict()
+ temporary_result = result
+ for element in splitted[:-1]:
+ if element not in temporary_result.keys():
+ temporary_result[element] = dict()
- temp = temp[element]
- temp[splitted[-1]] = value
+ temporary_result = temporary_result[element]
+ temporary_result[splitted[-1]] = value
return result
def reps(data):
return tuple([element for element in data if data.count(element) > 1])