Решение на Пет функции от Стоянка Йовкова

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

Към профила на Стоянка Йовкова

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 5 успешни тест(а)
  • 11 неуспешни тест(а)

Код

import functools
def is_pangram(sentence):
letters = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м',
'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
for letter in letters:
if letter not in sentence.lower():
result = False
break
else:
result = True
return result
def char_histogram(text):
symbol_count = {}
for symbol in text:
value = text.count(symbol)
symbol_count[symbol] = value
return symbol_count
def sort_by(func, arguments):
k = 0
repeat = 0
length = len(arguments) - 1
while k < length and repeat < length:
arg = arguments[k: k + 2]
number = functools.reduce(func, arg)
if number > 0:
arguments[k], arguments[k + 1] = arguments[k + 1], arguments[k]
k += 1
if k == length:
k = 0
repeat += 1
if repeat == length:
return arguments

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

EEEE..EEEEE..FF.
======================================================================
ERROR: test_list_of_latin_anagrams (test.TestAnagrams)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-mvz308/test.py", line 109, in test_list_of_latin_anagrams
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: test_with_different_cases (test.TestAnagrams)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-mvz308/test.py", line 125, in test_with_different_cases
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: test_with_different_symbols (test.TestAnagrams)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-mvz308/test.py", line 135, in test_with_different_symbols
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: test_with_list_of_cyrilic_anagrams (test.TestAnagrams)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-mvz308/test.py", line 117, in test_with_list_of_cyrilic_anagrams
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: test_another_group_by_type (test.TestGroupByType)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-mvz308/test.py", line 74, in test_another_group_by_type
    solution.group_by_type({(1, 2): 12, ('a', 1): 1, 1: 'b', 'c': 15}))
AttributeError: 'module' object has no attribute 'group_by_type'

======================================================================
ERROR: test_group_by_type (test.TestGroupByType)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-mvz308/test.py", line 68, in test_group_by_type
    solution.group_by_type({'a': 12, 'b': 1, 1: 'foo'}))
AttributeError: 'module' object has no attribute 'group_by_type'

======================================================================
ERROR: test_group_by_type_empty (test.TestGroupByType)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-mvz308/test.py", line 63, in test_group_by_type_empty
    self.assertEqual({}, solution.group_by_type({}))
AttributeError: 'module' object has no attribute 'group_by_type'

======================================================================
ERROR: test_group_by_type_with_frozen_set_key (test.TestGroupByType)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-mvz308/test.py", line 80, in test_group_by_type_with_frozen_set_key
    solution.group_by_type({(1, 2): 12, ('a', 1): 1, test_set: 15}))
AttributeError: 'module' object has no attribute 'group_by_type'

======================================================================
ERROR: test_group_by_type_with_functions (test.TestGroupByType)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-mvz308/test.py", line 90, in test_group_by_type_with_functions
    solution.group_by_type({
AttributeError: 'module' object has no attribute 'group_by_type'

======================================================================
FAIL: test_sort_by_empty (test.TestSortBy)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-mvz308/test.py", line 44, in test_sort_by_empty
    self.assertEqual([], solution.sort_by(lambda x, y: x - y, []))
AssertionError: [] != None

======================================================================
FAIL: test_sort_by_one_element (test.TestSortBy)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-mvz308/test.py", line 57, in test_sort_by_one_element
    self.assertEqual([1], solution.sort_by(lambda x, y: x - y, [1]))
AssertionError: [1] != None

----------------------------------------------------------------------
Ran 16 tests in 0.011s

FAILED (failures=2, errors=9)

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

Стоянка обнови решението на 19.03.2014 12:07 (преди над 10 години)

+def is_pangram(sentence):
+ letters = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м',
+ 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
+ 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
+ for item in letters:
+ if item not in sentence.lower():
+ result = False
+ break
+ else:
+ result = True
+ return result
+
+
+def char_histogram(text):
+ dictionary = {}
+ for item in text:
+ key = item
+ value = text.count(item)
+ dictionary.update({key: value})
+ return dictionary
+
+
+import functools
+
+
+def sort_by(func, arguments):
+ k = 0
+ repeat = 0
+ restart = True
+ length = len(arguments) - 1
+ while restart:
+ restart = False
+ while k < length and repeat < length:
+ number = functools.reduce(func, arguments[k:k + 2])
+ if number > 0:
+ arguments[k], arguments[k + 1] = arguments[k + 1], arguments[k]
+ k += 1
+ if k == length:
+ k = 0
+ repeat += 1
+ restart = True
+ if repeat == length:
+ break
+ return arguments
  • в is_pangram итерираш letters тоест по-добре е така for letter in letters:; използваш това, което итерираш в единствено число
  • това с result и break-ването не е хубаво; директно return-вай
  • key = item за какво ти е? защо не използваш директно item; и пак смени item на нещо, от което се състои text
  • ако искаш в даден речник да сложиш на ключ дадена стойност правиш d[key] = value, а не d.update({key: value})
  • не използвай типове в имената; dictionary
  • import-овете стоят най-отгоре във файла
  • трябва да преработиш sort_by, прекалено е сложно и хаотично за един sort
  • това, което правиш с reduce не е коректно, просто си извикай функцията с два аргументa; reduce не прави следното: да извика функцията с всички елементи на дадената колекция като аргументи, това, че в случая прави това е по-скоро инцидентно

Стоянка обнови решението на 19.03.2014 16:19 (преди над 10 години)

+import functools
+
+
def is_pangram(sentence):
letters = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м',
'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
- for item in letters:
- if item not in sentence.lower():
+ for letter in letters:
+ if letter not in sentence.lower():
result = False
break
else:
result = True
return result
def char_histogram(text):
- dictionary = {}
- for item in text:
- key = item
- value = text.count(item)
- dictionary.update({key: value})
- return dictionary
+ symbol_count = {}
+ for symbol in text:
+ value = text.count(symbol)
+ symbol_count[symbol] = value
+ return symbol_count
-import functools
-
-
def sort_by(func, arguments):
k = 0
repeat = 0
- restart = True
length = len(arguments) - 1
- while restart:
- restart = False
+ while k < length and repeat < length:
- while k < length and repeat < length:
+ arg = arguments[k: k + 2]
- number = functools.reduce(func, arguments[k:k + 2])
+ number = functools.reduce(func, arg)
- if number > 0:
+ if number > 0:
- arguments[k], arguments[k + 1] = arguments[k + 1], arguments[k]
+ arguments[k], arguments[k + 1] = arguments[k + 1], arguments[k]
- k += 1
+ k += 1
- if k == length:
+ if k == length:
- k = 0
+ k = 0
- repeat += 1
+ repeat += 1
- restart = True
+ if repeat == length:
- if repeat == length:
+ return arguments
- break
- return arguments