Решение на Пет функции от Михаил Станин

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

Към профила на Михаил Станин

Резултати

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

Код

def is_pangram(sentence, alphabet=None):
if alphabet is None:
alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if len(sentence) < len(alphabet):
return False
lowered_sentence = sentence.lower()
found_letters = set()
for _ in lowered_sentence:
if _ in alphabet:
found_letters.add(_)
if len(found_letters) == len(alphabet):
return True
return False
def char_histogram(text):
return dict([(c, text.count(c)) for c in text])
def cmp_to_key(mycmp):
class K(object):
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt_(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return K
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
return ({type(key): {key2: value_of for (key2, value_of)
in dictionary.items() if type(key2) == type(key)}
for (key, value) in dictionary.items()})
def isAnagram(word1, word2):
word1 = ''.join(sorted(word1))
word2 = ''.join(sorted(word2))
return word1 == word2
def anagrams(words):
sub_list = []
end_list = []
taken = set()
for word in words:
sub_list.append(word)
taken.add(word)
for another_word in words[1:]:
if isAnagram(word, another_word) and another_word not in taken:
sub_list.append(another_word)
words.remove(another_word)
taken.add(another_word)
end_list.append(sub_list)
sub_list = []
return end_list

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

.FF.............
======================================================================
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-4ya7pg/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-4ya7pg/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({'Tom Marvolo Riddle'})
frozenset({'I am Lord Voldemort'})

----------------------------------------------------------------------
Ran 16 tests in 0.010s

FAILED (failures=2)

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

Михаил обнови решението на 14.03.2014 14:51 (преди около 10 години)

+def is_pangram(sentence, alphabet=None):
+ if alphabet is None:
+ alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ if len(sentence) < len(alphabet):
+ return False
+ lowered_sentence = sentence.lower()
+ found_letters = set()
+ for _ in lowered_sentence:
+ if _ in alphabet:
+ found_letters.add(_)
+ if len(found_letters) == len(alphabet):
+ return True
+ return False
+
+def char_histogram(text):
+ return dict([(c, text.count(c)) for c in text])

Михаил обнови решението на 14.03.2014 14:52 (преди около 10 години)

def is_pangram(sentence, alphabet=None):
- if alphabet is None:
- alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if len(sentence) < len(alphabet):
return False
lowered_sentence = sentence.lower()
found_letters = set()
for _ in lowered_sentence:
if _ in alphabet:
found_letters.add(_)
if len(found_letters) == len(alphabet):
return True
return False
def char_histogram(text):
return dict([(c, text.count(c)) for c in text])

Михаил обнови решението на 14.03.2014 15:43 (преди около 10 години)

def is_pangram(sentence, alphabet=None):
alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if len(sentence) < len(alphabet):
return False
lowered_sentence = sentence.lower()
found_letters = set()
for _ in lowered_sentence:
if _ in alphabet:
found_letters.add(_)
if len(found_letters) == len(alphabet):
return True
return False
def char_histogram(text):
- return dict([(c, text.count(c)) for c in text])
+ return dict([(c, text.count(c)) for c in text])
+
+def group_by_type(dictionary):
+ return {type(key):{_:value_of for (_, value_of) in dictionary.items()\
+ if type(_) == type(key)} for (key, value) in dictionary.items()}

Михаил обнови решението на 14.03.2014 17:13 (преди около 10 години)

+
+
def is_pangram(sentence, alphabet=None):
alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if len(sentence) < len(alphabet):
return False
lowered_sentence = sentence.lower()
found_letters = set()
for _ in lowered_sentence:
if _ in alphabet:
found_letters.add(_)
if len(found_letters) == len(alphabet):
return True
return False
def char_histogram(text):
return dict([(c, text.count(c)) for c in text])
def group_by_type(dictionary):
return {type(key):{_:value_of for (_, value_of) in dictionary.items()\
if type(_) == type(key)} for (key, value) in dictionary.items()}

Михаил обнови решението на 16.03.2014 12:32 (преди около 10 години)

def is_pangram(sentence, alphabet=None):
- alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ if alphabet is None:
+ alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if len(sentence) < len(alphabet):
return False
lowered_sentence = sentence.lower()
found_letters = set()
for _ in lowered_sentence:
if _ in alphabet:
found_letters.add(_)
if len(found_letters) == len(alphabet):
return True
return False
-
+
+
def char_histogram(text):
return dict([(c, text.count(c)) for c in text])
+
+def cmp_to_key(mycmp):
+
+ class K(object):
+
+ def __init__(self, obj, *args):
+ self.obj = obj
+
+ def __lt__(self, other):
+ return mycmp(self.obj, other.obj) < 0
+
+ def __gt_(self, other):
+ return mycmp(self.obj, other.obj) > 0
+
+ def __eq__(self, other):
+ return mycmp(self.obj, other.obj) == 0
+
+ def __le__(self, other):
+ return mycmp(self.obj, other.obj) <= 0
+
+ def __ge__(self, other):
+ return mycmp(self.obj, other.obj) >= 0
+
+ def __ne__(self, other):
+ return mycmp(self.obj, other.obj) != 0
+
+ return K
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
def group_by_type(dictionary):
- return {type(key):{_:value_of for (_, value_of) in dictionary.items()\
- if type(_) == type(key)} for (key, value) in dictionary.items()}
+ return ({type(key): {key2: value_of for (key2, value_of)
+ in dictionary.items() if type(key2) == type(key)}
+ for (key, value) in dictionary.items()})
+
+
+def isAnagram(word1, word2):
+ word1 = ''.join(sorted(word1))
+ word2 = ''.join(sorted(word2))
+ return word1 == word2
+
+
+def anagrams(words):
+ sub_list = []
+ end_list = []
+ taken = set()
+ for word in words:
+ sub_list.append(word)
+ taken.add(word)
+ for another_word in words[1:]:
+ if isAnagram(word, another_word) and another_word not in taken:
+ sub_list.append(another_word)
+ words.remove(another_word)
+ taken.add(another_word)
+ end_list.append(sub_list)
+ sub_list = []
+ return end_list