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

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

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

Резултати

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

Код

from collections import Counter, defaultdict
from functools import cmp_to_key
import string
CYRILIC = 'абвгдежзийклмнопрстуфхцчшщъьюя'
LATIN = string.ascii_lowercase
ALPHABETS = CYRILIC + LATIN
def is_pangram(sentence):
letters = set(CYRILIC)
for letter in sentence.lower():
if letter in letters:
letters.remove(letter)
return not letters
def char_histogram(text):
return Counter(text)
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
grouped = defaultdict(dict)
for key, value in dictionary.items():
grouped[type(key)][key] = value
return grouped
def anagrams(words):
alpha_words = (filter(lambda c: c.isalpha(), w.lower()) for w in words)
words_ordered = [''.join(sorted(word)) for word in alpha_words]
result = defaultdict(list)
for i in range(len(words)):
result[words_ordered[i]].append(words[i])
return list(result.values())

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

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

OK

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

Беатрис обнови решението на 19.03.2014 14:33 (преди над 10 години)

+from collections import Counter, defaultdict
+from functools import cmp_to_key
+import string
+
+CYRILIC = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+LATIN = string.ascii_lowercase
+ALPHABETS = CYRILIC + LATIN
+
+
+def is_pangram(sentence):
+ letters = set(CYRILIC)
+ for letter in sentence.lower():
+ if letter in letters:
+ letters.remove(letter)
+ return not letters
+
+
+def char_histogram(text):
+ return Counter(text)
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ grouped = defaultdict(dict)
+ for key, value in dictionary.items():
+ grouped[type(key)][key] = value
+ return grouped
+
+
+def anagrams(words):
+ alpha_words = (filter(lambda c: c.isalpha(), w.lower()) for w in words)
+ words_ordered = [''.join(sorted(word)) for word in alpha_words]
+ result = defaultdict(list)
+ for i in range(len(words)):
+ result[words_ordered[i]].append(words[i])
+ return list(result.values())