Решение на Пет функции от Ивайло Бъчваров

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

Към профила на Ивайло Бъчваров

Резултати

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

Код

from functools import cmp_to_key
def is_pangram(sentence):
ALPHABET_LEN = 30
sentence = sentence.lower()
count_chars = {}
for char in sentence:
if char.isalpha():
if char in count_chars:
count_chars[char] += 1
else:
count_chars[char] = 1
return len(count_chars) == ALPHABET_LEN
def char_histogram(sentence):
count_chars = {}
for char in sentence:
if char in count_chars:
count_chars[char] += 1
else:
count_chars[char] = 1
return count_chars
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
gruped_by_type = {}
for key, value in dictionary.items():
key_type = type(key)
if key_type in gruped_by_type:
gruped_by_type[key_type].update({key: value})
else:
gruped_by_type[key_type] = {key: value}
return gruped_by_type
def isAnagram(word1, word2):
word1_chars = list(word1)
word1_chars.sort()
word2_chars = list(word2)
word2_chars.sort()
return word1_chars == word2_chars
def anagrams(words):
anagrams = [[]]
for word in words:
word_added = False
for anagram_list in anagrams:
if not len(anagram_list):
anagram_list.append(word)
word_added = True
elif isAnagram(anagram_list[0], word):
anagram_list.append(word)
word_added = True
break
if not word_added:
new_anagrams = []
new_anagrams.append(word)
anagrams.append(new_anagrams)
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-wmrqk9/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-wmrqk9/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({"So I'm cuter", 'Tom Cruise'})
Items in the second set but not the first:
frozenset({'Tom Cruise'})
frozenset({'Tom Marvolo Riddle'})
frozenset({'I am Lord Voldemort'})
frozenset({"So I'm cuter"})

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

FAILED (failures=2)

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

Ивайло обнови решението на 16.03.2014 18:56 (преди около 10 години)

+from functools import cmp_to_key
+
+
+def is_pangram(sentence):
+ sentence = sentence.lower()
+ count_chars = {}
+ for char in sentence:
+ if char.isalpha():
+ if char in count_chars:
+ count_chars[char] += 1
+ else:
+ count_chars[char] = 1
+
+ return len(count_chars) == 30
+
+
+def char_histogram(sentence):
+ count_chars = {}
+ for char in sentence:
+ if char in count_chars:
+ count_chars[char] += 1
+ else:
+ count_chars[char] = 1
+
+ return count_chars
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ gruped_by_type = {}
+ for key, value in dictionary.items():
+ key_type = type(key)
+ if key_type in gruped_by_type:
+ gruped_by_type[key_type].update({key: value})
+ else:
+ gruped_by_type[key_type] = {key: value}
+
+ return gruped_by_type
+
+
+def isAnagram(word1, word2):
+ word1_list = list(word1)
+ word1_list.sort()
+ word2_list = list(word2)
+ word2_list.sort()
+
+ return word1_list == word2_list
+
+
+def anagrams(words):
+ pass

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

from functools import cmp_to_key
def is_pangram(sentence):
+ ALPHABET_LEN = 30
+
sentence = sentence.lower()
count_chars = {}
for char in sentence:
if char.isalpha():
if char in count_chars:
count_chars[char] += 1
else:
count_chars[char] = 1
- return len(count_chars) == 30
+ return len(count_chars) == ALPHABET_LEN
def char_histogram(sentence):
count_chars = {}
for char in sentence:
if char in count_chars:
count_chars[char] += 1
else:
count_chars[char] = 1
return count_chars
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
gruped_by_type = {}
for key, value in dictionary.items():
key_type = type(key)
if key_type in gruped_by_type:
gruped_by_type[key_type].update({key: value})
else:
gruped_by_type[key_type] = {key: value}
return gruped_by_type
def isAnagram(word1, word2):
- word1_list = list(word1)
- word1_list.sort()
- word2_list = list(word2)
- word2_list.sort()
+ word1_chars = list(word1)
+ word1_chars.sort()
+ word2_chars = list(word2)
+ word2_chars.sort()
+ return word1_chars == word2_chars
- return word1_list == word2_list
-
def anagrams(words):
- pass
+ anagrams = [[]]
+
+ for word in words:
+ word_added = False
+ for anagram_list in anagrams:
+ if not len(anagram_list):
+ anagram_list.append(word)
+ word_added = True
+ elif isAnagram(anagram_list[0], word):
+ anagram_list.append(word)
+ word_added = True
+ break
+
+ if not word_added:
+ new_anagrams = []
+ new_anagrams.append(word)
+ anagrams.append(new_anagrams)
+
+ return anagrams