Решение на Пет функции от Димитър Трендафилов

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

Към профила на Димитър Трендафилов

Резултати

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

Код

import functools
BG_ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
def is_pangram(sentence):
contained_letters = {letter for letter in sentence.lower()
if letter in BG_ALPHABET}
return contained_letters == BG_ALPHABET
def char_histogram(text):
return {chars: text.count(chars) for chars in text}
def sort_by(func, arguments):
return sorted(arguments, key=functools.cmp_to_key(func))
def group_by_type(dictionary):
grouped_by_type = {}
for key in dictionary.keys():
grouped_by_type.setdefault(
type(key), {key: dictionary[key]})[key] = dictionary[key]
return grouped_by_type
def anagrams(words):
histograms = {}
for word in words:
temp = ''.join(c.lower() for c in word if c.isalnum())
histograms[word] = char_histogram(temp)
lists_of_anagrams = []
checked_anagrams = []
for word in words:
if histograms[word] not in checked_anagrams:
checked_anagrams.append(histograms[word])
lists_of_anagrams.append([word])
else:
index_to_append = checked_anagrams.index(histograms[word])
lists_of_anagrams[index_to_append].append(word)
return lists_of_anagrams

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

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

OK

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

Димитър обнови решението на 18.03.2014 22:29 (преди над 10 години)

+import functools
+BG_ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
+ 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
+ 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
+
+
+def is_pangram(sentence):
+ sentence = sentence.lower()
+ letters_in = {letter for letter in sentence if letter in BG_ALPHABET}
+ return letters_in == BG_ALPHABET
+
+
+def char_histogram(text):
+ return {chars: text.count(chars) for chars in text}
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=functools.cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ grouped_by_type = {}
+
+ for key in dictionary.keys():
+
+ if type(key) in grouped_by_type.keys():
+ grouped_by_type[type(key)][key] = dictionary[key]
+
+ else:
+ grouped_by_type[type(key)] = {key: dictionary[key]}
+
+ return grouped_by_type
+
+
+def anagrams(words):
+ dict_of_histograms = {}
+
+ for word in words:
+ word = word.lower()
+ dict_of_histograms[word] = char_histogram(word)
+
+ list_of_anagrams = []
+ checked_anagrams = []
+
+ for word in words:
+
+ if dict_of_histograms[word] not in checked_anagrams:
+ checked_anagrams.append(dict_of_histograms[word])
+ list_of_anagrams.append([word])
+
+ else:
+ index_to_append = checked_anagrams.index(dict_of_histograms[word])
+ list_of_anagrams[index_to_append].append(word)
+
+ return list_of_anagrams
  • в is_pangram по-скоро contained_letters отколкото letters_in
  • в is_pangram {letter for letter in sentence.lower() if letter in BG_ALPHABET} е по-добре; в крайна сметка, използвай имена, ако ще ги използваш на няколко места или израза, в който ги използваш, стане много дълъг
  • в group_by_type новите редове на 25 и 28 ред са в повече
  • group_by_type можеш да я подобриш, ако си прегледаш хитринките
  • не използвай типове като части от имена(dict_of_histograms)
  • в anagrams пак новите редове ми идват в повече
  • хубаво правиш .lower() на подадените думи в anagrams, но трябва да взимаш предвид само буквите в думите; в момента ти броиш и знаци като ,, !, . и т.н.

Димитър обнови решението на 19.03.2014 16:14 (преди над 10 години)

import functools
+
+from string import punctuation
+
BG_ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
def is_pangram(sentence):
- sentence = sentence.lower()
- letters_in = {letter for letter in sentence if letter in BG_ALPHABET}
- return letters_in == BG_ALPHABET
+ contained_letters = {letter for letter in sentence.lower()
+ if letter in BG_ALPHABET}
+ return contained_letters == BG_ALPHABET
def char_histogram(text):
return {chars: text.count(chars) for chars in text}
def sort_by(func, arguments):
return sorted(arguments, key=functools.cmp_to_key(func))
def group_by_type(dictionary):
grouped_by_type = {}
for key in dictionary.keys():
+ grouped_by_type.setdefault(
+ type(key), {key: dictionary[key]})[key] = dictionary[key]
- if type(key) in grouped_by_type.keys():
- grouped_by_type[type(key)][key] = dictionary[key]
-
- else:
- grouped_by_type[type(key)] = {key: dictionary[key]}
-
return grouped_by_type
def anagrams(words):
- dict_of_histograms = {}
+ histograms = {}
for word in words:
- word = word.lower()
- dict_of_histograms[word] = char_histogram(word)
+ temp = ''.join(c.lower() for c in word if c.isalnum())
+ histograms[word] = char_histogram(temp)
- list_of_anagrams = []
+ lists_of_anagrams = []
checked_anagrams = []
for word in words:
-
- if dict_of_histograms[word] not in checked_anagrams:
- checked_anagrams.append(dict_of_histograms[word])
- list_of_anagrams.append([word])
-
+ if histograms[word] not in checked_anagrams:
+ checked_anagrams.append(histograms[word])
+ lists_of_anagrams.append([word])
else:
- index_to_append = checked_anagrams.index(dict_of_histograms[word])
- list_of_anagrams[index_to_append].append(word)
+ index_to_append = checked_anagrams.index(histograms[word])
+ lists_of_anagrams[index_to_append].append(word)
- return list_of_anagrams
+ return lists_of_anagrams

Димитър обнови решението на 19.03.2014 16:16 (преди над 10 години)

import functools
-
-from string import punctuation
-
BG_ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'}
def is_pangram(sentence):
contained_letters = {letter for letter in sentence.lower()
if letter in BG_ALPHABET}
return contained_letters == BG_ALPHABET
def char_histogram(text):
return {chars: text.count(chars) for chars in text}
def sort_by(func, arguments):
return sorted(arguments, key=functools.cmp_to_key(func))
def group_by_type(dictionary):
grouped_by_type = {}
for key in dictionary.keys():
grouped_by_type.setdefault(
type(key), {key: dictionary[key]})[key] = dictionary[key]
return grouped_by_type
def anagrams(words):
histograms = {}
for word in words:
temp = ''.join(c.lower() for c in word if c.isalnum())
histograms[word] = char_histogram(temp)
lists_of_anagrams = []
checked_anagrams = []
for word in words:
if histograms[word] not in checked_anagrams:
checked_anagrams.append(histograms[word])
lists_of_anagrams.append([word])
else:
index_to_append = checked_anagrams.index(histograms[word])
lists_of_anagrams[index_to_append].append(word)
return lists_of_anagrams