Решение на Пет функции от Дарина Кръстева

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

Към профила на Дарина Кръстева

Резултати

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

Код

CYRILLIC_ALPHABET = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
def is_pangram(sentence):
sentence.lower()
for letter in CYRILLIC_ALPHABET:
if letter not in sentence:
return False
else:
return True
def char_histogram(text):
histogram = {}
for symbol in text:
histogram.setdefault(symbol, 0)
histogram[symbol] += 1
return histogram
def sort_by(func, arguments):
sorted_arguments = arguments
lenght_of_arguments = len(sorted_arguments)
for index in range(1, lenght_of_arguments):
for second_index in range(0, lenght_of_arguments - 1):
if func(arguments[second_index], arguments[second_index + 1]) > 0:
sorted_arguments[second_index], \
sorted_arguments[second_index + 1] = \
sorted_arguments[second_index + 1], \
sorted_arguments[second_index]
return sorted_arguments
def group_by_type(dictionary):
grouped_by_type = {}
for key in dictionary.keys():
grouped_by_type.setdefault(type(key), {})[key] \
= dictionary[key]
return grouped_by_type
def are_anagrams(first_word, second_word):
first_word.lower()
second_word.lower()
if first_word is second_word:
return False
first_word_letters = {symbol for symbol in first_word if symbol.isalpha()}
second_word_letters = {symbol for symbol in second_word
if symbol.isalpha()}
if len(first_word_letters) != len(second_word_letters):
return False
else:
for letter in first_word_letters:
if letter not in second_word_letters:
return False
return True
def anagrams(words):
grouped_anagrams = []
for word in words:
anagram_found = False
if len(grouped_anagrams) == 0:
grouped_anagrams.append([word])
anagram_found = True
for group in grouped_anagrams:
if are_anagrams(group[0], word):
group.append(word)
anagram_found = True
break
if not anagram_found:
grouped_anagrams.append([word])
return grouped_anagrams

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

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

======================================================================
FAIL: test_with_pangrams (test.TestIsPangram)
----------------------------------------------------------------------
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-d681zq/test.py", line 15, in test_with_pangrams
    solution.is_pangram('За миг бях в чужд, скърцащ плюшен фотьойл.'))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 16 tests in 0.013s

FAILED (failures=3)

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

Дарина обнови решението на 16.03.2014 16:46 (преди над 10 години)

+CYRILLIC_ALPHABET = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
+ 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ',
+ 'ъ', 'ь', 'ю', 'я')
+
+def is_pangram(sentence):
+ sentence.lower()
+ for letter_index in range(30):
+ if CYRILLIC_ALPHABET[letter_index] not in sentence:
+ return False
+ else:
+ return True
+
+def char_histogram(text):
+ histogram = dict()
+ for symbol in text:
+ if symbol in histogram.keys():
+ histogram[symbol] += 1
+ else:
+ histogram[symbol] = 1
+ return histogram
+
+def sort_by(func, arguments):
+ for index in range(len(arguments) - 1):
+ for second_index in range(index + 1, len(arguments)):
+ if func(arguments[index], arguments[second_index]) > 0:
+ dummy_element = arguments[index]
+ arguments[index] = arguments[second_index]
+ arguments[second_index] = dummy_element
+ return arguments
+
+def group_by_type(dictionary):
+ grouped_by_type = {}
+ for key in dictionary.keys():
+ if type(key) not in grouped_by_type.keys():
+ grouped_by_type[type(key)] = {}
+ grouped_by_type[type(key)][key] = dictionary[key]
+ else:
+ grouped_by_type[type(key)][key] = dictionary[key]
+ return grouped_by_type
+
+def are_anagrams(first_word, second_word):
+ if len(first_word) != len(second_word) or first_word == second_word:
+ return False
+ else:
+ for symbol in first_word:
+ if symbol not in second_word:
+ return False
+ return True
+
+def anagrams(words):
+ grouped_anagrams = []
+ for word in words:
+ anagram_found = False
+ if len(grouped_anagrams) == 0:
+ grouped_anagrams.append([word])
+ anagram_found = True
+ for group in grouped_anagrams:
+ if are_anagrams(group[0], word):
+ group.append(word)
+ anagram_found = True
+ break
+ if not anagram_found:
+ grouped_anagrams.append([word])
+ return grouped_anagrams

Дарина обнови решението на 16.03.2014 16:48 (преди над 10 години)

CYRILLIC_ALPHABET = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ',
'ъ', 'ь', 'ю', 'я')
def is_pangram(sentence):
sentence.lower()
for letter_index in range(30):
if CYRILLIC_ALPHABET[letter_index] not in sentence:
return False
else:
return True
def char_histogram(text):
- histogram = dict()
+ histogram = {}
for symbol in text:
if symbol in histogram.keys():
histogram[symbol] += 1
else:
histogram[symbol] = 1
return histogram
def sort_by(func, arguments):
for index in range(len(arguments) - 1):
for second_index in range(index + 1, len(arguments)):
if func(arguments[index], arguments[second_index]) > 0:
dummy_element = arguments[index]
arguments[index] = arguments[second_index]
arguments[second_index] = dummy_element
return arguments
def group_by_type(dictionary):
grouped_by_type = {}
for key in dictionary.keys():
if type(key) not in grouped_by_type.keys():
grouped_by_type[type(key)] = {}
grouped_by_type[type(key)][key] = dictionary[key]
else:
grouped_by_type[type(key)][key] = dictionary[key]
return grouped_by_type
def are_anagrams(first_word, second_word):
if len(first_word) != len(second_word) or first_word == second_word:
return False
else:
for symbol in first_word:
if symbol not in second_word:
return False
return True
def anagrams(words):
grouped_anagrams = []
for word in words:
anagram_found = False
if len(grouped_anagrams) == 0:
grouped_anagrams.append([word])
anagram_found = True
for group in grouped_anagrams:
if are_anagrams(group[0], word):
group.append(word)
anagram_found = True
break
if not anagram_found:
grouped_anagrams.append([word])
return grouped_anagrams
  • Индентацията на кода ти не е напълно ок.
  • type(key) може да се изнесе в променлива, ако се преизползва.
  • dummy_element не е хубаво име за променлива.
  • if type(key) not in grouped_by_type.keys(): - тази проверка можеш да я избегнеш ако ползваш defaultdict.

Дарина обнови решението на 19.03.2014 09:38 (преди над 10 години)

CYRILLIC_ALPHABET = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
- 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ',
- 'ъ', 'ь', 'ю', 'я')
+ 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
+ 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
-def is_pangram(sentence):
+
+def is_pangram(sentence):
sentence.lower()
- for letter_index in range(30):
- if CYRILLIC_ALPHABET[letter_index] not in sentence:
+ for letter in CYRILLIC_ALPHABET:
+ if letter not in sentence:
return False
else:
return True
+
def char_histogram(text):
histogram = {}
for symbol in text:
- if symbol in histogram.keys():
+ histogram.setdefault(symbol, 0)
histogram[symbol] += 1
- else:
- histogram[symbol] = 1
return histogram
+
def sort_by(func, arguments):
- for index in range(len(arguments) - 1):
- for second_index in range(index + 1, len(arguments)):
- if func(arguments[index], arguments[second_index]) > 0:
- dummy_element = arguments[index]
- arguments[index] = arguments[second_index]
- arguments[second_index] = dummy_element
- return arguments
+ sorted_arguments = arguments
+ lenght_of_arguments = len(sorted_arguments)
+ for index in range(1, lenght_of_arguments):
+ for second_index in range(0, lenght_of_arguments - 1):
+ if func(arguments[second_index], arguments[second_index + 1]) > 0:
+ sorted_arguments[second_index], \
+ sorted_arguments[second_index + 1] = \
+ sorted_arguments[second_index + 1], \
+ sorted_arguments[second_index]
+ return sorted_arguments
+
def group_by_type(dictionary):
grouped_by_type = {}
for key in dictionary.keys():
- if type(key) not in grouped_by_type.keys():
- grouped_by_type[type(key)] = {}
- grouped_by_type[type(key)][key] = dictionary[key]
- else:
- grouped_by_type[type(key)][key] = dictionary[key]
+ grouped_by_type.setdefault(type(key), {})[key] \
+ = dictionary[key]
return grouped_by_type
+
def are_anagrams(first_word, second_word):
- if len(first_word) != len(second_word) or first_word == second_word:
- return False
- else:
- for symbol in first_word:
- if symbol not in second_word:
- return False
- return True
+ first_word.lower()
+ second_word.lower()
+ if first_word is second_word:
+ return False
+ first_word_letters = {symbol for symbol in first_word if symbol.isalpha()}
+ second_word_letters = {symbol for symbol in second_word
+ if symbol.isalpha()}
+ if len(first_word_letters) != len(second_word_letters):
+ return False
+ else:
+ for letter in first_word_letters:
+ if letter not in second_word_letters:
+ return False
+ return True
-def anagrams(words):
+
+def anagrams(words):
grouped_anagrams = []
for word in words:
anagram_found = False
if len(grouped_anagrams) == 0:
grouped_anagrams.append([word])
anagram_found = True
for group in grouped_anagrams:
if are_anagrams(group[0], word):
group.append(word)
anagram_found = True
break
if not anagram_found:
grouped_anagrams.append([word])
return grouped_anagrams