Решение на Пет функции от Теодор Драганов

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

Към профила на Теодор Драганов

Резултати

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

Код

BG_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
def is_pangram(sentence):
for bg_letter in BG_ALPHABET:
if bg_letter not in sentence and bg_letter.upper() not in sentence:
return False
return True
def char_histogram(text):
return {key: text.count(key) for key in text if text.count(key) != 0}
def sort_by(function, to_sort):
for left_to_right in range(len(to_sort)-1):
for right_to_left in range(len(to_sort)-1, left_to_right, -1):
if function(to_sort[left_to_right], to_sort[right_to_left]) > 0:
to_sort[left_to_right], to_sort[right_to_left] \
= to_sort[right_to_left], to_sort[left_to_right]
return to_sort
def group_by_type(dictionary):
sorted_by_type = {}
for key in dictionary:
if type(key) in sorted_by_type:
sorted_by_type[type(key)][key] = dictionary[key]
else:
sorted_by_type[type(key)] = {key: dictionary[key]}
return sorted_by_type
def alpha_histogram(text):
return {key: text.lower().count(key) for key in text.lower()
if key.isalpha() and text.lower().count(key) != 0}
def anagrams(words):
anagrams_sorted = []
for word in words:
anagrams_of_word = sorted(
[anagram for anagram in words
if alpha_histogram(anagram) == alpha_histogram(word)])
if anagrams_of_word not in anagrams_sorted:
anagrams_sorted.append(anagrams_of_word)
return anagrams_sorted

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

...............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-1s7tf13/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, 4, 2, 3, 1, 5]

First differing element 1:
2
4

- [0, 2, 4, 1, 3, 5]
?        ^     ---

+ [0, 4, 2, 3, 1, 5]
?     +++   ^


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

FAILED (failures=1)

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

Теодор обнови решението на 14.03.2014 14:13 (преди над 10 години)

+BG_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
+
+def is_pangram(sentence):
+ for bg_letter in BG_ALPHABET:
+ if bg_letter not in sentence:
+ return False
+ return True
+
+def char_histogram(text):
+ return {key : text.count(key) for key in text if text.count(key) != 0}
+
+def sort_by(function, list_to_sort):
+ for index in range(len(list_to_sort)-1):
+ if function(list_to_sort[index], list_to_sort[index+1]) > 0:
+ list_to_sort[index], list_to_sort[index+1] = \
+ list_to_sort[index+1], list_to_sort[index]
+ return list_to_sort
+
+def group_by_type(dictionary):
+ dict_by_type = {}
+ for key in dictionary:
+ if type(key) in dict_by_type:
+ dict_by_type[type(key)][key] = dictionary[key]
+ else:
+ dict_by_type[type(key)] = {key:dictionary[key]}
+ return dict_by_type
+
+def anagrams(words):
+ anagrams_sorted = []
+ for word in words:
+ anagrams_of_word = sorted([anagram for anagram in words \
+ if char_histogram(anagram)==char_histogram(word)])
+ if anagrams_of_word not in anagrams_sorted:
+ anagrams_sorted.append(anagrams_of_word)
+ return anagrams_sorted

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

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

Теодор обнови решението на 16.03.2014 01:03 (преди над 10 години)

BG_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
def is_pangram(sentence):
for bg_letter in BG_ALPHABET:
- if bg_letter not in sentence:
+ if bg_letter not in sentence or bg_letter.upper() not in sentence:
return False
return True
def char_histogram(text):
return {key : text.count(key) for key in text if text.count(key) != 0}
def sort_by(function, list_to_sort):
for index in range(len(list_to_sort)-1):
if function(list_to_sort[index], list_to_sort[index+1]) > 0:
list_to_sort[index], list_to_sort[index+1] = \
list_to_sort[index+1], list_to_sort[index]
return list_to_sort
def group_by_type(dictionary):
dict_by_type = {}
for key in dictionary:
if type(key) in dict_by_type:
dict_by_type[type(key)][key] = dictionary[key]
else:
dict_by_type[type(key)] = {key:dictionary[key]}
return dict_by_type
def anagrams(words):
anagrams_sorted = []
for word in words:
anagrams_of_word = sorted([anagram for anagram in words \
if char_histogram(anagram)==char_histogram(word)])
if anagrams_of_word not in anagrams_sorted:
anagrams_sorted.append(anagrams_of_word)
return anagrams_sorted

Теодор обнови решението на 16.03.2014 02:05 (преди над 10 години)

BG_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
def is_pangram(sentence):
for bg_letter in BG_ALPHABET:
if bg_letter not in sentence or bg_letter.upper() not in sentence:
return False
return True
def char_histogram(text):
return {key : text.count(key) for key in text if text.count(key) != 0}
-def sort_by(function, list_to_sort):
- for index in range(len(list_to_sort)-1):
- if function(list_to_sort[index], list_to_sort[index+1]) > 0:
- list_to_sort[index], list_to_sort[index+1] = \
- list_to_sort[index+1], list_to_sort[index]
- return list_to_sort
+def sort_by(function, items_to_sort):
+ for left_to_right in range(len(items_to_sort)-1) :
+ for right_to_left in range(len(items_to_sort)-1, left_to_right, -1):
+ if function(items_to_sort[left_to_right], \
+ items_to_sort[right_to_left]) > 0:
+ items_to_sort[left_to_right], items_to_sort[right_to_left] \
+ = items_to_sort[right_to_left], items_to_sort[left_to_right]
+ return items_to_sort
def group_by_type(dictionary):
- dict_by_type = {}
+ sorted_by_type = {}
for key in dictionary:
- if type(key) in dict_by_type:
- dict_by_type[type(key)][key] = dictionary[key]
+ if type(key) in sorted_by_type:
+ sorted_by_type[type(key)][key] = dictionary[key]
else:
- dict_by_type[type(key)] = {key:dictionary[key]}
- return dict_by_type
+ sorted_by_type[type(key)] = {key:dictionary[key]}
+ return sorted_by_type
def anagrams(words):
anagrams_sorted = []
for word in words:
anagrams_of_word = sorted([anagram for anagram in words \
if char_histogram(anagram)==char_histogram(word)])
if anagrams_of_word not in anagrams_sorted:
anagrams_sorted.append(anagrams_of_word)
- return anagrams_sorted
+ return anagrams_sorted
+
+print(sort_by(lambda x, y: len(x) - len(y), ['abc', 'a', 'ab']))

Теодор обнови решението на 16.03.2014 02:13 (преди над 10 години)

BG_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
def is_pangram(sentence):
for bg_letter in BG_ALPHABET:
- if bg_letter not in sentence or bg_letter.upper() not in sentence:
+ if bg_letter not in sentence and bg_letter.upper() not in sentence:
return False
return True
def char_histogram(text):
return {key : text.count(key) for key in text if text.count(key) != 0}
def sort_by(function, items_to_sort):
for left_to_right in range(len(items_to_sort)-1) :
for right_to_left in range(len(items_to_sort)-1, left_to_right, -1):
if function(items_to_sort[left_to_right], \
items_to_sort[right_to_left]) > 0:
items_to_sort[left_to_right], items_to_sort[right_to_left] \
= items_to_sort[right_to_left], items_to_sort[left_to_right]
return items_to_sort
def group_by_type(dictionary):
sorted_by_type = {}
for key in dictionary:
if type(key) in sorted_by_type:
sorted_by_type[type(key)][key] = dictionary[key]
else:
sorted_by_type[type(key)] = {key:dictionary[key]}
return sorted_by_type
def anagrams(words):
anagrams_sorted = []
for word in words:
anagrams_of_word = sorted([anagram for anagram in words \
if char_histogram(anagram)==char_histogram(word)])
if anagrams_of_word not in anagrams_sorted:
anagrams_sorted.append(anagrams_of_word)
- return anagrams_sorted
-
+ return anagrams_sorted
-print(sort_by(lambda x, y: len(x) - len(y), ['abc', 'a', 'ab']))

Теодор обнови решението на 18.03.2014 03:55 (преди над 10 години)

BG_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
+
def is_pangram(sentence):
for bg_letter in BG_ALPHABET:
if bg_letter not in sentence and bg_letter.upper() not in sentence:
return False
return True
+
def char_histogram(text):
- return {key : text.count(key) for key in text if text.count(key) != 0}
+ return {key: text.count(key) for key in text if text.count(key) != 0}
-def sort_by(function, items_to_sort):
- for left_to_right in range(len(items_to_sort)-1) :
- for right_to_left in range(len(items_to_sort)-1, left_to_right, -1):
- if function(items_to_sort[left_to_right], \
- items_to_sort[right_to_left]) > 0:
- items_to_sort[left_to_right], items_to_sort[right_to_left] \
- = items_to_sort[right_to_left], items_to_sort[left_to_right]
- return items_to_sort
+def sort_by(function, sort_items):
+ for left_to_right in range(len(sort_items)-1):
+ for right_to_left in range(len(sort_items)-1, left_to_right, -1):
+ if function(sort_items[left_to_right],
+ sort_items[right_to_left]) > 0:
+ sort_items[left_to_right], sort_items[right_to_left] \
+ = sort_items[right_to_left], sort_items[left_to_right]
+ return sort_items
+
+
def group_by_type(dictionary):
sorted_by_type = {}
for key in dictionary:
if type(key) in sorted_by_type:
sorted_by_type[type(key)][key] = dictionary[key]
else:
- sorted_by_type[type(key)] = {key:dictionary[key]}
+ sorted_by_type[type(key)] = {key: dictionary[key]}
return sorted_by_type
+
+def alpha_histogram(text):
+ return {key: text.count(key) for key in text
+ if key.isalpha() and text.count(key) != 0}
+
+
def anagrams(words):
anagrams_sorted = []
for word in words:
- anagrams_of_word = sorted([anagram for anagram in words \
- if char_histogram(anagram)==char_histogram(word)])
+ anagrams_of_word = sorted(
+ [anagram for anagram in words
+ if alpha_histogram(anagram) == alpha_histogram(word)])
if anagrams_of_word not in anagrams_sorted:
anagrams_sorted.append(anagrams_of_word)
- return anagrams_sorted
+ return anagrams_sorted

Теодор обнови решението на 18.03.2014 13:53 (преди над 10 години)

BG_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
def is_pangram(sentence):
for bg_letter in BG_ALPHABET:
if bg_letter not in sentence and bg_letter.upper() not in sentence:
return False
return True
def char_histogram(text):
return {key: text.count(key) for key in text if text.count(key) != 0}
-def sort_by(function, sort_items):
- for left_to_right in range(len(sort_items)-1):
- for right_to_left in range(len(sort_items)-1, left_to_right, -1):
- if function(sort_items[left_to_right],
- sort_items[right_to_left]) > 0:
- sort_items[left_to_right], sort_items[right_to_left] \
- = sort_items[right_to_left], sort_items[left_to_right]
- return sort_items
+def sort_by(function, to_sort):
+ for left_to_right in range(len(to_sort)-1):
+ for right_to_left in range(len(to_sort)-1, left_to_right, -1):
+ if function(to_sort[left_to_right], to_sort[right_to_left]) > 0:
+ to_sort[left_to_right], to_sort[right_to_left] \
+ = to_sort[right_to_left], to_sort[left_to_right]
+ return to_sort
def group_by_type(dictionary):
sorted_by_type = {}
for key in dictionary:
if type(key) in sorted_by_type:
sorted_by_type[type(key)][key] = dictionary[key]
else:
sorted_by_type[type(key)] = {key: dictionary[key]}
return sorted_by_type
def alpha_histogram(text):
- return {key: text.count(key) for key in text
- if key.isalpha() and text.count(key) != 0}
+ return {key: text.lower().count(key) for key in text.lower()
+ if key.isalpha() and text.lower().count(key) != 0}
def anagrams(words):
anagrams_sorted = []
for word in words:
anagrams_of_word = sorted(
[anagram for anagram in words
if alpha_histogram(anagram) == alpha_histogram(word)])
if anagrams_of_word not in anagrams_sorted:
anagrams_sorted.append(anagrams_of_word)
return anagrams_sorted