Решение на Пет функции от Момчил Анастасов

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

Към профила на Момчил Анастасов

Резултати

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

Код

def is_pangram(sentence):
sentence = sentence.lower()
counter = 0
alphabet = {
'а':0, 'б':0, 'в':0, 'г':0, 'д':0, 'е':0,
'ж':0, 'з':0, 'и':0, 'й':0, 'к':0, 'л':0,
'м':0, 'н':0, 'о':0, 'п':0, 'р':0, 'с':0,
'т':0, 'у':0, 'ф':0, 'х':0, 'ц':0, 'ч':0,
'ш':0, 'щ':0, 'ъ':0, 'ь':0, 'ю':0, 'я':0
}
for character in sentence:
for letter in alphabet:
if character == letter and alphabet[letter] == 0:
alphabet[letter] += 1
counter += 1
if counter != len(alphabet):
return False
else:
return True
def char_histogram(text):
chars = dict()
for character in text:
if character not in chars.keys():
chars.update({character:0})
chars[character] += 1
return chars

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

EEEE..EEEEE..EEE
======================================================================
ERROR: test_list_of_latin_anagrams (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-yin968/test.py", line 109, in test_list_of_latin_anagrams
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: 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-yin968/test.py", line 125, in test_with_different_cases
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: 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-yin968/test.py", line 135, in test_with_different_symbols
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: test_with_list_of_cyrilic_anagrams (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-yin968/test.py", line 117, in test_with_list_of_cyrilic_anagrams
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: test_another_group_by_type (test.TestGroupByType)
----------------------------------------------------------------------
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-yin968/test.py", line 74, in test_another_group_by_type
    solution.group_by_type({(1, 2): 12, ('a', 1): 1, 1: 'b', 'c': 15}))
AttributeError: 'module' object has no attribute 'group_by_type'

======================================================================
ERROR: test_group_by_type (test.TestGroupByType)
----------------------------------------------------------------------
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-yin968/test.py", line 68, in test_group_by_type
    solution.group_by_type({'a': 12, 'b': 1, 1: 'foo'}))
AttributeError: 'module' object has no attribute 'group_by_type'

======================================================================
ERROR: test_group_by_type_empty (test.TestGroupByType)
----------------------------------------------------------------------
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-yin968/test.py", line 63, in test_group_by_type_empty
    self.assertEqual({}, solution.group_by_type({}))
AttributeError: 'module' object has no attribute 'group_by_type'

======================================================================
ERROR: test_group_by_type_with_frozen_set_key (test.TestGroupByType)
----------------------------------------------------------------------
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-yin968/test.py", line 80, in test_group_by_type_with_frozen_set_key
    solution.group_by_type({(1, 2): 12, ('a', 1): 1, test_set: 15}))
AttributeError: 'module' object has no attribute 'group_by_type'

======================================================================
ERROR: test_group_by_type_with_functions (test.TestGroupByType)
----------------------------------------------------------------------
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-yin968/test.py", line 90, in test_group_by_type_with_functions
    solution.group_by_type({
AttributeError: 'module' object has no attribute 'group_by_type'

======================================================================
ERROR: test_sort_by_empty (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-yin968/test.py", line 44, in test_sort_by_empty
    self.assertEqual([], solution.sort_by(lambda x, y: x - y, []))
AttributeError: 'module' object has no attribute 'sort_by'

======================================================================
ERROR: test_sort_by_one_element (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-yin968/test.py", line 57, in test_sort_by_one_element
    self.assertEqual([1], solution.sort_by(lambda x, y: x - y, [1]))
AttributeError: 'module' object has no attribute 'sort_by'

======================================================================
ERROR: 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-yin968/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]))
AttributeError: 'module' object has no attribute 'sort_by'

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

FAILED (errors=12)

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

Момчил обнови решението на 18.03.2014 23:15 (преди над 10 години)

+def is_pangram(sentence):
+ counter = 0
+ alphabet_dict = {'а':0, 'б':0, 'в':0, 'г':0, 'д':0, 'е':0,
+ 'ж':0, 'з':0, 'и':0, 'й':0, 'к':0, 'л':0,
+ 'м':0, 'н':0, 'о':0, 'п':0, 'р':0, 'с':0,
+ 'т':0, 'у':0, 'ф':0, 'х':0, 'ц':0, 'ч':0,
+ 'ш':0, 'щ':0, 'ъ':0, 'ь':0, 'ю':0, 'я':0}
+
+ for letter in sentence:
+ for key in alphabet_dict:
+ if key == letter:
+ if alphabet_dict[key] == 0:
+ alphabet_dict[key] += 1
+ counter += 1
+
+ if counter != len(alphabet_dict):
+ return False
+ else:
+ return True
+
+def char_histogram(text):
+ char_dict = dict()
+
+ for letter in text:
+ if letter not in char_dict.keys():
+ char_dict.update({letter:1})
+ else:
+ char_dict[letter] += 1
+
+ return char_dict
  • не използвай типове, когато именуваш променливи (alphabet_dict, char_dict)
  • речникът в is_pangram не е подравнен спрямо PEP8
  • защо използваш речник в is_pangram, като никъде не използваш стойностите му, за да определиш дали изречението е панграм?
  • в хитринките можеш да намериш как да си подобриш char_histogram

Момчил обнови решението на 19.03.2014 15:07 (преди над 10 години)

def is_pangram(sentence):
+ sentence = sentence.lower()
counter = 0
- alphabet_dict = {'а':0, 'б':0, 'в':0, 'г':0, 'д':0, 'е':0,
- 'ж':0, 'з':0, 'и':0, 'й':0, 'к':0, 'л':0,
- 'м':0, 'н':0, 'о':0, 'п':0, 'р':0, 'с':0,
- 'т':0, 'у':0, 'ф':0, 'х':0, 'ц':0, 'ч':0,
- 'ш':0, 'щ':0, 'ъ':0, 'ь':0, 'ю':0, 'я':0}
+ alphabet = {
+ 'а':0, 'б':0, 'в':0, 'г':0, 'д':0, 'е':0,
+ 'ж':0, 'з':0, 'и':0, 'й':0, 'к':0, 'л':0,
+ 'м':0, 'н':0, 'о':0, 'п':0, 'р':0, 'с':0,
+ 'т':0, 'у':0, 'ф':0, 'х':0, 'ц':0, 'ч':0,
+ 'ш':0, 'щ':0, 'ъ':0, 'ь':0, 'ю':0, 'я':0
+ }
+
+ for character in sentence:
+ for letter in alphabet:
+ if character == letter and alphabet[letter] == 0:
+ alphabet[letter] += 1
+ counter += 1
- for letter in sentence:
- for key in alphabet_dict:
- if key == letter:
- if alphabet_dict[key] == 0:
- alphabet_dict[key] += 1
- counter += 1
-
- if counter != len(alphabet_dict):
+ if counter != len(alphabet):
return False
else:
return True
def char_histogram(text):
- char_dict = dict()
+ chars = dict()
- for letter in text:
- if letter not in char_dict.keys():
- char_dict.update({letter:1})
- else:
- char_dict[letter] += 1
+ for character in text:
+ if character not in chars.keys():
+ chars.update({character:0})
+ chars[character] += 1
- return char_dict
+ return chars