Решение на Пет функции от Велина Дойчева

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

Към профила на Велина Дойчева

Резултати

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

Код

from collections import Counter, defaultdict
from functools import cmp_to_key
from itertools import groupby
alphabet_bg = set('абвгдежзийклмнопрстуфхцчшщьъюя')
alphabet_en = set('abcdefghijklmnopqrstuvwxyz')
def is_pangram(sentence):
return alphabet_bg.issubset(set(sentence.lower()))
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):
key_types = set(type(key) for key in dictionary.keys())
return dict((key_type, dict((key, dictionary[key])
for key in dictionary.keys() if type(key) == key_type))
for key_type in key_types)
def filtered(old_word):
word = old_word.lower()
return sorted(filter(lambda x: x in alphabet_bg or x in alphabet_en, word))
def group_anagrams(words):
return groupby(sorted(words, key=filtered), key=filtered)
def anagrams(words):
return [list(anagrams) for _, anagrams in group_anagrams(words)]

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

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

OK

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

Велина обнови решението на 16.03.2014 23:59 (преди над 10 години)

+from collections import Counter
+from functools import cmp_to_key
+from itertools import groupby
+
+alphabet = set(['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', \
+ 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', \
+ 'ц','ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'])
+
+def is_pangram(sentence):
+ return alphabet.issubset(set(sentence.lower()))
+
+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):
+ key_types = set(type(key) for key in dictionary.keys())
+ return dict((key_type, dict((key, dictionary[key]) for key in dictionary.keys() if type(key) == key_type)) for key_type in key_types)
+
+def anagrams(words):
+ return [list(anagrams) for _, anagrams in groupby(sorted(words, key=sorted), key = set)]

Велина обнови решението на 17.03.2014 00:00 (преди над 10 години)

from collections import Counter
from functools import cmp_to_key
from itertools import groupby
alphabet = set(['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', \
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', \
'ц','ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'])
def is_pangram(sentence):
return alphabet.issubset(set(sentence.lower()))
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):
key_types = set(type(key) for key in dictionary.keys())
return dict((key_type, dict((key, dictionary[key]) for key in dictionary.keys() if type(key) == key_type)) for key_type in key_types)
def anagrams(words):
- return [list(anagrams) for _, anagrams in groupby(sorted(words, key=sorted), key = set)]
+ return [list(anagrams) for _, anagrams in groupby(sorted(words, key = sorted), key = set)]

Да, супер е, но се погрижи за дългите редове и виж как се форматира по-дълъг списък според PEP8

Също така погледни форумите и ще видиш, че anagrams не се интересува от символите, а само от буквите и не е case-sensitive

Велина обнови решението на 19.03.2014 15:51 (преди над 10 години)

from collections import Counter
from functools import cmp_to_key
from itertools import groupby
-alphabet = set(['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', \
- 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', \
- 'ц','ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'])
+alphabet_bg = set('абвгдежзийклмнопрстуфхцчшщьъюя')
+alphabet_en = set('abcdefghijklmnopqrstuvwxyz')
+
+
def is_pangram(sentence):
- return alphabet.issubset(set(sentence.lower()))
+ return alphabet_bg.issubset(set(sentence.lower()))
+
def char_histogram(text):
return Counter(text)
+
def sort_by(func, arguments):
- return sorted(arguments, key = cmp_to_key(func))
+ return sorted(arguments, key=cmp_to_key(func))
+
def group_by_type(dictionary):
key_types = set(type(key) for key in dictionary.keys())
return dict((key_type, dict((key, dictionary[key]) for key in dictionary.keys() if type(key) == key_type)) for key_type in key_types)
+
+def filtered(old_word):
+ word = old_word.lower()
+ return sorted(filter(lambda x: x in alphabet_bg or x in alphabet_en, word))
+
+
+def group_anagrams(words):
+ return groupby(sorted(words, key=filtered), key=filtered)
+
+
def anagrams(words):
- return [list(anagrams) for _, anagrams in groupby(sorted(words, key = sorted), key = set)]
+ return [list(anagrams) for _, anagrams in group_anagrams(words)]

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

-from collections import Counter
+from collections import Counter, defaultdict
from functools import cmp_to_key
from itertools import groupby
alphabet_bg = set('абвгдежзийклмнопрстуфхцчшщьъюя')
alphabet_en = set('abcdefghijklmnopqrstuvwxyz')
def is_pangram(sentence):
return alphabet_bg.issubset(set(sentence.lower()))
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):
key_types = set(type(key) for key in dictionary.keys())
- return dict((key_type, dict((key, dictionary[key]) for key in dictionary.keys() if type(key) == key_type)) for key_type in key_types)
+ return dict((key_type, dict((key, dictionary[key])
+ for key in dictionary.keys() if type(key) == key_type))
+ for key_type in key_types)
def filtered(old_word):
word = old_word.lower()
return sorted(filter(lambda x: x in alphabet_bg or x in alphabet_en, word))
def group_anagrams(words):
return groupby(sorted(words, key=filtered), key=filtered)
def anagrams(words):
return [list(anagrams) for _, anagrams in group_anagrams(words)]