Решение на Пет функции от Людмила Савова

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

Към профила на Людмила Савова

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 13 успешни тест(а)
  • 3 неуспешни тест(а)

Код

def is_pangram(sentence):
ALPHABET = sorted({'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'})
#All letters to lower-case
sentence = sentence.lower()
#Remove special characters and punctuation
all_letters = ''.join(e for e in sentence if e.isalnum())
#Take all the letters once, store them in unique_letters
unique_letters = "".join(set(all_letters))
#Order letters is alphabetical order
ordered_unique_letters = sorted(unique_letters)
#Return True if ordered unique letters of sentence are the alphabet
return ordered_unique_letters == ALPHABET
#//////////////////////////////////////////////////////////////////////
def char_histogram(text):
char_hist = {}
for letter in text:
if letter in char_hist:
char_hist[letter] += 1
else:
char_hist[letter] = 1
return char_hist
#//////////////////////////////////////////////////////////////////////
def group_by_type(dictionary):
grouped_dictionary = {}
for key in dictionary:
object_class = key.__class__
if object_class in grouped_dictionary:
value_dict_type = grouped_dictionary[object_class]
value_dict_type[key] = dictionary[key]
grouped_dictionary[object_class] = value_dict_type
else:
grouped_dictionary[object_class] = {key: dictionary[key]}
return grouped_dictionary
#/////////////////////////////////////////////////////////////////////
def anagrams(words):
#Contains the final result, a list of list that contains anagram words
result = []
#Pick the first word and find all other words that are anagrams.
#All anagram words are removed from the words list
#When there are no words left in words we're done!
while len(words) != 0:
#Contains words that are anagrams
mid_list = []
#Each word will be tested against test_word
test_word = words[0]
#Add test_word to the list with intermediate results(mid_list)
mid_list.append(test_word)
#First element is kept in test_word and is no longer needed
words.pop(0)
#Iterates through all words in list words
i = 0
while i < len(words):
#If elements are anagrams, add words[i] to mid_list and pop it
#Otherwise proceed with the next word
if is_anagram(test_word, words[i]) is True:
mid_list.append(words[i])
words.pop(i)
else:
i += 1
#Add the intermediate result to final result
result.append(mid_list)
return result
#/////////////////////////////////////////////////////////////////////
def is_anagram(word1, word2):
sorted_word1 = sorted(word1)
sorted_word2 = sorted(word2)
if sorted_word1 == sorted_word2:
return True
else:
return False
#/////////////////////////////////////////////////////////////////////
def sort_by(func, arguments):
for i in range(0, len(arguments)):
for j in range(i, len(arguments)):
if func(arguments[i], arguments[j]) > 0:
arguments[i], arguments[j] = arguments[j], arguments[i]
return arguments

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

.FF............F
======================================================================
FAIL: test_with_different_cases (test.TestAnagrams)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-1ace9q7/test.py", line 125, in test_with_different_cases
    set(map(frozenset, solution.anagrams(words))))
AssertionError: Items in the first set but not the second:
frozenset({'Dave Barry', 'Ray Adverb'})
Items in the second set but not the first:
frozenset({'Ray Adverb'})
frozenset({'Dave Barry'})

======================================================================
FAIL: test_with_different_symbols (test.TestAnagrams)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-1ace9q7/test.py", line 135, in test_with_different_symbols
    set(map(frozenset, solution.anagrams(words))))
AssertionError: Items in the first set but not the second:
frozenset({'I am Lord Voldemort', 'Tom Marvolo Riddle'})
frozenset({"So I'm cuter", 'Tom Cruise'})
Items in the second set but not the first:
frozenset({'Tom Cruise'})
frozenset({'Tom Marvolo Riddle'})
frozenset({"So I'm cuter"})
frozenset({'I am Lord Voldemort'})

======================================================================
FAIL: test_sort_by_simple_test (test.TestSortBy)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-1ace9q7/test.py", line 54, in test_sort_by_simple_test
    solution.sort_by(lambda x, y: x % 2 - y % 2, [0, 1, 2, 3, 4, 5]))
AssertionError: Lists differ: [0, 2, 4, 1, 3, 5] != [0, 2, 4, 3, 1, 5]

First differing element 3:
1
3

- [0, 2, 4, 1, 3, 5]
?              ---

+ [0, 2, 4, 3, 1, 5]
?           +++


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

FAILED (failures=3)

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

Людмила обнови решението на 16.03.2014 17:10 (преди над 10 години)

+def is_pangram(sentence):
+ ALPHABET = sorted({'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
+ 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
+ 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'})
+ #All letters to lower-case
+ sentence = sentence.lower()
+ #Remove special characters and punctuation
+ all_letters = ''.join(e for e in sentence if e.isalnum())
+ #Take all the letters once, store them in unique_letters
+ unique_letters = "".join(set(all_letters))
+ #Order letters is alphabetical order
+ ordered_unique_letters = sorted(unique_letters)
+
+ #Return True if ordered unique letters of sentence are the alphabet
+ return ordered_unique_letters == ALPHABET
+
+#//////////////////////////////////////////////////////////////////////
+
+
+def char_histogram(text):
+
+ char_hist = {}
+ for letter in text:
+ if letter in char_hist:
+ char_hist[letter] += 1
+ else:
+ char_hist[letter] = 1
+
+ return char_hist
+
+#//////////////////////////////////////////////////////////////////////
+
+
+def group_by_type(dictionary):
+
+ grouped_dictionary = {}
+ for key in dictionary:
+ object_class = key.__class__
+ if object_class in grouped_dictionary:
+ value_dict_type = grouped_dictionary[object_class]
+ value_dict_type[key] = dictionary[key]
+ grouped_dictionary[object_class] = value_dict_type
+ else:
+ grouped_dictionary[object_class] = {key: dictionary[key]}
+
+ return grouped_dictionary
+
+#/////////////////////////////////////////////////////////////////////
+
+
+def anagrams(words):
+
+ #Contains the final result, a list of list that contains anagram words
+ result = []
+
+ #Pick the first word and find all other words that are anagrams.
+ #All anagram words are removed from the words list
+ #When there are no words left in words we're done!
+ while len(words) != 0:
+
+ #Contains words that are anagrams
+ mid_list = []
+ #Each word will be tested against test_word
+ test_word = words[0]
+ #Add test_word to the list with intermediate results(mid_list)
+ mid_list.append(test_word)
+ #First element is kept in test_word and is no longer needed
+ words.pop(0)
+
+ #Iterates through all words in list words
+ i = 0
+ while i < len(words):
+
+ #If elements are anagrams, add words[i] to mid_list and pop it
+ #Otherwise proceed with the next word
+ if is_anagram(test_word, words[i]) is True:
+ mid_list.append(words[i])
+ words.pop(i)
+ else:
+ i += 1
+
+ #Add the intermediate result to final result
+ result.append(mid_list)
+
+ return result
+
+#/////////////////////////////////////////////////////////////////////
+
+
+def is_anagram(word1, word2):
+ sorted_word1 = sorted(word1)
+ sorted_word2 = sorted(word2)
+ if sorted_word1 == sorted_word2:
+ return True
+ else:
+ return False

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

def is_pangram(sentence):
ALPHABET = sorted({'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'})
#All letters to lower-case
sentence = sentence.lower()
#Remove special characters and punctuation
all_letters = ''.join(e for e in sentence if e.isalnum())
#Take all the letters once, store them in unique_letters
unique_letters = "".join(set(all_letters))
#Order letters is alphabetical order
ordered_unique_letters = sorted(unique_letters)
#Return True if ordered unique letters of sentence are the alphabet
return ordered_unique_letters == ALPHABET
#//////////////////////////////////////////////////////////////////////
def char_histogram(text):
char_hist = {}
for letter in text:
if letter in char_hist:
char_hist[letter] += 1
else:
char_hist[letter] = 1
return char_hist
#//////////////////////////////////////////////////////////////////////
def group_by_type(dictionary):
grouped_dictionary = {}
for key in dictionary:
object_class = key.__class__
if object_class in grouped_dictionary:
value_dict_type = grouped_dictionary[object_class]
value_dict_type[key] = dictionary[key]
grouped_dictionary[object_class] = value_dict_type
else:
grouped_dictionary[object_class] = {key: dictionary[key]}
return grouped_dictionary
#/////////////////////////////////////////////////////////////////////
def anagrams(words):
#Contains the final result, a list of list that contains anagram words
result = []
#Pick the first word and find all other words that are anagrams.
#All anagram words are removed from the words list
#When there are no words left in words we're done!
while len(words) != 0:
#Contains words that are anagrams
mid_list = []
#Each word will be tested against test_word
test_word = words[0]
#Add test_word to the list with intermediate results(mid_list)
mid_list.append(test_word)
#First element is kept in test_word and is no longer needed
words.pop(0)
#Iterates through all words in list words
i = 0
while i < len(words):
#If elements are anagrams, add words[i] to mid_list and pop it
#Otherwise proceed with the next word
if is_anagram(test_word, words[i]) is True:
mid_list.append(words[i])
words.pop(i)
else:
i += 1
#Add the intermediate result to final result
result.append(mid_list)
return result
#/////////////////////////////////////////////////////////////////////
def is_anagram(word1, word2):
sorted_word1 = sorted(word1)
sorted_word2 = sorted(word2)
if sorted_word1 == sorted_word2:
return True
else:
return False
+
+#/////////////////////////////////////////////////////////////////////
+
+
+def sort_by(func, arguments):
+ #Create a dictionary with keys - arguments from list and corresponding
+ #order penalty. In the beginning all argument's order penalty is zero.
+ #Penalty will be increased according to result of func(). Argument
+ #which is second will receive a penalty of 1.
+ order_penalty = [0] * len(arguments)
+ arguments_dict = dict(zip(arguments, order_penalty))
+
+ #Iterating through arguments. Taking each one and comparing it to all that
+ #stand after it in arguments list.
+ for i in range(0, len(arguments)):
+ for j in range(i, len(arguments)-1):
+ #If func of compared arguments is positive, give penalty to first one,
+ #else, give penalty to the second one.
+ if func(arguments[i], arguments[j]) > 0:
+ arguments_dict[arguments[i]] += 1
+ else:
+ arguments_dict[arguments[j]] += 1
+
+ #Result list from sorting arguments_dict by value in ascending order.
+ result = sorted(arguments_dict.keys())
+
+ return result

Людмила обнови решението на 16.03.2014 23:10 (преди над 10 години)

def is_pangram(sentence):
ALPHABET = sorted({'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'})
#All letters to lower-case
sentence = sentence.lower()
#Remove special characters and punctuation
all_letters = ''.join(e for e in sentence if e.isalnum())
#Take all the letters once, store them in unique_letters
unique_letters = "".join(set(all_letters))
#Order letters is alphabetical order
ordered_unique_letters = sorted(unique_letters)
#Return True if ordered unique letters of sentence are the alphabet
return ordered_unique_letters == ALPHABET
#//////////////////////////////////////////////////////////////////////
def char_histogram(text):
char_hist = {}
for letter in text:
if letter in char_hist:
char_hist[letter] += 1
else:
char_hist[letter] = 1
return char_hist
#//////////////////////////////////////////////////////////////////////
def group_by_type(dictionary):
grouped_dictionary = {}
for key in dictionary:
object_class = key.__class__
if object_class in grouped_dictionary:
value_dict_type = grouped_dictionary[object_class]
value_dict_type[key] = dictionary[key]
grouped_dictionary[object_class] = value_dict_type
else:
grouped_dictionary[object_class] = {key: dictionary[key]}
return grouped_dictionary
#/////////////////////////////////////////////////////////////////////
def anagrams(words):
#Contains the final result, a list of list that contains anagram words
result = []
#Pick the first word and find all other words that are anagrams.
#All anagram words are removed from the words list
#When there are no words left in words we're done!
while len(words) != 0:
#Contains words that are anagrams
mid_list = []
#Each word will be tested against test_word
test_word = words[0]
#Add test_word to the list with intermediate results(mid_list)
mid_list.append(test_word)
#First element is kept in test_word and is no longer needed
words.pop(0)
#Iterates through all words in list words
i = 0
while i < len(words):
#If elements are anagrams, add words[i] to mid_list and pop it
#Otherwise proceed with the next word
if is_anagram(test_word, words[i]) is True:
mid_list.append(words[i])
words.pop(i)
else:
i += 1
#Add the intermediate result to final result
result.append(mid_list)
return result
#/////////////////////////////////////////////////////////////////////
def is_anagram(word1, word2):
sorted_word1 = sorted(word1)
sorted_word2 = sorted(word2)
if sorted_word1 == sorted_word2:
return True
else:
return False
#/////////////////////////////////////////////////////////////////////
def sort_by(func, arguments):
- #Create a dictionary with keys - arguments from list and corresponding
- #order penalty. In the beginning all argument's order penalty is zero.
- #Penalty will be increased according to result of func(). Argument
- #which is second will receive a penalty of 1.
- order_penalty = [0] * len(arguments)
- arguments_dict = dict(zip(arguments, order_penalty))
-
- #Iterating through arguments. Taking each one and comparing it to all that
- #stand after it in arguments list.
for i in range(0, len(arguments)):
- for j in range(i, len(arguments)-1):
- #If func of compared arguments is positive, give penalty to first one,
- #else, give penalty to the second one.
- if func(arguments[i], arguments[j]) > 0:
- arguments_dict[arguments[i]] += 1
- else:
- arguments_dict[arguments[j]] += 1
+ for j in range(i, len(arguments)):
+ if func(arguments[i], arguments[j]) >= 0:
+ arguments[i], arguments[j] = arguments[j], arguments[i]
- #Result list from sorting arguments_dict by value in ascending order.
- result = sorted(arguments_dict.keys())
+ return arguments
-
- return result

Людмила обнови решението на 16.03.2014 23:16 (преди над 10 години)

def is_pangram(sentence):
ALPHABET = sorted({'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'})
#All letters to lower-case
sentence = sentence.lower()
#Remove special characters and punctuation
all_letters = ''.join(e for e in sentence if e.isalnum())
#Take all the letters once, store them in unique_letters
unique_letters = "".join(set(all_letters))
#Order letters is alphabetical order
ordered_unique_letters = sorted(unique_letters)
#Return True if ordered unique letters of sentence are the alphabet
return ordered_unique_letters == ALPHABET
#//////////////////////////////////////////////////////////////////////
def char_histogram(text):
char_hist = {}
for letter in text:
if letter in char_hist:
char_hist[letter] += 1
else:
char_hist[letter] = 1
return char_hist
#//////////////////////////////////////////////////////////////////////
def group_by_type(dictionary):
grouped_dictionary = {}
for key in dictionary:
object_class = key.__class__
if object_class in grouped_dictionary:
value_dict_type = grouped_dictionary[object_class]
value_dict_type[key] = dictionary[key]
grouped_dictionary[object_class] = value_dict_type
else:
grouped_dictionary[object_class] = {key: dictionary[key]}
return grouped_dictionary
#/////////////////////////////////////////////////////////////////////
def anagrams(words):
#Contains the final result, a list of list that contains anagram words
result = []
#Pick the first word and find all other words that are anagrams.
#All anagram words are removed from the words list
#When there are no words left in words we're done!
while len(words) != 0:
#Contains words that are anagrams
mid_list = []
#Each word will be tested against test_word
test_word = words[0]
#Add test_word to the list with intermediate results(mid_list)
mid_list.append(test_word)
#First element is kept in test_word and is no longer needed
words.pop(0)
#Iterates through all words in list words
i = 0
while i < len(words):
#If elements are anagrams, add words[i] to mid_list and pop it
#Otherwise proceed with the next word
if is_anagram(test_word, words[i]) is True:
mid_list.append(words[i])
words.pop(i)
else:
i += 1
#Add the intermediate result to final result
result.append(mid_list)
return result
#/////////////////////////////////////////////////////////////////////
def is_anagram(word1, word2):
sorted_word1 = sorted(word1)
sorted_word2 = sorted(word2)
if sorted_word1 == sorted_word2:
return True
else:
return False
#/////////////////////////////////////////////////////////////////////
def sort_by(func, arguments):
for i in range(0, len(arguments)):
for j in range(i, len(arguments)):
- if func(arguments[i], arguments[j]) >= 0:
+ if func(arguments[i], arguments[j]) > 0:
arguments[i], arguments[j] = arguments[j], arguments[i]
return arguments
  • Когато декларираш set, и след това го подадеш на функцията sorted, се връща list, няма смисъл от цялата операция.
  • Не е нужно да използваш #///////.../ за сепаратор
  • sorted_word1, sorted_word2, да слагаш типа на обекта в имета на променливата не е добра практика.