Решение на Пет функции от Деян Камбуров

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

Към профила на Деян Камбуров

Резултати

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

Код

from functools import cmp_to_key
def is_pangram(sentence):
alphabet = set([
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
])
for letter in sentence.lower():
if letter in alphabet:
alphabet.remove(letter)
return not alphabet
def char_histogram(text):
histogram = {}
for character in text:
if character in histogram:
histogram[character] += 1
else:
histogram[character] = 1
return histogram
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
type_groups = {}
for key in dictionary.keys():
if type(key) not in type_groups:
type_groups[type(key)] = {key: dictionary[key]}
else:
type_groups[type(key)][key] = dictionary[key]
return type_groups
def anagrams(words):
anagrams_collection = []
while words:
anagrams = find_anagrams(words[0], words)
anagrams_collection.append(anagrams)
words = clear_anagrams(words[0], words)
return anagrams_collection
def find_anagrams(anagram, words):
anagrams = []
for word in words:
if are_anagrams(anagram, word) and are_anagrams(word, anagram):
anagrams.append(word)
return anagrams
def clear_anagrams(anagram, words):
remaining_words = []
for word in words:
if not(are_anagrams(anagram, word) and are_anagrams(word, anagram)):
remaining_words.append(word)
return remaining_words
def are_anagrams(first_phrase, second_phrase):
second_phrase = second_phrase.lower()
for symbol in first_phrase.lower():
second_phrase = second_phrase.replace(symbol, '', 1)
for symbol in second_phrase:
if symbol.isalpha():
return False
return True

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

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

OK

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

Деян обнови решението на 18.03.2014 00:15 (преди над 10 години)

+from functools import cmp_to_key
+
+
+def is_pangram(sentence):
+ alphabet = set([
+ 'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
+ 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
+ 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
+ ])
+
+ for letter in sentence.lower():
+ if letter in alphabet:
+ alphabet.remove(letter)
+
+ return not alphabet
+
+
+def char_histogram(text):
+ histogram = {}
+
+ for character in text:
+ if character in histogram:
+ histogram[character] += 1
+ else:
+ histogram[character] = 1
+
+ return histogram
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ type_dictionary = {}
+
+ for key in dictionary.keys():
+ if type(key) not in type_dictionary:
+ type_dictionary[type(key)] = {key: dictionary[key]}
+ else:
+ type_dictionary[type(key)][key] = dictionary[key]
+
+ return type_dictionary
+
+
+def anagrams(words):
+ anagrams_collection = []
+
+ while words:
+ anagrams = find_anagrams(words[0], words)
+ anagrams_collection.append(anagrams)
+ words = clear_anagrams(words[0], words)
+
+ return anagrams_collection
+
+
+def find_anagrams(anagram, words):
+ anagrams = []
+
+ for word in words:
+ if are_anagrams(anagram, word):
+ anagrams.append(word)
+
+ return anagrams
+
+
+def clear_anagrams(anagram, words):
+ new_words = []
+
+ for word in words:
+ if not are_anagrams(anagram, word):
+ new_words.append(word)
+
+ return new_words
+
+
+def are_anagrams(first_phrase, second_phrase):
+ second_phrase = second_phrase.lower()
+
+ for symbol in first_phrase.lower():
+ second_phrase = second_phrase.replace(symbol, '', 1)
+ for symbol in second_phrase:
+ if symbol.isalpha():
+ return False
+
+ return True

Деян обнови решението на 19.03.2014 01:29 (преди над 10 години)

from functools import cmp_to_key
def is_pangram(sentence):
alphabet = set([
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
])
for letter in sentence.lower():
if letter in alphabet:
alphabet.remove(letter)
return not alphabet
def char_histogram(text):
histogram = {}
for character in text:
if character in histogram:
histogram[character] += 1
else:
histogram[character] = 1
return histogram
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
- type_dictionary = {}
+ type_groups = {}
for key in dictionary.keys():
- if type(key) not in type_dictionary:
- type_dictionary[type(key)] = {key: dictionary[key]}
+ if type(key) not in type_groups:
+ type_groups[type(key)] = {key: dictionary[key]}
else:
- type_dictionary[type(key)][key] = dictionary[key]
+ type_groups[type(key)][key] = dictionary[key]
- return type_dictionary
+ return type_groups
def anagrams(words):
anagrams_collection = []
while words:
anagrams = find_anagrams(words[0], words)
anagrams_collection.append(anagrams)
words = clear_anagrams(words[0], words)
return anagrams_collection
def find_anagrams(anagram, words):
anagrams = []
for word in words:
- if are_anagrams(anagram, word):
+ if are_anagrams(anagram, word) and are_anagrams(word, anagram):
anagrams.append(word)
return anagrams
def clear_anagrams(anagram, words):
- new_words = []
+ remaining_words = []
for word in words:
- if not are_anagrams(anagram, word):
- new_words.append(word)
+ if not(are_anagrams(anagram, word) and are_anagrams(word, anagram)):
+ remaining_words.append(word)
- return new_words
+ return remaining_words
def are_anagrams(first_phrase, second_phrase):
second_phrase = second_phrase.lower()
for symbol in first_phrase.lower():
second_phrase = second_phrase.replace(symbol, '', 1)
for symbol in second_phrase:
if symbol.isalpha():
return False
return True