Решение на Пет функции от Виктор Иванов

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

Към профила на Виктор Иванов

Резултати

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

Код

def is_pangram(sentence):
alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
used = {}
for c in sentence:
used[c.lower()] = True
for letter in alphabet:
if letter not in used:
return False
return True
def char_histogram(text):
count = {}
for c in text:
if c in count:
count[c] += 1
else:
count[c] = 1
return count
def sort_by(func, arguments):
result = []
length = len(arguments)
if length < 2:
return arguments
middle = length // 2
left = sort_by(func, arguments[:middle])
right = sort_by(func, arguments[middle:])
left_length = len(left)
right_length = len(right)
left_index = 0
right_index = 0
while left_index < left_length and right_index < right_length:
if func(left[left_index], right[right_index]) > 0:
result.append(right[right_index])
right_index += 1
else:
result.append(left[left_index])
left_index += 1
result += left[left_index:]
result += right[right_index:]
return result
def group_by_type(dictionary):
result = {}
for key in dictionary.keys():
if type(key) not in result:
result[type(key)] = {}
result[type(key)][key] = dictionary[key]
return result
def letters_histogram(text):
count = {}
for c in text:
ord_c = ord(c.lower())
if ord_c >= ord('a') and ord_c <= ord('z'):
if c in count:
count[c.lower()] += 1
else:
count[c.lower()] = 1
return count
def anagrams(words):
used = {}
result = []
for w1 in words:
if w1 not in used:
current_result = [w1]
used[w1] = True
for w2 in words:
if (w2 not in used and
letters_histogram(w1) == letters_histogram(w2)):
current_result.append(w2)
used[w2] = True
result.append(current_result)
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-1opiykz/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-1opiykz/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'})
Items in the second set but not the first:
frozenset({'Tom Marvolo Riddle'})
frozenset({'I am Lord Voldemort'})

----------------------------------------------------------------------
Ran 16 tests in 0.012s

FAILED (failures=2)

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

Виктор обнови решението на 14.03.2014 14:42 (преди около 10 години)

+def is_pangram(sentence):
+
+ alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
+ 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
+ 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
+ used = {}
+ for c in sentence:
+ used[c.lower()] = True
+ for letter in alphabet:
+ if letter not in used:
+ return False
+ return True
+
+def char_histogram(text):
+
+ count = {}
+ for c in text:
+ if c in count:
+ count[c] += 1
+ else:
+ count[c] = 1
+ return count
+
+def sort_by(func, arguments):
+
+ res = []
+ length = len(arguments)
+ if length < 2:
+ return arguments
+ mid = length // 2
+ left = sort_by(func, arguments[:mid])
+ right = sort_by(func, arguments[mid:])
+ l1 = len(left)
+ l2 = len(right)
+ i = 0
+ j = 0
+ while i < l1 and j < l2:
+ if func(left[i], right[j]) > 0:
+ res.append(right[j])
+ j += 1
+ else:
+ res.append(left[i])
+ i += 1
+ res += left[i:]
+ res += right[j:]
+ return res
+
+def group_by_type(dictionary):
+
+ res = {}
+ for key in dictionary.keys():
+ if type(key) not in res:
+ res[type(key)] = {}
+ res[type(key)][key] = dictionary[key]
+ return res
+
+def anagrams(words):
+
+ used = {}
+ res = []
+ for w1 in words:
+ if w1 not in used:
+ current_res = [w1]
+ used[w1] = True
+ for w2 in words:
+ if w2 not in used and char_histogram(w1) == char_histogram(w2):
+ current_res.append(w2)
+ used[w2] = True
+ res.append(current_res)
+ return res
+

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

  • Твоето решение не прави разлика между буква и символ.
  • Дюд, пишеш C код в кожата на Python (кожата на питон... get it? get it?).
  • res, i, j, l1, l2 и mid не означават нищо. Опитай да именоваш нещата си по-адекватно

Виктор обнови решението на 16.03.2014 14:39 (преди около 10 години)

def is_pangram(sentence):
alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
used = {}
for c in sentence:
used[c.lower()] = True
for letter in alphabet:
if letter not in used:
return False
return True
def char_histogram(text):
count = {}
for c in text:
if c in count:
count[c] += 1
else:
count[c] = 1
return count
def sort_by(func, arguments):
- res = []
+ result = []
length = len(arguments)
if length < 2:
return arguments
- mid = length // 2
- left = sort_by(func, arguments[:mid])
- right = sort_by(func, arguments[mid:])
- l1 = len(left)
- l2 = len(right)
- i = 0
- j = 0
- while i < l1 and j < l2:
- if func(left[i], right[j]) > 0:
- res.append(right[j])
- j += 1
+ middle = length // 2
+ left = sort_by(func, arguments[:middle])
+ right = sort_by(func, arguments[middle:])
+ left_length = len(left)
+ right_length = len(right)
+ left_index = 0
+ right_index = 0
+ while left_index < left_length and right_index < right_length:
+ if func(left[left_index], right[right_index]) > 0:
+ result.append(right[right_index])
+ right_index += 1
else:
- res.append(left[i])
- i += 1
- res += left[i:]
- res += right[j:]
- return res
+ result.append(left[left_index])
+ left_index += 1
+ result += left[left_index:]
+ result += right[right_index:]
+ return result
def group_by_type(dictionary):
- res = {}
+ result = {}
for key in dictionary.keys():
- if type(key) not in res:
- res[type(key)] = {}
- res[type(key)][key] = dictionary[key]
- return res
+ if type(key) not in result:
+ result[type(key)] = {}
+ result[type(key)][key] = dictionary[key]
+ return result
+
+def letters_histogram(text):
+
+ count = {}
+ for c in text:
+ ord_c = ord(c.lower())
+ if ord_c >= ord('a') and ord_c <= ord('z'):
+ if c in count:
+ count[c.lower()] += 1
+ else:
+ count[c.lower()] = 1
+ return count
+
def anagrams(words):
used = {}
- res = []
+ result = []
for w1 in words:
if w1 not in used:
- current_res = [w1]
+ current_result = [w1]
used[w1] = True
for w2 in words:
- if w2 not in used and char_histogram(w1) == char_histogram(w2):
- current_res.append(w2)
+ if (w2 not in used and
+ letters_histogram(w1) == letters_histogram(w2)):
+ current_result.append(w2)
used[w2] = True
- res.append(current_res)
- return res
+ result.append(current_result)
-
+ return result