Никола обнови решението на 14.03.2014 00:27 (преди почти 11 години)
+from itertools import groupby
+from collections import defaultdict
+from functools import cmp_to_key
+
+ALPHABET_SIZE = 30
+def is_pangram(sentence):
+ chars_in_text = [char_code for char_code in range(ord('а'), ord('я') + 1)
+ if chr(char_code) in sentence.lower()]
+ return len(chars_in_text) == ALPHABET_SIZE
+
+
+def char_histogram(text):
+ return { letter: text.count(letter) for letter in text }
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ # Since itertools.groupby requires a pre-sorted list:
+ sorted_keys = sorted(dictionary.keys(), key=lambda x: str(type(x)))
+ entries_by_type = [(class_type, list(keys)) for class_type, keys in
+ groupby(sorted_keys, type)]
+
+ result = defaultdict(dict)
+ for class_type, key_list in entries_by_type:
+ for key in key_list:
+ result[class_type][key] = dictionary[key]
+
+ return dict(result)
+
+
+def is_anagram_to(word, possible_anagram):
+ return char_histogram(word) == char_histogram(possible_anagram)
+
+
+def anagrams(words):
+ grouped_by_anagrams = dict()
+ for word in words:
+ grouped_by_anagrams[word] = [other for other in words
+ if is_anagram_to(word, other)]
+ # Join the strings and convert to set to remove duplicates
+ # (Can't convert to set right away since lists are mutable)
+ result = {",".join(value) for value in grouped_by_anagrams.values()}
+ result = [value.split(",") for value in result]
+ return result
Анаграма е дума или фраза образувана от буквите на друга дума или фраза, чрез пермутация.
- Твоето решение не прави разлика между буква и символ.
- Прави разлика и между малки и големи букви