Решение на Пет малки функции от Илиян Якимов

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

Към профила на Илиян Якимов

Резултати

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

Код

def extract_type(catalogue, current_type):
result = ""
for tpl in catalogue:
if type(tpl[0]) is current_type:
result += (str(tpl[0])) * tpl[1]
return result
def reversed_dict(catalogue):
result = {}
for key in catalogue:
if not catalogue[key] in result:
result[catalogue[key]] = key
return result
def value_is_dict(key, catalogue, result):
"""if value is dictrionary -> converts it to specified form"""
if type(catalogue) != dict:
result[key] = catalogue
else:
for obj in catalogue:
value_is_dict(key + '.' + obj, catalogue[obj], result)
return result
def flatten_dict(catalogue):
result = {}
for key in catalogue:
if type(catalogue[key]) == dict:
value_is_dict(key, catalogue[key], result)
else:
result[key] = catalogue[key]
return result
def get_before_symbol(sequence):
"""return all symbols before '.'"""
result = ""
i = 0
for symbol in sequence:
if symbol != '.':
result += symbol
else:
return result
def get_after_symbol(sequence):
"""return all symbols after '.'"""
result = ""
length = len(get_before_symbol(sequence))
for x in sequence[length+1:]:
result += x
return result
def make_value_dict(key, value, result):
"""the opposite of value_is_dict i.e. from specified form to dict"""
if key.count('.') == 0:
result[key] = value
else:
new_key = get_before_symbol(key)
if new_key in result:
make_value_dict(get_after_symbol(key), value, result[new_key])
else:
result[new_key] = {}
make_value_dict(get_after_symbol(key), value, result[new_key])
return result
def unflatten_dict(catalogue):
result = {}
for key in catalogue:
if key.count('.') == 0:
result[key] = catalogue[key]
else:
make_value_dict(key, catalogue[key], result)
return result
def reps(catalogue):
result = []
for x in catalogue:
if catalogue.count(x) > 1:
result.append(x)
return tuple(result)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.162s

OK

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

Илиян обнови решението на 17.03.2015 21:41 (преди над 9 години)

+def extract_type(catalogue, current_type):
+ result = ""
+ for cortege in catalogue:
+ if type(cortege[0]) == current_type:
+ result += (str(cortege[0])) * cortege[1]
+ return result
+
+
+def reversed_dict(catalogue):
+ result = {}
+ for key in catalogue:
+ if not catalogue[key] in result:
+ result[catalogue[key]] = key
+ return result
+
+
+def value_is_dict(key, catalogue, res_dict): # help for flatten_dict
+ if type(catalogue) != dict:
+ res_dict[key] = catalogue
+ else:
+ for obj in catalogue:
+ value_is_dict(key + '.' + obj, catalogue[obj], res_dict)
+ return res_dict
+
+
+def flatten_dict(catalogue):
+ result = {}
+ for key in catalogue:
+ if type(catalogue[key]) == dict:
+ value_is_dict(key, catalogue[key], result)
+ else:
+ result[key] = catalogue[key]
+ return result
+
+
+def get_before_symbol(string):
+ res = ""
+ i = 0
+ while i < len(string):
+ if string[i] != '.':
+ res += string[i]
+ else:
+ return res
+ i += 1
+ return res
+
+
+def get_after_symbol(string):
+ res = ""
+ length = len(get_before_symbol(string))
+ for x in string[length+1:]:
+ res += x
+ return res
+
+
+def make_value_dict(key, value, res_dict): # help for unflatten_dict
+ if key.count('.') == 0:
+ res_dict[key] = value
+ else:
+ f_key = get_before_symbol(key)
+ if f_key in res_dict:
+ make_value_dict(get_after_symbol(key), value, res_dict[f_key])
+ else:
+ res_dict[f_key] = {}
+ make_value_dict(get_after_symbol(key), value, res_dict[f_key])
+ return res_dict
+
+
+def unflatten_dict(catalogue):
+ res = {}
+ for key in catalogue:
+ if key.count('.') == 0:
+ res[key] = catalogue[key]
+ else:
+ make_value_dict(key, catalogue[key], res)
+ return res
+
+
+def reps(catalogue):
+ result = []
+ for x in catalogue:
+ if catalogue.count(x) > 1:
+ result.append(x)
+ return tuple(result)
  • Документирай какво правят helper функциите ти, а не къде ги ползваш и не използвай коментари за това, а docstrings.
  • На места имаш лоши имена като f_key иres`
  • На дргуи места имаш още по-лоши имена, тъй като си указал техния тип като res_dict, string и cortege
  • Не обхождаме неща в Python така:
i = 0
while i < len(string):
    ...
    i += 1

За обхождането: т.е. трябва да го направя с for ? А за имената на функциите как стои въпроса- като изходя от това как вие сте ги дали, следва че, в техните имена може да присъстват dict,list и т.н. ?

Илиян обнови решението на 18.03.2015 13:39 (преди над 9 години)

def extract_type(catalogue, current_type):
result = ""
- for cortege in catalogue:
- if type(cortege[0]) == current_type:
- result += (str(cortege[0])) * cortege[1]
+ for tpl in catalogue:
+ if type(tpl[0]) == current_type:
+ result += (str(tpl[0])) * tpl[1]
return result
def reversed_dict(catalogue):
result = {}
for key in catalogue:
if not catalogue[key] in result:
result[catalogue[key]] = key
return result
-def value_is_dict(key, catalogue, res_dict): # help for flatten_dict
+def value_is_dict(key, catalogue, result):
+ """if value is dictrionary -> converts it to specified form"""
if type(catalogue) != dict:
- res_dict[key] = catalogue
+ result[key] = catalogue
else:
for obj in catalogue:
- value_is_dict(key + '.' + obj, catalogue[obj], res_dict)
- return res_dict
+ value_is_dict(key + '.' + obj, catalogue[obj], result)
+ return result
def flatten_dict(catalogue):
result = {}
for key in catalogue:
if type(catalogue[key]) == dict:
value_is_dict(key, catalogue[key], result)
else:
result[key] = catalogue[key]
return result
-def get_before_symbol(string):
- res = ""
+def get_before_symbol(sequence):
+ """return all symbols before '.'"""
+ result = ""
i = 0
- while i < len(string):
- if string[i] != '.':
- res += string[i]
+ for symbol in sequence:
+ if symbol != '.':
+ result += symbol
else:
- return res
- i += 1
- return res
+ return result
-def get_after_symbol(string):
- res = ""
- length = len(get_before_symbol(string))
- for x in string[length+1:]:
- res += x
- return res
+def get_after_symbol(sequence):
+ """return all symbols after '.'"""
+ result = ""
+ length = len(get_before_symbol(sequence))
+ for x in sequence[length+1:]:
+ result += x
+ return result
-def make_value_dict(key, value, res_dict): # help for unflatten_dict
+def make_value_dict(key, value, result):
+ """the opposite of value_is_dict i.e. from specified form to dict"""
if key.count('.') == 0:
- res_dict[key] = value
+ result[key] = value
else:
- f_key = get_before_symbol(key)
- if f_key in res_dict:
- make_value_dict(get_after_symbol(key), value, res_dict[f_key])
+ new_key = get_before_symbol(key)
+ if new_key in result:
+ make_value_dict(get_after_symbol(key), value, result[new_key])
else:
- res_dict[f_key] = {}
- make_value_dict(get_after_symbol(key), value, res_dict[f_key])
- return res_dict
+ result[new_key] = {}
+ make_value_dict(get_after_symbol(key), value, result[new_key])
+ return result
def unflatten_dict(catalogue):
- res = {}
+ result = {}
for key in catalogue:
if key.count('.') == 0:
- res[key] = catalogue[key]
+ result[key] = catalogue[key]
else:
- make_value_dict(key, catalogue[key], res)
- return res
+ make_value_dict(key, catalogue[key], result)
+ return result
def reps(catalogue):
result = []
for x in catalogue:
if catalogue.count(x) > 1:
result.append(x)
return tuple(result)

Мисля, че оправих всички забележки. Ако може да погледнете и тестовете, които съм качил, че да знам дали функциите ми са наред, а и другите да имат повече какво да тестват.

Илиян обнови решението на 18.03.2015 21:41 (преди над 9 години)

def extract_type(catalogue, current_type):
result = ""
for tpl in catalogue:
- if type(tpl[0]) == current_type:
+ if type(tpl[0]) is current_type:
result += (str(tpl[0])) * tpl[1]
return result
def reversed_dict(catalogue):
result = {}
for key in catalogue:
if not catalogue[key] in result:
result[catalogue[key]] = key
return result
def value_is_dict(key, catalogue, result):
"""if value is dictrionary -> converts it to specified form"""
if type(catalogue) != dict:
result[key] = catalogue
else:
for obj in catalogue:
value_is_dict(key + '.' + obj, catalogue[obj], result)
return result
def flatten_dict(catalogue):
result = {}
for key in catalogue:
if type(catalogue[key]) == dict:
value_is_dict(key, catalogue[key], result)
else:
result[key] = catalogue[key]
return result
def get_before_symbol(sequence):
"""return all symbols before '.'"""
result = ""
i = 0
for symbol in sequence:
if symbol != '.':
result += symbol
else:
return result
def get_after_symbol(sequence):
"""return all symbols after '.'"""
result = ""
length = len(get_before_symbol(sequence))
for x in sequence[length+1:]:
result += x
return result
def make_value_dict(key, value, result):
"""the opposite of value_is_dict i.e. from specified form to dict"""
if key.count('.') == 0:
result[key] = value
else:
new_key = get_before_symbol(key)
if new_key in result:
make_value_dict(get_after_symbol(key), value, result[new_key])
else:
result[new_key] = {}
make_value_dict(get_after_symbol(key), value, result[new_key])
return result
def unflatten_dict(catalogue):
result = {}
for key in catalogue:
if key.count('.') == 0:
result[key] = catalogue[key]
else:
make_value_dict(key, catalogue[key], result)
return result
def reps(catalogue):
result = []
for x in catalogue:
if catalogue.count(x) > 1:
result.append(x)
return tuple(result)