Георги обнови решението на 18.03.2014 23:49 (преди над 10 години)
+from collections import defaultdict
+
+def is_pangram(sentence):
+ chars_present = {chr(i): False for i in range(1072, 1104) if i!= 1101 and i!=1099}
+ for char in sentence.lower():
+ chars_present[char] = True
+ for char, is_char_present in chars_present.items():
+ if not is_char_present:
+ return False
+ return True
+
+def char_histogram(string):
+ histogram = {}
+ for char in string:
+ if char in histogram:
+ histogram[char] +=1
+ else:
+ histogram[char] = 1
+ return histogram
+
+def sort_by(func, arguments):
+ length = len(arguments) - 1
+ sorted = False
+ while not sorted:
+ sorted = True
+ for i in range(length):
+ if func(arguments[i],arguments[i+1]) > 0:
+ sorted = False
+ arguments[i], arguments[i+1] = arguments[i+1], arguments[i]
+ return arguments
+
+def group_by_type(dictionary):
+ type_dict = defaultdict(dict)
+ for key, value in dictionary.items():
+ type_dict[type(key)][key] = value
+ return dict(type_dict)
+
+def anagrams(words):
+ anagrams_groups = []
+ for word in words:
+ has_anagram = False
+ for anagram_group in anagrams_groups:
+ if (char_histogram(anagram_group[0]) == char_histogram(word)):
+ anagram_group.append(word)
+ has_anagram = True
+ if not has_anagram:
+ anagrams_groups.append([word])
+ return anagrams_groups
Това може да се упрости, вече имаш импортнат defaultdict
, използвай го:
if char in histogram:
histogram[char] +=1
else:
histogram[char] = 1
И на двете места в трябва да има интервали: if i != 1101 and i != 1099
. Кода не спазва напълно PEP8.