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

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

Към профила на Веселин Генадиев

Резултати

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

Код

from itertools import groupby
from functools import cmp_to_key
import re
def is_pangram(sentence):
alphabet = set('абвгдежзийклмнопрстуфхцчшщъьюя')
letters = {letter for letter in sentence.lower() if letter in alphabet}
return letters == alphabet
def char_histogram(text):
return {key: text.count(key) for key in text}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
keys = sorted(dictionary.keys(), key=lambda key: hash(type(key)))
return {group_type: {key: dictionary[key] for key in group_keys}
for group_type, group_keys in groupby(keys, type)}
def letter_histogram(word):
return char_histogram(re.findall('[^\W0-9_]', word.lower()))
def anagrams(words):
seen = []
histograms = [histogram for histogram in map(letter_histogram, words)
if histogram not in seen and not seen.append(histogram)]
return [[word for word in words if histogram == letter_histogram(word)]
for histogram in histograms]

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

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

OK

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

Веселин обнови решението на 13.03.2014 02:17 (преди около 10 години)

+import itertools
+import functools
+
+
+def is_pangram(sentence):
+ alphabet = set('абвгдежзийклмнопрстуфхцчшщъьюя')
+ letters = {letter for letter in sentence.lower() if letter in alphabet}
+ return letters == alphabet
+
+
+def char_histogram(text):
+ return {key: text.count(key) for key in text}
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=functools.cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ keys = list(dictionary.keys())
+ types = [key for key, _ in itertools.groupby(keys, type)]
+ return {key_type: {key: dictionary[key] for key in keys
+ if type(key) == key_type} for key_type in types}
+
+
+def anagrams(words):
+ return [list(value) for _, value
+ in itertools.groupby(words, char_histogram)]

Веселин обнови решението на 14.03.2014 02:42 (преди около 10 години)

-import itertools
-import functools
+from itertools import groupby
+from functools import cmp_to_key
def is_pangram(sentence):
alphabet = set('абвгдежзийклмнопрстуфхцчшщъьюя')
letters = {letter for letter in sentence.lower() if letter in alphabet}
return letters == alphabet
def char_histogram(text):
return {key: text.count(key) for key in text}
def sort_by(func, arguments):
- return sorted(arguments, key=functools.cmp_to_key(func))
+ return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
- keys = list(dictionary.keys())
- types = [key for key, _ in itertools.groupby(keys, type)]
- return {key_type: {key: dictionary[key] for key in keys
- if type(key) == key_type} for key_type in types}
+ keys = sorted(dictionary.keys(), key=lambda key: hash(type(key)))
+ return {group_type: {key: dictionary[key] for key in group_keys}
+ for group_type, group_keys in groupby(keys, type)}
def anagrams(words):
- return [list(value) for _, value
- in itertools.groupby(words, char_histogram)]
+ seen = []
+ histograms = [histogram for histogram in map(char_histogram, words)
+ if histogram not in seen and not seen.append(histogram)]
+ return [list(filter(lambda word: char_histogram(word) == histogram,
+ words)) for histogram in histograms]

Веселин обнови решението на 14.03.2014 14:48 (преди около 10 години)

from itertools import groupby
from functools import cmp_to_key
def is_pangram(sentence):
alphabet = set('абвгдежзийклмнопрстуфхцчшщъьюя')
letters = {letter for letter in sentence.lower() if letter in alphabet}
return letters == alphabet
def char_histogram(text):
return {key: text.count(key) for key in text}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
keys = sorted(dictionary.keys(), key=lambda key: hash(type(key)))
return {group_type: {key: dictionary[key] for key in group_keys}
for group_type, group_keys in groupby(keys, type)}
def anagrams(words):
seen = []
histograms = [histogram for histogram in map(char_histogram, words)
if histogram not in seen and not seen.append(histogram)]
- return [list(filter(lambda word: char_histogram(word) == histogram,
- words)) for histogram in histograms]
+ return [[word for word in words if histogram == char_histogram(word)]
+ for histogram in histograms]

Веселин обнови решението на 16.03.2014 18:39 (преди около 10 години)

from itertools import groupby
from functools import cmp_to_key
+import re
def is_pangram(sentence):
alphabet = set('абвгдежзийклмнопрстуфхцчшщъьюя')
letters = {letter for letter in sentence.lower() if letter in alphabet}
return letters == alphabet
def char_histogram(text):
return {key: text.count(key) for key in text}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
keys = sorted(dictionary.keys(), key=lambda key: hash(type(key)))
return {group_type: {key: dictionary[key] for key in group_keys}
for group_type, group_keys in groupby(keys, type)}
+def letter_histogram(word):
+ return char_histogram(re.findall("[a-zа-я]", word.lower()))
+
+
def anagrams(words):
seen = []
- histograms = [histogram for histogram in map(char_histogram, words)
+ histograms = [histogram for histogram in map(letter_histogram, words)
if histogram not in seen and not seen.append(histogram)]
- return [[word for word in words if histogram == char_histogram(word)]
+ return [[word for word in words if histogram == letter_histogram(word)]
for histogram in histograms]

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

from itertools import groupby
from functools import cmp_to_key
import re
def is_pangram(sentence):
alphabet = set('абвгдежзийклмнопрстуфхцчшщъьюя')
letters = {letter for letter in sentence.lower() if letter in alphabet}
return letters == alphabet
def char_histogram(text):
return {key: text.count(key) for key in text}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
keys = sorted(dictionary.keys(), key=lambda key: hash(type(key)))
return {group_type: {key: dictionary[key] for key in group_keys}
for group_type, group_keys in groupby(keys, type)}
def letter_histogram(word):
- return char_histogram(re.findall("[a-zа-я]", word.lower()))
+ return char_histogram(re.findall('[a-zа-я]', word.lower()))
def anagrams(words):
seen = []
histograms = [histogram for histogram in map(letter_histogram, words)
if histogram not in seen and not seen.append(histogram)]
return [[word for word in words if histogram == letter_histogram(word)]
for histogram in histograms]

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

from itertools import groupby
from functools import cmp_to_key
import re
def is_pangram(sentence):
alphabet = set('абвгдежзийклмнопрстуфхцчшщъьюя')
letters = {letter for letter in sentence.lower() if letter in alphabet}
return letters == alphabet
def char_histogram(text):
return {key: text.count(key) for key in text}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
keys = sorted(dictionary.keys(), key=lambda key: hash(type(key)))
return {group_type: {key: dictionary[key] for key in group_keys}
for group_type, group_keys in groupby(keys, type)}
def letter_histogram(word):
- return char_histogram(re.findall('[a-zа-я]', word.lower()))
+ return char_histogram(re.findall('[^\W0-9_]', word.lower()))
def anagrams(words):
seen = []
histograms = [histogram for histogram in map(letter_histogram, words)
if histogram not in seen and not seen.append(histogram)]
return [[word for word in words if histogram == letter_histogram(word)]
for histogram in histograms]