Решение на Пет функции от Йордан Петров

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

Към профила на Йордан Петров

Резултати

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

Код

def is_pangram(sentence):
alfabet_set = set(u'абвгдежзийклмнопрстуфхцчшщъьюя')
subset = alfabet_set & set(sentence.lower())
return subset == alfabet_set
def char_histogram(text):
return {symbol: text.count(symbol) for symbol in text}
def sort_by(func, arguments):
my_key = func
return sorted(arguments, key=func)
def group_by_type(dictionary):
result = {}
for (key, value) in dictionary.items():
if result.get(type(key)) != None:
result[type(key)][key] = value
else:
result[type(key)] = {key : value}
return result
def anagrams(words):
result = {}
for word in words:
hash_ = hash(frozenset(word))
if result.get(hash_) != None:
result[hash_].append(word)
else:
result[hash_] = [word]
return result.values()

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

.FF...........EE
======================================================================
ERROR: 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-l2my10/test.py", line 57, in test_sort_by_one_element
    self.assertEqual([1], solution.sort_by(lambda x, y: x - y, [1]))
  File "/tmp/d20140319-21201-l2my10/solution.py", line 11, in sort_by
    return sorted(arguments, key=func)
TypeError: <lambda>() missing 1 required positional argument: 'y'

======================================================================
ERROR: test_sort_by_simple_test (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-l2my10/test.py", line 54, in test_sort_by_simple_test
    solution.sort_by(lambda x, y: x % 2 - y % 2, [0, 1, 2, 3, 4, 5]))
  File "/tmp/d20140319-21201-l2my10/solution.py", line 11, in sort_by
    return sorted(arguments, key=func)
TypeError: <lambda>() missing 1 required positional argument: 'y'

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

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

FAILED (failures=2, errors=2)

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

Йордан обнови решението на 19.03.2014 16:54 (преди над 10 години)

+def is_pangram(sentence):
+ alfabet_set = set(u'абвгдежзийклмнопрстуфхцчшщъьюя')
+ subset = alfabet_set & set(sentence.lower())
+ return subset == alfabet_set
+
+def char_histogram(text):
+ return {symbol: text.count(symbol) for symbol in text}
+
+def sort_by(func, arguments):
+ my_key = func
+ return sorted(arguments, key=func)
+
+def group_by_type(dictionary):
+ result = {}
+ for (key, value) in dictionary.items():
+ if result.get(type(key)) != None:
+ result[type(key)][key] = value
+ else:
+ result[type(key)] = {key : value}
+ return result
+
+def anagrams(words):
+ result = {}
+ for word in words:
+ hash_ = hash(frozenset(word))
+ if result.get(hash_) != None:
+ result[hash_].append(word)
+ else:
+ result[hash_] = [word]
+ return result.values()