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

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

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

Резултати

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

Код

from itertools import groupby
def is_pangram(sentence):
alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for i in range(0, len(sentence)):
if sentence[i].lower() in alphabet:
alphabet = alphabet.replace(sentence[i].lower(), '')
return alphabet == ''
def char_histogram(text):
result = dict()
for i in range(0, len(text)):
if text[i] in result:
result[text[i]] += 1
else:
result.update({text[i]: 1})
return result
def sort_by(func, arguments):
for i in range(0, len(arguments)):
for j in range(0, len(arguments)):
if func(arguments[i], arguments[j]) < 1:
argument = arguments[i]
arguments[i] = arguments[j]
arguments[j] = argument
return arguments
def group_by_type(dictionary):
types = []
result = dict()
for key in dictionary:
if type(key) not in types:
types.append(type(key))
for i in range(0, len(types)):
group = dict()
for j in dictionary:
if types[i] == type(j) and types[i] not in result:
group.update({j: dictionary.get(j)})
result.update({types[i]: group})
return result
def anagrams(words):
return ([list(result) for key, result in
groupby(sorted(words, key=sorted), sorted)])

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

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

First differing element 0:
0
4

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

----------------------------------------------------------------------
Ran 16 tests in 0.015s

FAILED (failures=3)

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

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

+from itertools import groupby
+
+
+def is_pangram(sentence):
+ alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ for i in range(0, len(sentence)):
+ if sentence[i].lower() in alphabet:
+ alphabet = alphabet.replace(sentence[i].lower(), '')
+ return alphabet == ''
+
+
+def char_histogram(text):
+ result = dict()
+ for i in range(0, len(text)):
+ if text[i] in result:
+ result[text[i]] += 1
+ else:
+ result.update({text[i]: 1})
+ return result
+
+
+def sort_by(func, arguments):
+ for i in range(0, len(arguments)):
+ for j in range(0, len(arguments)):
+ if func(arguments[i], arguments[j]) < 1:
+ temp_argument = arguments[i]
+ arguments[i] = arguments[j]
+ arguments[j] = temp_argument
+ return arguments
+
+
+def group_by_type(dictionary):
+ types = []
+ result = dict()
+ for key in dictionary:
+ if type(key) not in types:
+ types.append(type(key))
+ for i in range(0, len(types)):
+ temp = dict()
+ for j in dictionary:
+ if types[i] == type(j) and types[i] not in result:
+ temp.update({j: dictionary.get(j)})
+ result.update({types[i]: temp})
+ return result
+
+
+def anagrams(words):
+ return ([list(result) for key, result in
+ groupby(sorted(words, key=sorted), sorted)])

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

from itertools import groupby
def is_pangram(sentence):
alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for i in range(0, len(sentence)):
if sentence[i].lower() in alphabet:
alphabet = alphabet.replace(sentence[i].lower(), '')
return alphabet == ''
def char_histogram(text):
result = dict()
for i in range(0, len(text)):
if text[i] in result:
result[text[i]] += 1
else:
result.update({text[i]: 1})
return result
def sort_by(func, arguments):
for i in range(0, len(arguments)):
for j in range(0, len(arguments)):
if func(arguments[i], arguments[j]) < 1:
- temp_argument = arguments[i]
+ argument = arguments[i]
arguments[i] = arguments[j]
- arguments[j] = temp_argument
+ arguments[j] = argument
return arguments
def group_by_type(dictionary):
types = []
result = dict()
for key in dictionary:
if type(key) not in types:
types.append(type(key))
for i in range(0, len(types)):
- temp = dict()
+ group = dict()
for j in dictionary:
if types[i] == type(j) and types[i] not in result:
- temp.update({j: dictionary.get(j)})
- result.update({types[i]: temp})
+ group.update({j: dictionary.get(j)})
+ result.update({types[i]: group})
return result
def anagrams(words):
return ([list(result) for key, result in
groupby(sorted(words, key=sorted), sorted)])