Решение на Пет функции от Ирина Иванова

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

Към профила на Ирина Иванова

Резултати

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

Код

from functools import cmp_to_key, reduce
from collections import defaultdict
ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def is_pangram(phrase):
return len([x for x in ALPHABET if x in phrase.lower()]) == len(ALPHABET)
def char_histogram(text):
return reduce(lambda d, x: dict(d, **{x: d.get(x, 0) + 1}), text, {})
def sort_by(func, words):
return sorted(words, key=cmp_to_key(func))
def group_by_type(wordbook):
group_wordbook = defaultdict(dict)
for x, v in wordbook.items():
group_wordbook[type(x)].update({x: v})
return group_wordbook
def letters(word):
return {x.lower() for x in word if x.isalpha()}
def anagrams(words):
groups = [[y for y in words if letters(y) == letters(x)] for x in words]
return list(map(list, set(map(tuple, groups))))

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

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

OK

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

Ирина обнови решението на 17.03.2014 12:35 (преди над 10 години)

+ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+
+def is_pangram(phrase):
+ return len([x for x in ALPHABET if x in phrase.lower()]) == len(ALPHABET)
+
+
+from functools import reduce
+
+def char_histogram(text):
+ return reduce(lambda d, x: dict(d, **{x: d.get(x, 0) + 1}), text, {})
+
+
+from functools import cmp_to_key
+
+def sort_by(func, list_will_be_sorted):
+ return sorted(list_will_be_sorted, key=cmp_to_key(func))
+
+
+from collections import defaultdict
+
+def group_by_type(dict_will_be_grouped):
+ group_dict = defaultdict(dict)
+ for x, v in dict_will_be_grouped.items():
+ group_dict[type(x)].update({x: v})
+ return group_dict
+
+def anagrams(word):
+ aw = [[y for y in word if set(y.lower()) == set(x.lower())] for x in word]
+ return list(map(list, set(map(tuple, aw))))

Ирина обнови решението на 17.03.2014 12:37 (преди над 10 години)

ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def is_pangram(phrase):
return len([x for x in ALPHABET if x in phrase.lower()]) == len(ALPHABET)
-
from functools import reduce
def char_histogram(text):
return reduce(lambda d, x: dict(d, **{x: d.get(x, 0) + 1}), text, {})
-
from functools import cmp_to_key
def sort_by(func, list_will_be_sorted):
return sorted(list_will_be_sorted, key=cmp_to_key(func))
-
from collections import defaultdict
def group_by_type(dict_will_be_grouped):
group_dict = defaultdict(dict)
for x, v in dict_will_be_grouped.items():
group_dict[type(x)].update({x: v})
return group_dict
def anagrams(word):
aw = [[y for y in word if set(y.lower()) == set(x.lower())] for x in word]
return list(map(list, set(map(tuple, aw))))

Ирина обнови решението на 17.03.2014 13:10 (преди над 10 години)

+from functools import reduce
+from functools import cmp_to_key
+from collections import defaultdict
+
ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def is_pangram(phrase):
return len([x for x in ALPHABET if x in phrase.lower()]) == len(ALPHABET)
-from functools import reduce
-
def char_histogram(text):
return reduce(lambda d, x: dict(d, **{x: d.get(x, 0) + 1}), text, {})
-from functools import cmp_to_key
-
def sort_by(func, list_will_be_sorted):
return sorted(list_will_be_sorted, key=cmp_to_key(func))
-from collections import defaultdict
-
def group_by_type(dict_will_be_grouped):
group_dict = defaultdict(dict)
for x, v in dict_will_be_grouped.items():
group_dict[type(x)].update({x: v})
+ print(group_dict)
return group_dict
def anagrams(word):
aw = [[y for y in word if set(y.lower()) == set(x.lower())] for x in word]
return list(map(list, set(map(tuple, aw))))

Ирина обнови решението на 17.03.2014 15:48 (преди над 10 години)

from functools import reduce
from functools import cmp_to_key
from collections import defaultdict
ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def is_pangram(phrase):
return len([x for x in ALPHABET if x in phrase.lower()]) == len(ALPHABET)
def char_histogram(text):
return reduce(lambda d, x: dict(d, **{x: d.get(x, 0) + 1}), text, {})
def sort_by(func, list_will_be_sorted):
return sorted(list_will_be_sorted, key=cmp_to_key(func))
def group_by_type(dict_will_be_grouped):
group_dict = defaultdict(dict)
for x, v in dict_will_be_grouped.items():
group_dict[type(x)].update({x: v})
- print(group_dict)
return group_dict
def anagrams(word):
aw = [[y for y in word if set(y.lower()) == set(x.lower())] for x in word]
return list(map(list, set(map(tuple, aw))))
  • Имената на променливите не са напълно ок: aw -> anagram_words, list_will_be_sorted -> list_to_sort типа на обекта не трябва да се съдържа в името на променливата.
  • Импорта от functools може да стане така: from functools import reduce, cmp_to_key

Ирина обнови решението на 19.03.2014 15:21 (преди над 10 години)

-from functools import reduce
-from functools import cmp_to_key
+from functools import cmp_to_key, reduce
from collections import defaultdict
ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def is_pangram(phrase):
return len([x for x in ALPHABET if x in phrase.lower()]) == len(ALPHABET)
def char_histogram(text):
return reduce(lambda d, x: dict(d, **{x: d.get(x, 0) + 1}), text, {})
-def sort_by(func, list_will_be_sorted):
- return sorted(list_will_be_sorted, key=cmp_to_key(func))
+def sort_by(func, words):
+ return sorted(words, key=cmp_to_key(func))
-def group_by_type(dict_will_be_grouped):
- group_dict = defaultdict(dict)
- for x, v in dict_will_be_grouped.items():
- group_dict[type(x)].update({x: v})
- return group_dict
+def group_by_type(wordbook):
+ group_wordbook = defaultdict(dict)
+ for x, v in wordbook.items():
+ group_wordbook[type(x)].update({x: v})
+ return group_wordbook
-def anagrams(word):
- aw = [[y for y in word if set(y.lower()) == set(x.lower())] for x in word]
+def letters(word):
- return list(map(list, set(map(tuple, aw))))
+ return {x.lower() for x in word if x.isalpha()}
+
+def anagrams(words):
+ groups = [[y for y in words if letters(y) == letters(x)] for x in words]
+ return list(map(list, set(map(tuple, groups))))