Решение на Пет функции от Емилиан Станков

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

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

Резултати

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

Код

def is_pangram(sentence):
alphabet = ('А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж', 'З', 'И', 'Й', 'К', 'Л',
'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч',
'Ш', 'Щ', 'Ъ', 'Ь', 'Ю', 'Я')
sentence = sentence.upper()
is_pangram = False
for letter in alphabet:
if letter in sentence:
is_pangram = True
else:
is_pangram = False
break
return is_pangram
def char_histogram(text):
histogram = {}
for char in text:
if char not in histogram.keys():
histogram[char] = 1
else:
histogram[char] += 1
return histogram
def sort_by(func, arguments):
for i in range(len(arguments) - 1):
for j in range(len(arguments) - 1):
if i != j and func(arguments[i], arguments[j]) > 0:
placeholder = arguments[i]
arguments[i] = arguments[i + 1]
arguments[i + 1] = placeholder
return arguments
def group_by_type(dictionary):
grouped_dictionary = {}
for key, value in dictionary.items():
if type(key) not in grouped_dictionary.keys():
grouped_dictionary[type(key)] = {key: value}
else:
grouped_dictionary[type(key)][key] = value
return grouped_dictionary
def anagrams(words):
anagrams = []
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']
for word in words:
current_word_anagrams = []
word_letters_only = ''
for letter in word:
if letter.lower() in alphabet:
word_letters_only += letter
for other_word in words:
other_word_letters_only = ''
for letter in other_word:
if letter.lower() in alphabet:
other_word_letters_only += letter
sorted_word_letters = sorted(word_letters_only.lower())
sorted_other_word_letters = sorted(other_word_letters_only.lower())
if sorted_word_letters == sorted_other_word_letters:
if word not in current_word_anagrams:
current_word_anagrams.append(word)
if other_word not in current_word_anagrams:
current_word_anagrams.append(other_word)
current_word_anagrams = sorted(current_word_anagrams)
if current_word_anagrams not in anagrams:
anagrams.append(current_word_anagrams)
return anagrams

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

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

First differing element 2:
4
3

- [0, 2, 4, 1, 3, 5]
+ [0, 2, 3, 4, 5, 1]

----------------------------------------------------------------------
Ran 16 tests in 0.015s

FAILED (failures=1)

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

Емилиан обнови решението на 13.03.2014 00:53 (преди над 10 години)

+def is_pangram(sentence):
+ alphabet = ('А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж', 'З', 'И', 'Й', 'К', 'Л',
+ 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч',
+ 'Ш', 'Щ', 'Ъ', 'Ь', 'Ю', 'Я')
+ sentence = sentence.upper()
+ is_pangram = False
+ for letter in alphabet:
+ if letter in sentence:
+ is_pangram = True
+ else:
+ is_pangram = False
+ break
+
+ return is_pangram
+
+
+def char_histogram(text):
+ histogram = {}
+ for char in text:
+ if char not in histogram.keys():
+ histogram[char] = 1
+ else:
+ histogram[char] += 1
+
+ return histogram
+
+
+def sort_by(func, arguments):
+ for i in range(len(arguments) - 1):
+ if func(arguments[i], arguments[i + 1]) > 0:
+ temp = arguments[i]
+ arguments[i] = arguments[i + 1]
+ arguments[i + 1] = temp
+
+ return arguments
+
+
+def group_by_type(dictionary):
+ grouped_dictionary = {}
+ for key, value in dictionary.items():
+ if type(key) not in grouped_dictionary.keys():
+ grouped_dictionary[type(key)] = {key: value}
+ else:
+ grouped_dictionary[type(key)][key] = value
+
+ return grouped_dictionary
+
+
+def anagrams(words):
+ anagrams = []
+ for word in words:
+ temp = []
+ for other_word in words:
+ if sorted(word) == sorted(other_word):
+ if word not in temp:
+ temp.append(word)
+ if other_word not in temp:
+ temp.append(other_word)
+ temp = sorted(temp)
+ if temp not in anagrams:
+ anagrams.append(temp)
+
+ return anagrams

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

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

Емилиан обнови решението на 16.03.2014 22:44 (преди над 10 години)

def is_pangram(sentence):
alphabet = ('А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж', 'З', 'И', 'Й', 'К', 'Л',
'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч',
'Ш', 'Щ', 'Ъ', 'Ь', 'Ю', 'Я')
sentence = sentence.upper()
is_pangram = False
for letter in alphabet:
if letter in sentence:
is_pangram = True
else:
is_pangram = False
break
return is_pangram
def char_histogram(text):
histogram = {}
for char in text:
if char not in histogram.keys():
histogram[char] = 1
else:
histogram[char] += 1
return histogram
def sort_by(func, arguments):
for i in range(len(arguments) - 1):
- if func(arguments[i], arguments[i + 1]) > 0:
- temp = arguments[i]
- arguments[i] = arguments[i + 1]
- arguments[i + 1] = temp
+ for j in range(len(arguments) - 1):
+ if i != j and func(arguments[i], arguments[j]) > 0:
+ placeholder = arguments[i]
+ arguments[i] = arguments[i + 1]
+ arguments[i + 1] = placeholder
return arguments
def group_by_type(dictionary):
grouped_dictionary = {}
for key, value in dictionary.items():
if type(key) not in grouped_dictionary.keys():
grouped_dictionary[type(key)] = {key: value}
else:
grouped_dictionary[type(key)][key] = value
return grouped_dictionary
def anagrams(words):
anagrams = []
+ 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']
for word in words:
- temp = []
+ current_word_anagrams = []
+ word_letters_only = ''
+
+ for letter in word:
+ if letter.lower() in alphabet:
+ word_letters_only += letter
+
for other_word in words:
- if sorted(word) == sorted(other_word):
- if word not in temp:
- temp.append(word)
- if other_word not in temp:
- temp.append(other_word)
- temp = sorted(temp)
- if temp not in anagrams:
- anagrams.append(temp)
+ other_word_letters_only = ''
+ for letter in other_word:
+ if letter.lower() in alphabet:
+ other_word_letters_only += letter
- return anagrams
+ sorted_word_letters = sorted(word_letters_only.lower())
+ sorted_other_word_letters = sorted(other_word_letters_only.lower())
+ if sorted_word_letters == sorted_other_word_letters:
+ if word not in current_word_anagrams:
+ current_word_anagrams.append(word)
+ if other_word not in current_word_anagrams:
+ current_word_anagrams.append(other_word)
+ current_word_anagrams = sorted(current_word_anagrams)
+
+ if current_word_anagrams not in anagrams:
+ anagrams.append(current_word_anagrams)
+
+ return anagrams