Решение на Пет функции от Даяна Маринова

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

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

Резултати

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

Код

def is_pangram(sentence):
sentence = sentence.lower()
char_in_sentence = []
for char in sentence:
if char not in char_in_sentence and char.isalpha():
char_in_sentence.append(char)
if len(char_in_sentence) == 30:
return True
return False
def char_histogram(sentence):
char_in_sentence = {}
for char in sentence:
if char not in char_in_sentence:
char_in_sentence[char] = 1
else:
char_in_sentence[char] += 1
return char_in_sentence
import functools
def sort_by(func, arguments):
result = sorted(arguments, key=functools.cmp_to_key(func))
return result
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)].update({key: dictionary[key]})
return result
def anagrams(words):
d = {}
result = []
for item in words:
if "".join(sorted(item)) not in d:
d["".join(sorted(item))] = [item]
else:
d["".join(sorted(item))].append(item)
for key in d:
result.append(d[key])
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-fwhvdk/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-fwhvdk/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({"So I'm cuter", 'Tom Cruise'})
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.010s

FAILED (failures=2)

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

Даяна обнови решението на 19.03.2014 16:56 (преди над 10 години)

+def is_pangram(sentence):
+ sentence = sentence.lower()
+ char_in_sentence = []
+ for char in sentence:
+ if char not in char_in_sentence and char.isalpha():
+ char_in_sentence.append(char)
+ if len(char_in_sentence) == 30:
+ return True
+ return False
+
+
+def char_histogram(sentence):
+ char_in_sentence = {}
+ for char in sentence:
+ if char not in char_in_sentence:
+ char_in_sentence[char] = 1
+ else:
+ char_in_sentence[char] += 1
+ return char_in_sentence
+
+
+import functools
+
+
+def sort_by(func, arguments):
+ result = sorted(arguments, key=functools.cmp_to_key(func))
+ return result
+
+
+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)].update({key: dictionary[key]})
+ return result
+
+
+def anagrams(words):
+ d = {}
+ result = []
+ for item in words:
+ if "".join(sorted(item)) not in d:
+ d["".join(sorted(item))] = [item]
+ else:
+ d["".join(sorted(item))].append(item)
+ for key in d:
+ result.append(d[key])
+ return result