Решение на Пет функции от Иван Котов

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

Към профила на Иван Котов

Резултати

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

Код

def is_pangram(sentance):
lower_letters = sentance.lower()
cirilic = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for letter in lower_letters:
if letter not in cirilic:
lower_letters.replace(letter,'')
for letter in cirilic:
if letter not in lower_letters:
return False
return True
def char_histogram(text):
histogram = {}
for letter in text:
histogram[letter] = histogram.pop(letter,0) + 1
return histogram
from itertools import groupby
def anagrams(words):
return [list(anagrams) for key, anagrams in groupby(sorted(words, key = sorted), sorted)]
def sort_by(function, arguments):
for i in range(0, len(arguments)):
for j in range(i, len(arguments)):
if function(arguments[i], arguments[j]) > 0:
temp = arguments[i]
arguments[i] = arguments[j]
arguments[j] = temp
return arguments

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

.FF...EEEEE....F
======================================================================
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-58gcm9/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-58gcm9/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-58gcm9/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-58gcm9/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-58gcm9/test.py", line 90, in test_group_by_type_with_functions
    solution.group_by_type({
AttributeError: 'module' object has no attribute 'group_by_type'

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

======================================================================
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-58gcm9/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.016s

FAILED (failures=3, errors=5)

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

Иван обнови решението на 13.03.2014 17:11 (преди около 10 години)

+def is_pangram(string):
+ dictionary = {}
+ cirilic = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л','м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я',' ']
+ string = string.replace(',', '')
+ string = string.replace('.', '')
+ string = string.replace('?', '')
+ string = string.replace('!', '')
+ for letter in string.lower():
+ if letter not in dictionary:
+ if letter in cirilic:
+ dictionary[letter] = 1
+ else:
+ dictionary[letter] += 1
+ for letter in cirilic:
+ if letter not in dictionary:
+ return False
+ return True
+
  • Не индентираш с 4 спейса, това може да ти коства всички точки, които би изкарал от задачата.
  • Измисли по-хубав начин за пазене на всички букви, списъкът не е добра идея.
  • След като напишеш останалите функции си помисли за по-елегеантен начин за проверка от всички вложени цикли.

Иван обнови решението на 17.03.2014 15:08 (преди около 10 години)

def is_pangram(string):
- dictionary = {}
- cirilic = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л','м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я',' ']
- string = string.replace(',', '')
- string = string.replace('.', '')
- string = string.replace('?', '')
- string = string.replace('!', '')
- for letter in string.lower():
- if letter not in dictionary:
- if letter in cirilic:
- dictionary[letter] = 1
- else:
- dictionary[letter] += 1
- for letter in cirilic:
- if letter not in dictionary:
- return False
- return True
+ dictionary = {}
+ cirilic = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л','м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я',' ']
+ string = string.replace(',', '')
+ string = string.replace('.', '')
+ string = string.replace('?', '')
+ string = string.replace('!', '')
+ for letter in string.lower():
+ if letter not in dictionary:
+ if letter in cirilic:
+ dictionary[letter] = 1
+ else:
+ dictionary[letter] += 1
+ for letter in cirilic:
+ if letter not in dictionary:
+ return False
+ return True

Иван обнови решението на 17.03.2014 15:48 (преди около 10 години)

def is_pangram(string):
dictionary = {}
cirilic = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л','м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я',' ']
string = string.replace(',', '')
string = string.replace('.', '')
string = string.replace('?', '')
string = string.replace('!', '')
for letter in string.lower():
if letter not in dictionary:
if letter in cirilic:
dictionary[letter] = 1
else:
dictionary[letter] += 1
for letter in cirilic:
if letter not in dictionary:
return False
return True
+
+def char_histogram(my_string):
+ histogram = {}
+ for letter in my_string: histogram[letter] = histogram.pop(letter,0) + 1
+ return histogram

Измисли по-хубав начин за пазене на всички букви, списъкът не е добра идея.

Измисли и по-добър начин да не вземаш под внимание разните символи. И най-вече, измисли по-адекватно име от string на твоята променлива. Това my_string дори не искам да го коментирам. Говорили сме 4 пъти за тези имена.

Иван обнови решението на 18.03.2014 23:39 (преди около 10 години)

-def is_pangram(string):
- dictionary = {}
- cirilic = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л','м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я',' ']
- string = string.replace(',', '')
- string = string.replace('.', '')
- string = string.replace('?', '')
- string = string.replace('!', '')
- for letter in string.lower():
- if letter not in dictionary:
- if letter in cirilic:
- dictionary[letter] = 1
- else:
- dictionary[letter] += 1
+def is_pangram(sentance):
+ lower_letters = sentance.lower()
+ cirilic = 'абвгдежзийклмнопрстуфхцчшщъьюч'
+ for letter in lower_letters:
+ if letter not in cirilic:
+ lower_letters.replace(letter,'')
for letter in cirilic:
- if letter not in dictionary:
+ if letter not in lower_letters:
return False
return True
-def char_histogram(my_string):
+def char_histogram(text):
histogram = {}
- for letter in my_string: histogram[letter] = histogram.pop(letter,0) + 1
- return histogram
+ for letter in text:
+ histogram[letter] = histogram.pop(letter,0) + 1
+ return histogram
+
+
+
+def sort_by(function, arguments):
+ for i in range(0, len(arguments)):
+ for j in range(i, len(arguments)):
+ if function(arguments[i], arguments[j]) > 0:
+ temp = arguments[i]
+ arguments[i] = arguments[j]
+ arguments[j] = temp
+ return arguments

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

def is_pangram(sentance):
lower_letters = sentance.lower()
- cirilic = 'абвгдежзийклмнопрстуфхцчшщъьюч'
+ cirilic = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for letter in lower_letters:
if letter not in cirilic:
lower_letters.replace(letter,'')
for letter in cirilic:
if letter not in lower_letters:
return False
return True
def char_histogram(text):
histogram = {}
for letter in text:
histogram[letter] = histogram.pop(letter,0) + 1
return histogram
-
+from itertools import groupby
+def anagrams(words):
+ return [list(anagrams) for key, anagrams in groupby(sorted(words, key = sorted), sorted)]
def sort_by(function, arguments):
for i in range(0, len(arguments)):
for j in range(i, len(arguments)):
if function(arguments[i], arguments[j]) > 0:
temp = arguments[i]
arguments[i] = arguments[j]
arguments[j] = temp
return arguments