Решение на Пет функции от Мария Кърчева

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

Към профила на Мария Кърчева

Резултати

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

Код

import re
def is_pangram(sentence):
alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for char in alphabet:
if char not in sentence.lower():
return False
return True
def char_histogram(text):
hist = {}
for char in text:
if char in hist.keys():
hist[char] += 1
else:
hist[char] = 1
return hist
def group_by_type(dictionary):
result = {}
for key, value in dictionary.items():
if type(key) not in result.keys():
result[type(key)] = {}
result[type(key)][key] = value
return result
def group_by_type(dictionary):
result = {}
for key, value in dictionary.items():
if type(key) not in result.keys():
result[type(key)] = {}
result[type(key)][key] = value
return result
def are_anagrams(a, b):
dict_a = char_histogram(re.sub(r"\W", '', a))
dict_b = char_histogram(re.sub(r"\W", '', b))
return len(set(dict_a.items()) ^ set(dict_b.items())) == 0
def anagrams(words):
result = []
for word in words:
word_is_anagram_of_group = False
for group in result:
if are_anagrams(word.lower(), group[0].lower()):
group.append(word)
word_is_anagram_of_group = True
if not word_is_anagram_of_group:
result.append([word])
return result

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

.............EEE
======================================================================
ERROR: test_sort_by_empty (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-1g71gfh/test.py", line 44, in test_sort_by_empty
    self.assertEqual([], solution.sort_by(lambda x, y: x - y, []))
AttributeError: 'module' object has no attribute 'sort_by'

======================================================================
ERROR: test_sort_by_one_element (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-1g71gfh/test.py", line 57, in test_sort_by_one_element
    self.assertEqual([1], solution.sort_by(lambda x, y: x - y, [1]))
AttributeError: 'module' object has no attribute 'sort_by'

======================================================================
ERROR: 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-1g71gfh/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]))
AttributeError: 'module' object has no attribute 'sort_by'

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

FAILED (errors=3)

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

Мария обнови решението на 19.03.2014 13:33 (преди над 10 години)

+def is_pangram(sentence):
+ alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ for char in alphabet:
+ if char not in sentence.lower():
+ return False
+ return True
+
+
+def char_histogram(text):
+ hist = {}
+ for char in text:
+ if char in hist.keys():
+ hist[char] += 1
+ else:
+ hist[char] = 1
+ return hist
+
+
+def sort_by(func, arguments):
+ for x in range(len(arguments) - 1):
+ for y in range(x,len(arguments)):
+ if func(arguments[x], arguments[y]) > 0:
+ arguments[x], arguments[y] = arguments[y], arguments[x]
+ return arguments
+
+
+def group_by_type(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ if type(key) not in result.keys():
+ result[type(key)] = {}
+ result[type(key)][key] = value
+ return result
+
+
+def are_anagrams(a, b):
+ dict_a = char_histogram(a)
+ dict_b = char_histogram(b)
+ return len(set(dict_a.items()) ^ set(dict_b.items())) == 0
+
+
+def anagrams(words):
+ result = []
+ for word in words:
+ word_is_anagram_of_group = False
+ for group in result:
+ if are_anagrams(word, group[0]):
+ group.append(word)
+ word_is_anagram_of_group = True
+ if not word_is_anagram_of_group:
+ result.append([word])
+ return result

Мария обнови решението на 19.03.2014 13:58 (преди над 10 години)

+import re
+
+
def is_pangram(sentence):
alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for char in alphabet:
if char not in sentence.lower():
return False
return True
def char_histogram(text):
hist = {}
for char in text:
if char in hist.keys():
hist[char] += 1
else:
hist[char] = 1
return hist
-def sort_by(func, arguments):
- for x in range(len(arguments) - 1):
- for y in range(x,len(arguments)):
- if func(arguments[x], arguments[y]) > 0:
- arguments[x], arguments[y] = arguments[y], arguments[x]
- return arguments
+def group_by_type(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ if type(key) not in result.keys():
+ result[type(key)] = {}
+ result[type(key)][key] = value
+ return result
def group_by_type(dictionary):
result = {}
for key, value in dictionary.items():
if type(key) not in result.keys():
result[type(key)] = {}
result[type(key)][key] = value
return result
def are_anagrams(a, b):
- dict_a = char_histogram(a)
- dict_b = char_histogram(b)
+ dict_a = char_histogram(re.sub(r"\W", '', a))
+ dict_b = char_histogram(re.sub(r"\W", '', b))
return len(set(dict_a.items()) ^ set(dict_b.items())) == 0
def anagrams(words):
result = []
for word in words:
word_is_anagram_of_group = False
for group in result:
- if are_anagrams(word, group[0]):
+ if are_anagrams(word.lower(), group[0].lower()):
group.append(word)
word_is_anagram_of_group = True
if not word_is_anagram_of_group:
result.append([word])
- return result
+ return result