Решение на Пет функции от Божидар Михайлов

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

Към профила на Божидар Михайлов

Резултати

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

Код

from collections import defaultdict
from functools import cmp_to_key
from re import sub
def is_pangram(sentence):
alphabet = set("абвгдежзийклмнопрстуфхцчшщъьюя")
return alphabet.issubset(sentence.lower())
def char_histogram(text):
return {letter: text.count(letter) for letter in set(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):
def get_letters(word): return sub('[\W\d_]+', '', word).lower()
grouped = defaultdict(list)
for word in words:
key = frozenset(char_histogram(get_letters(word)).items())
grouped[key].append(word)
return list(grouped.values())

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.010s

OK

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

Божидар обнови решението на 15.03.2014 17:03 (преди над 10 години)

+from collections import defaultdict
+from re import sub
+
+def is_pangram(sentence):
+ alphabet = set("абвгдежзийклмнопрстуфхцчшщъьюя")
+ return alphabet.issubset(sentence.lower())
+
+def char_histogram(text):
+ histogram = defaultdict(int)
+ for letter in text:
+ histogram[letter] += 1
+ return histogram
+
+def sort_by(func, arguments):
+ class SortWrapper:
+ def __init__(self, data):
+ self.data = data
+
+ def __lt__(self, other):
+ return func(self.data, other.data) <= 0
+
+ wrapped_arguments = [SortWrapper(arg) for arg in arguments]
+ arguments[:] = [wrapper.data for wrapper in sorted(wrapped_arguments)]
+ return arguments
+
+def group_by_type(dictionary):
+ grouped = defaultdict(dict)
+ for key, value in dictionary.items():
+ grouped[type(key)][key] = value
+ return grouped
+
+def anagrams(words):
+ def get_letters(word): return sub('\W+', '', word).lower()
+ grouped = defaultdict(list)
+ for word in words:
+ key = frozenset(char_histogram(get_letters(word)).items())
+ grouped[key].append(word)
+ return list(grouped.values())

Божидар обнови решението на 16.03.2014 00:44 (преди над 10 години)

from collections import defaultdict
+from functools import cmp_to_key
from re import sub
def is_pangram(sentence):
alphabet = set("абвгдежзийклмнопрстуфхцчшщъьюя")
return alphabet.issubset(sentence.lower())
def char_histogram(text):
histogram = defaultdict(int)
for letter in text:
histogram[letter] += 1
return histogram
def sort_by(func, arguments):
- class SortWrapper:
- def __init__(self, data):
- self.data = data
-
- def __lt__(self, other):
- return func(self.data, other.data) <= 0
-
- wrapped_arguments = [SortWrapper(arg) for arg in arguments]
- arguments[:] = [wrapper.data for wrapper in sorted(wrapped_arguments)]
+ arguments.sort(key=cmp_to_key(func))
return arguments
def group_by_type(dictionary):
grouped = defaultdict(dict)
for key, value in dictionary.items():
grouped[type(key)][key] = value
return grouped
def anagrams(words):
def get_letters(word): return sub('\W+', '', word).lower()
grouped = defaultdict(list)
for word in words:
key = frozenset(char_histogram(get_letters(word)).items())
grouped[key].append(word)
return list(grouped.values())

Божидар обнови решението на 16.03.2014 00:51 (преди над 10 години)

from collections import defaultdict
from functools import cmp_to_key
from re import sub
def is_pangram(sentence):
alphabet = set("абвгдежзийклмнопрстуфхцчшщъьюя")
return alphabet.issubset(sentence.lower())
def char_histogram(text):
histogram = defaultdict(int)
for letter in text:
histogram[letter] += 1
return histogram
def sort_by(func, arguments):
- arguments.sort(key=cmp_to_key(func))
- return 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):
def get_letters(word): return sub('\W+', '', word).lower()
grouped = defaultdict(list)
for word in words:
key = frozenset(char_histogram(get_letters(word)).items())
grouped[key].append(word)
- return list(grouped.values())
+ return list(grouped.values())

Божидар обнови решението на 17.03.2014 10:27 (преди над 10 години)

from collections import defaultdict
from functools import cmp_to_key
from re import sub
def is_pangram(sentence):
alphabet = set("абвгдежзийклмнопрстуфхцчшщъьюя")
return alphabet.issubset(sentence.lower())
def char_histogram(text):
histogram = defaultdict(int)
for letter in text:
histogram[letter] += 1
return histogram
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):
- def get_letters(word): return sub('\W+', '', word).lower()
+ def get_letters(word): return sub('[\W\d_]+', '', word).lower()
grouped = defaultdict(list)
for word in words:
key = frozenset(char_histogram(get_letters(word)).items())
grouped[key].append(word)
return list(grouped.values())

Божидар обнови решението на 17.03.2014 18:15 (преди над 10 години)

from collections import defaultdict
from functools import cmp_to_key
from re import sub
def is_pangram(sentence):
alphabet = set("абвгдежзийклмнопрстуфхцчшщъьюя")
return alphabet.issubset(sentence.lower())
def char_histogram(text):
- histogram = defaultdict(int)
- for letter in text:
- histogram[letter] += 1
- return histogram
+ return {letter: text.count(letter) for letter in set(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):
def get_letters(word): return sub('[\W\d_]+', '', word).lower()
grouped = defaultdict(list)
for word in words:
key = frozenset(char_histogram(get_letters(word)).items())
grouped[key].append(word)
- return list(grouped.values())
+ return list(grouped.values())