Решение на Пет функции от Йордан Данчев

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

Към профила на Йордан Данчев

Резултати

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

Код

#1
import sys
alphabet = ['а', 'б', 'в','г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м',
'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ','ъ','ь','ю','я']
def isPangram(string):
dictionary = {}
for letter in string.lower():
if letter not in dictionary:
if letter in alphabet:
dictionary[letter] = 1
else:
dictionary[letter] += 1
for letter in alphabet:
if letter not in dictionary:
return False
return True
print(isPangram("Ах, чудна българска земьо, полюшвай цъфтящи жита!"))
#5
def anagrams(words):
anagrams={}
for i in words:
k=''.join(sorted(i))
anagrams.setdefault(k,[])
anagrams[k].append(i)
return anagrams.values()
print(anagrams(["army","mary","ramy","astronomer","moonstarer","debit card","bad credit","bau"]))
#2
def char_histogram(text):
dict={}
for i in text:
if i not in dict.keys():
dict[i]=1
else:
dict[i]+=1
return(dict)
print(char_histogram("This is an example!"))
#3
def sort_by(func,argument):
return sorter(func,key=len)
print(sorted("abc, a, ab".split(), key=str.lower))

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

True
dict_values([['debit card', 'bad credit'], ['bau'], ['astronomer', 'moonstarer'], ['army', 'mary', 'ramy']])
{'x': 1, 's': 2, 'p': 1, 'T': 1, 'i': 2, 'h': 1, 'n': 1, 'm': 1, 'l': 1, 'a': 2, ' ': 3, '!': 1, 'e': 2}
['a,', 'ab', 'abc,']
.FF...EEEEEEEEEE
======================================================================
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-138b7fa/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-138b7fa/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-138b7fa/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-138b7fa/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-138b7fa/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_with_other_sentences (test.TestIsPangram)
----------------------------------------------------------------------
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-138b7fa/test.py", line 19, in test_with_other_sentences
    solution.is_pangram('Малката пухкава панда яде бамбук.'))
AttributeError: 'module' object has no attribute 'is_pangram'

======================================================================
ERROR: test_with_pangrams (test.TestIsPangram)
----------------------------------------------------------------------
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-138b7fa/test.py", line 11, in test_with_pangrams
    solution.is_pangram(
AttributeError: 'module' object has no attribute 'is_pangram'

======================================================================
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-138b7fa/test.py", line 44, in test_sort_by_empty
    self.assertEqual([], solution.sort_by(lambda x, y: x - y, []))
  File "/tmp/d20140319-21201-138b7fa/solution.py", line 59, in sort_by
    return sorter(func,key=len)
NameError: global name 'sorter' is not defined

======================================================================
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-138b7fa/test.py", line 57, in test_sort_by_one_element
    self.assertEqual([1], solution.sort_by(lambda x, y: x - y, [1]))
  File "/tmp/d20140319-21201-138b7fa/solution.py", line 59, in sort_by
    return sorter(func,key=len)
NameError: global name 'sorter' is not defined

======================================================================
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-138b7fa/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]))
  File "/tmp/d20140319-21201-138b7fa/solution.py", line 59, in sort_by
    return sorter(func,key=len)
NameError: global name 'sorter' is not defined

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

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

FAILED (failures=2, errors=10)

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

Йордан обнови решението на 19.03.2014 16:10 (преди около 10 години)

+#1
+
+import sys
+
+alphabet = ['а', 'б', 'в','г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м',
+ 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ','ъ','ь','ю','я']
+
+def isPangram(string):
+
+ dictionary = {}
+ for letter in string.lower():
+ if letter not in dictionary:
+ if letter in alphabet:
+ dictionary[letter] = 1
+ else:
+ dictionary[letter] += 1
+
+ for letter in alphabet:
+ if letter not in dictionary:
+ return False
+
+ return True
+print(isPangram("Ах, чудна българска земьо, полюшвай цъфтящи жита!"))
+
+
+#5
+
+def anagrams(words):
+ anagrams={}
+ for i in words:
+ k=''.join(sorted(i))
+ anagrams.setdefault(k,[])
+ anagrams[k].append(i)
+
+ return anagrams.values()
+print(anagrams(["army","mary","ramy","astronomer","moonstarer","debit card","bad credit","bau"]))
+
+
+
+#2
+
+def char_histogram(text):
+
+ dict={}
+ for i in text:
+ if i not in dict.keys():
+ dict[i]=1
+ else:
+ dict[i]+=1
+ return(dict)
+print(char_histogram("This is an example!"))
+
+
+
+
+#3
+
+def sort_by(func,argument):
+ return sorter(func,key=len)
+print(sorted("abc, a, ab".split(), key=str.lower))