Решение на Пет функции от Стефан Колчев

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

Към профила на Стефан Колчев

Резултати

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

Код

from collections import defaultdict
def is_pangram(sentence):
letters = dict()
sentence_to_lower = sentence.lower()
for letter in range(1072, 1104):
letters[chr(letter)] = False
del letters[chr(1099)]
del letters[chr(1101)]
for letter in sentence_to_lower:
if ord(letter) >= 1072 and ord(letter) <= 1103 and not letters[letter]:
letters[letter] = True
for value in letters.values():
if not value:
return False
return True
def char_histogram(text):
return {letter: text.count(letter) for letter in text}
def sort_by(func, arguments):
for index in range(0, len(arguments) - 1):
for other_index in range(index, len(arguments)):
if func(arguments[index], arguments[other_index]) > 0:
temp = arguments[index]
arguments[index] = arguments[other_index]
arguments[other_index] = temp
return arguments
def group_by_type(dictionary):
sorted_by_type = defaultdict(dict)
for key, value in dictionary.items():
sorted_by_type[type(key)].update({key: value})
return sorted_by_type
def anagrams(words):
sorted_words = defaultdict(list)
for word in words:
sorted_words[' '.join(sorted(word.lower()))].append(word)
return [value for key, value in sorted_words.items()]

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

..F............F
======================================================================
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-1ceapd7/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({"So I'm cuter", 'Tom Cruise'})
frozenset({'Tom Marvolo Riddle', 'I am Lord Voldemort'})
Items in the second set but not the first:
frozenset({'Tom Cruise'})
frozenset({'Tom Marvolo Riddle'})
frozenset({"So I'm cuter"})
frozenset({'I am Lord Voldemort'})

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

First differing element 3:
1
3

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

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


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

FAILED (failures=2)

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

Стефан обнови решението на 19.03.2014 16:30 (преди над 10 години)

+from collections import defaultdict
+
+
+def is_pangram(sentence):
+ letters = dict()
+ sentence_to_lower = sentence.lower()
+ for letter in range(1072, 1104):
+ letters[chr(letter)] = False
+ del letters[chr(1099)]
+ del letters[chr(1101)]
+
+ for letter in sentence_to_lower:
+ if ord(letter) >= 1072 and ord(letter) <= 1103 and not letters[letter]:
+ letters[letter] = True
+
+ for value in letters.values():
+ if not value:
+ return False
+
+ return True
+
+
+def char_histogram(text):
+ return {letter: text.count(letter) for letter in text}
+
+
+def sort_by(func, arguments):
+ for index in range(0, len(arguments)):
+ for other_index in range(index, len(arguments)):
+ if func(arguments[index], arguments[other_index]) > 0:
+ temp = arguments[index]
+ arguments[index] = arguments[other_index]
+ arguments[other_index] = temp
+
+ return arguments
+
+
+def group_by_type(dictionary):
+ sorted_by_type = defaultdict(dict)
+ for key, value in dictionary.items():
+ sorted_by_type[type(key)].update({key: value})
+
+ return sorted_by_type
+
+
+def anagrams(words):
+ sorted_words = defaultdict(list)
+ for word in words:
+ sorted_words[' '.join(sorted(word.lower()))].append(word)
+
+ return [value for key, value in sorted_words.items()]

Стефан обнови решението на 19.03.2014 16:39 (преди над 10 години)

from collections import defaultdict
def is_pangram(sentence):
letters = dict()
sentence_to_lower = sentence.lower()
for letter in range(1072, 1104):
letters[chr(letter)] = False
del letters[chr(1099)]
del letters[chr(1101)]
for letter in sentence_to_lower:
if ord(letter) >= 1072 and ord(letter) <= 1103 and not letters[letter]:
letters[letter] = True
for value in letters.values():
if not value:
return False
return True
def char_histogram(text):
return {letter: text.count(letter) for letter in text}
def sort_by(func, arguments):
- for index in range(0, len(arguments)):
+ for index in range(0, len(arguments) - 1):
for other_index in range(index, len(arguments)):
if func(arguments[index], arguments[other_index]) > 0:
temp = arguments[index]
arguments[index] = arguments[other_index]
arguments[other_index] = temp
return arguments
def group_by_type(dictionary):
sorted_by_type = defaultdict(dict)
for key, value in dictionary.items():
sorted_by_type[type(key)].update({key: value})
return sorted_by_type
def anagrams(words):
sorted_words = defaultdict(list)
for word in words:
sorted_words[' '.join(sorted(word.lower()))].append(word)
return [value for key, value in sorted_words.items()]