Решение на Пет функции от Стоян Ефтимов

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

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

Резултати

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

Код

from collections import Counter
def is_pangram(sentence):
sentence = sentence.lower()
ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'}
used_letters = set()
for letter in sentence:
if letter in ALPHABET:
used_letters.add(letter)
return used_letters == ALPHABET
def char_histogram(text):
return Counter(text)
def sort_by(func, arguments):
for i in range(0, len(arguments) - 1):
for j in range(0, len(arguments) - i - 1):
if func(arguments[j], arguments[j + 1]) > 0:
arguments[j], arguments[j + 1] \
= arguments[j + 1], arguments[j]
return arguments
def group_by_type(dictionary):
grouped_dictionary = {}
for key in dictionary:
if type(key) not in grouped_dictionary.keys():
grouped_dictionary[type(key)] = {key: dictionary[key]}
grouped_dictionary[type(key)][key] = dictionary[key]
return grouped_dictionary
def are_anagrams(word1, word2):
word1_sorted = sorted(
[symbol.lower() for symbol in word1 if symbol.isalpha()]
)
word2_sorted = sorted(
[symbol.lower() for symbol in word2 if symbol.isalpha()]
)
return tuple(word1_sorted) == tuple(word2_sorted)
def anagrams(words):
anagrams_in_words = set()
words_sorted_in_anagrams = {}
for word in words:
anagrams_in_words.add(
tuple(sorted(
[symbol.lower() for symbol in word if symbol.isalpha()])
)
)
for anagram in anagrams_in_words:
words_sorted_in_anagrams[anagram] = []
for word in words:
for anagram in anagrams_in_words:
if are_anagrams(word, anagram):
words_sorted_in_anagrams[anagram].append(word)
return list(words_sorted_in_anagrams.values())
print(char_histogram('aaabbc'))

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

Counter({'a': 3, 'b': 2, 'c': 1})
................
----------------------------------------------------------------------
Ran 16 tests in 0.009s

OK

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

Стоян обнови решението на 14.03.2014 19:54 (преди над 10 години)

+def is_pangram(sentence):
+ sentence = sentence.lower()
+ ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о',
+ 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'}
+ used_letters = set()
+ for letter in sentence:
+ if letter in ALPHABET:
+ used_letters.add(letter)
+ return used_letters == ALPHABET
+
+
+def char_histogram(text):
+ histogram = {}
+ different_letters_in_text = set(text)
+ for letter in different_letters_in_text:
+ histogram[letter] = text.count(letter)
+ return histogram
+
+
+def sort_by(func, arguments):
+ for i in range(0, len(arguments) - 1):
+ for j in range(0, len(arguments) - i - 1):
+ if func(arguments[j], arguments[j + 1]) > 0:
+ arguments[j], arguments[j + 1] = arguments[j + 1], arguments[j]
+ return arguments
+
+
+def group_by_type(dictionary):
+ grouped_dictionary = {}
+ for key in dictionary:
+ if type(key) not in grouped_dictionary.keys():
+ grouped_dictionary[type(key)] = {key: dictionary[key]}
+ grouped_dictionary[type(key)][key] = dictionary[key]
+ return grouped_dictionary
+
+
+def are_anagrams(word1, word2):
+ return tuple(sorted(word1)) == tuple(sorted(word2))
+
+
+def anagrams(words):
+ anagrams_in_words = set()
+ dict_of_anagrams = {}
+ for word in words:
+ anagrams_in_words.add(tuple(sorted(word)))
+ for anagram in anagrams_in_words:
+ dict_of_anagrams[anagram] = []
+ for word in words:
+ for anagram in anagrams_in_words:
+ if are_anagrams(word, anagram):
+ dict_of_anagrams[anagram].append(word)
+ return list(dict_of_anagrams.values())

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

def is_pangram(sentence):
sentence = sentence.lower()
- ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о',
- 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'}
+ ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
+ 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
+ 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'}
used_letters = set()
for letter in sentence:
if letter in ALPHABET:
used_letters.add(letter)
return used_letters == ALPHABET
def char_histogram(text):
histogram = {}
different_letters_in_text = set(text)
for letter in different_letters_in_text:
histogram[letter] = text.count(letter)
return histogram
def sort_by(func, arguments):
for i in range(0, len(arguments) - 1):
for j in range(0, len(arguments) - i - 1):
if func(arguments[j], arguments[j + 1]) > 0:
- arguments[j], arguments[j + 1] = arguments[j + 1], arguments[j]
+ arguments[j], arguments[j + 1] \
+ = arguments[j + 1], arguments[j]
return arguments
def group_by_type(dictionary):
grouped_dictionary = {}
for key in dictionary:
if type(key) not in grouped_dictionary.keys():
grouped_dictionary[type(key)] = {key: dictionary[key]}
grouped_dictionary[type(key)][key] = dictionary[key]
return grouped_dictionary
def are_anagrams(word1, word2):
- return tuple(sorted(word1)) == tuple(sorted(word2))
+ word1_sorted = sorted(
+ [symbol.lower() for symbol in word1 if symbol.isalpha()]
+ )
+ word2_sorted = sorted(
+ [symbol.lower() for symbol in word2 if symbol.isalpha()]
+ )
+ return tuple(word1_sorted) == tuple(word2_sorted)
def anagrams(words):
anagrams_in_words = set()
- dict_of_anagrams = {}
+ words_sorted_in_anagrams = {}
for word in words:
- anagrams_in_words.add(tuple(sorted(word)))
+ anagrams_in_words.add(
+ tuple(sorted(
+ [symbol.lower() for symbol in word if symbol.isalpha()])
+ )
+ )
for anagram in anagrams_in_words:
- dict_of_anagrams[anagram] = []
+ words_sorted_in_anagrams[anagram] = []
for word in words:
for anagram in anagrams_in_words:
if are_anagrams(word, anagram):
- dict_of_anagrams[anagram].append(word)
- return list(dict_of_anagrams.values())
+ words_sorted_in_anagrams[anagram].append(word)
+ return list(words_sorted_in_anagrams.values())

Стоян обнови решението на 19.03.2014 11:11 (преди над 10 години)

+from collections import Counter
+
def is_pangram(sentence):
sentence = sentence.lower()
ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'}
used_letters = set()
for letter in sentence:
if letter in ALPHABET:
used_letters.add(letter)
return used_letters == ALPHABET
def char_histogram(text):
- histogram = {}
- different_letters_in_text = set(text)
- for letter in different_letters_in_text:
- histogram[letter] = text.count(letter)
- return histogram
+ return Counter(text)
def sort_by(func, arguments):
for i in range(0, len(arguments) - 1):
for j in range(0, len(arguments) - i - 1):
if func(arguments[j], arguments[j + 1]) > 0:
arguments[j], arguments[j + 1] \
= arguments[j + 1], arguments[j]
return arguments
def group_by_type(dictionary):
grouped_dictionary = {}
for key in dictionary:
if type(key) not in grouped_dictionary.keys():
grouped_dictionary[type(key)] = {key: dictionary[key]}
grouped_dictionary[type(key)][key] = dictionary[key]
return grouped_dictionary
def are_anagrams(word1, word2):
word1_sorted = sorted(
[symbol.lower() for symbol in word1 if symbol.isalpha()]
)
word2_sorted = sorted(
[symbol.lower() for symbol in word2 if symbol.isalpha()]
)
return tuple(word1_sorted) == tuple(word2_sorted)
def anagrams(words):
anagrams_in_words = set()
words_sorted_in_anagrams = {}
for word in words:
anagrams_in_words.add(
tuple(sorted(
[symbol.lower() for symbol in word if symbol.isalpha()])
)
)
for anagram in anagrams_in_words:
words_sorted_in_anagrams[anagram] = []
for word in words:
for anagram in anagrams_in_words:
if are_anagrams(word, anagram):
words_sorted_in_anagrams[anagram].append(word)
return list(words_sorted_in_anagrams.values())
+
+print(char_histogram('aaabbc'))