Решение на Пет функции от Стефани Цакова

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

Към профила на Стефани Цакова

Резултати

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

Код

SMALL_LETTERS = [chr(code)
for code in range(1072, 1104)
if code != 1101 and code != 1099]
def is_pangram(sentence):
return [letter
for letter in SMALL_LETTERS
if letter not in sentence
and chr(ord(letter) - 32) not in sentence] == []
def frequency(symbol, sentence):
return sum([1 for letter in sentence if letter == symbol])
def char_histogram(text):
items_frequency = {}
for letter in text:
if letter not in items_frequency.keys():
items_frequency[letter] = frequency(letter, text)
return items_frequency
def find_all_of_type(current_type, dictionary):
all_of_current_type = {}
for key, value in dictionary.items():
if type(key) == current_type:
all_of_current_type[key] = value
return all_of_current_type
def group_by_type(dictionary):
grouped_items = {}
for key in dictionary.keys():
if type(key) not in grouped_items.keys():
grouped_items[type(key)] = find_all_of_type(
type(key), dictionary)
return grouped_items
def sort_by(func, arguments):
# make copy of the initial object
args_copy = [argument for argument in arguments]
for i in range(len(arguments) - 1):
for j in range(i + 1, len(arguments)):
if func(args_copy[i], args_copy[j]) > 0:
args_copy[i], args_copy[j] = args_copy[j], args_copy[i]
return args_copy
def take_all_of_kind(words, current_word):
all_of_kind = []
for word in words:
if check_two_words(current_word, word):
all_of_kind.append(word)
return all_of_kind
def check_two_words(first_word, second_word):
letters_first = [letter for letter in first_word.lower()
if letter.isalpha()]
letters_second = [letter for letter in second_word.lower()
if letter.isalpha()]
return sorted(letters_first) == sorted(letters_second)
def not_in(visited_words, current_word):
for word in visited_words:
if check_two_words(word, current_word):
return False
return True
def anagrams(words):
visited_words = []
sorted_words = []
for word in words:
if not_in(visited_words, word):
visited_words.append(word)
sorted_words.append(take_all_of_kind(words, word))
return sorted_words

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

...............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-gie17s/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.015s

FAILED (failures=1)

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

Стефани обнови решението на 13.03.2014 00:31 (преди около 10 години)

+LETTERS = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'й', 'и', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
+
+def is_pangram(sentence):
+ return [letter for letter in LETTERS if letter not in sentence] == []
+
+def frequency (symbol, sentence):
+ return sum([1 for letter in sentence if letter == symbol])
+
+def char_histogram(text):
+ dictionary = {}
+ for letter in text:
+ if letter not in dictionary.keys():
+ dictionary[letter] = frequency(letter, text)
+ return dictionary
+
+
+def find_all_of_type(types, dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ if type(key) == types:
+ result[key] = value
+ return result
+
+
+def group_by_type(dictionary):
+ result_dictionary = {}
+ for key in dictionary.keys():
+ if type(key) not in dictionary.keys():
+ result_dictionary[type(key)] = find_all_of_type(type(key), dictionary)
+ return result_dictionary
+
+def sort_by(func, arguments):
+ for i in range (0, len(arguments)):
+ for j in range(0, len(arguments)):
+ if func(arguments[i], arguments[j]) < 0:
+ arguments[i], arguments[j] = arguments[j], arguments[i]
+ return arguments
+
+def take_all_of_kind(words, current_word):
+ all_of_kind = [current_word]
+ for word in words:
+ if check_two_words(current_word, word):
+ all_of_kind.append(word)
+ return all_of_kind
+
+
+def check_two_words(first_word, second_word):
+ return sorted(first_word) == sorted(second_word)
+
+def not_in(visited_words, current_word):
+ for word in visited_words:
+ if check_two_words(word, current_word):
+ return False
+ return True
+
+def anagrams(words):
+ visited_words = []
+ sorted_words = []
+ for word in words:
+ if not_in(visited_words, word):
+ sorted_words.append(take_all_of_kind(words, word))
+ return sorted_words
+
+
+
+

Стефани обнови решението на 13.03.2014 13:40 (преди около 10 години)

-LETTERS = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'й', 'и', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
+SMALL_LETTERS = [chr(code) for code in range(1072,1104) if code != 1101 and code != 1099]
def is_pangram(sentence):
- return [letter for letter in LETTERS if letter not in sentence] == []
+ return [letter for letter in SMALL_LETTERS if letter not in sentence and chr(ord(letter) - 32) not in sentence] == []
def frequency (symbol, sentence):
return sum([1 for letter in sentence if letter == symbol])
def char_histogram(text):
dictionary = {}
for letter in text:
if letter not in dictionary.keys():
dictionary[letter] = frequency(letter, text)
return dictionary
def find_all_of_type(types, dictionary):
result = {}
for key, value in dictionary.items():
if type(key) == types:
result[key] = value
- return result
+ return result
def group_by_type(dictionary):
result_dictionary = {}
for key in dictionary.keys():
if type(key) not in dictionary.keys():
result_dictionary[type(key)] = find_all_of_type(type(key), dictionary)
return result_dictionary
def sort_by(func, arguments):
for i in range (0, len(arguments)):
for j in range(0, len(arguments)):
if func(arguments[i], arguments[j]) < 0:
arguments[i], arguments[j] = arguments[j], arguments[i]
return arguments
def take_all_of_kind(words, current_word):
all_of_kind = [current_word]
for word in words:
if check_two_words(current_word, word):
all_of_kind.append(word)
return all_of_kind
def check_two_words(first_word, second_word):
return sorted(first_word) == sorted(second_word)
+
def not_in(visited_words, current_word):
for word in visited_words:
if check_two_words(word, current_word):
return False
return True
def anagrams(words):
visited_words = []
sorted_words = []
for word in words:
if not_in(visited_words, word):
sorted_words.append(take_all_of_kind(words, word))
return sorted_words
-
-
-
-

Стефани обнови решението на 13.03.2014 13:48 (преди около 10 години)

SMALL_LETTERS = [chr(code) for code in range(1072,1104) if code != 1101 and code != 1099]
def is_pangram(sentence):
return [letter for letter in SMALL_LETTERS if letter not in sentence and chr(ord(letter) - 32) not in sentence] == []
def frequency (symbol, sentence):
return sum([1 for letter in sentence if letter == symbol])
def char_histogram(text):
dictionary = {}
for letter in text:
if letter not in dictionary.keys():
dictionary[letter] = frequency(letter, text)
return dictionary
def find_all_of_type(types, dictionary):
result = {}
for key, value in dictionary.items():
if type(key) == types:
result[key] = value
return result
def group_by_type(dictionary):
result_dictionary = {}
for key in dictionary.keys():
if type(key) not in dictionary.keys():
result_dictionary[type(key)] = find_all_of_type(type(key), dictionary)
return result_dictionary
def sort_by(func, arguments):
for i in range (0, len(arguments)):
for j in range(0, len(arguments)):
if func(arguments[i], arguments[j]) < 0:
arguments[i], arguments[j] = arguments[j], arguments[i]
return arguments
def take_all_of_kind(words, current_word):
- all_of_kind = [current_word]
+ all_of_kind = []
for word in words:
if check_two_words(current_word, word):
all_of_kind.append(word)
return all_of_kind
def check_two_words(first_word, second_word):
return sorted(first_word) == sorted(second_word)
def not_in(visited_words, current_word):
for word in visited_words:
if check_two_words(word, current_word):
return False
return True
def anagrams(words):
visited_words = []
sorted_words = []
for word in words:
if not_in(visited_words, word):
+ visited_words.append(word)
sorted_words.append(take_all_of_kind(words, word))
return sorted_words

Става дума за следните грешки:

solution.py:1:50: E231 missing whitespace after ','
solution.py:1:80: E501 line too long (89 > 79 characters)
solution.py:3:1: E302 expected 2 blank lines, found 1
solution.py:4:80: E501 line too long (121 > 79 characters)
solution.py:6:1: E302 expected 2 blank lines, found 1
solution.py:6:14: E211 whitespace before '('
solution.py:9:1: E302 expected 2 blank lines, found 1
solution.py:29:80: E501 line too long (82 > 79 characters)
solution.py:32:1: E302 expected 2 blank lines, found 1
solution.py:33:19: E211 whitespace before '('
solution.py:39:1: E302 expected 2 blank lines, found 1
solution.py:50:1: W293 blank line contains whitespace
solution.py:57:1: E302 expected 2 blank lines, found 1

Стефани обнови решението на 15.03.2014 21:48 (преди около 10 години)

-SMALL_LETTERS = [chr(code) for code in range(1072,1104) if code != 1101 and code != 1099]
+SMALL_LETTERS = [chr(code)
+ for code in range(1072, 1104)
+ if code != 1101 and code != 1099]
+
def is_pangram(sentence):
- return [letter for letter in SMALL_LETTERS if letter not in sentence and chr(ord(letter) - 32) not in sentence] == []
+ return [letter
+ for letter in SMALL_LETTERS
+ if letter not in sentence
+ and chr(ord(letter) - 32) not in sentence] == []
-def frequency (symbol, sentence):
+
+def frequency(symbol, sentence):
return sum([1 for letter in sentence if letter == symbol])
+
def char_histogram(text):
dictionary = {}
for letter in text:
if letter not in dictionary.keys():
dictionary[letter] = frequency(letter, text)
return dictionary
def find_all_of_type(types, dictionary):
result = {}
for key, value in dictionary.items():
- if type(key) == types:
+ if isinstance(key, types):
result[key] = value
return result
def group_by_type(dictionary):
result_dictionary = {}
for key in dictionary.keys():
if type(key) not in dictionary.keys():
- result_dictionary[type(key)] = find_all_of_type(type(key), dictionary)
+ result_dictionary[type(key)] = find_all_of_type(
+ type(key), dictionary)
return result_dictionary
+
def sort_by(func, arguments):
- for i in range (0, len(arguments)):
+ for i in range(0, len(arguments)):
for j in range(0, len(arguments)):
if func(arguments[i], arguments[j]) < 0:
arguments[i], arguments[j] = arguments[j], arguments[i]
return arguments
+
def take_all_of_kind(words, current_word):
all_of_kind = []
for word in words:
if check_two_words(current_word, word):
all_of_kind.append(word)
return all_of_kind
def check_two_words(first_word, second_word):
return sorted(first_word) == sorted(second_word)
-
+
def not_in(visited_words, current_word):
for word in visited_words:
if check_two_words(word, current_word):
return False
return True
+
def anagrams(words):
visited_words = []
sorted_words = []
for word in words:
if not_in(visited_words, word):
visited_words.append(word)
sorted_words.append(take_all_of_kind(words, word))
return sorted_words

Стефани обнови решението на 16.03.2014 22:45 (преди около 10 години)

SMALL_LETTERS = [chr(code)
- for code in range(1072, 1104)
- if code != 1101 and code != 1099]
+ for code
+ in range(1072, 1104) if code != 1101 and code != 1099]
def is_pangram(sentence):
return [letter
for letter in SMALL_LETTERS
if letter not in sentence
and chr(ord(letter) - 32) not in sentence] == []
def frequency(symbol, sentence):
return sum([1 for letter in sentence if letter == symbol])
def char_histogram(text):
- dictionary = {}
+ items_frequency = {}
for letter in text:
- if letter not in dictionary.keys():
- dictionary[letter] = frequency(letter, text)
- return dictionary
+ if letter not in items_frequency.keys():
+ items_frequency[letter] = frequency(letter, text)
+ return items_frequency
-def find_all_of_type(types, dictionary):
- result = {}
+def find_all_of_type(current_type, dictionary):
+ all_of_current_type = {}
for key, value in dictionary.items():
- if isinstance(key, types):
- result[key] = value
- return result
+ if type(key) == current_type:
+ all_of_current_type[key] = value
+ return all_of_current_type
def group_by_type(dictionary):
- result_dictionary = {}
+ grouped_items = {}
for key in dictionary.keys():
- if type(key) not in dictionary.keys():
- result_dictionary[type(key)] = find_all_of_type(
+ if type(key) not in grouped_items.keys():
+ grouped_items[type(key)] = find_all_of_type(
type(key), dictionary)
- return result_dictionary
+ return grouped_items
def sort_by(func, arguments):
+ args_copy = arguments
for i in range(0, len(arguments)):
- for j in range(0, len(arguments)):
- if func(arguments[i], arguments[j]) < 0:
- arguments[i], arguments[j] = arguments[j], arguments[i]
- return arguments
+ for j in range(i + 1, len(arguments)):
+ if func(args_copy[i], args_copy[j]) > 0:
+ args_copy[i], args_copy[j] = args_copy[j], args_copy[i]
+ return args_copy
def take_all_of_kind(words, current_word):
all_of_kind = []
for word in words:
if check_two_words(current_word, word):
all_of_kind.append(word)
return all_of_kind
def check_two_words(first_word, second_word):
return sorted(first_word) == sorted(second_word)
def not_in(visited_words, current_word):
for word in visited_words:
if check_two_words(word, current_word):
return False
return True
def anagrams(words):
visited_words = []
sorted_words = []
for word in words:
if not_in(visited_words, word):
visited_words.append(word)
sorted_words.append(take_all_of_kind(words, word))
return sorted_words

Стефани обнови решението на 17.03.2014 00:38 (преди около 10 години)

SMALL_LETTERS = [chr(code)
- for code
- in range(1072, 1104) if code != 1101 and code != 1099]
+ for code in range(1072, 1104)
+ if code != 1101 and code != 1099]
def is_pangram(sentence):
return [letter
for letter in SMALL_LETTERS
if letter not in sentence
and chr(ord(letter) - 32) not in sentence] == []
def frequency(symbol, sentence):
return sum([1 for letter in sentence if letter == symbol])
def char_histogram(text):
items_frequency = {}
for letter in text:
if letter not in items_frequency.keys():
items_frequency[letter] = frequency(letter, text)
return items_frequency
def find_all_of_type(current_type, dictionary):
all_of_current_type = {}
for key, value in dictionary.items():
if type(key) == current_type:
all_of_current_type[key] = value
return all_of_current_type
def group_by_type(dictionary):
grouped_items = {}
for key in dictionary.keys():
if type(key) not in grouped_items.keys():
grouped_items[type(key)] = find_all_of_type(
type(key), dictionary)
return grouped_items
def sort_by(func, arguments):
args_copy = arguments
for i in range(0, len(arguments)):
for j in range(i + 1, len(arguments)):
if func(args_copy[i], args_copy[j]) > 0:
args_copy[i], args_copy[j] = args_copy[j], args_copy[i]
return args_copy
def take_all_of_kind(words, current_word):
all_of_kind = []
for word in words:
if check_two_words(current_word, word):
all_of_kind.append(word)
return all_of_kind
def check_two_words(first_word, second_word):
- return sorted(first_word) == sorted(second_word)
+ letters_first = [letter for letter in first_word if letter.isalpha()]
+ letters_second = [letter for letter in second_word if letter.isalpha()]
+ return sorted(letters_first) == sorted(letters_second)
def not_in(visited_words, current_word):
for word in visited_words:
if check_two_words(word, current_word):
return False
return True
def anagrams(words):
visited_words = []
sorted_words = []
for word in words:
if not_in(visited_words, word):
visited_words.append(word)
sorted_words.append(take_all_of_kind(words, word))
return sorted_words

Анаграма е дума или фраза образувана от буквите на друга дума или фраза, чрез пермутация.

  • Твоето решение не прави разлика между буква и символ
  • Проверката ти в sort_by изпуска няколко случая. Помисли как да я генерализираш

@Кирил, в последното решение на anagram, което бях качила, имам при сравнение на 2 думи letters_first = [letter for letter in first_word if letter.isalpha()] letters_second = [letter for letter in second_word if letter.isalpha()] Това не гарантира ли, че няма да се взимат предвид при сравнението '-', whitespace и т.н. Благодаря предварително! А в sort_by алгоритъмът е стабилен, откъде би могло да идва разминаване ?

  • Права си. По адекватен начин проверяваш дали нещо е символ. Проблемът ти е, че правиш разлика между малки и главни букви, а не трябва :)
  • Грешката ти е толкова коварна, че ще ти дам силен hint: Обърни внимание на тези два вложени for-а. Дали правят точно това, което очакваш във всички случаи?
  • args_copy = arguments не прави копие на arguments. Не, че изобщо ти трябва такова...

Стефани обнови решението на 18.03.2014 20:55 (преди около 10 години)

SMALL_LETTERS = [chr(code)
for code in range(1072, 1104)
if code != 1101 and code != 1099]
def is_pangram(sentence):
return [letter
for letter in SMALL_LETTERS
if letter not in sentence
and chr(ord(letter) - 32) not in sentence] == []
def frequency(symbol, sentence):
return sum([1 for letter in sentence if letter == symbol])
def char_histogram(text):
items_frequency = {}
for letter in text:
if letter not in items_frequency.keys():
items_frequency[letter] = frequency(letter, text)
return items_frequency
def find_all_of_type(current_type, dictionary):
all_of_current_type = {}
for key, value in dictionary.items():
if type(key) == current_type:
all_of_current_type[key] = value
return all_of_current_type
def group_by_type(dictionary):
grouped_items = {}
for key in dictionary.keys():
if type(key) not in grouped_items.keys():
grouped_items[type(key)] = find_all_of_type(
type(key), dictionary)
return grouped_items
def sort_by(func, arguments):
- args_copy = arguments
- for i in range(0, len(arguments)):
+ # make copy of the initial object
+ args_copy = [argument for argument in arguments]
+ for i in range(len(arguments) - 1):
for j in range(i + 1, len(arguments)):
if func(args_copy[i], args_copy[j]) > 0:
args_copy[i], args_copy[j] = args_copy[j], args_copy[i]
return args_copy
def take_all_of_kind(words, current_word):
all_of_kind = []
for word in words:
if check_two_words(current_word, word):
all_of_kind.append(word)
return all_of_kind
def check_two_words(first_word, second_word):
- letters_first = [letter for letter in first_word if letter.isalpha()]
- letters_second = [letter for letter in second_word if letter.isalpha()]
+ letters_first = [letter for letter in first_word.lower()
+ if letter.isalpha()]
+ letters_second = [letter for letter in second_word.lower()
+ if letter.isalpha()]
return sorted(letters_first) == sorted(letters_second)
def not_in(visited_words, current_word):
for word in visited_words:
if check_two_words(word, current_word):
return False
return True
def anagrams(words):
visited_words = []
sorted_words = []
for word in words:
if not_in(visited_words, word):
visited_words.append(word)
sorted_words.append(take_all_of_kind(words, word))
return sorted_words