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

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

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

Резултати

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

Код

from collections import defaultdict, Counter
from string import ascii_letters
CYRILLIC_ALPHABET = {chr(1072 + i) for i in range(32)} - {'ы', 'э'}
ALPHABET = set(ascii_letters).union(CYRILLIC_ALPHABET)
def is_pangram(sentence):
return all([char in sentence.lower() for char in CYRILLIC_ALPHABET])
def char_histogram(text):
return Counter(text)
def sort_by(func, arguments):
coll = list(arguments)
for x in range(len(coll)):
for y in range(x + 1, len(coll)):
if func(coll[x], coll[y]) > 0:
coll[x], coll[y] = coll[y], coll[x]
return coll
def group_by_type(dictionary):
result = defaultdict(dict)
for key, value in dictionary.items():
result[type(key)].update({key: value})
return result
def letters_histogram(word):
letters = [letter.lower() for letter in word if letter in ALPHABET]
return Counter(letters)
def anagrams(words):
histograms = [letters_histogram(word) for word in words]
hashable_histograms = [frozenset(histogram.items())
for histogram in histograms]
result = defaultdict(list)
for letters, word in zip(hashable_histograms, words):
result[letters].append(word)
return list(result.values())

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

...............F
======================================================================
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-mfrphm/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=1)

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

Георги обнови решението на 16.03.2014 20:06 (преди над 10 години)

+CYRILLIC_ALPHABET = {chr(1072 + i) for i in range(32)} - {'ы', 'э'}
+
+def is_pangram(sentence):
+ return all([char in sentence.lower() for char in CYRILLIC_ALPHABET])
+
+from collections import defaultdict
+
+def char_histogram(text):
+ histogram = defaultdict(lambda: 0)
+ for char in text:
+ histogram[char] += 1
+ return histogram
+
+def sort_by(func, coll):
+ if len(coll) <= 0:
+ return []
+ pivot = coll[0]
+ smaller = list(filter(lambda x: func(pivot, x) > 0, coll))
+ equal = list(filter(lambda x: x == pivot, coll))
+ bigger = list(filter(lambda x: func(x, pivot) > 0, coll))
+ return sort_by(func, smaller) + equal + sort_by(func, bigger)
+
+def group_by_type(dictionary):
+ result = defaultdict(dict)
+ for key, value in dictionary.items():
+ result[type(key)].update({key: value})
+ return result
+
+def anagrams(words):
+ letters_sets = map(lambda word: frozenset(word.lower()), words)
+ result = defaultdict(list)
+ for letters_set, word in zip(letters_sets, words):
+ result[letters_set].append(word)
+ return list(result.values())

Георги обнови решението на 16.03.2014 21:35 (преди над 10 години)

CYRILLIC_ALPHABET = {chr(1072 + i) for i in range(32)} - {'ы', 'э'}
def is_pangram(sentence):
return all([char in sentence.lower() for char in CYRILLIC_ALPHABET])
from collections import defaultdict
def char_histogram(text):
histogram = defaultdict(lambda: 0)
for char in text:
histogram[char] += 1
return histogram
def sort_by(func, coll):
- if len(coll) <= 0:
+ if coll == []:
return []
pivot = coll[0]
- smaller = list(filter(lambda x: func(pivot, x) > 0, coll))
equal = list(filter(lambda x: x == pivot, coll))
- bigger = list(filter(lambda x: func(x, pivot) > 0, coll))
- return sort_by(func, smaller) + equal + sort_by(func, bigger)
+ lesser = list(filter(lambda x: func(x, pivot) < 0, coll))
+ greater = list(filter(lambda x: func(x, pivot) > 0, coll))
+ return sort_by(func, lesser) + equal + sort_by(func, greater)
def group_by_type(dictionary):
result = defaultdict(dict)
for key, value in dictionary.items():
result[type(key)].update({key: value})
return result
def anagrams(words):
letters_sets = map(lambda word: frozenset(word.lower()), words)
result = defaultdict(list)
for letters_set, word in zip(letters_sets, words):
result[letters_set].append(word)
return list(result.values())

Георги обнови решението на 16.03.2014 23:34 (преди над 10 години)

CYRILLIC_ALPHABET = {chr(1072 + i) for i in range(32)} - {'ы', 'э'}
def is_pangram(sentence):
return all([char in sentence.lower() for char in CYRILLIC_ALPHABET])
from collections import defaultdict
def char_histogram(text):
histogram = defaultdict(lambda: 0)
for char in text:
histogram[char] += 1
return histogram
def sort_by(func, coll):
if coll == []:
return []
pivot = coll[0]
- equal = list(filter(lambda x: x == pivot, coll))
+ equal = list(filter(lambda x: func(x, pivot) == 0, coll))
lesser = list(filter(lambda x: func(x, pivot) < 0, coll))
greater = list(filter(lambda x: func(x, pivot) > 0, coll))
return sort_by(func, lesser) + equal + sort_by(func, greater)
def group_by_type(dictionary):
result = defaultdict(dict)
for key, value in dictionary.items():
result[type(key)].update({key: value})
return result
def anagrams(words):
letters_sets = map(lambda word: frozenset(word.lower()), words)
result = defaultdict(list)
for letters_set, word in zip(letters_sets, words):
result[letters_set].append(word)
return list(result.values())

Георги обнови решението на 16.03.2014 23:56 (преди над 10 години)

CYRILLIC_ALPHABET = {chr(1072 + i) for i in range(32)} - {'ы', 'э'}
def is_pangram(sentence):
return all([char in sentence.lower() for char in CYRILLIC_ALPHABET])
from collections import defaultdict
def char_histogram(text):
histogram = defaultdict(lambda: 0)
for char in text:
histogram[char] += 1
return histogram
-def sort_by(func, coll):
- if coll == []:
- return []
- pivot = coll[0]
- equal = list(filter(lambda x: func(x, pivot) == 0, coll))
- lesser = list(filter(lambda x: func(x, pivot) < 0, coll))
- greater = list(filter(lambda x: func(x, pivot) > 0, coll))
- return sort_by(func, lesser) + equal + sort_by(func, greater)
+def sort_by(func, arguments):
+ coll = list(arguments)
+ for x in range(len(coll)):
+ for y in range(x + 1, len(coll)):
+ if func(coll[x], coll[y]) > 0:
+ coll[x], coll[y] = coll[y], coll[x]
+ return coll
def group_by_type(dictionary):
result = defaultdict(dict)
for key, value in dictionary.items():
result[type(key)].update({key: value})
return result
def anagrams(words):
letters_sets = map(lambda word: frozenset(word.lower()), words)
result = defaultdict(list)
for letters_set, word in zip(letters_sets, words):
result[letters_set].append(word)
return list(result.values())

Георги обнови решението на 19.03.2014 15:04 (преди над 10 години)

+from collections import defaultdict, Counter
+from itertools import groupby
+
CYRILLIC_ALPHABET = {chr(1072 + i) for i in range(32)} - {'ы', 'э'}
def is_pangram(sentence):
return all([char in sentence.lower() for char in CYRILLIC_ALPHABET])
-from collections import defaultdict
-
def char_histogram(text):
- histogram = defaultdict(lambda: 0)
- for char in text:
- histogram[char] += 1
- return histogram
+ return Counter(text)
def sort_by(func, arguments):
coll = list(arguments)
for x in range(len(coll)):
for y in range(x + 1, len(coll)):
if func(coll[x], coll[y]) > 0:
coll[x], coll[y] = coll[y], coll[x]
return coll
def group_by_type(dictionary):
result = defaultdict(dict)
for key, value in dictionary.items():
result[type(key)].update({key: value})
return result
def anagrams(words):
- letters_sets = map(lambda word: frozenset(word.lower()), words)
+ words_histograms = [Counter(word.lower()) for word in words]
+ hashable_histograms = [frozenset(h.items()) for h in words_histograms]
result = defaultdict(list)
- for letters_set, word in zip(letters_sets, words):
- result[letters_set].append(word)
+ for letters, word in zip(hashable_histograms, words):
+ result[letters].append(word)
return list(result.values())

Георги обнови решението на 19.03.2014 15:58 (преди над 10 години)

from collections import defaultdict, Counter
from itertools import groupby
CYRILLIC_ALPHABET = {chr(1072 + i) for i in range(32)} - {'ы', 'э'}
def is_pangram(sentence):
return all([char in sentence.lower() for char in CYRILLIC_ALPHABET])
def char_histogram(text):
return Counter(text)
def sort_by(func, arguments):
coll = list(arguments)
for x in range(len(coll)):
for y in range(x + 1, len(coll)):
if func(coll[x], coll[y]) > 0:
coll[x], coll[y] = coll[y], coll[x]
return coll
def group_by_type(dictionary):
result = defaultdict(dict)
for key, value in dictionary.items():
result[type(key)].update({key: value})
return result
def anagrams(words):
words_histograms = [Counter(word.lower()) for word in words]
- hashable_histograms = [frozenset(h.items()) for h in words_histograms]
+ hashable_histograms = [frozenset(histogram.items())
+ for histogram in words_histograms]
result = defaultdict(list)
for letters, word in zip(hashable_histograms, words):
result[letters].append(word)
return list(result.values())

Георги обнови решението на 19.03.2014 16:27 (преди над 10 години)

from collections import defaultdict, Counter
from itertools import groupby
+from string import ascii_letters
CYRILLIC_ALPHABET = {chr(1072 + i) for i in range(32)} - {'ы', 'э'}
+ALPHABET = set(ascii_letters).union(CYRILLIC_ALPHABET)
def is_pangram(sentence):
return all([char in sentence.lower() for char in CYRILLIC_ALPHABET])
def char_histogram(text):
return Counter(text)
def sort_by(func, arguments):
coll = list(arguments)
for x in range(len(coll)):
for y in range(x + 1, len(coll)):
if func(coll[x], coll[y]) > 0:
coll[x], coll[y] = coll[y], coll[x]
return coll
def group_by_type(dictionary):
result = defaultdict(dict)
for key, value in dictionary.items():
result[type(key)].update({key: value})
return result
+def letters_histogram(word):
+ letters = "".join(filter(lambda letter: letter in ALPHABET, word))
+ return char_histogram(letters.lower())
+
def anagrams(words):
- words_histograms = [Counter(word.lower()) for word in words]
+ histograms = [letters_histogram(word) for word in words]
hashable_histograms = [frozenset(histogram.items())
- for histogram in words_histograms]
+ for histogram in histograms]
result = defaultdict(list)
for letters, word in zip(hashable_histograms, words):
result[letters].append(word)
return list(result.values())

Георги обнови решението на 19.03.2014 16:28 (преди над 10 години)

from collections import defaultdict, Counter
-from itertools import groupby
from string import ascii_letters
CYRILLIC_ALPHABET = {chr(1072 + i) for i in range(32)} - {'ы', 'э'}
ALPHABET = set(ascii_letters).union(CYRILLIC_ALPHABET)
def is_pangram(sentence):
return all([char in sentence.lower() for char in CYRILLIC_ALPHABET])
def char_histogram(text):
return Counter(text)
def sort_by(func, arguments):
coll = list(arguments)
for x in range(len(coll)):
for y in range(x + 1, len(coll)):
if func(coll[x], coll[y]) > 0:
coll[x], coll[y] = coll[y], coll[x]
return coll
def group_by_type(dictionary):
result = defaultdict(dict)
for key, value in dictionary.items():
result[type(key)].update({key: value})
return result
def letters_histogram(word):
letters = "".join(filter(lambda letter: letter in ALPHABET, word))
return char_histogram(letters.lower())
def anagrams(words):
histograms = [letters_histogram(word) for word in words]
hashable_histograms = [frozenset(histogram.items())
for histogram in histograms]
result = defaultdict(list)
for letters, word in zip(hashable_histograms, words):
result[letters].append(word)
return list(result.values())

Георги обнови решението на 19.03.2014 16:32 (преди над 10 години)

from collections import defaultdict, Counter
from string import ascii_letters
CYRILLIC_ALPHABET = {chr(1072 + i) for i in range(32)} - {'ы', 'э'}
ALPHABET = set(ascii_letters).union(CYRILLIC_ALPHABET)
def is_pangram(sentence):
return all([char in sentence.lower() for char in CYRILLIC_ALPHABET])
def char_histogram(text):
return Counter(text)
def sort_by(func, arguments):
coll = list(arguments)
for x in range(len(coll)):
for y in range(x + 1, len(coll)):
if func(coll[x], coll[y]) > 0:
coll[x], coll[y] = coll[y], coll[x]
return coll
def group_by_type(dictionary):
result = defaultdict(dict)
for key, value in dictionary.items():
result[type(key)].update({key: value})
return result
def letters_histogram(word):
- letters = "".join(filter(lambda letter: letter in ALPHABET, word))
- return char_histogram(letters.lower())
+ letters = [letter.lower() for letter in word if letter in ALPHABET]
+ return Counter(letters)
def anagrams(words):
histograms = [letters_histogram(word) for word in words]
hashable_histograms = [frozenset(histogram.items())
for histogram in histograms]
result = defaultdict(list)
for letters, word in zip(hashable_histograms, words):
result[letters].append(word)
return list(result.values())