Решение на Пет функции от Стоян Христов

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

Към профила на Стоян Христов

Резултати

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

Код

ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л',
'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
def is_pangram(sentence):
return ALPHABET <= set(sentence.lower())
def char_histogram(text):
symbols_count = {}
for symbol in text:
if symbol in symbols_count:
symbols_count[symbol] += 1
else:
symbols_count[symbol] = 1
return symbols_count
def sort_by(func, arguments):
is_not_sorted = True
while is_not_sorted:
is_not_sorted = False
for i in range(0, len(arguments) - 1):
if func(arguments[i], arguments[i + 1]) > 0:
arguments[i], arguments[i + 1] = arguments[i + 1], arguments[i]
is_not_sorted = True
return arguments
def group_by_type(dictionary):
grouped = {}
for key in dictionary:
grouped.setdefault(type(key), {})[key] = dictionary[key]
return grouped
def are_anagrams(word, another_word):
symbols_in_word = char_histogram(word.lower())
symbols_in_another_word = char_histogram(another_word.lower())
for key in symbols_in_word:
if key.isalpha() and key in symbols_in_another_word:
if symbols_in_word[key] != symbols_in_another_word[key]:
return False
elif key.isalpha() and key not in symbols_in_another_word:
return False
return True
def anagrams(words):
anagram_words = []
checked_words = set()
for word in words:
if word not in checked_words:
angarams_with_word = [element for element in words if \
are_anagrams(word, element)]
anagram_words.append(angarams_with_word)
for checked_word in angarams_with_word:
checked_words.add(checked_word)
return anagram_words

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

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

OK

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

Стоян обнови решението на 16.03.2014 22:02 (преди над 10 години)

+ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л',
+ 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
+ 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
+
+
+def is_pangram(sentence):
+ return ALPHABET <= set(sentence.lower())
+
+
+def char_histogram(text):
+ symbols_count = {}
+ for symbol in text:
+ if symbol in symbols_count:
+ symbols_count[symbol] += 1
+ else:
+ symbols_count[symbol] = 1
+
+ return symbols_count
+
+
+def sort_by(func, arguments):
+ IS_NOT_SORTED = True
+
+ while IS_NOT_SORTED:
+ IS_NOT_SORTED = False
+ for i in range(0, len(arguments) - 1):
+ if func(arguments[i], arguments[i + 1]) > 0:
+ arguments[i], arguments[i + 1] = arguments[i + 1], arguments[i]
+ IS_NOT_SORTED = True
+
+ return arguments
+
+
+def group_by_type(dictionary):
+ new_dictionary = {}
+ for key in dictionary:
+ new_dictionary.setdefault(type(key), {})[key] = dictionary[key]
+
+ return new_dictionary
+
+
+def are_anagrams(word, another_word):
+ symbols_in_word = char_histogram(word.lower())
+ symbols_in_another_word = char_histogram(another_word.lower())
+
+ for key in symbols_in_word:
+ if key.isalpha() and key in symbols_in_another_word:
+ if symbols_in_word[key] != symbols_in_another_word[key]:
+ return False
+ elif key.isalpha() and key not in symbols_in_another_word:
+ return False
+
+ return True
+
+
+def anagrams(words):
+ anagram_words = []
+ checked_words = set()
+
+ for word in words:
+ if word not in checked_words:
+ angarams_with_word = [el for el in words if are_anagrams(word, el)]
+ anagram_words.append(angarams_with_word)
+ for w in angarams_with_word:
+ checked_words.add(w)
+
+ return anagram_words
  • IS_NOT_SORTED не е константа за да е с главни букви
  • w не е адекватно име на променлива.
  • new_dictionary - всяка променлива до известна степен е "нова", а типа не е необходимо да го описваш в името на променливата. Помисли за по-добра алтернатива.

Стоян обнови решението на 17.03.2014 17:39 (преди над 10 години)

ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л',
'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
def is_pangram(sentence):
return ALPHABET <= set(sentence.lower())
def char_histogram(text):
symbols_count = {}
for symbol in text:
if symbol in symbols_count:
symbols_count[symbol] += 1
else:
symbols_count[symbol] = 1
return symbols_count
def sort_by(func, arguments):
- IS_NOT_SORTED = True
+ is_not_sorted = True
- while IS_NOT_SORTED:
- IS_NOT_SORTED = False
+ while is_not_sorted:
+ is_not_sorted = False
for i in range(0, len(arguments) - 1):
if func(arguments[i], arguments[i + 1]) > 0:
arguments[i], arguments[i + 1] = arguments[i + 1], arguments[i]
- IS_NOT_SORTED = True
+ is_not_sorted = True
return arguments
def group_by_type(dictionary):
- new_dictionary = {}
+ grouped = {}
for key in dictionary:
- new_dictionary.setdefault(type(key), {})[key] = dictionary[key]
+ grouped.setdefault(type(key), {})[key] = dictionary[key]
- return new_dictionary
+ return grouped
def are_anagrams(word, another_word):
symbols_in_word = char_histogram(word.lower())
symbols_in_another_word = char_histogram(another_word.lower())
for key in symbols_in_word:
if key.isalpha() and key in symbols_in_another_word:
if symbols_in_word[key] != symbols_in_another_word[key]:
return False
elif key.isalpha() and key not in symbols_in_another_word:
return False
return True
def anagrams(words):
anagram_words = []
checked_words = set()
for word in words:
if word not in checked_words:
angarams_with_word = [el for el in words if are_anagrams(word, el)]
anagram_words.append(angarams_with_word)
- for w in angarams_with_word:
- checked_words.add(w)
+ for checked_word in angarams_with_word:
+ checked_words.add(checked_word)
return anagram_words
+

Стоян обнови решението на 17.03.2014 18:39 (преди над 10 години)

ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л',
'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
def is_pangram(sentence):
return ALPHABET <= set(sentence.lower())
def char_histogram(text):
symbols_count = {}
for symbol in text:
if symbol in symbols_count:
symbols_count[symbol] += 1
else:
symbols_count[symbol] = 1
return symbols_count
def sort_by(func, arguments):
is_not_sorted = True
while is_not_sorted:
is_not_sorted = False
for i in range(0, len(arguments) - 1):
if func(arguments[i], arguments[i + 1]) > 0:
arguments[i], arguments[i + 1] = arguments[i + 1], arguments[i]
is_not_sorted = True
return arguments
def group_by_type(dictionary):
grouped = {}
for key in dictionary:
grouped.setdefault(type(key), {})[key] = dictionary[key]
return grouped
def are_anagrams(word, another_word):
symbols_in_word = char_histogram(word.lower())
symbols_in_another_word = char_histogram(another_word.lower())
for key in symbols_in_word:
if key.isalpha() and key in symbols_in_another_word:
if symbols_in_word[key] != symbols_in_another_word[key]:
return False
elif key.isalpha() and key not in symbols_in_another_word:
return False
return True
def anagrams(words):
anagram_words = []
checked_words = set()
for word in words:
if word not in checked_words:
- angarams_with_word = [el for el in words if are_anagrams(word, el)]
+ angarams_with_word = [element for element in words if \
+ are_anagrams(word, element)]
anagram_words.append(angarams_with_word)
for checked_word in angarams_with_word:
checked_words.add(checked_word)
return anagram_words