Решение на Пет малки функции от Клара Кайралах

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

Към профила на Клара Кайралах

Резултати

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

Код

import itertools
def extract_type(text, the_type):
result = ''
for i in range(0, len(text)):
if type(text[i][0]) is the_type:
result += str(str(text[i][0]) * text[i][1])
return result
def reversed_dict(dictionary):
return dict(zip(dictionary.values(), dictionary.keys()))
def flatten_dict(dictionary, the_key=''):
result = {}
for key, val in dictionary.items():
new_key = the_key + key
if isinstance(val, dict):
result.update(flatten_dict(val, new_key + '.'))
else:
result[new_key] = val
return result
def unflatten_dict(dictionary):
unflatten_dictionary = dict()
for key, value in dictionary.items():
key_split = key.split(".")
result = unflatten_dictionary
for part in key_split[:-1]:
if part not in result:
result[part] = dict()
result = result[part]
result[key_split[-1]] = value
return unflatten_dictionary
def reps(numbers):
filtered_numbers = filter(
lambda a: a if numbers.count(a) > 1 else 0, numbers)
return tuple(list(filtered_numbers))

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.160s

OK

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

Клара обнови решението на 18.03.2015 00:28 (преди над 9 години)

+import itertools
+
+
+def extract_type(the_list, the_type):
+ the_result = ''
+ for i in range(0, len(the_list)):
+ if type(the_list[i][0]) is the_type:
+ the_result += str(str(the_list[i][0]) * the_list[i][1])
+ return the_result
+
+
+def reversed_dict(the_dict):
+ return dict(zip(the_dict.values(), the_dict.keys()))
+
+
+def flatten_dict(the_dict, the_key=''):
+ final_dict = {}
+ for key, val in the_dict.items():
+ new_key = the_key + key
+ if isinstance(val, dict):
+ final_dict.update(flatten_dict(val, new_key + '.'))
+ else:
+ final_dict[new_key] = val
+ return final_dict
+
+
+def unflatten_dict(the_dict):
+ resultDict = dict()
+ for key, value in the_dict.items():
+ key_split = key.split(".")
+ d = resultDict
+ for part in key_split[:-1]:
+ if part not in d:
+ d[part] = dict()
+ d = d[part]
+ d[key_split[-1]] = value
+ return resultDict
+
+
+def reps(the_list):
+ the_result = filter(lambda a: a if the_list.count(a) > 1 else 0, the_list)
+ return tuple(list(the_result))
  • Всички имена трябва да са в snake_case
  • Имената трябва да са смислени - the dict, d, the_result не значат нищо.
  • Не слагай типа на променливата в името ѝ, за да не се окаже в един момент, че речникът ти се казва list :)

Клара обнови решението на 19.03.2015 23:25 (преди над 9 години)

import itertools
-def extract_type(the_list, the_type):
- the_result = ''
- for i in range(0, len(the_list)):
- if type(the_list[i][0]) is the_type:
- the_result += str(str(the_list[i][0]) * the_list[i][1])
- return the_result
+def extract_type(text, the_type):
+ result_string = ''
+ for i in range(0, len(text)):
+ if type(text[i][0]) is the_type:
+ result_string += str(str(text[i][0]) * text[i][1])
+ return result_string
-def reversed_dict(the_dict):
- return dict(zip(the_dict.values(), the_dict.keys()))
+def reversed_dict(dictionary):
+ return dict(zip(dictionary.values(), dictionary.keys()))
-def flatten_dict(the_dict, the_key=''):
+def flatten_dict(dictionary, the_key=''):
final_dict = {}
- for key, val in the_dict.items():
+ for key, val in dictionary.items():
new_key = the_key + key
if isinstance(val, dict):
final_dict.update(flatten_dict(val, new_key + '.'))
else:
final_dict[new_key] = val
return final_dict
-def unflatten_dict(the_dict):
- resultDict = dict()
- for key, value in the_dict.items():
+def unflatten_dict(dictionary):
+ result_dictionary = dict()
+ for key, value in dictionary.items():
key_split = key.split(".")
- d = resultDict
+ temp_dictionary = result_dictionary
for part in key_split[:-1]:
- if part not in d:
- d[part] = dict()
- d = d[part]
- d[key_split[-1]] = value
- return resultDict
+ if part not in temp_dictionary:
+ temp_dictionary[part] = dict()
+ temp_dictionary = temp_dictionary[part]
+ temp_dictionary[key_split[-1]] = value
+ return result_dictionary
-def reps(the_list):
- the_result = filter(lambda a: a if the_list.count(a) > 1 else 0, the_list)
+def reps(numbers):
- return tuple(list(the_result))
+ filtered_numbers = filter(
+ lambda a: a if numbers.count(a) > 1 else 0, numbers)
+ return tuple(list(filtered_numbers))

Клара обнови решението на 19.03.2015 23:35 (преди над 9 години)

import itertools
def extract_type(text, the_type):
- result_string = ''
+ result = ''
for i in range(0, len(text)):
if type(text[i][0]) is the_type:
- result_string += str(str(text[i][0]) * text[i][1])
- return result_string
+ result += str(str(text[i][0]) * text[i][1])
+ return result
def reversed_dict(dictionary):
return dict(zip(dictionary.values(), dictionary.keys()))
def flatten_dict(dictionary, the_key=''):
- final_dict = {}
+ result = {}
for key, val in dictionary.items():
new_key = the_key + key
if isinstance(val, dict):
- final_dict.update(flatten_dict(val, new_key + '.'))
+ result.update(flatten_dict(val, new_key + '.'))
else:
- final_dict[new_key] = val
- return final_dict
+ result[new_key] = val
+ return result
def unflatten_dict(dictionary):
- result_dictionary = dict()
+ unflatten_dictionary = dict()
for key, value in dictionary.items():
key_split = key.split(".")
- temp_dictionary = result_dictionary
+ result = unflatten_dictionary
for part in key_split[:-1]:
- if part not in temp_dictionary:
- temp_dictionary[part] = dict()
- temp_dictionary = temp_dictionary[part]
- temp_dictionary[key_split[-1]] = value
- return result_dictionary
+ if part not in result:
+ result[part] = dict()
+ result = result[part]
+ result[key_split[-1]] = value
+ return unflatten_dictionary
def reps(numbers):
filtered_numbers = filter(
lambda a: a if numbers.count(a) > 1 else 0, numbers)
return tuple(list(filtered_numbers))