Решение на Пет функции от Гергана Маврова

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

Към профила на Гергана Маврова

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 14 успешни тест(а)
  • 2 неуспешни тест(а)

Код

import functools
from collections import Counter
def is_pangram(sentence):
cyrillic = {'я', 'ю', 'ь', 'ъ', 'щ', 'ш', 'ч', 'ц', 'х', 'ф', 'у', 'т',
'с', 'р', 'м', 'п', 'о', 'н', 'л', 'к', 'й', 'и', 'з', 'ж',
'е', 'д', 'г', 'в', 'б', 'а'}
alphabet = {character for character in set(sentence.lower())
if character in cyrillic}
if len(alphabet) == 30:
return True
return False
def char_histogram(text):
symbol_count = Counter(list(text))
return symbol_count
def sort_by(func, arguments):
result = sorted(arguments, key=functools.cmp_to_key(func))
return(result)
def group_by_type(dictionary):
new_dictionary = {type(new_key_type):
{old_key: old_value for old_key, old_value in
dictionary.items() if type(old_key) == type(new_key_type)
} for new_key_type in dictionary.keys()}
return(new_dictionary)
def to_characters(word):
new_word = "".join(filter(lambda c: c.isalpha(), word))
return new_word
def anagrams(words):
unique_words = []
for each in words:
if sorted(to_characters(each).lower()) not in unique_words:
unique_words.append(sorted(to_characters(each)))
grouped_words = [[member for member in words
if unique == sorted(to_characters(member).lower())]
for unique in unique_words]
return(grouped_words)

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

.FF.............
======================================================================
FAIL: 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-1duug1j/test.py", line 125, in test_with_different_cases
    set(map(frozenset, solution.anagrams(words))))
AssertionError: Items in the first set but not the second:
frozenset({'Ray Adverb', 'Dave Barry'})
Items in the second set but not the first:
frozenset()

======================================================================
FAIL: 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-1duug1j/test.py", line 135, in test_with_different_symbols
    set(map(frozenset, solution.anagrams(words))))
AssertionError: Items in the first set but not the second:
frozenset({'Tom Cruise', "So I'm cuter"})
frozenset({'I am Lord Voldemort', 'Tom Marvolo Riddle'})
Items in the second set but not the first:
frozenset()

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

FAILED (failures=2)

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

Гергана обнови решението на 19.03.2014 15:45 (преди около 10 години)

+import functools
+from collections import Counter
+
+
+def is_pangram(sentence):
+ cyrillic = {'я', 'ю', 'ь', 'ъ', 'щ', 'ш', 'ч', 'ц', 'х', 'ф', 'у', 'т',
+ 'с', 'р', 'м', 'п', 'о', 'н', 'л', 'к', 'й', 'и', 'з', 'ж',
+ 'е', 'д', 'г', 'в', 'б', 'а'}
+ alphabet = {character for character in set(sentence.lower())
+ if character in cyrillic}
+ if len(alphabet) == 30:
+ return True
+ return False
+
+
+def char_histogram(text):
+ symbol_count = Counter(list(text))
+ return symbol_count
+
+
+def sort_by(func, arguments):
+ result = sorted(arguments, key=functools.cmp_to_key(func))
+ return(result)
+
+
+def group_by_type(dictionary):
+ new_dictionary = {type(new_key_type):
+ {old_key: old_value for old_key, old_value in
+ dictionary.items() if type(old_key) == type(new_key_type)
+ } for new_key_type in dictionary.keys()}
+ return(new_dictionary)
+
+
+def to_characters(word):
+ new_word = "".join(filter(lambda c: c.isalpha(), word))
+ return new_word
+
+
+def anagrams(words):
+ unique_words = []
+ for each in words:
+ if sorted(to_characters(each).lower()) not in unique_words:
+ unique_words.append(sorted(to_characters(each)))
+ grouped_words = [[member for member in words
+ if unique == sorted(to_characters(member).lower())]
+ for unique in unique_words]
+ return(grouped_words)