Решение на Пет функции от Васил Тодоров

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

Към профила на Васил Тодоров

Резултати

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

Код

BULGARIAN_ALPHABET = {
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
'р', 'с', 'т', 'у', 'ф', 'ж', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
def is_pangram(sentence):
return BULGARIAN_ALPHABET - set(sentence.lower()) == set()
def char_histogram(text):
return {symbol: text.count(symbol) for symbol in text}
def sort_by(func, arguments):
for i in range(1, len(arguments)):
for j in range(i, 0, -1):
if func(arguments[j-1], arguments[j]) > 0:
arguments[j-1], arguments[j] = arguments[j], arguments[j-1]
else:
break
return arguments
def group_by_type(dictionary):
result = {}
for key in dictionary:
if type(key) in result.keys():
result[type(key)][key] = dictionary[key]
else:
result[type(key)] = {key: dictionary[key]}
return result
def check_contains(first_word, second_word):
for symbol in first_word:
if first_word.count(symbol) != second_word.count(symbol):
if symbol.isalpha():
return False
return True
def are_anagram(first_word_param, second_word_param):
first_word = first_word_param.lower()
second_word = second_word_param.lower()
if not check_contains(first_word, second_word):
return False
return check_contains(second_word, first_word)
def anagrams(words):
used_words = set()
result = []
for word in words:
if word not in used_words:
anagrams_of_word = [w for w in words if are_anagram(w, word)]
for used_word in anagrams_of_word:
used_words.add(used_word)
result.append(anagrams_of_word)
return result

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.008s

OK

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

Васил обнови решението на 14.03.2014 14:43 (преди над 10 години)

+BULGARIAN_ALPHABET = {
+ 'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
+ 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
+ 'р', 'с', 'т', 'у', 'ф', 'ж', 'ц', 'ч',
+ 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
+
+
+def is_pangram(sentence):
+ return BULGARIAN_ALPHABET - set(sentence.lower()) == set()
+
+
+def char_histogram(text):
+ return {x: text.count(x) for x in text}
+
+
+def sort_by(func, arguments):
+ for i in range(0, len(arguments) - 1):
+ for j in range(i + 1, len(arguments)):
+ if func(arguments[i], arguments[j]) > 0:
+ tmp = arguments[i]
+ arguments[i] = arguments[j]
+ arguments[j] = tmp
+ return arguments
+
+
+def group_by_type(dictionary):
+ result = {}
+ for key in dictionary:
+ if type(key) in result.keys():
+ result[type(key)][key] = dictionary[key]
+ else:
+ result[type(key)] = {key: dictionary[key]}
+ return result
+
+
+def are_anagram(first_word, second_word):
+ for symbol in first_word:
+ if first_word.count(symbol) != second_word.count(symbol):
+ return False
+ if len(first_word) != len(second_word):
+ return False
+ return True
+
+
+def anagrams(words):
+ used_words = set()
+ result = []
+ for word in words:
+ if word not in used_words:
+ anagrams_of_word = [w for w in words if are_anagram(w, word)]
+ for w in anagrams_of_word:
+ used_words.add(w)
+ result.append(anagrams_of_word)
+ return result
  • В Python можеш да размениш стойностите на две променливи просто като направиш a, b = b, a
  • Можеш да избегнеш името x
  • Замисли се дали нямаш случаи, в които сортирането ти не работи както очакваш
  • Погледни пак условието на anagrams и ще видиш, че искаме еднакви букви, а не символи

Васил обнови решението на 16.03.2014 17:03 (преди над 10 години)

BULGARIAN_ALPHABET = {
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
'р', 'с', 'т', 'у', 'ф', 'ж', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
def is_pangram(sentence):
return BULGARIAN_ALPHABET - set(sentence.lower()) == set()
def char_histogram(text):
- return {x: text.count(x) for x in text}
+ return {symbol: text.count(symbol) for symbol in text}
def sort_by(func, arguments):
- for i in range(0, len(arguments) - 1):
- for j in range(i + 1, len(arguments)):
- if func(arguments[i], arguments[j]) > 0:
- tmp = arguments[i]
- arguments[i] = arguments[j]
- arguments[j] = tmp
+ for i in range(1, len(arguments)):
+ for j in range(i, 0, -1):
+ if func(arguments[j-1], arguments[j]) > 0:
+ arguments[j-1], arguments[j] = arguments[j], arguments[j-1]
+ else:
+ break
return arguments
def group_by_type(dictionary):
result = {}
for key in dictionary:
if type(key) in result.keys():
result[type(key)][key] = dictionary[key]
else:
result[type(key)] = {key: dictionary[key]}
return result
-def are_anagram(first_word, second_word):
+def is_letter(symbol):
+ return ord(symbol) >= ord('a') and ord(symbol) <= ord('z')
+
+
+def check_contains(first_word, second_word):
for symbol in first_word:
if first_word.count(symbol) != second_word.count(symbol):
- return False
- if len(first_word) != len(second_word):
- return False
+ if is_letter(symbol):
+ return False
return True
+def are_anagram(first_word_param, second_word_param):
+ first_word = first_word_param.lower()
+ second_word = second_word_param.lower()
+ if not check_contains(first_word, second_word):
+ return False
+ return check_contains(second_word, first_word)
+
+
def anagrams(words):
used_words = set()
result = []
for word in words:
if word not in used_words:
anagrams_of_word = [w for w in words if are_anagram(w, word)]
- for w in anagrams_of_word:
- used_words.add(w)
+ for used_word in anagrams_of_word:
+ used_words.add(used_word)
result.append(anagrams_of_word)
return result

Васил обнови решението на 17.03.2014 22:08 (преди над 10 години)

BULGARIAN_ALPHABET = {
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
'р', 'с', 'т', 'у', 'ф', 'ж', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
def is_pangram(sentence):
return BULGARIAN_ALPHABET - set(sentence.lower()) == set()
def char_histogram(text):
return {symbol: text.count(symbol) for symbol in text}
def sort_by(func, arguments):
for i in range(1, len(arguments)):
for j in range(i, 0, -1):
if func(arguments[j-1], arguments[j]) > 0:
arguments[j-1], arguments[j] = arguments[j], arguments[j-1]
else:
break
return arguments
def group_by_type(dictionary):
result = {}
for key in dictionary:
if type(key) in result.keys():
result[type(key)][key] = dictionary[key]
else:
result[type(key)] = {key: dictionary[key]}
return result
-def is_letter(symbol):
- return ord(symbol) >= ord('a') and ord(symbol) <= ord('z')
-
-
def check_contains(first_word, second_word):
for symbol in first_word:
if first_word.count(symbol) != second_word.count(symbol):
- if is_letter(symbol):
+ if symbol.isalpha():
return False
return True
def are_anagram(first_word_param, second_word_param):
first_word = first_word_param.lower()
second_word = second_word_param.lower()
if not check_contains(first_word, second_word):
return False
return check_contains(second_word, first_word)
def anagrams(words):
used_words = set()
result = []
for word in words:
if word not in used_words:
anagrams_of_word = [w for w in words if are_anagram(w, word)]
for used_word in anagrams_of_word:
used_words.add(used_word)
result.append(anagrams_of_word)
return result