Решение на Пет функции от Петър Камбуров

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

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

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 14 успешни тест(а)
  • 2 неуспешни тест(а)

Код

from functools import cmp_to_key
def is_pangram(sentence):
BULGARIAN_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
return set(BULGARIAN_ALPHABET) <= set(sentence.lower())
def char_histogram(text):
histogram = {}
for unique_symbol in set(text):
histogram[unique_symbol] = text.count(unique_symbol)
return histogram
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
group_types = {}
for key, value in dictionary.items():
group_types.setdefault(type(key), {})[key] = value
return group_types
def anagrams(words):
group_anagrams = {}
for word in words:
sorted_letters = "".join(x.lower() for x in sorted(word)
if x.isalpha())
group_anagrams.setdefault(sorted_letters, []).append(word)
anagrams = []
for anagram in group_anagrams.values():
anagrams.append(anagram)
return anagrams

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

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

----------------------------------------------------------------------
Ran 16 tests in 0.009s

FAILED (failures=2)

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

Петър обнови решението на 19.03.2014 00:43 (преди над 10 години)

+from functools import cmp_to_key
+
+
+def is_pangram(sentence):
+ bulgarian_alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ return set(bulgarian_alphabet) <= set(sentence.lower())
+
+
+def char_histogram(text):
+ histogram = {}
+ for unique_symbol in set(text):
+ histogram[unique_symbol] = text.count(unique_symbol)
+ return histogram
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ group_types = {}
+ for key, value in dictionary.items():
+ group_types.setdefault(type(key), {key: value})
+ group_types[type(key)][key] = value
+ return group_types
+
+
+def anagrams(words):
+ group_anagrams = {}
+ for word in words:
+ sorted_letters = "".join(x.lower() for x in sorted(word)
+ if x.isalpha())
+ group_anagrams.setdefault(sorted_letters, [word])
+ group_anagrams[sorted_letters].append(word)
+ anagrams = []
+ for (x, y) in group_anagrams.items():
+ anagrams.append(y)
+ return anagrams

Петър обнови решението на 19.03.2014 09:13 (преди над 10 години)

from functools import cmp_to_key
def is_pangram(sentence):
bulgarian_alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
return set(bulgarian_alphabet) <= set(sentence.lower())
def char_histogram(text):
histogram = {}
for unique_symbol in set(text):
histogram[unique_symbol] = text.count(unique_symbol)
return histogram
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
group_types = {}
for key, value in dictionary.items():
group_types.setdefault(type(key), {key: value})
group_types[type(key)][key] = value
return group_types
def anagrams(words):
group_anagrams = {}
for word in words:
sorted_letters = "".join(x.lower() for x in sorted(word)
if x.isalpha())
group_anagrams.setdefault(sorted_letters, [word])
group_anagrams[sorted_letters].append(word)
anagrams = []
- for (x, y) in group_anagrams.items():
+ for x, y in group_anagrams.items():
anagrams.append(y)
return anagrams
  • Добра идея е bulgarian_alphabet да бъде изведено като константа
  • x, y не са добри имена за променливи, освен ако не става дума за координатна система, освен това x не се използва никъде
  • Прави ми впечатление, че използваш setdefault по усложнен начин.

      group_types.setdefault(type(key), {key: value})
      group_types[type(key)][key] = value
    

Може да стане:

    group_types.setdefault(type(key), {})[key] = value

А това:

    group_anagrams.setdefault(sorted_letters, [word])
    group_anagrams[sorted_letters].append(word)

Става на:

    group_anagrams.setdefault(sorted_letters, []).append(word)

Петър обнови решението на 19.03.2014 15:42 (преди над 10 години)

from functools import cmp_to_key
def is_pangram(sentence):
- bulgarian_alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
- return set(bulgarian_alphabet) <= set(sentence.lower())
+ BULGARIAN_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ return set(BULGARIAN_ALPHABET) <= set(sentence.lower())
def char_histogram(text):
histogram = {}
for unique_symbol in set(text):
histogram[unique_symbol] = text.count(unique_symbol)
return histogram
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
group_types = {}
for key, value in dictionary.items():
- group_types.setdefault(type(key), {key: value})
- group_types[type(key)][key] = value
+ group_types.setdefault(type(key), {})[key] = value
return group_types
def anagrams(words):
group_anagrams = {}
for word in words:
sorted_letters = "".join(x.lower() for x in sorted(word)
if x.isalpha())
- group_anagrams.setdefault(sorted_letters, [word])
- group_anagrams[sorted_letters].append(word)
+ group_anagrams.setdefault(sorted_letters, []).append(word)
anagrams = []
- for x, y in group_anagrams.items():
- anagrams.append(y)
+ for anagram in group_anagrams.values():
+ anagrams.append(anagram)
return anagrams