Решение на Пет функции от Димитър Мутафчиев

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

Към профила на Димитър Мутафчиев

Резултати

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

Код

BG_ALPHA = set([c for c in "абвгдежзийклмнопрстуфхцчшщъьюя"])
def is_pangram(sentence):
lower_sentence = sentence.lower()
letters = set([c for c in lower_sentence if c.isalpha()])
return (letters.issuperset(BG_ALPHA))
def char_histogram(text):
histogram = dict()
for c in text:
histogram[c] = histogram.setdefault(c,0) + 1
return histogram
from functools import cmp_to_key
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
result = dict()
for key, value in dictionary.items():
result.setdefault(type(key),{}).update({key: value})
return result
def anagrams(words):
wordsets = map(frozenset,words)
groups_of_words = dict()
for w in words:
groups_of_words.setdefault(frozenset(w),[]).append(w)
return list(groups_of_words.values())

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

.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-1q3t5hk/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({'Dave Barry', 'Ray Adverb'})
Items in the second set but not the first:
frozenset({'Ray Adverb'})
frozenset({'Dave Barry'})

======================================================================
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-1q3t5hk/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 Marvolo Riddle', 'I am Lord Voldemort'})
frozenset({'Tom Cruise', "So I'm cuter"})
Items in the second set but not the first:
frozenset({'Tom Marvolo Riddle'})
frozenset({"So I'm cuter"})
frozenset({'Tom Cruise'})
frozenset({'I am Lord Voldemort'})

----------------------------------------------------------------------
Ran 16 tests in 0.015s

FAILED (failures=2)

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

Димитър обнови решението на 16.03.2014 20:22 (преди над 10 години)

+BG_ALPHA = set([c for c in "абвгдежзийклмнопрстуфхцчшщъьюя"])
+def is_pangram(sentence):
+ lower_sentence = sentence.lower()
+ letters = set([c for c in lower_sentence if c.isalpha()])
+ return (letters.issuperset(BG_ALPHA))
+
+def char_histogram(text):
+ histogram = dict()
+ for c in text:
+ histogram[c] = histogram.setdefault(c,0) + 1
+ return histogram
+
+from functools import cmp_to_key
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+def group_by_type(dictionary):
+ result = dict()
+ for key, value in dictionary.items():
+ result.setdefault(type(key),{}).update({key: value})
+ return result
+
+def anagrams(words):
+ wordsets = map(frozenset,words)
+ groups_of_words = dict()
+ for w in words:
+ groups_of_words.setdefault(frozenset(w),[]).append(w)
+ return list(groups_of_words.values())