Решение на Пет функции от Илия Тобов

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

Към профила на Илия Тобов

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 16 успешни тест(а)
  • 0 неуспешни тест(а)

Код

from collections import Counter
from functools import cmp_to_key
ALPHABET = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
def is_pangram(sentence):
return all(letter in list(sentence.lower()) for letter in ALPHABET)
def char_histogram(text):
return {letter: count for(letter, count) in Counter(text).items()}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
pairs = [(type(key), {key: value}) for key, value in dictionary.items()]
result = {}
for key, value in pairs:
result.setdefault(key, {}).update(value)
return result
def key(word):
word_letters = [char for char in list(word.lower()) if char.isalpha()]
return tuple(sorted(word_letters))
def anagrams(words):
result = {key(word): find_all_anagrams(word, words) for word in words}
return list(result.values())
def is_anagram(word, anagram):
return sorted(key(word.lower())) == sorted(key(anagram.lower()))
def find_all_anagrams(anagram, words):
return [word for word in words if is_anagram(word, anagram)]

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.011s

OK

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

Илия обнови решението на 17.03.2014 12:51 (преди над 10 години)

+from collections import Counter
+from functools import cmp_to_key
+
+
+ALPHABET = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
+ 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
+ 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
+
+def char_histogram(text):
+ return {letter: count for(letter, count) in Counter(text).items()}
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+def group_by_type(dictionary):
+ pairs = [(type(key), {key: value}) for key, value in dictionary.items()]
+ result = {}
+ for key, value in pairs:
+ result.setdefault(key, {}).update(value)
+
+ return result
+
+def key(word):
+ return tuple(sorted(word))
+
+def anagrams(words):
+ return list({key(word): all_anagrams(word, words) for word in words}.values())
+
+def is_anagram(word, anagram):
+ return sorted(word) == sorted(anagram)
+
+def all_anagrams(anagram, words):
+ return [word for word in words if is_anagram(word, anagram)]

Знам, че имам един ред над 79 символа. Сега видях дискусията във форума за това, че трябва да се гледат само буквите, а не всички символи. Скоро време ще оправя тези 2 неща : )

Илия обнови решението на 17.03.2014 16:25 (преди над 10 години)

from collections import Counter
from functools import cmp_to_key
ALPHABET = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
def char_histogram(text):
return {letter: count for(letter, count) in Counter(text).items()}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
pairs = [(type(key), {key: value}) for key, value in dictionary.items()]
result = {}
for key, value in pairs:
result.setdefault(key, {}).update(value)
return result
def key(word):
- return tuple(sorted(word))
+ word_letters = [char for char in list(word.lower()) if char.isalpha()]
+ return tuple(sorted(word_letters))
def anagrams(words):
- return list({key(word): all_anagrams(word, words) for word in words}.values())
+ result = {key(word): find_all_anagrams(word, words) for word in words}
+ return list(result.values())
def is_anagram(word, anagram):
- return sorted(word) == sorted(anagram)
+ return sorted(key(word.lower())) == sorted(key(anagram.lower()))
-def all_anagrams(anagram, words):
+def find_all_anagrams(anagram, words):
return [word for word in words if is_anagram(word, anagram)]

Илия обнови решението на 18.03.2014 11:26 (преди над 10 години)

from collections import Counter
from functools import cmp_to_key
ALPHABET = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
+
+def is_pangram(sentence):
+ return all(letter in list(sentence.lower()) for letter in ALPHABET)
+
def char_histogram(text):
return {letter: count for(letter, count) in Counter(text).items()}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
pairs = [(type(key), {key: value}) for key, value in dictionary.items()]
result = {}
for key, value in pairs:
result.setdefault(key, {}).update(value)
return result
def key(word):
word_letters = [char for char in list(word.lower()) if char.isalpha()]
return tuple(sorted(word_letters))
def anagrams(words):
result = {key(word): find_all_anagrams(word, words) for word in words}
return list(result.values())
def is_anagram(word, anagram):
return sorted(key(word.lower())) == sorted(key(anagram.lower()))
def find_all_anagrams(anagram, words):
return [word for word in words if is_anagram(word, anagram)]