Решение на Пет функции от Лъчезар Николов

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

Към профила на Лъчезар Николов

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 12 успешни тест(а)
  • 4 неуспешни тест(а)

Код

BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def is_pangram(sentence):
alphabet = set(BULGARIAN_ALPHABET)
for letter in sentence.lower():
if letter in alphabet:
alphabet.remove(letter)
return not bool(alphabet)
def char_histogram(text):
histogram = {symbol: 0 for symbol in text}
for symbol in text:
histogram[symbol] += 1
return histogram
def sort_by(func, arguments):
sorted_arguments = list(arguments)
for i in range(1, len(sorted_arguments)):
j = i - 1
key = sorted_arguments[i]
while (func(key, sorted_arguments[j]) < 0) and (j >= 0):
sorted_arguments[j + 1] = sorted_arguments[j]
j -= 1
sorted_arguments[j + 1] = key
return sorted_arguments
def group_by_type(dictionary):
type_groups = {type(key): {} for key in dictionary}
for key in dictionary:
type_groups[type(key)][key] = dictionary[key]
return type_groups
def remove_non_alpha(text):
return ''.join([_ for _ in text if _.isalpha()])
def are_anagrams(first_word, second_word):
lhs = ''.join(sorted(remove_punctuation(first_word.lower())))
rhs = ''.join(sorted(remove_punctuation(second_word.lower())))
return lhs == rhs
def anagrams(words):
groups = [[a for a in words if are_anagrams(a, w)] for w in words]
return list(map(list, set(map(frozenset, groups))))

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

EEEE............
======================================================================
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-ggxldz/test.py", line 109, in test_list_of_latin_anagrams
    set(map(frozenset, solution.anagrams(words))))
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in anagrams
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in <listcomp>
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in <listcomp>
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 42, in are_anagrams
    lhs = ''.join(sorted(remove_punctuation(first_word.lower())))
NameError: global name 'remove_punctuation' is not defined

======================================================================
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-ggxldz/test.py", line 125, in test_with_different_cases
    set(map(frozenset, solution.anagrams(words))))
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in anagrams
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in <listcomp>
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in <listcomp>
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 42, in are_anagrams
    lhs = ''.join(sorted(remove_punctuation(first_word.lower())))
NameError: global name 'remove_punctuation' is not defined

======================================================================
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-ggxldz/test.py", line 135, in test_with_different_symbols
    set(map(frozenset, solution.anagrams(words))))
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in anagrams
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in <listcomp>
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in <listcomp>
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 42, in are_anagrams
    lhs = ''.join(sorted(remove_punctuation(first_word.lower())))
NameError: global name 'remove_punctuation' is not defined

======================================================================
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-ggxldz/test.py", line 117, in test_with_list_of_cyrilic_anagrams
    set(map(frozenset, solution.anagrams(words))))
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in anagrams
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in <listcomp>
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 48, in <listcomp>
    groups = [[a for a in words if are_anagrams(a, w)] for w in words]
  File "/tmp/d20140319-21201-ggxldz/solution.py", line 42, in are_anagrams
    lhs = ''.join(sorted(remove_punctuation(first_word.lower())))
NameError: global name 'remove_punctuation' is not defined

----------------------------------------------------------------------
Ran 16 tests in 0.010s

FAILED (errors=4)

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

Лъчезар обнови решението на 19.03.2014 03:18 (преди над 10 години)

+BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+
+def is_pangram(sentence):
+ alphabet = set(BULGARIAN_ALPHABET)
+ for letter in sentence.lower():
+ if letter in alphabet:
+ alphabet.remove(letter)
+ return not bool(alphabet)
+
+
+def char_histogram(text):
+ histogram = {symbol: 0 for symbol in text}
+ for symbol in text:
+ histogram[symbol] += 1
+ return histogram
+
+
+def sort_by(func, arguments):
+ sorted_arguments = list(arguments)
+ for i in range(1, len(sorted_arguments)):
+ j = i - 1
+ key = sorted_arguments[i]
+ while (func(key, sorted_arguments[j]) < 0) and (j >= 0):
+ sorted_arguments[j + 1] = sorted_arguments[j]
+ j -= 1
+ sorted_arguments[j + 1] = key
+ return sorted_arguments
+
+
+def group_by_type(dictionary):
+ type_groups = {type(key): {} for key in dictionary}
+ for key in dictionary:
+ type_groups[type(key)][key] = dictionary[key]
+ return type_groups
+
+
+def remove_punctuation(text):
+ return ''.join([_ for _ in text if _.isalpha()])
+
+
+def are_anagrams(first_word, second_word):
+ lhs = ''.join(sorted(remove_punctuation(first_word.lower())))
+ rhs = ''.join(sorted(remove_punctuation(second_word.lower())))
+ return lhs == rhs
+
+
+def anagrams(words):
+ groups = [[_ for _ in words if are_anagrams(_, w)] for w in words]
+ return list(map(list, set(map(frozenset, groups))))
  • това, което правиш с речниците в char_histogram и group_by_type го има вградено(единия начин го има в лекция, а другия в хитринка)
  • по условие на sort_by се подава списък; тогава за какво ти е sorted_arguments = list(arguments), можеш директно да използваш arguments
  • това условие според мен не прави нищо j >= 0; не виждам случай, в който j ще е отрицателно
  • _ се използва като placeholder за нещо, което няма да изпозлваш; в remove_punctuation очевидно го използваш на 3 места
  • remove_punctuation може би трябва да се казва remove_non_letters, защото освен пунктуационните значи маха и числата
  • какво е lhs и rhs? промени ги
  • в are_anagrams можеш да направиш сравнението и без да join-ваш, директно със списъците върнати от sorted
  • в anagrams пак тези _

Лъчезар обнови решението на 19.03.2014 16:59 (преди над 10 години)

BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def is_pangram(sentence):
alphabet = set(BULGARIAN_ALPHABET)
for letter in sentence.lower():
if letter in alphabet:
alphabet.remove(letter)
return not bool(alphabet)
def char_histogram(text):
histogram = {symbol: 0 for symbol in text}
for symbol in text:
histogram[symbol] += 1
return histogram
def sort_by(func, arguments):
sorted_arguments = list(arguments)
for i in range(1, len(sorted_arguments)):
j = i - 1
key = sorted_arguments[i]
while (func(key, sorted_arguments[j]) < 0) and (j >= 0):
sorted_arguments[j + 1] = sorted_arguments[j]
j -= 1
sorted_arguments[j + 1] = key
return sorted_arguments
def group_by_type(dictionary):
type_groups = {type(key): {} for key in dictionary}
for key in dictionary:
type_groups[type(key)][key] = dictionary[key]
return type_groups
-def remove_punctuation(text):
+def remove_non_alpha(text):
return ''.join([_ for _ in text if _.isalpha()])
def are_anagrams(first_word, second_word):
lhs = ''.join(sorted(remove_punctuation(first_word.lower())))
rhs = ''.join(sorted(remove_punctuation(second_word.lower())))
return lhs == rhs
def anagrams(words):
- groups = [[_ for _ in words if are_anagrams(_, w)] for w in words]
- return list(map(list, set(map(frozenset, groups))))
+ groups = [[a for a in words if are_anagrams(a, w)] for w in words]
+ return list(map(list, set(map(frozenset, groups))))