Решение на Пет функции от Петър Лалов

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

Към профила на Петър Лалов

Резултати

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

Код

def is_pangram(sentence):
alfabet_set = set(u'абвгдежзийклмнопрстуфхцчшщъьюя')
subset = alfabet_set & set(sentence.lower())
return subset == alfabet_set
def char_histogram(string):
result = {}
for char in string:
if char in result.keys():
result[char] = result[char] + 1
else:
result[char] = 1
return result
def sort_by(func, arguments):
for i in range(0, len(arguments) - 1):
if func(arguments[i], arguments[i+1]) > 0:
arguments[i], arguments[i+1] = arguments[i+1], arguments[i]
return arguments
def group_by_type(dictionary):
result = {}
for (key, value) in dictionary.items():
if result.get(type(key)) != None:
result[type(key)][key] = value
else:
result[type(key)] = {key : value}
return result
def anagrams(words):
result = {}
for word in words:
hash_ = hash(frozenset(word))
if result.get(hash_) != None:
result[hash_].append(word)
else:
result[hash_] = [word]
return result.values()

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

.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-ncf5w8/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-ncf5w8/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({"So I'm cuter"})
frozenset({'I am Lord Voldemort'})
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-ncf5w8/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, 1, 4, 3, 5]

First differing element 2:
4
1

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

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


----------------------------------------------------------------------
Ran 16 tests in 0.010s

FAILED (failures=3)

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

Петър обнови решението на 14.03.2014 11:13 (преди почти 11 години)

+def is_pangram(sentence):
+ alfabet_set = set(u'абвгдежзийклмнопрстуфхцчшщъьюя')
+ subset = alfabet_set & set(sentence.lower())
+ return subset == alfabet_set
+
+def char_histogram(string):
+ result = {}
+ for char in string:
+ if char in result.keys():
+ result[char] = result[char] + 1
+ else:
+ result[char] = 1
+ return result
+
+def sort_by(func, arguments):
+ for i in range(0, len(arguments) - 1):
+ if func(arguments[i], arguments[i+1]) > 0:
+ arguments[i], arguments[i+1] = arguments[i+1], arguments[i]
+ return arguments
+
+def group_by_type(dictionary):
+ result = {}
+ for (key, value) in dictionary.items():
+ if result.has_key(type(key)):
+ result[type(key)][key] = value
+ else:
+ result[type(key)] = {key : value}
+ return result
+
+def anagrams(words):
+ result = {}
+ for word in words:
+ hash_ = hash(frozenset(word))
+ if result.has_key(hash_):
+ result[hash_].append(word)
+ else:
+ result[hash_] = [word]
+ return result.values()

Петър обнови решението на 17.03.2014 11:20 (преди почти 11 години)

def is_pangram(sentence):
alfabet_set = set(u'абвгдежзийклмнопрстуфхцчшщъьюя')
subset = alfabet_set & set(sentence.lower())
return subset == alfabet_set
def char_histogram(string):
result = {}
for char in string:
if char in result.keys():
result[char] = result[char] + 1
else:
result[char] = 1
return result
def sort_by(func, arguments):
for i in range(0, len(arguments) - 1):
if func(arguments[i], arguments[i+1]) > 0:
arguments[i], arguments[i+1] = arguments[i+1], arguments[i]
return arguments
def group_by_type(dictionary):
result = {}
for (key, value) in dictionary.items():
- if result.has_key(type(key)):
+ if result.get(type(key)) != None:
result[type(key)][key] = value
else:
result[type(key)] = {key : value}
return result
def anagrams(words):
result = {}
for word in words:
hash_ = hash(frozenset(word))
- if result.has_key(hash_):
+ if result.get(hash_) != None:
result[hash_].append(word)
else:
result[hash_] = [word]
return result.values()
+

Анаграма е дума или фраза образувана от буквите на друга дума или фраза, чрез пермутация.

  • Твоето решение не прави разлика между буква и символ
  • Проверката ти в sort_by изпуска няколко случая. Помисли как да я генерализираш