Решение на Пет функции от Станислав Гатев

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

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

Резултати

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

Код

from itertools import groupby
from functools import cmp_to_key
def is_pangram(text):
return set('абвгдежзийклмнопрстуфхцчшщъьяю').issubset(set(text.lower()))
def char_histogram(text):
return {char: text.count(char) for char in text}
def sort_by(comparator, sequence):
return sorted(sequence, key=cmp_to_key(comparator))
def subdict(dictionary, keys):
return {key: dictionary[key] for key in keys}
def compose(f, g):
return lambda x: f(g(x))
def group_by_type(dictionary):
return {group_type: subdict(dictionary, keys) for group_type, keys in
groupby(sorted(dictionary.keys(), key=compose(id, type)), type)}
def letters_in_word(word):
return ''.join(sorted([char.lower() for char in word if char.isalpha()]))
def anagrams(words):
return [list(anagrams) for _, anagrams in
groupby(sorted(words, key=letters_in_word), letters_in_word)]

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.009s

OK

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

Станислав обнови решението на 12.03.2014 21:59 (преди над 10 години)

+from itertools import groupby
+from functools import cmp_to_key
+
+
+def is_pangram(text):
+ return set('абвгдежзийклмнопрстуфхцчшщъьяю').issubset(set(text.lower()))
+
+
+def char_histogram(text):
+ return {char: text.count(char) for char in text}
+
+
+def sort_by(comparator, sequence):
+ return sorted(sequence, key=cmp_to_key(comparator))
+
+
+def group_by_type(dictionary):
+ return {t: {k: dictionary[k] for k in keys}
+ for t, keys in groupby(dictionary.keys(), type)}
+
+
+def anagrams(words):
+ return [list(v) for k, v in groupby(words, set)]

Станислав обнови решението на 12.03.2014 22:41 (преди над 10 години)

from itertools import groupby
from functools import cmp_to_key
def is_pangram(text):
return set('абвгдежзийклмнопрстуфхцчшщъьяю').issubset(set(text.lower()))
def char_histogram(text):
return {char: text.count(char) for char in text}
def sort_by(comparator, sequence):
return sorted(sequence, key=cmp_to_key(comparator))
def group_by_type(dictionary):
- return {t: {k: dictionary[k] for k in keys}
- for t, keys in groupby(dictionary.keys(), type)}
+ unique_type = lambda key: id(type(key))
+ groups = groupby(sorted(dictionary.keys(), key=unique_type), type)
+ return {t: {k: dictionary[k] for k in keys} for t, keys in groups}
def anagrams(words):
- return [list(v) for k, v in groupby(words, set)]
+ return [list(v) for _, v in groupby(words, set)]

Станислав обнови решението на 16.03.2014 01:40 (преди над 10 години)

from itertools import groupby
from functools import cmp_to_key
def is_pangram(text):
return set('абвгдежзийклмнопрстуфхцчшщъьяю').issubset(set(text.lower()))
def char_histogram(text):
return {char: text.count(char) for char in text}
def sort_by(comparator, sequence):
return sorted(sequence, key=cmp_to_key(comparator))
+def subdict(dictionary, keys):
+ return {key: dictionary[key] for key in keys}
+
+
+def compose(f, g):
+ return lambda x: f(g(x))
+
+
def group_by_type(dictionary):
- unique_type = lambda key: id(type(key))
- groups = groupby(sorted(dictionary.keys(), key=unique_type), type)
- return {t: {k: dictionary[k] for k in keys} for t, keys in groups}
+ return {group_type: subdict(dictionary, keys) for group_type, keys in
+ groupby(sorted(dictionary.keys(), key=compose(id, type)), type)}
+def letters_in_word(word):
+ return ''.join(sorted([char.lower() for char in word if char.isalpha()]))
+
+
def anagrams(words):
- return [list(v) for _, v in groupby(words, set)]
+ return [list(anagrams) for _, anagrams in
+ groupby(sorted(words, key=letters_in_word), letters_in_word)]