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

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

Към профила на Емил Великов

Резултати

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

Код

def repeat(symbol, num_of_reps):
result = ""
for i in range(num_of_reps):
result += str(symbol)
return result
def extract_type(input_, type_):
result = ""
for element in input_:
if type(element[0]) is type_:
result += repeat(element[0], element[1])
return result
def reversed_dict(input_):
result = {}
for key, value in input_.items():
result[value] = key
return result
def flatten_dict(input_, main_key=""):
result = {}
for key, value in input_.items():
new_key = main_key + "." + key if main_key != "" else key
if type(value) is dict:
result.update(flatten_dict(value, new_key))
else:
result[new_key] = value
return result
def unflatten_dict(input_):
result = {}
for key, value in input_.items():
key_parts = key.split('.')
new_dict = result
while len(key_parts) != 1:
if key_parts[0] not in new_dict:
new_dict[key_parts[0]] = {}
new_dict = new_dict[key_parts[0]]
key_parts.pop(0)
new_dict[key_parts[-1]] = value
return result
def reps(arguments):
return tuple(x for x in arguments if arguments.count(x) > 1)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.135s

OK

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

Емил обнови решението на 20.03.2015 15:29 (преди над 9 години)

+def repeat(symbol, num_of_reps):
+ result = ""
+ for i in range(num_of_reps):
+ result += str(symbol)
+ return result
+
+
+def extract_type(input_, type_):
+ result = ""
+ for element in input_:
+ if type(element[0]) is type_:
+ result += repeat(element[0], element[1])
+ return result
+
+
+def reversed_dict(input_):
+ result = {}
+ for key, value in input_.items():
+ result[value] = key
+ return result
+
+
+def flatten_dict(input_, main_key=""):
+ result = {}
+ for key, value in input_.items():
+ new_key = main_key + "." + key if main_key != "" else key
+ if type(value) is dict:
+ result.update(flatten_dict(value, new_key))
+ else:
+ result[new_key] = value
+ return result
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ key_parts = key.split(".")
+ new_dict = result
+ for part in key_parts[:-1]:
+ if part not in new_dict:
+ new_dict[part] = {}
+ new_dict = new_dict[part]
+ new_dict[key_parts[-1]] = value
+ return result
+
+
+def reps(arguments):
+ return tuple(x for x in arguments if arguments.count(x) > 1)

Емил обнови решението на 20.03.2015 18:28 (преди над 9 години)

def repeat(symbol, num_of_reps):
result = ""
for i in range(num_of_reps):
result += str(symbol)
return result
def extract_type(input_, type_):
result = ""
for element in input_:
if type(element[0]) is type_:
result += repeat(element[0], element[1])
return result
def reversed_dict(input_):
result = {}
for key, value in input_.items():
result[value] = key
return result
def flatten_dict(input_, main_key=""):
result = {}
for key, value in input_.items():
new_key = main_key + "." + key if main_key != "" else key
if type(value) is dict:
result.update(flatten_dict(value, new_key))
else:
result[new_key] = value
return result
-def unflatten_dict(dictionary):
- result = {}
- for key, value in dictionary.items():
- key_parts = key.split(".")
- new_dict = result
- for part in key_parts[:-1]:
- if part not in new_dict:
- new_dict[part] = {}
- new_dict = new_dict[part]
- new_dict[key_parts[-1]] = value
- return result
-
-
def reps(arguments):
return tuple(x for x in arguments if arguments.count(x) > 1)

Емил обнови решението на 21.03.2015 13:27 (преди над 9 години)

def repeat(symbol, num_of_reps):
result = ""
for i in range(num_of_reps):
result += str(symbol)
return result
def extract_type(input_, type_):
result = ""
for element in input_:
if type(element[0]) is type_:
result += repeat(element[0], element[1])
return result
def reversed_dict(input_):
result = {}
for key, value in input_.items():
result[value] = key
return result
def flatten_dict(input_, main_key=""):
result = {}
for key, value in input_.items():
new_key = main_key + "." + key if main_key != "" else key
if type(value) is dict:
result.update(flatten_dict(value, new_key))
else:
result[new_key] = value
return result
+def unflatten_dict(input_):
+ result = {}
+ for key, value in input_.items():
+ key_parts = key.split('.')
+ new_dict = result
+ while len(key_parts) != 1:
+ if key_parts[0] not in new_dict:
+ new_dict[key_parts[0]] = {}
+ new_dict = new_dict[key_parts[0]]
+ key_parts.pop(0)
+ new_dict[key_parts[-1]] = value
+ return result
+
+
def reps(arguments):
return tuple(x for x in arguments if arguments.count(x) > 1)