Решение на Пет функции от Александър Стоичков

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

Към профила на Александър Стоичков

Резултати

  • 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-1lxq7f8/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-1lxq7f8/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({'Tom Cruise', "So I'm cuter"})
Items in the second set but not the first:
frozenset({'Tom Marvolo Riddle'})
frozenset({"So I'm cuter"})
frozenset({'I am Lord Voldemort'})
frozenset({'Tom Cruise'})

======================================================================
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-1lxq7f8/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.010s

FAILED (failures=3)

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

Александър обнови решението на 19.03.2014 12:30 (преди около 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:
+ arguments[i], arguments[j] = arguments[j], arguments[i]
+ return arguments