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

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

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

Резултати

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

Код

ALPHABET = {chr(1072 + i) for i in range(32) if i != 27 and i != 29}
def is_pangram(sentence):
sentence = sentence.lower()
symbols = [x for x in sentence]
for letter in ALPHABET:
if letter not in symbols:
return False
return True
def char_histogram(text):
symbols = [x for x in text]
histogram = {}
for symbol in symbols:
if symbol in histogram:
histogram[symbol] += 1
else:
histogram[symbol] = 1
return histogram
def sort_by(func, arguments):
length = len(arguments)
for i in range(length - 1):
for j in range(length - 1, i, -1):
result_of_comparison = func(arguments[j - 1], arguments[j])
if result_of_comparison > 0:
arguments[j], arguments[j - 1] = arguments[j - 1], arguments[j]
return arguments
def group_by_type(dictionary):
group_by_type_elements = {}
keys = dictionary.keys()
for key in keys:
type_of_key = type(key)
if type_of_key not in group_by_type_elements:
group_by_type_elements[type_of_key] = {}
group_by_type_elements[type_of_key][key] = dictionary[key]
return group_by_type_elements
def compare_dict(dictionary1, dictionary2):
keys1 = dictionary1.keys()
keys2 = dictionary2.keys()
if len(keys1) != len(keys2):
return False
for key in keys1:
if key not in keys2:
return False
if dictionary1[key] != dictionary2[key]:
return False
return True
def anagrams(words):
histogram = {}
anagrams = list()
for word in words:
histogram[word] = char_histogram(word)
is_appended = False
for anagram in anagrams:
current_anagram = anagram[0]
if compare_dict(histogram[current_anagram],
histogram[word]) is True:
anagram.append(word)
is_appended = True
break
if is_appended is False:
anagrams.append([word])
return anagrams

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

.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-1b5xi6x/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({'Ray Adverb', 'Dave Barry'})
Items in the second set but not the first:
frozenset({'Dave Barry'})
frozenset({'Ray Adverb'})

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

----------------------------------------------------------------------
Ran 16 tests in 0.009s

FAILED (failures=2)

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

Мария обнови решението на 17.03.2014 12:09 (преди над 10 години)

+alphabet = {chr(1072 + i) for i in range(32) if i != 27 and i != 29}
+
+
+def is_pangram(sentence):
+ sentence = sentence.lower()
+ symbols = [x for x in sentence]
+ for letter in alphabet:
+ if letter not in symbols:
+ return False
+ return True
+
+
+def char_histogram(text):
+ symbols = [x for x in text]
+ histogram = {}
+ for symbol in symbols:
+ if symbol in histogram:
+ histogram[symbol] += 1
+ else:
+ histogram[symbol] = 1
+ return histogram
+
+
+def sort_by(func, arguments):
+ length = len(arguments)
+ for i in range(length - 1):
+ for j in range(length - 1, i, -1):
+ compare = func(arguments[j - 1], arguments[j])
+ if compare > 0:
+ arguments[j], arguments[j - 1] = arguments[j - 1], arguments[j]
+ return arguments
+
+
+def group_by_type(dictionary):
+ type_dict = {}
+ keys = dictionary.keys()
+ for key in keys:
+ if type(key) not in type_dict:
+ type_dict[type(key)] = {}
+ type_dict[type(key)][key] = dictionary[key]
+ return type_dict
+
+
+def compare_dict(dict1, dict2):
+ keys1 = dict1.keys()
+ keys2 = dict2.keys()
+ if len(keys1) != len(keys2):
+ return False
+ for key in keys1:
+ if key not in keys2:
+ return False
+ if dict1[key] != dict2[key]:
+ return False
+ return True
+
+
+def anagrams(words):
+ hist = {}
+ anagrams = list()
+ for word in words:
+ hist[word] = char_histogram(word)
+ flag = False
+ for anagram in anagrams:
+ current_anagram = anagram[0]
+ if compare_dict(hist[current_anagram], hist[word]) is True:
+ anagram.append(word)
+ flag = True
+ break
+ if flag is False:
+ anagrams.append([word])
+ return anagrams
  • alphabet е константа и трябва да е наименована с главни букви
  • type(key) може да се изнесе в отделна променлива, защото се преизползва
  • имената на променливите на трябва да съдържат типа на обекта
  • доста от имената са неясни: flag, hist, compare

Мария обнови решението на 18.03.2014 18:06 (преди над 10 години)

-alphabet = {chr(1072 + i) for i in range(32) if i != 27 and i != 29}
+ALPHABET = {chr(1072 + i) for i in range(32) if i != 27 and i != 29}
def is_pangram(sentence):
sentence = sentence.lower()
symbols = [x for x in sentence]
- for letter in alphabet:
+ for letter in ALPHABET:
if letter not in symbols:
return False
return True
def char_histogram(text):
symbols = [x for x in text]
histogram = {}
for symbol in symbols:
if symbol in histogram:
histogram[symbol] += 1
else:
histogram[symbol] = 1
return histogram
def sort_by(func, arguments):
length = len(arguments)
for i in range(length - 1):
for j in range(length - 1, i, -1):
- compare = func(arguments[j - 1], arguments[j])
- if compare > 0:
+ result_of_comparison = func(arguments[j - 1], arguments[j])
+ if result_of_comparison > 0:
arguments[j], arguments[j - 1] = arguments[j - 1], arguments[j]
return arguments
def group_by_type(dictionary):
- type_dict = {}
+ group_by_type_elements = {}
keys = dictionary.keys()
for key in keys:
- if type(key) not in type_dict:
- type_dict[type(key)] = {}
- type_dict[type(key)][key] = dictionary[key]
- return type_dict
+ type_of_key = type(key)
+ if type_of_key not in group_by_type_elements:
+ group_by_type_elements[type_of_key] = {}
+ group_by_type_elements[type_of_key][key] = dictionary[key]
+ return group_by_type_elements
-def compare_dict(dict1, dict2):
- keys1 = dict1.keys()
- keys2 = dict2.keys()
+def compare_dict(dictionary1, dictionary2):
+ keys1 = dictionary1.keys()
+ keys2 = dictionary2.keys()
if len(keys1) != len(keys2):
return False
for key in keys1:
if key not in keys2:
return False
- if dict1[key] != dict2[key]:
+ if dictionary1[key] != dictionary2[key]:
return False
return True
def anagrams(words):
- hist = {}
+ histogram = {}
anagrams = list()
for word in words:
- hist[word] = char_histogram(word)
- flag = False
+ histogram[word] = char_histogram(word)
+ is_appended = False
for anagram in anagrams:
current_anagram = anagram[0]
- if compare_dict(hist[current_anagram], hist[word]) is True:
+ if compare_dict(histogram[current_anagram],
+ histogram[word]) is True:
anagram.append(word)
- flag = True
+ is_appended = True
break
- if flag is False:
+ if is_appended is False:
anagrams.append([word])
return anagrams