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

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

Към профила на Мария Димитрова

Резултати

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

Код

def extract_type(primary, type_of_elements):
result = str()
for x in primary:
if type(x[0]) is type_of_elements:
result = result + ''.join(str(x[0]) * (x[1]))
return result
def reversed_dict (primary = dict()):
transposed = dict((second, first) for first, second in primary.items())
return transposed
def flatten_dict(primary):
def expand(first, second):
if isinstance(second, dict):
return [ (first + '.' + key, value) for key, value in flatten_dict(second).items() ]
else:
return [ (first, second) ]
items = [ item for key, value in primary.items() for item in expand(key, value) ]
return dict(items)
def unflatten_dict(primary):
new_dict = dict()
for first, second in primary.items():
parts = first.split(".")
dictionry = new_dict
for part in parts[:-1]:
if part not in dictionry:
dictionry[part] = dict()
dictionry = dictionry[part]
dictionry[parts[-1]] = second
return new_dict
def reps(primary):
result = ()
for x in primary:
if primary.count(x) > 1:
result = result + (x, )
return result

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.138s

OK

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

Мария обнови решението на 19.03.2015 23:46 (преди над 9 години)

+def extract_type(primary, type_of_elements):
+ result = []
+ for x in primary:
+ if type(x[0]) is type_of_elements:
+ result.append(x[0] * x[1])
+ string_result = ''.join(result)
+ return string_result
+
+
+def reversed_dict (primary = dict()):
+ transposed = dict((second, first) for first, second in primary.items())
+ return transposed
+
+
+def flatten_dict(primary):
+ def expand(first, second):
+ if isinstance(second, dict):
+ return [ (first + '.' + key, value) for key, value in flatten_dict(second).items() ]
+ else:
+ return [ (first, second) ]
+ items = [ item for key, value in primary.items() for item in expand(key, value) ]
+ return dict(items)
+
+
+def unflatten_dict(primary):
+ new_dict = dict()
+ for first, second in primary.items():
+ parts = first.split(".")
+ dictionry = new_dict
+ for part in parts[:-1]:
+ if part not in dictionry:
+ dictionry[part] = dict()
+ dictionry = dictionry[part]
+ dictionry[parts[-1]] = second
+ return new_dict
+
+
+def reps(primary):
+ new_list = []
+ for x in primary:
+ if primary.count(x) > 1:
+ new_list.append(x)
+ return new_list

Мария обнови решението на 20.03.2015 00:37 (преди над 9 години)

def extract_type(primary, type_of_elements):
result = []
for x in primary:
if type(x[0]) is type_of_elements:
result.append(x[0] * x[1])
string_result = ''.join(result)
return string_result
def reversed_dict (primary = dict()):
transposed = dict((second, first) for first, second in primary.items())
return transposed
def flatten_dict(primary):
def expand(first, second):
if isinstance(second, dict):
return [ (first + '.' + key, value) for key, value in flatten_dict(second).items() ]
else:
return [ (first, second) ]
items = [ item for key, value in primary.items() for item in expand(key, value) ]
return dict(items)
def unflatten_dict(primary):
new_dict = dict()
for first, second in primary.items():
parts = first.split(".")
dictionry = new_dict
for part in parts[:-1]:
if part not in dictionry:
dictionry[part] = dict()
dictionry = dictionry[part]
dictionry[parts[-1]] = second
return new_dict
def reps(primary):
- new_list = []
+ result = ()
for x in primary:
if primary.count(x) > 1:
- new_list.append(x)
- return new_list
+ result = result + (x, )
+ return result

Мария обнови решението на 23.03.2015 08:53 (преди над 9 години)

def extract_type(primary, type_of_elements):
- result = []
+ result = str()
for x in primary:
if type(x[0]) is type_of_elements:
- result.append(x[0] * x[1])
- string_result = ''.join(result)
- return string_result
+ result = result + ''.join(str(x[0]) * (x[1]))
+ return result
def reversed_dict (primary = dict()):
transposed = dict((second, first) for first, second in primary.items())
return transposed
def flatten_dict(primary):
def expand(first, second):
if isinstance(second, dict):
return [ (first + '.' + key, value) for key, value in flatten_dict(second).items() ]
else:
return [ (first, second) ]
items = [ item for key, value in primary.items() for item in expand(key, value) ]
return dict(items)
def unflatten_dict(primary):
new_dict = dict()
for first, second in primary.items():
parts = first.split(".")
dictionry = new_dict
for part in parts[:-1]:
if part not in dictionry:
dictionry[part] = dict()
dictionry = dictionry[part]
dictionry[parts[-1]] = second
return new_dict
def reps(primary):
result = ()
for x in primary:
if primary.count(x) > 1:
result = result + (x, )
return result