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

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

Към профила на Теодор Костадинов

Резултати

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

Код

def is_pangram(sentence):
bg_alphabet = {chr(1040+i) for i in range(32)}
used_letters = set()
for letter in sentence.upper():
if letter in bg_alphabet:
used_letters.add(letter)
return len(used_letters) == 30
from collections import Counter
def char_histogram(text):
dict_counter = Counter()
for letter in text:
dict_counter.update(Counter(letter))
return dict_counter
def sort_by(func, arguments):
for i in range(len(arguments)-1):
for j in range(len(arguments)-1):
change = func(arguments[j], arguments[j+1])
if change > 0:
swap = arguments[j]
arguments[j] = arguments[j+1]
arguments[j+1] = swap
return arguments
def group_by_type(dictionary):
dict_by_type = {}
for key, value in dictionary.items():
if type(key) in dict_by_type:
dict_by_type[type(key)].update({key:value})
else:
dict_by_type[type(key)] = {key:value}
return dict_by_type
def anagrams(words):
hist_dict = {}
anagram_groups = list()
lenght_groups = list()
words.sort(key = len)
length_list = list()
for i in range(1, len(words)):
if len(words[i-1]) == len(words[i]):
length_list.append(words[i-1])
else:
length_list.append(words[i-1])
lenght_groups.append(length_list)
length_list = list()
repeating = list()
for words in lenght_groups:
for i in range(len(words)):
anagarm_list = list()
for j in range(len(words)):
if cmp(char_histogram(words[i]),char_histogram(words[j])):
anagram_list.append(words[j])
repeating.append(anagram_list)
return repeating

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

EFEF............
======================================================================
ERROR: test_list_of_latin_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-18ta4it/test.py", line 109, in test_list_of_latin_anagrams
    set(map(frozenset, solution.anagrams(words))))
  File "/tmp/d20140319-21201-18ta4it/solution.py", line 56, in anagrams
    if cmp(char_histogram(words[i]),char_histogram(words[j])):
NameError: global name 'cmp' is not defined

======================================================================
ERROR: 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-18ta4it/test.py", line 135, in test_with_different_symbols
    set(map(frozenset, solution.anagrams(words))))
  File "/tmp/d20140319-21201-18ta4it/solution.py", line 56, in anagrams
    if cmp(char_histogram(words[i]),char_histogram(words[j])):
NameError: global name 'cmp' is not defined

======================================================================
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-18ta4it/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'})

======================================================================
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-18ta4it/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({'кавалер', 'акварел'})

----------------------------------------------------------------------
Ran 16 tests in 0.016s

FAILED (failures=2, errors=2)

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

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

+def is_pangram(sentence):
+ bg_alphabet = {chr(1040+i) for i in range(32)}
+ used_letters = set()
+ for letter in sentence.upper():
+ if letter in bg_alphabet:
+ used_letters.add(letter)
+ return len(used_letters) == 30
+
+from collections import Counter
+def char_histogram(text):
+ dict_counter = Counter()
+ for letter in text:
+ dict_counter.update(Counter(letter))
+ return dict_counter
+
+
+def sort_by(func, arguments):
+ for i in range(len(arguments)-1):
+ for j in range(len(arguments)-1):
+ change = func(arguments[j], arguments[j+1])
+ if change > 0:
+ swap = arguments[j]
+ arguments[j] = arguments[j+1]
+ arguments[j+1] = swap
+ return arguments
+
+def group_by_type(dictionary):
+ dict_by_type = {}
+ for key, value in dictionary.items():
+ if type(key) in dict_by_type:
+ dict_by_type[type(key)].update({key:value})
+ else:
+ dict_by_type[type(key)] = {key:value}
+ return dict_by_type
+
+def anagrams(words):
+ hist_dict = {}
+
+ anagram_groups = list()
+ lenght_groups = list()
+ words.sort(key = len)
+ length_list = list()
+ for i in range(1, len(words)):
+ if len(words[i-1]) == len(words[i]):
+ length_list.append(words[i-1])
+ else:
+ length_list.append(words[i-1])
+ lenght_groups.append(length_list)
+ length_list = list()
+
+ repeating = list()
+ for words in lenght_groups:
+ for i in range(len(words)):
+ anagarm_list = list()
+ for j in range(len(words)):
+ if cmp(char_histogram(words[i]),char_histogram(words[j])):
+ anagram_list.append(words[j])
+ repeating.append(anagram_list)
+
+ return repeating