Решение на Пет функции от Мария Кръстева

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

Към профила на Мария Кръстева

Резултати

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

Код

from functools import cmp_to_key
ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
def is_pangram(sentence):
sentence = sentence.lower()
for letter in ALPHABET:
if sentence.count(letter) == 0:
return False
return True
def char_histogram(text):
result = {}
for letter in text:
result[letter] = text.count(letter)
return result
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
result = {}
for key in dictionary:
if not type(key) in result:
result[type(key)] = {key: dictionary[key]}
else:
result[type(key)][key] = dictionary[key]
return result
def remove_non_letters(sorted_word):
result = []
for letter in sorted_word:
if letter.isalpha():
result.append(letter)
return result
def anagrams_of_word(word, text):
result = []
for w in text:
if (remove_non_letters(sorted(word.lower())) ==
remove_non_letters(sorted(w.lower()))):
result.append(w)
return result
def not_in(word, text):
for words in text:
if word in words:
return False
return True
def anagrams(words):
result = []
for word in words:
if not_in(word, result):
result.append(anagrams_of_word(word, words))
return result

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

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

OK

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

Мария обнови решението на 17.03.2014 00:05 (преди над 10 години)

+from functools import cmp_to_key
+
+
+def is_pangram(sentence):
+ sentence = sentence.lower()
+ alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ for letter in alphabet:
+ if sentence.count(letter) == 0:
+ return False
+ return True
+
+
+def char_histogram(text):
+ result = {}
+ for letter in text:
+ result[letter] = text.count(letter)
+ return result
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ result = {}
+ for key in dictionary:
+ if not type(key) in result:
+ result[type(key)] = {key: dictionary[key]}
+ else:
+ result[type(key)][key] = dictionary[key]
+ return result
+
+
+def remove_white_spaces_and_dashes(sorted_word):
+ for i in range(0, sorted_word.count(" ")):
+ sorted_word.remove(" ")
+ for i in range(0, sorted_word.count("-")):
+ sorted_word.remove("-")
+ return sorted_word
+
+
+def anagrams_of_word(word, text):
+ result = []
+ for w in text:
+ if (remove_white_spaces_and_dashes(sorted(word.lower())) ==
+ remove_white_spaces_and_dashes(sorted(w.lower()))):
+ result.append(w)
+ return result
+
+
+def not_in(word, list_of_lists):
+ for list in list_of_lists:
+ if word in list:
+ return False
+ return True
+
+
+def anagrams(words):
+ result = []
+ for word in words:
+ if not_in(word, result):
+ result.append(anagrams_of_word(word, words))
+ return result
  • list_of_lists е много неподходящо име, казахме ви, че не искаме да се съдържа типа в името
  • азбуката може да се изнесе като константа извън функцията
  • забележи, че anagrams не се интересува от символите, а само от буквите

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

from functools import cmp_to_key
+ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
+
def is_pangram(sentence):
sentence = sentence.lower()
- alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
- for letter in alphabet:
+ for letter in ALPHABET:
if sentence.count(letter) == 0:
return False
return True
def char_histogram(text):
result = {}
for letter in text:
result[letter] = text.count(letter)
return result
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
result = {}
for key in dictionary:
if not type(key) in result:
result[type(key)] = {key: dictionary[key]}
else:
result[type(key)][key] = dictionary[key]
return result
-def remove_white_spaces_and_dashes(sorted_word):
- for i in range(0, sorted_word.count(" ")):
- sorted_word.remove(" ")
- for i in range(0, sorted_word.count("-")):
- sorted_word.remove("-")
- return sorted_word
+def remove_non_letters(sorted_word):
+ result = []
+ for letter in sorted_word:
+ if letter.isalpha():
+ result.append(letter)
+ return result
def anagrams_of_word(word, text):
result = []
for w in text:
- if (remove_white_spaces_and_dashes(sorted(word.lower())) ==
- remove_white_spaces_and_dashes(sorted(w.lower()))):
+ if (remove_non_letters(sorted(word.lower())) ==
+ remove_non_letters(sorted(w.lower()))):
result.append(w)
return result
-def not_in(word, list_of_lists):
- for list in list_of_lists:
- if word in list:
+def not_in(word, text):
+ for words in text:
+ if word in words:
return False
return True
def anagrams(words):
result = []
for word in words:
if not_in(word, result):
result.append(anagrams_of_word(word, words))
return result
+