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

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

Към профила на Ралица Великова

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 12 успешни тест(а)
  • 4 неуспешни тест(а)

Код

LETTERS = list('абвгдежзийклмнопрстуфхцчшщъьюя')
def is_pangram(sentence):
index = 0
missing_letter = False
while (index < len(LETTERS)) and not missing_letter:
if LETTERS[index] in list(sentence):
index += 1
else:
missing_letter = True
return not missing_letter
def char_histogram(text):
histogram = {}
for thing in text:
if thing in histogram:
histogram[thing] += 1
else:
histogram[thing] = 1
return histogram
def group_by_type(dictionary):
grouped = {}
for thing in dictionary:
if type(thing) in grouped:
grouped[type(thing)][thing] = dictionary[thing]
else:
grouped[type(thing)] = {thing: dictionary[thing]}
return grouped
def sort_by(func, argument):
is_sorted = []
while argument:
index_min = 0
for index in range(1, len(argument)):
if func(argument[index], argument[index_min]) > 0:
index_min = index
is_sorted.append(argument.pop(index_min))
return is_sorted
def sind_anagrams(first, second):
return set(first) == set(second)
def anagrams(words):
are_anagrams = []
while words:
word = words.pop()
concrete_anagrams = [word]
for other_word in list(words):
if sind_anagrams(other_word, word):
concrete_anagrams.append(other_word)
words.remove(other_word)
are_anagrams.append(concrete_anagrams)
return are_anagrams

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

.FF.........F..F
======================================================================
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-56y4dg/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-56y4dg/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({"So I'm cuter"})
frozenset({'I am Lord Voldemort'})
frozenset({'Tom Marvolo Riddle'})

======================================================================
FAIL: test_with_pangrams (test.TestIsPangram)
----------------------------------------------------------------------
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-56y4dg/test.py", line 15, in test_with_pangrams
    solution.is_pangram('За миг бях в чужд, скърцащ плюшен фотьойл.'))
AssertionError: False is not true

======================================================================
FAIL: test_sort_by_simple_test (test.TestSortBy)
----------------------------------------------------------------------
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-56y4dg/test.py", line 54, in test_sort_by_simple_test
    solution.sort_by(lambda x, y: x % 2 - y % 2, [0, 1, 2, 3, 4, 5]))
AssertionError: Lists differ: [0, 2, 4, 1, 3, 5] != [1, 3, 5, 0, 2, 4]

First differing element 0:
0
1

- [0, 2, 4, 1, 3, 5]
+ [1, 3, 5, 0, 2, 4]

----------------------------------------------------------------------
Ran 16 tests in 0.011s

FAILED (failures=4)

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

Ралица обнови решението на 19.03.2014 16:51 (преди над 10 години)

+LETTERS = list('абвгдежзийклмнопрстуфхцчшщъьюя')
+
+
+def is_pangram(sentence):
+ index = 0
+ missing_letter = False
+ while (index < len(LETTERS)) and not missing_letter:
+ if LETTERS[index] in list(sentence):
+ index += 1
+ else:
+ missing_letter = True
+ return not missing_letter
+
+
+def char_histogram(text):
+ histogram = {}
+ for thing in text:
+ if thing in histogram:
+ histogram[thing] += 1
+ else:
+ histogram[thing] = 1
+ return histogram
+
+
+def group_by_type(dictionary):
+ grouped = {}
+ for thing in dictionary:
+ if type(thing) in grouped:
+ grouped[type(thing)][thing] = dictionary[thing]
+ else:
+ grouped[type(thing)] = {thing: dictionary[thing]}
+ return grouped
+
+
+def sort_by(func, argument):
+ is_sorted = []
+ while argument:
+ index_min = 0
+ for index in range(1, len(argument)):
+ if func(argument[index], argument[index_min]) > 0:
+ index_min = index
+ is_sorted.append(argument.pop(index_min))
+ return is_sorted
+
+
+def sind_anagrams(first, second):
+ return set(first) == set(second)
+
+
+def anagrams(words):
+ are_anagrams = []
+ while words:
+ word = words.pop()
+ concrete_anagrams = [word]
+ for other_word in list(words):
+ if sind_anagrams(other_word, word):
+ concrete_anagrams.append(other_word)
+ words.remove(other_word)
+ are_anagrams.append(concrete_anagrams)
+ return are_anagrams