Решение на Пет функции от Антония Чекръкчиева

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

Към профила на Антония Чекръкчиева

Резултати

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

Код

from functools import cmp_to_key
def char_histogram(text):
histogram_dict = {}
for word in text:
if word in histogram_dict:
histogram_dict[word] = histogram_dict[word] + 1
else:
histogram_dict[word] = 1
return (histogram_dict)
def is_pangram(sentence):
sentence_histogram = char_histogram(sentence.upper())
alpha_histogram = [i for i in sentence_histogram.keys() if i.isalpha()]
return (len(alpha_histogram) == 30)
def sort_by(func, arguments):
arguments.sort(key=cmp_to_key(func))
return arguments
def group_by_type(dictionary):
dictionary_by_type = {}
for i in dictionary.keys():
dictionary_by_type[type(i)] = dict([(x, dictionary[x]) for x in dictionary.keys() if type(x) == type(i)])
return dictionary_by_type
def get_letters(word):
return sorted(list(i for i in word if i.isalpha()))
def anagrams(words):
word_list = []
for word in words:
list_with_anagrams_word = list([word2 for word2 in words if get_letters(word) == get_letters(word2)])
if list_with_anagrams_word not in word_list:
word_list.append(list_with_anagrams_word)
return (word_list)

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

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

----------------------------------------------------------------------
Ran 16 tests in 0.013s

FAILED (failures=2)

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

Антония обнови решението на 14.03.2014 00:23 (преди над 10 години)

+from functools import cmp_to_key
+
+def char_histogram(text):
+ histogram_dict = {}
+
+ for word in text:
+ if word in histogram_dict:
+ histogram_dict[word] = histogram_dict[word] + 1
+ else:
+ histogram_dict[word] = 1
+
+ return (histogram_dict)
+
+def is_pangram(sentence):
+
+ sentence_histogram = char_histogram(sentence.upper())
+ punctuation = [' ', ',', '.', '-', '!', '?']
+ for i in punctuation:
+ if i in sentence_histogram.keys():
+ del sentence_histogram[i]
+ return (len(sentence_histogram) == 30)
+
+def sort_by(func, arguments):
+ arguments.sort(key=cmp_to_key(func))
+ return arguments
+
+def group_by_type(dictionary):
+
+ dictionary_by_type = {}
+ for i in dictionary.keys():
+ dictionary_by_type[type(i)] = dict([(x, dictionary[x]) for x in dictionary.keys() if type(x) == type(i)])
+ return dictionary_by_type
+
+def anagrams(words):
+ n = []
+ for s in words:
+ a = list([x for x in words if sorted(list(s)) == sorted(list(x))])
+ if a not in n:
+ n.append(a)
+
+ return (n)

Антония обнови решението на 17.03.2014 10:54 (преди над 10 години)

from functools import cmp_to_key
def char_histogram(text):
histogram_dict = {}
for word in text:
if word in histogram_dict:
histogram_dict[word] = histogram_dict[word] + 1
else:
histogram_dict[word] = 1
return (histogram_dict)
def is_pangram(sentence):
sentence_histogram = char_histogram(sentence.upper())
punctuation = [' ', ',', '.', '-', '!', '?']
for i in punctuation:
if i in sentence_histogram.keys():
del sentence_histogram[i]
return (len(sentence_histogram) == 30)
def sort_by(func, arguments):
arguments.sort(key=cmp_to_key(func))
return arguments
def group_by_type(dictionary):
dictionary_by_type = {}
for i in dictionary.keys():
dictionary_by_type[type(i)] = dict([(x, dictionary[x]) for x in dictionary.keys() if type(x) == type(i)])
return dictionary_by_type
+def get_letters(word):
+ return sorted(list(i for i in word if i.isalpha()))
+
def anagrams(words):
- n = []
- for s in words:
+ anagrams_list = []
- a = list([x for x in words if sorted(list(s)) == sorted(list(x))])
+ for word in words:
- if a not in n:
+ list_with_anagrams_word = list([word2 for word2 in words if get_letters(word) == get_letters(word2)])
- n.append(a)
+ if list_with_anagrams_word not in anagrams_list:
-
+ anagrams_list.append(list_with_anagrams_word)
- return (n)
+ return (anagrams_list)
  • Не се опитвай да махнеш всички символи, за които се сетиш. Много са. Просто търси символите, които са букви.
  • Не трябва да правиш разлика между малки и големи букви
  • Не слагай типа, в името на променлива. Демек anagrams_list е лошо име.

Антония обнови решението на 19.03.2014 14:22 (преди над 10 години)

from functools import cmp_to_key
def char_histogram(text):
histogram_dict = {}
for word in text:
if word in histogram_dict:
histogram_dict[word] = histogram_dict[word] + 1
else:
histogram_dict[word] = 1
return (histogram_dict)
def is_pangram(sentence):
sentence_histogram = char_histogram(sentence.upper())
- punctuation = [' ', ',', '.', '-', '!', '?']
- for i in punctuation:
- if i in sentence_histogram.keys():
- del sentence_histogram[i]
- return (len(sentence_histogram) == 30)
+ alpha_histogram = [i for i in sentence_histogram.keys() if i.isalpha()]
+ return (len(alpha_histogram) == 30)
+
def sort_by(func, arguments):
arguments.sort(key=cmp_to_key(func))
return arguments
def group_by_type(dictionary):
dictionary_by_type = {}
for i in dictionary.keys():
dictionary_by_type[type(i)] = dict([(x, dictionary[x]) for x in dictionary.keys() if type(x) == type(i)])
return dictionary_by_type
def get_letters(word):
return sorted(list(i for i in word if i.isalpha()))
def anagrams(words):
- anagrams_list = []
+ word_list = []
for word in words:
list_with_anagrams_word = list([word2 for word2 in words if get_letters(word) == get_letters(word2)])
- if list_with_anagrams_word not in anagrams_list:
- anagrams_list.append(list_with_anagrams_word)
+ if list_with_anagrams_word not in word_list:
- return (anagrams_list)
+ word_list.append(list_with_anagrams_word)
+ return (word_list)