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

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

Към профила на Йордан Дикманов

Резултати

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

Код

import functools
def is_pangram(sentence):
alphabet = [chr(i) for i in range(ord('а'), ord('я') + 1)
if i != ord('ы') and i != ord('э')]
for letter in alphabet:
if letter not in sentence.lower():
return False
return True
def char_histogram(sentence):
count_each_letter = {}
for letter in sentence:
if letter not in count_each_letter:
count_each_letter[letter] = sentence.count(letter)
return count_each_letter
def sort_by(funct, arguments):
arguments.sort(key=functools.cmp_to_key(funct))
return arguments
def group_by_type(dictionary):
dictionary_by_type = {}
for key in dictionary.keys():
if key.__class__ not in dictionary_by_type:
dictionary_by_type[key.__class__] = {}
if key not in dictionary_by_type[key.__class__]:
dictionary_by_type[key.__class__][key] = dictionary[key]
return dictionary_by_type
def is_anagram(first_word, second_word):
for letter in first_word.lower():
if letter.isalpha() and letter not in second_word.lower():
return False
return True
def anagrams(words):
all_anagrams = []
for first_word in words:
word_permutation = [second_word for second_word in words
if is_anagram(first_word, second_word)]
if word_permutation not in all_anagrams:
all_anagrams.append(word_permutation)
return all_anagrams

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.012s

OK

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

Йордан обнови решението на 16.03.2014 22:52 (преди над 10 години)

+import functools
+def is_pangram(sentence):
+ alphabet = [chr(i) for i in range(ord('а'), ord('я') + 1)
+ if i != ord('ы') and i != ord('э')]
+ for letter in alphabet:
+ if letter not in sentence.lower():
+ return False
+ return True
+
+def char_histogram(sentence):
+ count_each_letter = {}
+ for letter in sentence:
+ if letter not in count_each_letter:
+ count_each_letter[letter] = sentence.count(letter)
+ return count_each_letter
+
+def sort_by(funct, arguments):
+ arguments.sort(key =functools.cmp_to_key(funct))
+ return arguments
+
+def group_by_type(dictionary):
+ new_dictionary={}
+ for key in dictionary.keys():
+ if key.__class__ not in new_dictionary:
+ new_dictionary[key.__class__]={}
+ if key not in new_dictionary[key.__class__]:
+ new_dictionary[key.__class__][key] = dictionary[key]
+ return new_dictionary
+def same_strings(word1, word2):
+ for letter in word1:
+ if letter.isalpha() and letter not in word2: return False
+ return True
+
+def anagrams(word):
+ all_anagrams=[]
+ for word1 in word:
+ word_permutation = [word2 for word2 in word
+ if same_strings(word1, word2)]
+ if word_permutation not in all_anagrams:
+ all_anagrams.append(word_permutation)
+ return all_anagrams

Йордан обнови решението на 16.03.2014 23:15 (преди над 10 години)

import functools
+
+
def is_pangram(sentence):
- alphabet = [chr(i) for i in range(ord('а'), ord('я') + 1)
- if i != ord('ы') and i != ord('э')]
+ alphabet = [chr(i) for i in range(ord('а'), ord('я') + 1)
+ if i != ord('ы') and i != ord('э')]
for letter in alphabet:
if letter not in sentence.lower():
return False
return True
+
def char_histogram(sentence):
count_each_letter = {}
for letter in sentence:
if letter not in count_each_letter:
count_each_letter[letter] = sentence.count(letter)
return count_each_letter
+
def sort_by(funct, arguments):
- arguments.sort(key =functools.cmp_to_key(funct))
+ arguments.sort(key=functools.cmp_to_key(funct))
return arguments
+
def group_by_type(dictionary):
- new_dictionary={}
+ new_dictionary = {}
for key in dictionary.keys():
- if key.__class__ not in new_dictionary:
- new_dictionary[key.__class__]={}
+ if key.__class__ not in new_dictionary:
+ new_dictionary[key.__class__] = {}
if key not in new_dictionary[key.__class__]:
new_dictionary[key.__class__][key] = dictionary[key]
return new_dictionary
+
+
def same_strings(word1, word2):
for letter in word1:
- if letter.isalpha() and letter not in word2: return False
+ if letter.isalpha() and letter not in word2:
+ return False
return True
+
def anagrams(word):
- all_anagrams=[]
+ all_anagrams = []
for word1 in word:
- word_permutation = [word2 for word2 in word
- if same_strings(word1, word2)]
- if word_permutation not in all_anagrams:
+ word_permutation = [word2 for word2 in word
+ if same_strings(word1, word2)]
+ if word_permutation not in all_anagrams:
all_anagrams.append(word_permutation)
- return all_anagrams
+ return all_anagrams

Йордан обнови решението на 17.03.2014 22:17 (преди над 10 години)

import functools
def is_pangram(sentence):
alphabet = [chr(i) for i in range(ord('а'), ord('я') + 1)
if i != ord('ы') and i != ord('э')]
for letter in alphabet:
if letter not in sentence.lower():
return False
return True
def char_histogram(sentence):
count_each_letter = {}
for letter in sentence:
if letter not in count_each_letter:
count_each_letter[letter] = sentence.count(letter)
return count_each_letter
def sort_by(funct, arguments):
arguments.sort(key=functools.cmp_to_key(funct))
return arguments
def group_by_type(dictionary):
- new_dictionary = {}
+ dictionary_by_type = {}
for key in dictionary.keys():
- if key.__class__ not in new_dictionary:
- new_dictionary[key.__class__] = {}
- if key not in new_dictionary[key.__class__]:
- new_dictionary[key.__class__][key] = dictionary[key]
- return new_dictionary
+ if key.__class__ not in dictionary_by_type:
+ dictionary_by_type[key.__class__] = {}
+ if key not in dictionary_by_type[key.__class__]:
+ dictionary_by_type[key.__class__][key] = dictionary[key]
+ return dictionary_by_type
-def same_strings(word1, word2):
- for letter in word1:
- if letter.isalpha() and letter not in word2:
+def is_anagram(first_word, second_word):
+ for letter in first_word.lower():
+ if letter.isalpha() and letter not in second_word.lower():
return False
return True
-def anagrams(word):
+def anagrams(words):
all_anagrams = []
- for word1 in word:
- word_permutation = [word2 for word2 in word
- if same_strings(word1, word2)]
+ for first_word in words:
+ word_permutation = [second_word for second_word in words
+ if is_anagram(first_word, second_word)]
if word_permutation not in all_anagrams:
all_anagrams.append(word_permutation)
return all_anagrams