Решение на Пет функции от Тихомир Кожухарски

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

Към профила на Тихомир Кожухарски

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 11 успешни тест(а)
  • 5 неуспешни тест(а)

Код

from collections import Counter
ALPHABET = list('абвгдежзийклмнопрстуфхцчшщъьюя')
def is_pangram(sentence):
char_set = {char for char in sentence if char in ALPHABET}
return len(char_set) == len(ALPHABET)
def char_histogram(text):
return dict(Counter(list(text)))
def sort_by(func, arguments):
return selection_sort(func, arguments)
def selection_sort(func, arguments):
arguments_length = len(arguments)
for i in range(arguments_length):
for j in range(i):
if func(arguments[j], arguments[i]) > 0:
arguments[i], arguments[j] = arguments[j], arguments[i]
return arguments
def group_by_type(dictionary):
result = {}
for key, value in dictionary.items():
key_type = type(key)
if key_type in result:
result[key_type][key] = value
else:
result[key_type] = {key: value}
return result
def anagrams(words):
result = {}
for word in words:
chars = tuple(char_histogram(word).items())
if chars in result:
result[chars].append(word)
else:
result[chars] = [word]
return list(result.values())

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

.FFF........F..F
======================================================================
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-2fvwa7/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-2fvwa7/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({"So I'm cuter"})
frozenset({'Tom Marvolo Riddle'})
frozenset({'I am Lord Voldemort'})
frozenset({'Tom Cruise'})

======================================================================
FAIL: 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-2fvwa7/test.py", line 117, in test_with_list_of_cyrilic_anagrams
    set(map(frozenset, solution.anagrams(words))))
AssertionError: Items in the first set but not the second:
frozenset({'акварел', 'кавалер'})
Items in the second set but not the first:
frozenset({'кавалер'})
frozenset({'акварел'})

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

======================================================================
FAIL: 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-2fvwa7/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]))
AssertionError: Lists differ: [0, 2, 4, 1, 3, 5] != [0, 2, 4, 3, 1, 5]

First differing element 3:
1
3

- [0, 2, 4, 1, 3, 5]
?              ---

+ [0, 2, 4, 3, 1, 5]
?           +++


----------------------------------------------------------------------
Ran 16 tests in 0.012s

FAILED (failures=5)

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

Тихомир обнови решението на 16.03.2014 20:46 (преди над 10 години)

+from collections import Counter
+
+def is_pangram(sentence):
+ char_set = {char for char in sentence if char in is_pangram.alphabet}
+ return len(char_set) == len(is_pangram.alphabet)
+is_pangram.alphabet = list('абвгдежзийклмнопрстуфхцчшщъьюя')
+
+def char_histogram(text):
+ return dict(Counter(list(text)))
+
+def sort_by(func, arguments):
+ return selection_sort(func, arguments)
+
+def selection_sort(func, arguments):
+ arguments_length = len(arguments)
+ for i in range(arguments_length):
+ for j in range(i):
+ if func(arguments[j], arguments[i]) > 0:
+ arguments[i], arguments[j] = arguments[j], arguments[i]
+ return arguments
+
+def group_by_type(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ key_type = type(key)
+ if key_type in result:
+ result[key_type][key] = value
+ else:
+ result[key_type] = {key: value}
+ return result
+
+def anagrams(words):
+ result = {}
+ for word in words:
+ chars = tuple(char_histogram(word).items())
+ if chars in result:
+ result[chars].append(word)
+ else:
+ result[chars] = [word]
+
+ return [result[_] for _ in result]

Тихомир обнови решението на 19.03.2014 14:58 (преди над 10 години)

from collections import Counter
+ALPHABET = list('абвгдежзийклмнопрстуфхцчшщъьюя')
def is_pangram(sentence):
- char_set = {char for char in sentence if char in is_pangram.alphabet}
- return len(char_set) == len(is_pangram.alphabet)
-is_pangram.alphabet = list('абвгдежзийклмнопрстуфхцчшщъьюя')
+ char_set = {char for char in sentence if char in ALPHABET}
+ return len(char_set) == len(ALPHABET)
def char_histogram(text):
return dict(Counter(list(text)))
def sort_by(func, arguments):
return selection_sort(func, arguments)
def selection_sort(func, arguments):
arguments_length = len(arguments)
for i in range(arguments_length):
for j in range(i):
if func(arguments[j], arguments[i]) > 0:
arguments[i], arguments[j] = arguments[j], arguments[i]
return arguments
def group_by_type(dictionary):
result = {}
for key, value in dictionary.items():
key_type = type(key)
if key_type in result:
result[key_type][key] = value
else:
result[key_type] = {key: value}
return result
def anagrams(words):
result = {}
for word in words:
chars = tuple(char_histogram(word).items())
if chars in result:
result[chars].append(word)
else:
result[chars] = [word]
- return [result[_] for _ in result]
+ return list(result.values())