Решение на Пет функции от Ангел Ангелов

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

Към профила на Ангел Ангелов

Резултати

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

Код

ALPHABET_CYR = 'явертъуиопасдфгхйклзьцжбнмюч'
ALPHABET_LAT = 'qwertyuiopasdfghjklzxcvbnm'
def is_pangram(sentence):
return set(ALPHABET_CYR).issubset(set(sentence.lower()))
def char_histogram(text):
histogram = {}
for char in text:
if char in histogram:
histogram[char] += 1
else:
histogram[char] = 1
return histogram
def sort_by(func, arguments):
if len(arguments) <= 1:
return arguments
pivot = arguments.pop(0)
left = []
right = []
center = [pivot]
while arguments:
current_argument = arguments.pop(0)
coef = func(pivot, current_argument)
if coef > 0:
left.append(current_argument)
elif coef < 0:
right.append(current_argument)
else:
center.append(current_argument)
return sort_by(func, left) + center + sort_by(func, right)
def group_by_type(dictionary):
groups = {}
for key, value in dictionary.items():
if type(key) in groups:
groups[type(key)][key] = value
else:
groups[type(key)] = {key: value}
return groups
def word_letter_set(word):
return set(word).intersection(set(ALPHABET_CYR).union(set(ALPHABET_LAT)))
def anagrams(words):
classes = []
while words:
base_word = words.pop()
cur_class = [word for word in words
if word_letter_set(word) == word_letter_set(base_word)]
words = [word for word in words if word not in cur_class]
cur_class.append(base_word)
classes.append(cur_class)
return classes

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

.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-76tpuf/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-76tpuf/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({"So I'm cuter"})
frozenset({'Tom Marvolo Riddle'})
frozenset({'I am Lord Voldemort'})
frozenset({'Tom Cruise'})

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

FAILED (failures=2)

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

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

+ALPHABET_CYR = 'явертъуиопасдфгхйклзьцжбнмюч'
+ALPHABET_LAT = 'qwertyuiopasdfghjklzxcvbnm'
+
+
+def is_pangram(sentence):
+ return set(ALPHABET_CYR).issubset(set(sentence.lower()))
+
+
+def char_histogram(text):
+ histogram = {}
+ for char in text:
+ if char in histogram:
+ histogram[char] += 1
+ else:
+ histogram[char] = 1
+ return histogram
+
+
+def sort_by(func, arguments):
+ if len(arguments) <= 1:
+ return arguments
+ pivot = arguments.pop(0)
+ left = []
+ right = []
+ center = [pivot]
+ while arguments:
+ current_argument = arguments.pop(0)
+ coef = func(pivot, current_argument)
+ if coef > 0:
+ left.append(current_argument)
+ elif coef < 0:
+ right.append(current_argument)
+ else:
+ center.append(current_argument)
+ return sort_by(func, left) + center + sort_by(func, right)
+
+
+def group_by_type(dictionary):
+ groups = {}
+ for key, value in dictionary.items():
+ if type(key) in groups:
+ groups[type(key)][key] = value
+ else:
+ groups[type(key)] = {key: value}
+ return groups
+
+
+def word_letter_set(word):
+ return set(word).intersection(set(ALPHABET_CYR).union(set(ALPHABET_LAT)))
+
+
+def anagrams(words):
+ classes = []
+ while words:
+ base_word = words.pop()
+ cur_class = [word for word in words
+ if word_letter_set(word) == word_letter_set(base_word)]
+ words = [word for word in words if word not in cur_class]
+ cur_class.append(base_word)
+ classes.append(cur_class)
+ return classes