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

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

Към профила на Александър Чешмеджиев

Резултати

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

Код

def is_pangram(sentence):
alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж',
'з', 'и', 'й', 'к', 'л', 'м', 'н',
'о', 'п', 'р', 'с', 'т', 'у', 'ф',
'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь',
'ю', 'я']
is_pangram = False
for letter in sentence.lower():
if letter in alphabet:
alphabet.remove(letter)
if len(alphabet) == 0:
is_pangram = True
return is_pangram
def char_histogram(text):
char_histogram = {}
for letter in text:
if letter not in char_histogram:
char_histogram[letter] = 1
else:
char_histogram[letter] += 1
return char_histogram
def group_by_type(dictionary):
grouped_dictionary = {}
for key in dictionary:
if type(key) not in grouped_dictionary:
grouped_dictionary[type(key)] = {key: dictionary[key]}
else:
grouped_dictionary[type(key)][key] = dictionary[key]
return grouped_dictionary
def is_anagram(string, compared_string):
anagram = True
if len(string) != len(compared_string):
return False
for string_letter in string:
if string_letter not in compared_string:
anagram = False
return anagram
def sorted_anagram_list(word, words):
sorted_anagrams = []
for string in words:
if is_anagram(word, string):
sorted_anagrams.append(string)
sorted_anagrams.sort()
return sorted_anagrams
def anagrams(words):
anagrams = []
for word in words:
if sorted_anagram_list(word, words) not in anagrams:
anagrams.append(sorted_anagram_list(word, words))
return anagrams
def sort_by(func, arguments):
for i in range(0, len(arguments)):
for j in range(0, len(arguments)):
if func(arguments[i], arguments[j]) > 0:
temp = arguments[i]
arguments[i] = arguments[j]
arguments[j] = temp
arguments.reverse()
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-1lxy2p1/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({'Ray Adverb', 'Dave Barry'})
Items in the second set but not the first:
frozenset({'Dave Barry'})
frozenset({'Ray Adverb'})

======================================================================
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-1lxy2p1/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({'I am Lord Voldemort'})
frozenset({"So I'm cuter"})
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-1lxy2p1/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] != [2, 4, 0, 5, 3, 1]

First differing element 0:
0
2

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

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

FAILED (failures=3)

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

Александър обнови решението на 18.03.2014 21:08 (преди над 10 години)

+def is_pangram(sentence):
+ alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж',
+ 'з', 'и', 'й', 'к', 'л', 'м', 'н',
+ 'о', 'п', 'р', 'с', 'т', 'у', 'ф',
+ 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь',
+ 'ю', 'я']
+ is_pangram = False
+ for letter in sentence.lower():
+ if letter in alphabet:
+ alphabet.remove(letter)
+ if len(alphabet) == 0:
+ is_pangram = True
+ return is_pangram
+
+def char_histogram(text):
+ char_histogram = {}
+ for letter in text:
+ if letter not in char_histogram:
+ char_histogram[letter] = 1
+ else:
+ char_histogram[letter] += 1
+ return char_histogram
+
+def group_by_type(dictionary):
+ grouped_dictionary = {}
+ for key in dictionary:
+ if type(key) not in grouped_dictionary:
+ grouped_dictionary[type(key)] = {key: dictionary[key]}
+ else:
+ grouped_dictionary[type(key)][key] = dictionary[key]
+ return grouped_dictionary
+
+def is_anagram(string, compared_string):
+ anagram = True
+ if len(string) != len(compared_string):
+ return False
+ for string_letter in string:
+ if string_letter not in compared_string:
+ anagram = False
+ return anagram
+
+def sorted_anagram_list(word, words):
+ sorted_anagrams = []
+ for string in words:
+ if is_anagram(word, string):
+ sorted_anagrams.append(string)
+ sorted_anagrams.sort()
+ return sorted_anagrams
+
+def anagram(words):
+ anagrams = []
+ for word in words:
+ if sorted_anagram_list(word, words) not in anagrams:
+ anagrams.append(sorted_anagram_list(word, words))
+ return anagrams
+
+def sort_by(func, arguments):
+ for i in range(0, len(arguments)):
+ for j in range(0, len(arguments)):
+ if func(arguments[i], arguments[j]) > 0:
+ temp = arguments[i]
+ arguments[i] = arguments[j]
+ arguments[j] = temp
+ arguments.reverse()
+ return arguments
  • char_histogram и group_by_type можеш да я подобриш, ако си прегледаш хитринките
  • сортировката не ти е стабилна, а по условие се иска да е такава (ако не знаеш какво е това питай тук)
  • забележи, че функцията се казва anagrams, а не anagram
  • при анаграмите - трябва да гледаш само буквите и то без значени дали са главни или малки
  • в Python се swap-ва така a, b = b, a

Александър обнови решението на 19.03.2014 00:08 (преди над 10 години)

def is_pangram(sentence):
alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж',
'з', 'и', 'й', 'к', 'л', 'м', 'н',
'о', 'п', 'р', 'с', 'т', 'у', 'ф',
'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь',
'ю', 'я']
is_pangram = False
for letter in sentence.lower():
if letter in alphabet:
alphabet.remove(letter)
if len(alphabet) == 0:
is_pangram = True
return is_pangram
def char_histogram(text):
char_histogram = {}
for letter in text:
if letter not in char_histogram:
char_histogram[letter] = 1
else:
char_histogram[letter] += 1
return char_histogram
def group_by_type(dictionary):
grouped_dictionary = {}
for key in dictionary:
if type(key) not in grouped_dictionary:
grouped_dictionary[type(key)] = {key: dictionary[key]}
else:
grouped_dictionary[type(key)][key] = dictionary[key]
return grouped_dictionary
def is_anagram(string, compared_string):
anagram = True
if len(string) != len(compared_string):
return False
for string_letter in string:
if string_letter not in compared_string:
anagram = False
return anagram
def sorted_anagram_list(word, words):
sorted_anagrams = []
for string in words:
if is_anagram(word, string):
sorted_anagrams.append(string)
sorted_anagrams.sort()
return sorted_anagrams
-def anagram(words):
+def anagrams(words):
anagrams = []
for word in words:
if sorted_anagram_list(word, words) not in anagrams:
anagrams.append(sorted_anagram_list(word, words))
return anagrams
def sort_by(func, arguments):
for i in range(0, len(arguments)):
for j in range(0, len(arguments)):
if func(arguments[i], arguments[j]) > 0:
temp = arguments[i]
arguments[i] = arguments[j]
arguments[j] = temp
arguments.reverse()
return arguments
+