Решение на Пет функции от Радослав Георгиев

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

Към профила на Радослав Георгиев

Резултати

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

Код

def is_pangram(sentence):
UPPER_LETTERS = 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ'
for letter in UPPER_LETTERS:
if sentence.find(letter) == -1 and sentence.find(letter.lower()) == -1:
return False
return True
def char_histogram(text):
histogram = dict()
for letter in text:
histogram[letter] = 0
for letter in text:
histogram[letter] += 1
return histogram
def sort_by(func, arguments):
for i in range(0, len(arguments) - 1):
extri = i
for j in range(i + 1, len(arguments)):
if func(arguments[extri], arguments[j]) > 0:
extri = j
if not extri == i:
arguments[extri], arguments[j] = arguments[j], arguments[extri]
arguments.reverse()
return arguments
def group_by_type(dictionary):
new_dict = dict()
for elem in dictionary:
new_dict[type(elem)] = dict()
for elem in dictionary:
new_dict[type(elem)][elem] = dictionary[elem]
return new_dict
def anagrams(words):
anagrams = []
anagram_sets = []
for word1 in set(words).difference(anagram_sets):
if not word1 in anagram_sets:
anagrams_word = [word1,]
anagram_sets.append(word1)
for word2 in set(words).difference(anagram_sets):
sorted_word1 = list(word1)
sorted_word1.sort()
sorted_word2 = list(word2)
sorted_word2.sort()
if not word1 is word2 and sorted_word1 == sorted_word2:
anagrams_word.append(word2)
anagram_sets.append(word2)
anagrams.append(anagrams_word)
return anagrams

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

.FF............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-nmn58v/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-nmn58v/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"})

======================================================================
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-nmn58v/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] != [2, 4, 3, 5, 1, 0]

First differing element 0:
0
2

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

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

FAILED (failures=3)

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

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

+def is_pangram(sentence):
+ UPPER_LETTERS = 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ'
+
+ for letter in UPPER_LETTERS:
+ if sentence.find(letter) == -1 and sentence.find(letter.lower()) == -1:
+ return False
+
+ return True
+
+def char_histogram(text):
+ histogram = dict()
+
+ for letter in text:
+ histogram[letter] = 0
+ for letter in text:
+ histogram[letter] += 1
+
+ return histogram
+
+def sort_by(func, arguments):
+ for i in range(0, len(arguments) - 1):
+ extri = i
+
+ for j in range(i + 1, len(arguments)):
+ if func(arguments[extri], arguments[j]) > 0:
+ extri = j
+
+ if not extri == i:
+ arguments[extri], arguments[j] = arguments[j], arguments[extri]
+
+ arguments.reverse()
+ return arguments
+
+def group_by_type(dictionary):
+ new_dict = dict()
+
+ for elem in dictionary:
+ new_dict[type(elem)] = dict()
+ for elem in dictionary:
+ new_dict[type(elem)][elem] = dictionary[elem]
+
+ return new_dict
+
+def anagrams(words):
+ anagrams = []
+ anagram_sets = []
+
+ for word1 in set(words).difference(anagram_sets):
+ if not word1 in anagram_sets:
+ anagrams_word = [word1,]
+ anagram_sets.append(word1)
+
+ for word2 in set(words).difference(anagram_sets):
+ sorted_word1 = list(word1)
+ sorted_word1.sort()
+ sorted_word2 = list(word2)
+ sorted_word2.sort()
+ if not word1 is word2 and sorted_word1 == sorted_word2:
+ anagrams_word.append(word2)
+ anagram_sets.append(word2)
+
+ anagrams.append(anagrams_word)
+
+ return anagrams