Решение на Пет функции от Елена Желева

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

Към профила на Елена Желева

Резултати

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

Код

from functools import cmp_to_key
from collections import defaultdict
def is_pangram(sentence):
all_letters = 'а б в г д е ж з и й к л м н о п р с т у ф х ц ч ш щ ъ ь ю я'
for i in all_letters:
if not i in sentence.lower():
return False
return True
def count_letter(letter, word):
result = 0
for i in word:
if i == letter:
result += 1
return result
def char_histogram(sentence):
histogram = {}
for i in sentence:
histogram[i] = count_letter(i, sentence)
return histogram
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
grouped_items = defaultdict(dict)
keys = dictionary.keys()
for key in keys:
if not type(key) in grouped_items:
grouped_items[type(key)] = {key: dictionary[key]}
else:
grouped_items[type(key)][key] = dictionary[key]
return dict(grouped_items)
def not_repeated(word, grouped_words):
for words in grouped_words:
if word in words:
return False
return True
def anagrams_of_word(word, words):
result = []
for i in words:
if (only_letters(sorted(word.lower())) ==
only_letters(sorted(i.lower()))):
result.append(i)
return result
def only_letters(word):
return [x for x in word if x.isalpha()]
def anagrams(words):
result = []
for word in words:
if not_repeated(word, result):
result.append(anagrams_of_word(word, words))
return result

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.013s

OK

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

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

+from functools import cmp_to_key
+
+
+def is_pangram(sentence):
+ all_letters = 'а б в г д е ж з и й к л м н о п р с т у ф х ц ш щ ъ ь ю я'
+ all_letters = all_letters.split(" ")
+ for i in all_letters:
+ if not i in sentence.lower():
+ return False
+ return True
+
+
+def count_letter(letter, word):
+ result = 0
+ for i in word:
+ if i == letter:
+ result += 1
+ return result
+
+
+def char_histogram(sentence):
+ histogram = {}
+ for i in range(len(sentence)):
+ histogram[sentence[i]] = count_letter(sentence[i], sentence)
+ return histogram
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ grouped_by_type = {}
+ keys = dictionary.keys()
+ for key in keys:
+ if not type(key) in grouped_by_type:
+ grouped_by_type[type(key)] = {key: dictionary[key]}
+ else:
+ grouped_by_type[type(key)][key] = dictionary[key]
+ return grouped_by_type
+
+
+def not_repeated(word, sentence):
+ for words in sentence:
+ if word in words:
+ return False
+ return True
+
+
+def anagrams_of_word(word, text):
+ result = []
+ for i in text:
+ if (only_letters(sorted(word.lower())) ==
+ only_letters(sorted(i.lower()))):
+ result.append(i)
+ return result
+
+
+def only_letters(word):
+ result = []
+ for i in word:
+ if i.isalpha():
+ result.append(i)
+ return result
+
+
+def anagrams(words):
+ result = []
+ for word in words:
+ if not_repeated(word, result):
+ result.append(anagrams_of_word(word, words))
+ return result
  • в is_pangram с for можеш да итерираш и стринг, не само списък, така че няма смисъл да минаваш от едното към другото
  • така и в char_histogram можеш директно да итерираш стринга for character in sentence:, а не да го правиш чрез индекси
  • можеш да си подобриш group_by_type, ако прегледаш хитринките
  • в not_repeated, sentence бих го сменил на нещо като grouped_words(в крайна сметка не подаваш изречение(стринг), а списък от списъци от думи) и в anagrams_of_word, text -> words
  • only_letters лесно можеш да го напишеш с някоя функция от по-висок ред(map, filter, reduce) или list comprehension и ще е по-идиоматично решение

Елена обнови решението на 19.03.2014 12:02 (преди около 10 години)

from functools import cmp_to_key
def is_pangram(sentence):
all_letters = 'а б в г д е ж з и й к л м н о п р с т у ф х ц ш щ ъ ь ю я'
- all_letters = all_letters.split(" ")
for i in all_letters:
if not i in sentence.lower():
return False
return True
def count_letter(letter, word):
result = 0
for i in word:
if i == letter:
result += 1
return result
def char_histogram(sentence):
histogram = {}
- for i in range(len(sentence)):
- histogram[sentence[i]] = count_letter(sentence[i], sentence)
+ for i in sentence:
+ histogram[i] = count_letter(i, sentence)
return histogram
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
grouped_by_type = {}
keys = dictionary.keys()
for key in keys:
if not type(key) in grouped_by_type:
grouped_by_type[type(key)] = {key: dictionary[key]}
else:
grouped_by_type[type(key)][key] = dictionary[key]
return grouped_by_type
-def not_repeated(word, sentence):
- for words in sentence:
+def not_repeated(word, grouped_words):
+ for words in grouped_words:
if word in words:
return False
return True
-def anagrams_of_word(word, text):
+def anagrams_of_word(word, words):
result = []
- for i in text:
+ for i in words:
if (only_letters(sorted(word.lower())) ==
only_letters(sorted(i.lower()))):
result.append(i)
return result
def only_letters(word):
- result = []
- for i in word:
- if i.isalpha():
- result.append(i)
- return result
+ return [x for x in word if x.isalpha()]
def anagrams(words):
result = []
for word in words:
if not_repeated(word, result):
result.append(anagrams_of_word(word, words))
return result

Елена обнови решението на 19.03.2014 12:26 (преди около 10 години)

from functools import cmp_to_key
def is_pangram(sentence):
- all_letters = 'а б в г д е ж з и й к л м н о п р с т у ф х ц ш щ ъ ь ю я'
+ all_letters = 'а б в г д е ж з и й к л м н о п р с т у ф х ц ч ш щ ъ ь ю я'
for i in all_letters:
if not i in sentence.lower():
return False
return True
def count_letter(letter, word):
result = 0
for i in word:
if i == letter:
result += 1
return result
def char_histogram(sentence):
histogram = {}
for i in sentence:
histogram[i] = count_letter(i, sentence)
return histogram
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
grouped_by_type = {}
keys = dictionary.keys()
for key in keys:
if not type(key) in grouped_by_type:
grouped_by_type[type(key)] = {key: dictionary[key]}
else:
grouped_by_type[type(key)][key] = dictionary[key]
return grouped_by_type
def not_repeated(word, grouped_words):
for words in grouped_words:
if word in words:
return False
return True
def anagrams_of_word(word, words):
result = []
for i in words:
if (only_letters(sorted(word.lower())) ==
only_letters(sorted(i.lower()))):
result.append(i)
return result
def only_letters(word):
return [x for x in word if x.isalpha()]
def anagrams(words):
result = []
for word in words:
if not_repeated(word, result):
result.append(anagrams_of_word(word, words))
return result

Елена обнови решението на 19.03.2014 16:55 (преди около 10 години)

from functools import cmp_to_key
+from collections import defaultdict
def is_pangram(sentence):
all_letters = 'а б в г д е ж з и й к л м н о п р с т у ф х ц ч ш щ ъ ь ю я'
for i in all_letters:
if not i in sentence.lower():
return False
return True
def count_letter(letter, word):
result = 0
for i in word:
if i == letter:
result += 1
return result
def char_histogram(sentence):
histogram = {}
for i in sentence:
histogram[i] = count_letter(i, sentence)
return histogram
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
- grouped_by_type = {}
+ grouped_items = defaultdict(dict)
keys = dictionary.keys()
for key in keys:
- if not type(key) in grouped_by_type:
- grouped_by_type[type(key)] = {key: dictionary[key]}
+ if not type(key) in grouped_items:
+ grouped_items[type(key)] = {key: dictionary[key]}
else:
- grouped_by_type[type(key)][key] = dictionary[key]
- return grouped_by_type
+ grouped_items[type(key)][key] = dictionary[key]
+ return dict(grouped_items)
def not_repeated(word, grouped_words):
for words in grouped_words:
if word in words:
return False
return True
def anagrams_of_word(word, words):
result = []
for i in words:
if (only_letters(sorted(word.lower())) ==
only_letters(sorted(i.lower()))):
result.append(i)
return result
def only_letters(word):
return [x for x in word if x.isalpha()]
def anagrams(words):
result = []
for word in words:
if not_repeated(word, result):
result.append(anagrams_of_word(word, words))
return result