Решение на Пет функции от Михаил Панайотов

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

Към профила на Михаил Панайотов

Резултати

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

Код

# -*- coding: utf-8 -*-
def is_pangram(sentence):
letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
return set(letters) - set(sentence.lower()) == set([])
def char_histogram(text):
histogram = {}
for letter in text:
letter_counter = 0
for i in range(len(text)):
if letter == text[i]:
letter_counter+=1
histogram[letter] = letter_counter
return histogram
import functools
def sort_by(func, arguments):
return sorted(arguments, key=functools.cmp_to_key(func))
def ExtractAlphanumeric(InputString):
from string import ascii_letters, digits
return "".join([ch for ch in InputString if ch in (ascii_letters + digits)])
def anagrams(words):
result = []
visited = []
for i in words:
anagram_words = []
for j in words:
if sum(map(ord, ExtractAlphanumeric(i.lower()))) == sum(map(ord, ExtractAlphanumeric(j.lower()))):
anagram_words.append(j)
if sorted(anagram_words) not in visited:
result.append(sorted(anagram_words))
visited.append(anagram_words)
unique = []
[unique.append(item) for item in result if item not in unique]
return unique

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

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

----------------------------------------------------------------------
Ran 16 tests in 0.017s

FAILED (errors=5)

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

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

+# -*- coding: utf-8 -*-
+def is_pangram(sentence):
+ letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ return set(letters) - set(sentence.lower()) == set([])
+
+def char_histogram(text):
+ histogram = {}
+ for letter in text:
+ letter_counter = 0
+ for i in range(len(text)):
+ if letter == text[i]:
+ letter_counter+=1
+ histogram[letter] = letter_counter
+ return histogram
+
+def sort_by(func, arguments):
+ arguments = sorted(arguments, func)
+ return arguments
+
+def anagrams(words):
+ sorted_words = [''.join(sorted(word)) for word in words]
+ result = []
+ visited = []
+ for word in sorted_words:
+ anagram_words = []
+ i = -1
+ for word2 in sorted_words:
+ i+=1
+ if word not in visited:
+ if word == word2:
+ anagram_words.append(words[i])
+ if anagram_words != []:
+ result.append(anagram_words)
+ visited.append(word)
+ return result
  • Буква и символ са две различни неща, по тази причина решението ти за anagrams не е коректно.
  • Помисли си за по-прост начин да изкажеш условието две фрази да са анаграми една на друга.
  • Разгледай внимателно как се чупи примерния тест върху твоето решение.
  • set няма нужда от аргумент, ако е празен
  • Вложените цикли в char_histogram могат да се изразат доста по-приятно с друга конструкция в python, която сме показвали.

Михаил обнови решението на 17.03.2014 21:50 (преди около 10 години)

# -*- coding: utf-8 -*-
def is_pangram(sentence):
letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
return set(letters) - set(sentence.lower()) == set([])
def char_histogram(text):
histogram = {}
for letter in text:
letter_counter = 0
for i in range(len(text)):
if letter == text[i]:
letter_counter+=1
histogram[letter] = letter_counter
return histogram
+import functools
def sort_by(func, arguments):
- arguments = sorted(arguments, func)
- return arguments
+ return sorted(arguments, key=functools.cmp_to_key(func))
+def ExtractAlphanumeric(InputString):
+ from string import ascii_letters, digits
+ return "".join([ch for ch in InputString if ch in (ascii_letters + digits)])
+
def anagrams(words):
- sorted_words = [''.join(sorted(word)) for word in words]
result = []
visited = []
- for word in sorted_words:
+ for i in words:
anagram_words = []
- i = -1
- for word2 in sorted_words:
+ for j in words:
- i+=1
+ if sum(map(ord, ExtractAlphanumeric(i.lower()))) == sum(map(ord, ExtractAlphanumeric(j.lower()))):
- if word not in visited:
+ anagram_words.append(j)
- if word == word2:
+ if sorted(anagram_words) not in visited:
- anagram_words.append(words[i])
+ result.append(sorted(anagram_words))
- if anagram_words != []:
+ visited.append(anagram_words)
- result.append(anagram_words)
+ unique = []
- visited.append(word)
+ [unique.append(item) for item in result if item not in unique]
- return result
+ return unique