Решение на Пет функции от Марио Даскалов

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

Към профила на Марио Даскалов

Резултати

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

Код

from functools import cmp_to_key
ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def groupby(func, iterable):
groupped = {}
for key, item in map(lambda item: (func(item), item), iterable):
groupped[key] = groupped.setdefault(key, []) + [item]
return groupped
def is_pangram(sentence):
return all([letter in sentence for letter in ALPHABET])
def char_histogram(text):
return dict(map(lambda symbol: (symbol, text.count(symbol)), text))
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
groupped = groupby(lambda item: type(item[0]), dictionary.items())
return {key: dict(value) for key, value in groupped.items()}
def anagrams(words):
groupped = groupby(lambda word: ''.join(sorted(word.lower())), words)
return list(groupped.values())

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

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

======================================================================
FAIL: test_with_pangrams (test.TestIsPangram)
----------------------------------------------------------------------
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-1kbm1dz/test.py", line 15, in test_with_pangrams
    solution.is_pangram('За миг бях в чужд, скърцащ плюшен фотьойл.'))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 16 tests in 0.009s

FAILED (failures=2)

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

Марио обнови решението на 15.03.2014 02:49 (преди над 10 години)

+from functools import cmp_to_key
+
+
+ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+
+
+def groupby(func, iterable):
+ groupped = {}
+ for key, item in map(lambda item: (func(item), item), iterable):
+ groupped[key] = groupped.setdefault(key, []) + [item]
+ return groupped
+
+
+def is_panagram(sentence):
+ return all([letter in sentence for letter in ALPHABET])
+
+
+def char_histogram(text):
+ return dict(map(lambda symbol: (symbol, text.count(symbol)), text))
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ groupped = groupby(lambda item: type(item[0]), dictionary.items())
+ return {key: dict(value) for key, value in groupped.items()}
+
+
+def anagrams(words):
+ return list(groupby(lambda word: ''.join(sorted(word)), words).values())

Марио обнови решението на 18.03.2014 00:26 (преди над 10 години)

from functools import cmp_to_key
ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def groupby(func, iterable):
groupped = {}
for key, item in map(lambda item: (func(item), item), iterable):
groupped[key] = groupped.setdefault(key, []) + [item]
return groupped
-def is_panagram(sentence):
+def is_pangram(sentence):
return all([letter in sentence for letter in ALPHABET])
def char_histogram(text):
return dict(map(lambda symbol: (symbol, text.count(symbol)), text))
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
groupped = groupby(lambda item: type(item[0]), dictionary.items())
return {key: dict(value) for key, value in groupped.items()}
def anagrams(words):
- return list(groupby(lambda word: ''.join(sorted(word)), words).values())
+ groupped = groupby(lambda word: ''.join(sorted(word.lower())), words)
+ return list(groupped.values())