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

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

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

Резултати

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

Код

BULGARIAN_ALPHABET = [
'а', 'б', 'в', 'г', 'д', 'е',
'ж', 'з', 'и', 'й', 'к', 'л',
'м', 'н', 'о', 'п', 'р', 'с',
'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
]
def is_pangram(sentence):
return all(character in list(sentence.lower())
for character in BULGARIAN_ALPHABET)
def char_histogram(text):
result = {}
letters = []
for character in list(text):
if character not in letters:
letters.append(character)
result[character] = list(text).count(character)
return result
def check_if_sorted(func, arguments):
for position in range(len(arguments) - 1):
if func(arguments[position], arguments[position + 1]) > 0:
return False
return True
def sort_by(func, arguments):
while not check_if_sorted(func, arguments):
for position in range(len(arguments) - 1):
if func(arguments[position], arguments[position + 1]) > 0:
arguments[position], arguments[position + 1] = \
arguments[position + 1], arguments[position]
return arguments
def value_of_key_type(key_type, dictionary):
result = {}
for key in dictionary:
if key_type is type(key):
result[key] = dictionary[key]
return result
def group_by_type(dictionary):
result = {}
key_types = []
for key in dictionary:
if type(key) not in key_types:
key_types.append(type(key))
result[type(key)] = value_of_key_type(type(key), dictionary)
return result
def word_letters(word):
return {key: value for key, value in char_histogram(word).items()
if key.isalpha()}
def letters_histogram(letters, words):
result = []
for word in words:
if word_letters(word) == letters:
result.append(word)
return result
def anagrams(words):
result = []
for word in words:
if letters_histogram(word_letters(word), words) not in result:
result.append(letters_histogram(word_letters(word), words))
return result

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

.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-h498on/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-h498on/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({'Tom Marvolo Riddle'})
frozenset({"So I'm cuter"})
frozenset({'I am Lord Voldemort'})

----------------------------------------------------------------------
Ran 16 tests in 0.016s

FAILED (failures=2)

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

Драгомир обнови решението на 14.03.2014 05:30 (преди около 10 години)

+def is_pangram(sentence):
+ return all(character in list(sentence.lower()) for character in [
+ 'а', 'б', 'в', 'г', 'д', 'е',
+ 'ж', 'з', 'и', 'й', 'к', 'л',
+ 'м', 'н', 'о', 'п', 'р', 'с',
+ 'т', 'у', 'ф', 'х', 'ц', 'ч',
+ 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
+ ])
+
+
+def anagrams_helper(other_word, words):
+ result = []
+ for word in words:
+ if all(character in other_word for character in list(word)):
+ result.append(word)
+ return result
+
+
+def anagrams(words):
+ result = []
+ for word in words:
+ if anagrams_helper(word, words) not in result:
+ result.append(anagrams_helper(word, words))
+ return result
+
+
+def char_histogram(text):
+ result = {}
+ letters = []
+ for character in list(text):
+ if character not in letters:
+ letters.append(character)
+ result[character] = list(text).count(character)
+ return result
+
+
+def group_by_type_helper(key_type, dictionary):
+ result = {}
+ for key in dictionary:
+ if key_type is type(key):
+ result[key] = dictionary[key]
+ return result
+
+
+def group_by_type(dictionary):
+ result = {}
+ key_types = []
+ for key in dictionary:
+ if type(key) not in key_types:
+ key_types.append(type(key))
+ result[type(key)] = group_by_type_helper(type(key), dictionary)
+ return result
+
+
+def check_if_sorted(func, arguments):
+ for position in range(len(arguments) - 1):
+ if func(arguments[position], arguments[position + 1]) > 0:
+ return False
+ return True
+
+
+def sort_by(func, arguments):
+ while not check_if_sorted(func, arguments):
+ for position in range(len(arguments) - 1):
+ if func(arguments[position], arguments[position + 1]) > 0:
+ arguments[position], arguments[position + 1] = \
+ arguments[position + 1], arguments[position]
+ return arguments

Драгомир обнови решението на 15.03.2014 19:22 (преди около 10 години)

def is_pangram(sentence):
return all(character in list(sentence.lower()) for character in [
'а', 'б', 'в', 'г', 'д', 'е',
'ж', 'з', 'и', 'й', 'к', 'л',
'м', 'н', 'о', 'п', 'р', 'с',
'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
])
def anagrams_helper(other_word, words):
result = []
for word in words:
- if all(character in other_word for character in list(word)):
+ if all(character in other_word.lower()
+ for character in list(word.lower())):
result.append(word)
return result
def anagrams(words):
result = []
for word in words:
if anagrams_helper(word, words) not in result:
result.append(anagrams_helper(word, words))
return result
def char_histogram(text):
result = {}
letters = []
for character in list(text):
if character not in letters:
letters.append(character)
result[character] = list(text).count(character)
return result
def group_by_type_helper(key_type, dictionary):
result = {}
for key in dictionary:
if key_type is type(key):
result[key] = dictionary[key]
return result
def group_by_type(dictionary):
result = {}
key_types = []
for key in dictionary:
if type(key) not in key_types:
key_types.append(type(key))
result[type(key)] = group_by_type_helper(type(key), dictionary)
return result
def check_if_sorted(func, arguments):
for position in range(len(arguments) - 1):
if func(arguments[position], arguments[position + 1]) > 0:
return False
return True
def sort_by(func, arguments):
while not check_if_sorted(func, arguments):
for position in range(len(arguments) - 1):
if func(arguments[position], arguments[position + 1]) > 0:
arguments[position], arguments[position + 1] = \
arguments[position + 1], arguments[position]
return arguments

В никакъв случай не добавяй коментар. Ако ти се наложи да коментираш код, написан на Python, в общия случай трябва просто да го пренапишеш.

Нужно е да се намери по-добро име и за двете. Мога да ти помогна с това, ако ми обясниш с по едно изречение какво прави всяка. :)

anagrams връща списък от списъци. При всяко викане anagrams_helper връща списък с думите, които са анаграми.

group_by_type_helper връща речник с всички елементи на първоначалния, които са от еднакъв тип. За първия пример >>> group_by_type({'a': 12, 'b': 1, 1: "foo"})

{<"class 'str'>: {'b': 1, 'a': 12}, <"class 'int'>: {1: 'foo'}} първото викане на group_by_type_helper ще върне речника {'b': 1, 'a': 12}, който отгаваря на типа str на ключовете, а второто ще върне {1: 'foo'}

И двете функции са написани с цел избягване от четворно влагане for -> if -> for -> if

Драгомир обнови решението на 16.03.2014 18:54 (преди около 10 години)

-def is_pangram(sentence):
- return all(character in list(sentence.lower()) for character in [
- 'а', 'б', 'в', 'г', 'д', 'е',
- 'ж', 'з', 'и', 'й', 'к', 'л',
- 'м', 'н', 'о', 'п', 'р', 'с',
- 'т', 'у', 'ф', 'х', 'ц', 'ч',
- 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
- ])
+BULGARIAN_ALPHABET = [
+ 'а', 'б', 'в', 'г', 'д', 'е',
+ 'ж', 'з', 'и', 'й', 'к', 'л',
+ 'м', 'н', 'о', 'п', 'р', 'с',
+ 'т', 'у', 'ф', 'х', 'ц', 'ч',
+ 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
+]
+ENGLISH_ALPHABET = [
+ 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l',
+ 'm', 'n', 'o', 'p', 'q', 'r',
+ 's', 't', 'u', 'v', 'w', 'x',
+ 'y', 'z'
+]
-def anagrams_helper(other_word, words):
- result = []
- for word in words:
- if all(character in other_word.lower()
- for character in list(word.lower())):
- result.append(word)
- return result
+def is_pangram(sentence):
+ return all(character in list(sentence.lower())
+ for character in BULGARIAN_ALPHABET)
-def anagrams(words):
- result = []
- for word in words:
- if anagrams_helper(word, words) not in result:
- result.append(anagrams_helper(word, words))
- return result
-
def char_histogram(text):
result = {}
letters = []
for character in list(text):
if character not in letters:
letters.append(character)
result[character] = list(text).count(character)
return result
+def check_if_sorted(func, arguments):
+ for position in range(len(arguments) - 1):
+ if func(arguments[position], arguments[position + 1]) > 0:
+ return False
+ return True
+
+
+def sort_by(func, arguments):
+ while not check_if_sorted(func, arguments):
+ for position in range(len(arguments) - 1):
+ if func(arguments[position], arguments[position + 1]) > 0:
+ arguments[position], arguments[position + 1] = \
+ arguments[position + 1], arguments[position]
+ return arguments
+
+
def group_by_type_helper(key_type, dictionary):
result = {}
for key in dictionary:
if key_type is type(key):
result[key] = dictionary[key]
return result
def group_by_type(dictionary):
result = {}
key_types = []
for key in dictionary:
if type(key) not in key_types:
key_types.append(type(key))
result[type(key)] = group_by_type_helper(type(key), dictionary)
return result
-def check_if_sorted(func, arguments):
- for position in range(len(arguments) - 1):
- if func(arguments[position], arguments[position + 1]) > 0:
- return False
- return True
+def word_letters(word):
+ return {key: value for key, value in char_histogram(word).items()
+ if key in BULGARIAN_ALPHABET or key in ENGLISH_ALPHABET}
-def sort_by(func, arguments):
- while not check_if_sorted(func, arguments):
+def letters_histogram(letters, words):
- for position in range(len(arguments) - 1):
+ result = []
- if func(arguments[position], arguments[position + 1]) > 0:
+ for word in words:
- arguments[position], arguments[position + 1] = \
+ if word_letters(word) == letters:
- arguments[position + 1], arguments[position]
+ result.append(word)
- return arguments
+ return result
+
+
+def anagrams(words):
+ result = []
+ for word in words:
+ if letters_histogram(word_letters(word), words) not in result:
+ result.append(letters_histogram(word_letters(word), words))
+ return result

Драгомир обнови решението на 17.03.2014 00:50 (преди около 10 години)

BULGARIAN_ALPHABET = [
'а', 'б', 'в', 'г', 'д', 'е',
'ж', 'з', 'и', 'й', 'к', 'л',
'м', 'н', 'о', 'п', 'р', 'с',
'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
]
ENGLISH_ALPHABET = [
'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x',
'y', 'z'
]
def is_pangram(sentence):
return all(character in list(sentence.lower())
for character in BULGARIAN_ALPHABET)
def char_histogram(text):
result = {}
letters = []
for character in list(text):
if character not in letters:
letters.append(character)
result[character] = list(text).count(character)
return result
def check_if_sorted(func, arguments):
for position in range(len(arguments) - 1):
if func(arguments[position], arguments[position + 1]) > 0:
return False
return True
def sort_by(func, arguments):
while not check_if_sorted(func, arguments):
for position in range(len(arguments) - 1):
if func(arguments[position], arguments[position + 1]) > 0:
arguments[position], arguments[position + 1] = \
arguments[position + 1], arguments[position]
return arguments
-def group_by_type_helper(key_type, dictionary):
+def value_of_key_type(key_type, dictionary):
result = {}
for key in dictionary:
if key_type is type(key):
result[key] = dictionary[key]
return result
def group_by_type(dictionary):
result = {}
key_types = []
for key in dictionary:
if type(key) not in key_types:
key_types.append(type(key))
- result[type(key)] = group_by_type_helper(type(key), dictionary)
+ result[type(key)] = value_of_key_type(type(key), dictionary)
return result
def word_letters(word):
return {key: value for key, value in char_histogram(word).items()
if key in BULGARIAN_ALPHABET or key in ENGLISH_ALPHABET}
def letters_histogram(letters, words):
result = []
for word in words:
if word_letters(word) == letters:
result.append(word)
return result
def anagrams(words):
result = []
for word in words:
if letters_histogram(word_letters(word), words) not in result:
result.append(letters_histogram(word_letters(word), words))
return result

Драгомир обнови решението на 17.03.2014 14:04 (преди около 10 години)

BULGARIAN_ALPHABET = [
'а', 'б', 'в', 'г', 'д', 'е',
'ж', 'з', 'и', 'й', 'к', 'л',
'м', 'н', 'о', 'п', 'р', 'с',
'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
]
-ENGLISH_ALPHABET = [
- 'a', 'b', 'c', 'd', 'e', 'f',
- 'g', 'h', 'i', 'j', 'k', 'l',
- 'm', 'n', 'o', 'p', 'q', 'r',
- 's', 't', 'u', 'v', 'w', 'x',
- 'y', 'z'
-]
-
def is_pangram(sentence):
return all(character in list(sentence.lower())
for character in BULGARIAN_ALPHABET)
def char_histogram(text):
result = {}
letters = []
for character in list(text):
if character not in letters:
letters.append(character)
result[character] = list(text).count(character)
return result
def check_if_sorted(func, arguments):
for position in range(len(arguments) - 1):
if func(arguments[position], arguments[position + 1]) > 0:
return False
return True
def sort_by(func, arguments):
while not check_if_sorted(func, arguments):
for position in range(len(arguments) - 1):
if func(arguments[position], arguments[position + 1]) > 0:
arguments[position], arguments[position + 1] = \
arguments[position + 1], arguments[position]
return arguments
def value_of_key_type(key_type, dictionary):
result = {}
for key in dictionary:
if key_type is type(key):
result[key] = dictionary[key]
return result
def group_by_type(dictionary):
result = {}
key_types = []
for key in dictionary:
if type(key) not in key_types:
key_types.append(type(key))
result[type(key)] = value_of_key_type(type(key), dictionary)
return result
def word_letters(word):
return {key: value for key, value in char_histogram(word).items()
- if key in BULGARIAN_ALPHABET or key in ENGLISH_ALPHABET}
+ if key.isalpha()}
def letters_histogram(letters, words):
result = []
for word in words:
if word_letters(word) == letters:
result.append(word)
return result
def anagrams(words):
result = []
for word in words:
if letters_histogram(word_letters(word), words) not in result:
result.append(letters_histogram(word_letters(word), words))
return result