Решение на Пет функции от Веляна Димова

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

Към профила на Веляна Димова

Резултати

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

Код

from functools import cmp_to_key
def is_pangram(sentence):
letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for letter in letters:
if not letter in sentence.lower():
return False
return True
def char_histogram(text):
result = {}
for symbol in text:
result[str(symbol)] = text.count(symbol)
return result
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
result = {}
for key in dictionary:
if not type(key) in result:
result[type(key)] = {key: dictionary[key]}
else:
result[type(key)][key] = dictionary[key]
return result
def is_anagram(word1, word2):
return sorted(list(word1)) == sorted(list(word2))
def word_anagram(word, words_list):
result = []
for second_word in words_list:
if is_anagram(word, second_word):
result.append(second_word)
return result
def anagrams(words):
result = []
for word in words:
if not word_anagram(word, words) in result:
result.append(word_anagram(word, words))
return result

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

.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-7mkuo3/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-7mkuo3/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'})

----------------------------------------------------------------------
Ran 16 tests in 0.015s

FAILED (failures=2)

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

Веляна обнови решението на 19.03.2014 00:20 (преди над 10 години)

+from functools import cmp_to_key
+
+def is_pangram(sentence):
+ letter_list = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ for letter in letter_list:
+ if not letter in sentence.lower():
+ return False
+ return True
+
+def char_histogram(text):
+ symbol_dict = {}
+ for symbol in text:
+ symbol_dict[str(symbol)] = text.count(symbol)
+
+ return symbol_dict
+
+def sort_by(func, arguments):
+ return sorted(arguments, key = cmp_to_key(func))
+
+def group_by_type(dictionary):
+ result = {}
+ for key in dictionary:
+ if not type(key) in result:
+ result[type(key)] = {key:dictionary[key]}
+ else:
+ result[type(key)][key] = dictionary[key]
+ return result
+
+def is_anagram(word1, word2):
+ return (sorted(list(word1)) == sorted(list(word2)))
+
+def word_anagram(word, words_list):
+ result = []
+ for word2 in words_list:
+ if is_anagram(word, word2):
+ result.append(word2)
+ return result
+
+def anagrams(words):
+ result = []
+ for word in words:
+ if not word_anagram(word, words) in result:
+ result.append(word_anagram(word, words))
+ return result

Веляна обнови решението на 19.03.2014 09:36 (преди над 10 години)

from functools import cmp_to_key
+
def is_pangram(sentence):
letter_list = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for letter in letter_list:
if not letter in sentence.lower():
return False
return True
+
def char_histogram(text):
symbol_dict = {}
for symbol in text:
symbol_dict[str(symbol)] = text.count(symbol)
-
return symbol_dict
+
def sort_by(func, arguments):
- return sorted(arguments, key = cmp_to_key(func))
+ return sorted(arguments, key=cmp_to_key(func))
+
def group_by_type(dictionary):
result = {}
for key in dictionary:
if not type(key) in result:
- result[type(key)] = {key:dictionary[key]}
+ result[type(key)] = {key: dictionary[key]}
else:
result[type(key)][key] = dictionary[key]
return result
+
def is_anagram(word1, word2):
return (sorted(list(word1)) == sorted(list(word2)))
+
def word_anagram(word, words_list):
result = []
for word2 in words_list:
if is_anagram(word, word2):
result.append(word2)
return result
+
def anagrams(words):
result = []
for word in words:
if not word_anagram(word, words) in result:
result.append(word_anagram(word, words))
- return result
+ return result
  • letter_list не е list, това е объркващо
  • не трябва типа на обекта да присъства в името на променливите: words_list, letter_list, symbol_dict
  • word2 не е добър избор за име на променлива

      if not type(key) in result:
          result[type(key)] = {key: dictionary[key]}
      else:
          result[type(key)][key] = dictionary[key]
    

Разгледай за defaultdict.

return (sorted(list(word1)) == sorted(list(word2))) - скобите в израза са илишни

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

from functools import cmp_to_key
def is_pangram(sentence):
- letter_list = 'абвгдежзийклмнопрстуфхцчшщъьюя'
- for letter in letter_list:
+ letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ for letter in letters:
if not letter in sentence.lower():
return False
return True
def char_histogram(text):
- symbol_dict = {}
+ result = {}
for symbol in text:
- symbol_dict[str(symbol)] = text.count(symbol)
- return symbol_dict
+ result[str(symbol)] = text.count(symbol)
+ return result
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
result = {}
for key in dictionary:
if not type(key) in result:
result[type(key)] = {key: dictionary[key]}
else:
result[type(key)][key] = dictionary[key]
return result
def is_anagram(word1, word2):
- return (sorted(list(word1)) == sorted(list(word2)))
+ return sorted(list(word1)) == sorted(list(word2))
def word_anagram(word, words_list):
result = []
- for word2 in words_list:
- if is_anagram(word, word2):
- result.append(word2)
+ for second_word in words_list:
+ if is_anagram(word, second_word):
+ result.append(second_word)
return result
def anagrams(words):
result = []
for word in words:
if not word_anagram(word, words) in result:
result.append(word_anagram(word, words))
return result