Решение на Пет функции от Ангел Цанев

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

Към профила на Ангел Цанев

Резултати

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

Код

from functools import cmp_to_key
from itertools import groupby
LETTERS = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м',
'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ',
'ъ', 'ь', 'ю', 'я']
def is_pangram(sentence):
return all(char in sentence or char.upper() in sentence for char in LETTERS)
def char_histogram(text):
return {char: text.count(char) for char in set(text)}
def sort_by(compare, iterable):
return sorted(iterable, key=cmp_to_key(compare))
def letters(text):
return ''.join(filter(str.isalpha, text)).lower()
def anagram_key(element):
return sorted(letters(element))
def anagrams(words):
grouped = groupby(sorted(words, key=anagram_key), anagram_key)
return [list(group) for word, group in grouped]
def type_key(element):
return str(type(element[0]))
def group_key(element):
return type(element[0])
def group_by_type(dictionary):
grouped = groupby(sorted(dictionary.items(), key=type_key), group_key)
return {kind: dict(group) for kind, group in grouped}

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

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

OK

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

Ангел обнови решението на 16.03.2014 18:34 (преди над 10 години)

+from functools import cmp_to_key
+from itertools import groupby
+
+LETTERS = list(map(chr, range(1072,1099))) + [chr(1100), chr(1102), chr(1103)]
+
+def is_pangram(sentence):
+ return all(char in sentence or char.upper() in sentence for char in LETTERS)
+
+def char_histogram(text):
+ unique = set(text)
+ return {char: text.count(char) for char in unique}
+
+def sort_by(compare, iterable):
+ return sorted(iterable, key=cmp_to_key(compare))
+
+def letters(input):
+ return ''.join(filter(str.isalpha, input)).lower()
+
+def anagram_key(element):
+ return sorted(letters(element))
+
+def anagrams(words):
+ grouped = groupby(sorted(words, key=anagram_key), anagram_key)
+ return [list(group) for word, group in grouped]
+
+def type_key(element):
+ return str(type(element[0]))
+
+def group_key(element):
+ return type(element[0])
+
+def group_by_type(dictionary):
+ grouped = groupby(sorted(dictionary.items(), key=type_key), group_key)
+ return {kind: dict(group) for kind, group in grouped}
  • какво е това? LETTERS = list(map(chr, range(1072,1099))) + [chr(1100), chr(1102), chr(1103)]; буквите не са много и може би, ако ги изпишеш няма да е нужно да google-вам, за да разбера реда
  • intput не е удачно име

Освен тези неща решенията ти са бомба :bomb:.

Ангел обнови решението на 16.03.2014 21:36 (преди над 10 години)

from functools import cmp_to_key
from itertools import groupby
-LETTERS = list(map(chr, range(1072,1099))) + [chr(1100), chr(1102), chr(1103)]
+LETTERS = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м',
+ 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ',
+ 'ъ', 'ь', 'ю', 'я']
+
def is_pangram(sentence):
return all(char in sentence or char.upper() in sentence for char in LETTERS)
def char_histogram(text):
unique = set(text)
return {char: text.count(char) for char in unique}
def sort_by(compare, iterable):
return sorted(iterable, key=cmp_to_key(compare))
-def letters(input):
- return ''.join(filter(str.isalpha, input)).lower()
+def letters(string):
+ return ''.join(filter(str.isalpha, string)).lower()
def anagram_key(element):
return sorted(letters(element))
def anagrams(words):
grouped = groupby(sorted(words, key=anagram_key), anagram_key)
return [list(group) for word, group in grouped]
def type_key(element):
return str(type(element[0]))
def group_key(element):
return type(element[0])
def group_by_type(dictionary):
grouped = groupby(sorted(dictionary.items(), key=type_key), group_key)
return {kind: dict(group) for kind, group in grouped}

Ангел обнови решението на 17.03.2014 15:58 (преди над 10 години)

from functools import cmp_to_key
from itertools import groupby
LETTERS = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м',
'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ',
'ъ', 'ь', 'ю', 'я']
def is_pangram(sentence):
return all(char in sentence or char.upper() in sentence for char in LETTERS)
def char_histogram(text):
unique = set(text)
return {char: text.count(char) for char in unique}
def sort_by(compare, iterable):
return sorted(iterable, key=cmp_to_key(compare))
-def letters(string):
- return ''.join(filter(str.isalpha, string)).lower()
+def letters(text):
+ return ''.join(filter(str.isalpha, text)).lower()
def anagram_key(element):
return sorted(letters(element))
def anagrams(words):
grouped = groupby(sorted(words, key=anagram_key), anagram_key)
return [list(group) for word, group in grouped]
def type_key(element):
return str(type(element[0]))
def group_key(element):
return type(element[0])
def group_by_type(dictionary):
grouped = groupby(sorted(dictionary.items(), key=type_key), group_key)
return {kind: dict(group) for kind, group in grouped}

Ангел обнови решението на 19.03.2014 13:57 (преди над 10 години)

from functools import cmp_to_key
from itertools import groupby
-
LETTERS = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м',
'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ',
'ъ', 'ь', 'ю', 'я']
def is_pangram(sentence):
return all(char in sentence or char.upper() in sentence for char in LETTERS)
def char_histogram(text):
- unique = set(text)
- return {char: text.count(char) for char in unique}
+ return {char: text.count(char) for char in set(text)}
def sort_by(compare, iterable):
return sorted(iterable, key=cmp_to_key(compare))
def letters(text):
return ''.join(filter(str.isalpha, text)).lower()
def anagram_key(element):
return sorted(letters(element))
def anagrams(words):
grouped = groupby(sorted(words, key=anagram_key), anagram_key)
return [list(group) for word, group in grouped]
def type_key(element):
return str(type(element[0]))
def group_key(element):
return type(element[0])
def group_by_type(dictionary):
grouped = groupby(sorted(dictionary.items(), key=type_key), group_key)
return {kind: dict(group) for kind, group in grouped}