Димитър обнови решението на 17.03.2014 00:30 (преди над 10 години)
+from collections import Counter, defaultdict
+from string import punctuation, whitespace
+
+CYRILLIC_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
+
+
+def is_pangram(sentence):
+ """checks if sentence is a is pangram"""
+
+ alphaset = set(CYRILLIC_ALPHABET)
+ return alphaset <= set(sentence.lower())
+
+
+def char_histogram(text):
+ """returns char histogram of text"""
+
+ histogram = Counter()
+ for letter in text:
+ histogram[letter] += 1
+ return dict(histogram)
+
+
+def sort_by(func, arguments):
+ """returns sorted arguments using func as comparer"""
+
+ sorted_list = list(arguments)
+ for i in range(1, len(sorted_list)):
+ save = sorted_list[i]
+ j = i
+ while j > 0 and func(sorted_list[j - 1], save) > 0:
+ sorted_list[j] = sorted_list[j - 1]
+ j -= 1
+ sorted_list[j] = save
+ return sorted_list
+
+
+def group_by_type(dictionary):
+ """returns new dictionary with type grouping of dictionary items"""
+
+ type_dictionary = defaultdict(dict)
+ for entry in dictionary:
+ entry_type = type(entry)
+ type_dictionary[entry_type][entry] = dictionary[entry]
+ return type_dictionary
+
+
+def anagrams(words):
+ """returns lists of anagrams of words"""
+
+ dictionary = defaultdict(list)
+ for word in words:
+ new_word = [letter for letter in word if letter not in punctuation
+ and letter not in whitespace]
+ sorted_word = tuple(sorted(new_word))
+ dictionary[sorted_word].append(word)
+ return [dictionary[entry] for entry in dictionary]
- Имената
sorted_list
,type_dictionary
,dictionary
иnew_word
са неподходящи. Вgroup_by_type
е приемливо да използвашdictionary
в последната функция са - Забележи, че в
anagrams
се интересуваме от буквите, а не от символите и освен това не еcase-sensitive