Решение на Пет функции от Емануел Стоянов

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

Към профила на Емануел Стоянов

Резултати

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

Код

def is_pangram(sentence):
letters = ['А', 'Б', 'В', 'Г', 'Д', 'Е',
'Ж', 'З', 'И', 'Й', 'К', 'Л',
'М', 'Н', 'О', 'П', 'Р', 'С',
'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч',
'Ш', 'Щ', 'Ъ', 'Ь', 'Ю', 'Я']
for letter in letters:
if letter not in sentence.upper():
return False
return True
def char_histogram(text):
histogram = {}
for char in text:
if char not in histogram:
histogram[char] = 1
else:
histogram[char] += 1
return histogram
def sort_by(func, arguments):
sorted_arguments = arguments
for i in range(0, len(sorted_arguments) - 1):
for j in range(i+1, len(sorted_arguments)):
if func(sorted_arguments[i], sorted_arguments[j]) >= 0:
sorted_arguments[i], sorted_arguments[j] = \
sorted_arguments[j], sorted_arguments[i]
return sorted_arguments
def group_by_type(dictionary):
type_dictionary = {}
for key in dictionary:
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 = []
for i in range(0, len(words)):
group_of_words = [words[i]]
for word in words[i+1:]:
if sorted(remove_not_letters(words[i])) == \
sorted(remove_not_letters(word)):
words.remove(word)
group_of_words.append(word)
anagrams.append(group_of_words)
if len(words) - len(anagrams) == 0:
break
if len(words) - len(anagrams) == 1:
words.reverse()
anagrams.append([words[0]])
return anagrams
return anagrams
def remove_not_letters(word):
word_of_letters = word
for char in word_of_letters:
if not char.isalpha():
word_of_letters = word_of_letters.replace(char, '')
return word_of_letters

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

.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-1aetx3i/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-1aetx3i/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-1aetx3i/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] != [4, 2, 0, 5, 3, 1]

First differing element 0:
0
4

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

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

FAILED (failures=3)

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

Емануел обнови решението на 18.03.2014 01:04 (преди над 10 години)

+def is_pangram(sentence):
+ letters = ['А', 'Б', 'В', 'Г', 'Д', 'Е',
+ 'Ж', 'З', 'И', 'Й', 'К', 'Л',
+ 'М', 'Н', 'О', 'П', 'Р', 'С',
+ 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч',
+ 'Ш', 'Щ', 'Ъ', 'Ь', 'Ю', 'Я']
+ for letter in letters:
+ if letter not in sentence.upper():
+ return False
+ return True
+
+
+def char_histogram(text):
+ histogram = {}
+ for char in text:
+ histogram[char] = 0
+ for char in text:
+ histogram[char] += 1
+ return histogram
+
+
+def sort_by(func, arguments):
+ for i in range(0, len(arguments)):
+ for j in range(i+1, len(arguments)):
+ if func(arguments[i], arguments[j]) > 0:
+ temp = arguments[i]
+ arguments[i] = arguments[j]
+ arguments[j] = temp
+ return arguments
+
+
+def group_by_type(dictionary):
+ type_dictionary = {}
+ for key in dictionary:
+ type_dictionary[type(key)] = {}
+ for key in dictionary:
+ type_dictionary[type(key)][key] = dictionary[key]
+ return type_dictionary
+
+
+def anagrams(words):
+ anagrams = []
+ for i in range(0, len(words)):
+ group_of_words = [words[i]]
+ for word in words[i+1:]:
+ if sorted(words[i]) == sorted(word):
+ words.remove(word)
+ group_of_words.append(word)
+ anagrams.append(group_of_words)
+ if len(words) - len(anagrams) == 0:
+ break
+ if len(words) - len(anagrams) == 1:
+ words.reverse()
+ anagrams.append([words[0]])
+ return anagrams
+ return anagrams

Емануел обнови решението на 19.03.2014 12:45 (преди над 10 години)

def is_pangram(sentence):
letters = ['А', 'Б', 'В', 'Г', 'Д', 'Е',
'Ж', 'З', 'И', 'Й', 'К', 'Л',
'М', 'Н', 'О', 'П', 'Р', 'С',
'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч',
'Ш', 'Щ', 'Ъ', 'Ь', 'Ю', 'Я']
for letter in letters:
if letter not in sentence.upper():
return False
return True
def char_histogram(text):
histogram = {}
for char in text:
- histogram[char] = 0
- for char in text:
- histogram[char] += 1
+ if char not in histogram:
+ histogram[char] = 1
+ else:
+ histogram[char] += 1
return histogram
def sort_by(func, arguments):
- for i in range(0, len(arguments)):
- for j in range(i+1, len(arguments)):
- if func(arguments[i], arguments[j]) > 0:
- temp = arguments[i]
- arguments[i] = arguments[j]
- arguments[j] = temp
- return arguments
+ sorted_arguments = arguments
+ for i in range(0, len(sorted_arguments) - 1):
+ for j in range(i+1, len(sorted_arguments)):
+ if func(sorted_arguments[i], sorted_arguments[j]) >= 0:
+ sorted_arguments[i], sorted_arguments[j] = \
+ sorted_arguments[j], sorted_arguments[i]
+ return sorted_arguments
def group_by_type(dictionary):
type_dictionary = {}
for key in dictionary:
- type_dictionary[type(key)] = {}
- for key in dictionary:
- type_dictionary[type(key)][key] = dictionary[key]
+ 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 = []
for i in range(0, len(words)):
group_of_words = [words[i]]
for word in words[i+1:]:
- if sorted(words[i]) == sorted(word):
+ if sorted(remove_not_letters(words[i])) == \
+ sorted(remove_not_letters(word)):
words.remove(word)
group_of_words.append(word)
anagrams.append(group_of_words)
if len(words) - len(anagrams) == 0:
break
if len(words) - len(anagrams) == 1:
words.reverse()
anagrams.append([words[0]])
return anagrams
- return anagrams
+ return anagrams
+
+
+def remove_not_letters(word):
+ word_of_letters = word
+ for char in word_of_letters:
+ if not char.isalpha():
+ word_of_letters = word_of_letters.replace(char, '')
+ return word_of_letters