Решение на Пет функции от Георги Йорданов

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

Към профила на Георги Йорданов

Резултати

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

Код

CYRILLIC_ALPHABET = (
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
)
SYMBOLS_TO_STRIP = (' ', '.', ',', '-', '?', '!', '\'', '_', ':', ';', '`')
def is_pangram(sentence):
lowered_sentence = sentence.lower()
for char in CYRILLIC_ALPHABET:
if lowered_sentence.find(char) == -1:
return False
return True
def char_histogram(text):
histogram = {}
for char in text:
if histogram.get(char) is None:
histogram[char] = 1
else:
histogram[char] += 1
return histogram
def sort_by(func, arguments):
arguments_copy = arguments
for i in range(1, len(arguments_copy)):
current = arguments_copy[i]
j = i - 1
while j >= 0 and func(arguments_copy[j], current) > 0:
arguments_copy[j+1] = arguments_copy[j]
j = j - 1
arguments_copy[j+1] = current
return arguments_copy
def group_by_type(dictionary):
grouped_elements = {}
for key in dictionary.keys():
keyType = type(key)
if grouped_elements.get(keyType) is None:
grouped_elements[keyType] = {key: dictionary[key]}
else:
grouped_elements[keyType].update({key: dictionary[key]})
return grouped_elements
def is_anagram(lhs_word, rhs_word):
return char_histogram(lhs_word) == char_histogram(rhs_word)
def strip_symbols(string):
stripped_string = string
for symbol in SYMBOLS_TO_STRIP:
if symbol in stripped_string:
stripped_string = stripped_string.replace(symbol, '')
return stripped_string
def find_anagrams(pattern, words, used_words):
stripped_pattern = strip_symbols(pattern).lower()
anagram_list = []
for word in words:
anagram = is_anagram(stripped_pattern, strip_symbols(word).lower())
if not used_words[word] and anagram:
anagram_list.append(word)
used_words[word] = True
return anagram_list
def anagrams(words):
anagram_lists = []
used_words = {word: False for word in words}
for word in words:
if not used_words[word]:
used_words[word] = True
anagram_list = [word]
anagram_list.extend(find_anagrams(word, words, used_words))
anagram_lists.append(anagram_list)
return anagram_lists

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.009s

OK

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

Георги обнови решението на 17.03.2014 11:21 (преди около 10 години)

+CYRILLIC_ALPHABET = (
+ 'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
+ 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
+ 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
+)
+
+SYMBOLS_TO_STRIP = (' ', '.', ',', '-', '?', '!', '\'', '_')
+
+
+def is_pangram(sentence):
+ lowered_sentence = sentence.lower()
+ for char in CYRILLIC_ALPHABET:
+ if lowered_sentence.find(char) == -1:
+ return False
+
+ return True
+
+
+def char_histogram(text):
+ histogram = {}
+ for char in text:
+ if histogram.get(char) is None:
+ histogram[char] = 1
+ else:
+ histogram[char] = histogram[char] + 1
+
+ return histogram
+
+
+def sort_by(func, arguments):
+ arguments_copy = arguments
+ for i in range(1, len(arguments_copy)):
+ current = arguments_copy[i]
+ j = i - 1
+ while j >= 0 and func(arguments_copy[j], current) > 0:
+ arguments_copy[j+1] = arguments_copy[j]
+ j = j - 1
+ arguments_copy[j+1] = current
+
+ return arguments_copy
+
+
+def group_by_type(dictionary):
+ grouped_elements = {}
+ for key in dictionary.keys():
+ if grouped_elements.get(type(key)) is None:
+ grouped_elements[type(key)] = {key: dictionary[key]}
+ else:
+ grouped_elements[type(key)].update({key: dictionary[key]})
+
+ return grouped_elements
+
+
+def is_anagram(lhs_word, rhs_word):
+ return char_histogram(lhs_word) == char_histogram(rhs_word)
+
+
+def strip_symbols(string):
+ stripped_string = string
+ for symbol in SYMBOLS_TO_STRIP:
+ if symbol in stripped_string:
+ stripped_string = stripped_string.replace(symbol, '')
+
+ return stripped_string
+
+
+def find_anagrams(pattern, words, used_words):
+ stripped_pattern = strip_symbols(pattern).lower()
+ anagram_list = []
+ for word in words:
+ if(not used_words[word] and
+ is_anagram(stripped_pattern,
+ strip_symbols(word).lower())):
+ anagram_list.append(word)
+ used_words[word] = True
+
+ return anagram_list
+
+
+def anagrams(words):
+ anagram_lists = []
+ used_words = {word: False for word in words}
+ for word in words:
+ if not used_words[word]:
+ used_words[word] = True
+ anagram_list = [word]
+ anagram_list.extend(find_anagrams(word, words, used_words))
+ anagram_lists.append(anagram_list)
+
+ return anagram_lists

type(key) може да се изнесе в отделна променлива, защото се преизползва няколко пъти.

Следния блок може да се напише и по-добре:

if(not used_words[word] and
        is_anagram(stripped_pattern,
                   strip_symbols(word).lower())):
  1. Резултата от извикването на функцията може да се изнесе в променлива, така ще си спестиш редове
  2. Не е необходимо да има скоби около израза
  3. Не е валиден по PEP8.

histogram[char] = histogram[char] + 1 може да стане histogram[char] += 1

Георги обнови решението на 19.03.2014 11:00 (преди около 10 години)

CYRILLIC_ALPHABET = (
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
)
-SYMBOLS_TO_STRIP = (' ', '.', ',', '-', '?', '!', '\'', '_')
+SYMBOLS_TO_STRIP = (' ', '.', ',', '-', '?', '!', '\'', '_', ':', ';', '`')
def is_pangram(sentence):
lowered_sentence = sentence.lower()
for char in CYRILLIC_ALPHABET:
if lowered_sentence.find(char) == -1:
return False
return True
def char_histogram(text):
histogram = {}
for char in text:
if histogram.get(char) is None:
histogram[char] = 1
else:
- histogram[char] = histogram[char] + 1
+ histogram[char] += 1
return histogram
def sort_by(func, arguments):
arguments_copy = arguments
for i in range(1, len(arguments_copy)):
current = arguments_copy[i]
j = i - 1
while j >= 0 and func(arguments_copy[j], current) > 0:
arguments_copy[j+1] = arguments_copy[j]
j = j - 1
arguments_copy[j+1] = current
return arguments_copy
def group_by_type(dictionary):
grouped_elements = {}
for key in dictionary.keys():
- if grouped_elements.get(type(key)) is None:
- grouped_elements[type(key)] = {key: dictionary[key]}
+ keyType = type(key)
+ if grouped_elements.get(keyType) is None:
+ grouped_elements[keyType] = {key: dictionary[key]}
else:
- grouped_elements[type(key)].update({key: dictionary[key]})
+ grouped_elements[keyType].update({key: dictionary[key]})
return grouped_elements
def is_anagram(lhs_word, rhs_word):
return char_histogram(lhs_word) == char_histogram(rhs_word)
def strip_symbols(string):
stripped_string = string
for symbol in SYMBOLS_TO_STRIP:
if symbol in stripped_string:
stripped_string = stripped_string.replace(symbol, '')
return stripped_string
def find_anagrams(pattern, words, used_words):
stripped_pattern = strip_symbols(pattern).lower()
anagram_list = []
for word in words:
- if(not used_words[word] and
- is_anagram(stripped_pattern,
- strip_symbols(word).lower())):
+ anagram = is_anagram(stripped_pattern, strip_symbols(word).lower())
+ if not used_words[word] and anagram:
anagram_list.append(word)
used_words[word] = True
return anagram_list
def anagrams(words):
anagram_lists = []
used_words = {word: False for word in words}
for word in words:
if not used_words[word]:
used_words[word] = True
anagram_list = [word]
anagram_list.extend(find_anagrams(word, words, used_words))
anagram_lists.append(anagram_list)
return anagram_lists