Решение на Пет функции от Иван Върбанов

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

Към профила на Иван Върбанов

Резултати

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

Код

from functools import cmp_to_key
def is_pangram(sentence):
alphabet = [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я',
]
count = 0
sentence = sentence.lower()
sentence = set(sentence)
for letter in alphabet:
if letter in sentence:
count += 1
if count == 30:
return True
else:
return False
def char_histogram(text):
characters = set(text)
characters = tuple(text)
dictionary = {}
for symbol in characters:
dictionary[symbol] = text.count(symbol)
return dictionary
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
dictionary_by_type = {}
for key in dictionary:
dictionary_by_type[type(key)] = {value: dictionary[value] for value in
dictionary if type(value) == type(key)}
return dictionary_by_type
def check_characters(first_word, second_word):
characters = []
for number in range(97, 123):
characters.append(chr(number))
first_word = first_word.lower()
second_word = second_word.lower()
for symbol in set(first_word):
if symbol not in characters:
first_word = first_word.replace(symbol, '')
for symbol in set(second_word):
if symbol not in characters:
second_word = second_word.replace(symbol, '')
if sorted(first_word) == sorted(second_word):
return True
else:
return False
def anagrams(words):
words.sort(key=len)
anagram_words = []
temporary_list = []
for first_word in range(len(words)):
for second_word in range(1, len(words)):
if words[first_word] != words[second_word]:
if check_characters(words[first_word], words[second_word]):
temporary_list.append(words[second_word])
words[second_word] = ''
temporary_list.append(words[first_word])
anagram_words.append(temporary_list)
temporary_list = []
while anagram_words.count(['']) != 0:
del anagram_words[anagram_words.index([''])]
return anagram_words

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.011s

OK

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

Иван обнови решението на 18.03.2014 01:59 (преди над 10 години)

+from functools import cmp_to_key
+
+
+def is_pangram(sentence):
+ alphabet = [
+ 'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
+ 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
+ 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
+ 'ш', 'щ', 'ъ', 'ь', 'ю', 'я',
+ ]
+ count = 0
+ sentence = sentence.lower()
+ sentence = set(sentence)
+ for letter in alphabet:
+ if letter in sentence:
+ count += 1
+ if count == 30:
+ return True
+ else:
+ return False
+
+
+def char_histogram(text):
+ new_text = set(text)
+ new_text = tuple(text)
+ new_dict = {}
+ for symbol in new_text:
+ new_dict[symbol] = text.count(symbol)
+ return new_dict
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ new_dictionary = {}
+ for key in dictionary:
+ new_dictionary[type(key)] = {value: dictionary[value] for value in
+ dictionary if type(value) == type(key)}
+ return new_dictionary
+
+
+def check_characters(first_word, second_word):
+ characters = []
+ for number in range(97, 123):
+ characters.append(chr(number))
+ first_word = first_word.lower()
+ second_word = second_word.lower()
+ for word_1 in set(first_word):
+ if word_1 not in characters:
+ first_word = first_word.replace(word_1, '')
+ for word_2 in set(second_word):
+ if word_2 not in characters:
+ second_word = second_word.replace(word_2, '')
+ if sorted(first_word) == sorted(second_word):
+ return True
+ else:
+ return False
+
+
+def anagrams(words):
+ words.sort(key=len)
+ anagram_words = []
+ temporary_list = []
+ for first_word in range(len(words)):
+ for second_word in range(1, len(words)):
+ if words[first_word] != words[second_word]:
+ if check_characters(words[first_word], words[second_word]):
+ temporary_list.append(words[second_word])
+ words[second_word] = ''
+ temporary_list.append(words[first_word])
+ anagram_words.append(temporary_list)
+ temporary_list = []
+ while anagram_words.count(['']) != 0:
+ del anagram_words[anagram_words.index([''])]
+ return anagram_words

Иван обнови решението на 18.03.2014 22:45 (преди над 10 години)

from functools import cmp_to_key
def is_pangram(sentence):
alphabet = [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я',
]
count = 0
sentence = sentence.lower()
sentence = set(sentence)
for letter in alphabet:
if letter in sentence:
count += 1
if count == 30:
return True
else:
return False
def char_histogram(text):
- new_text = set(text)
- new_text = tuple(text)
- new_dict = {}
- for symbol in new_text:
- new_dict[symbol] = text.count(symbol)
- return new_dict
+ characters = set(text)
+ characters = tuple(text)
+ dictionary = {}
+ for symbol in characters:
+ dictionary[symbol] = text.count(symbol)
+ return dictionary
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
- new_dictionary = {}
+ dictionary_by_type = {}
for key in dictionary:
- new_dictionary[type(key)] = {value: dictionary[value] for value in
+ dictionary_by_type[type(key)] = {value: dictionary[value] for value in
dictionary if type(value) == type(key)}
- return new_dictionary
+ return dictionary_by_type
def check_characters(first_word, second_word):
characters = []
for number in range(97, 123):
characters.append(chr(number))
first_word = first_word.lower()
second_word = second_word.lower()
- for word_1 in set(first_word):
- if word_1 not in characters:
- first_word = first_word.replace(word_1, '')
- for word_2 in set(second_word):
- if word_2 not in characters:
- second_word = second_word.replace(word_2, '')
+ for symbol in set(first_word):
+ if symbol not in characters:
+ first_word = first_word.replace(symbol, '')
+ for symbol in set(second_word):
+ if symbol not in characters:
+ second_word = second_word.replace(symbol, '')
if sorted(first_word) == sorted(second_word):
return True
else:
return False
def anagrams(words):
words.sort(key=len)
anagram_words = []
temporary_list = []
for first_word in range(len(words)):
for second_word in range(1, len(words)):
if words[first_word] != words[second_word]:
if check_characters(words[first_word], words[second_word]):
temporary_list.append(words[second_word])
words[second_word] = ''
temporary_list.append(words[first_word])
anagram_words.append(temporary_list)
temporary_list = []
while anagram_words.count(['']) != 0:
del anagram_words[anagram_words.index([''])]
return anagram_words