Иван обнови решението на 18.03.2014 21:29 (преди над 10 години)
+def is_cyrillic_letter(letter):
+ if letter == 'ы' or letter == 'э':
+ return False
+ return letter >= 'а' and letter <= 'я'
+
+
+def is_pangram(sentence):
+ letters = set()
+ sentence = sentence.lower()
+ for letter in sentence:
+ if is_cyrillic_letter(letter):
+ letters.add(letter)
+ return len(letters) == 30
+
+
+def char_histogram(text):
+ symbol_frequency = {}
+ for symbol in text:
+ if not (symbol in symbol_frequency):
+ symbol_frequency[symbol] = 0
+ symbol_frequency[symbol] += 1
+ return symbol_frequency
+
+
+def sort_by(func, arguments):
+ for x in range(len(arguments)):
+ for y in range(len(arguments) - 1, x, -1):
+ if func(arguments[y-1], arguments[y]) > 0:
+ arguments[y], arguments[y-1] = arguments[y-1], arguments[y]
+ return arguments
+
+
+def group_by_type(dictionary):
+ grouped = {}
+ for item in dictionary.items():
+ if not (type(item[0]) in grouped):
+ grouped[type(item[0])] = {item[0]: item[1]}
+ else:
+ grouped[type(item[0])].update({item[0]: item[1]})
+ return grouped
+
+
+def is_letter(letter):
+ return (letter >= 'a' and letter <= 'z') or is_cyrillic_letter(letter)
+
+
+def only_letters(word):
+ temp_word = ''
+ for symbol in word:
+ if is_letter(symbol):
+ temp_word += symbol
+ return temp_word
+
+
+def is_anagram(word1, word2):
+ word1 = only_letters(word1.lower())
+ word2 = only_letters(word2.lower())
+ return char_histogram(word1) == char_histogram(word2)
+
+
+def all_anagrams(temp_word, words):
+ return [word for word in words if is_anagram(temp_word, word)]
+
+
+def remove_anagrams(temp_word, words):
+ without_anagrams = []
+ for word in words:
+ if not is_anagram(word, temp_word):
+ without_anagrams.append(word)
+ return without_anagrams
+
+
+def anagrams(words):
+ sorted_anagrams = []
+ while len(words):
+ sorted_anagrams.append(all_anagrams(words[0], words))
+ temp_word = words[0]
+ words = remove_anagrams(temp_word, words)
+ return sorted_anagrams
-
char_histogram
иgroup_by_type
можеш да ги подобриш, ако си прегледаш хитринките - можеш да добавяш ключ-стойност в речник без
update
така:d[key] = value
- в
is_pangram
можеш направоfor letter in sentence.lower():
-
temp_word
не е гот, в крайна сметка по-добре еresult
, но в крайна сметка! - на 78-78 ред може направо
words = remove_anagrams(words[0], words)