Решение на Пет малки функции от Юлия Недялкова

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

Към профила на Юлия Недялкова

Резултати

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

Код

def extract_type(input_data, searched_type):
result = list()
for element, count in input_data:
if type(element) == searched_type:
n_times = count
while n_times > 0:
result.append(element)
n_times -= 1
return ''.join(result)
def reversed_dict(input_data):
result = {}
for key in input_data:
result[input_data[key]] = key
return result
def flatten_dict_helper(help_key, input_data, result):
for key in input_data:
if help_key == '':
current_key = key
else:
current_key = help_key + '.' + key
if type(input_data[key]) == dict:
flatten_dict_helper(current_key, input_data[key], result)
else:
result[current_key] = input_data[key]
def flatten_dict(input_data):
result = {}
flatten_dict_helper('', input_data, result)
return result
def reps(input_data):
result = list()
for index, value in enumerate(input_data):
first_part = input_data[:index]
second_part = input_data[index+1:]
if first_part.count(value) != 0 or second_part.count(value) != 0:
result.append(value)
return tuple(result)

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

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

OK

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

Юлия обнови решението на 23.03.2015 12:51 (преди над 9 години)

+def extract_type(input_data, searched_type):
+ result = list()
+ for element, count in input_data:
+ if type(element) == searched_type:
+ n_times = count
+ while n_times > 0:
+ result.append(element)
+ n_times -= 1
+ return ''.join(result)
+
+
+def reversed_dict(input_data):
+ result = {}
+ for key in input_data:
+ result[input_data[key]] = key
+ return result
+
+
+def flatten_dict_helper(help_key, input_data, result):
+ for key in input_data:
+ if help_key == '':
+ current_key = key
+ else:
+ current_key = help_key + '.' + key
+ if type(input_data[key]) == dict:
+ flatten_dict_helper(current_key, input_data[key], result)
+ else:
+ result[current_key] = input_data[key]
+
+
+def flatten_dict(input_data):
+ result = {}
+ flatten_dict_helper('', input_data, result)
+ return result
+
+
+def reps(input_data):
+ result = list()
+ for index, value in enumerate(input_data):
+ first_part = input_data[:index]
+ second_part = input_data[index+1:]
+ if first_part.count(value) != 0 or second_part.count(value) != 0:
+ result.append(value)
+ return tuple(result)

Юлия обнови решението на 23.03.2015 13:43 (преди над 9 години)

def extract_type(input_data, searched_type):
result = list()
for element, count in input_data:
if type(element) == searched_type:
n_times = count
while n_times > 0:
result.append(element)
n_times -= 1
return ''.join(result)
def reversed_dict(input_data):
result = {}
for key in input_data:
result[input_data[key]] = key
return result
def flatten_dict_helper(help_key, input_data, result):
for key in input_data:
if help_key == '':
current_key = key
else:
current_key = help_key + '.' + key
if type(input_data[key]) == dict:
flatten_dict_helper(current_key, input_data[key], result)
else:
result[current_key] = input_data[key]
def flatten_dict(input_data):
result = {}
flatten_dict_helper('', input_data, result)
return result
+def unflatten_dict_help(keys, value, result):
+ if len(keys) > 0:
+ result[keys[-1]] = unflatten_dict_help(keys[:-1], value, result)
+ else:
+ return value
+
+
+def unflatten_dict(input_data):
+ result = {}
+ for key in input_data:
+ list_of_keys = key.split('.')
+ result[list_of_keys[-1]] = unflatten_dict_help(
+ list_of_keys[:-1], input_data[key], result)
+ return result
+
+
def reps(input_data):
result = list()
for index, value in enumerate(input_data):
first_part = input_data[:index]
second_part = input_data[index+1:]
if first_part.count(value) != 0 or second_part.count(value) != 0:
result.append(value)
return tuple(result)

Юлия обнови решението на 23.03.2015 14:09 (преди над 9 години)

def extract_type(input_data, searched_type):
result = list()
for element, count in input_data:
if type(element) == searched_type:
n_times = count
while n_times > 0:
result.append(element)
n_times -= 1
return ''.join(result)
def reversed_dict(input_data):
result = {}
for key in input_data:
result[input_data[key]] = key
return result
def flatten_dict_helper(help_key, input_data, result):
for key in input_data:
if help_key == '':
current_key = key
else:
current_key = help_key + '.' + key
if type(input_data[key]) == dict:
flatten_dict_helper(current_key, input_data[key], result)
else:
result[current_key] = input_data[key]
def flatten_dict(input_data):
result = {}
flatten_dict_helper('', input_data, result)
return result
-def unflatten_dict_help(keys, value, result):
- if len(keys) > 0:
- result[keys[-1]] = unflatten_dict_help(keys[:-1], value, result)
- else:
- return value
-
-
-def unflatten_dict(input_data):
- result = {}
- for key in input_data:
- list_of_keys = key.split('.')
- result[list_of_keys[-1]] = unflatten_dict_help(
- list_of_keys[:-1], input_data[key], result)
- return result
-
-
def reps(input_data):
result = list()
for index, value in enumerate(input_data):
first_part = input_data[:index]
second_part = input_data[index+1:]
if first_part.count(value) != 0 or second_part.count(value) != 0:
result.append(value)
return tuple(result)