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

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

Към профила на Петър Парушев

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 12 успешни тест(а)
  • 4 неуспешни тест(а)

Код

import collections
def is_pangram(sentence):
sentence.lower()
letter_counter = 0
for unicode_index in range(ord('а'), ord('я')+1):
if chr(unicode_index) in sentence:
letter_counter += 1
return letter_counter == 30
def char_histogram(text):
result_histogram = collections.Counter()
for current_char in text:
result_histogram[current_char] += 1
return dict(result_histogram)
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 group_by_type(dictionary):
result = {}
for dict_key, dict_value in dictionary.items():
current_type = type(dict_key)
if current_type not in result:
result[current_type] = dict()
result[current_type][dict_key] = dict_value
return result
def anagrams(words):
result = []
for word in words:
anagrams_found = False
for nested_list in result:
if char_histogram(word) == char_histogram(nested_list[0]):
nested_list.append(word)
anagrams_found = True
if not anagrams_found:
result.append([word])
return result

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

.FF.........F..F
======================================================================
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-14fskue/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-14fskue/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({'Tom Marvolo Riddle', 'I am Lord Voldemort'})
frozenset({"So I'm cuter", 'Tom Cruise'})
Items in the second set but not the first:
frozenset({'I am Lord Voldemort'})
frozenset({'Tom Marvolo Riddle'})
frozenset({'Tom Cruise'})
frozenset({"So I'm cuter"})

======================================================================
FAIL: test_with_pangrams (test.TestIsPangram)
----------------------------------------------------------------------
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-14fskue/test.py", line 15, in test_with_pangrams
    solution.is_pangram('За миг бях в чужд, скърцащ плюшен фотьойл.'))
AssertionError: False is not true

======================================================================
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-14fskue/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.012s

FAILED (failures=4)

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

Петър обнови решението на 18.03.2014 03:47 (преди около 10 години)

+def is_pangram(sentence):
+ str.lower(sentence)
+ for unicode_index in range(int('430', 16), int('44F', 16)):
+ if unicode_index == int('44B', 16):
+ continue
+ if unicode_index == int('44D', 16):
+ continue
+ letter_found = False
+ current_unicode_char = chr(unicode_index)
+ for sentence_char in sentence:
+ if current_unicode_char == sentence_char:
+ letter_found = True
+ if letter_found:
+ continue
+ else:
+ return False
+ return True
+
+
+def char_histogram(text):
+ result_histogram = {}
+ for current_char in text:
+ if current_char in result_histogram:
+ result_histogram[current_char] += 1
+ else:
+ result_histogram[current_char] = 1
+ return result_histogram

Петър обнови решението на 19.03.2014 15:55 (преди около 10 години)

+import collections
+
+
def is_pangram(sentence):
- str.lower(sentence)
- for unicode_index in range(int('430', 16), int('44F', 16)):
- if unicode_index == int('44B', 16):
- continue
- if unicode_index == int('44D', 16):
- continue
- letter_found = False
- current_unicode_char = chr(unicode_index)
- for sentence_char in sentence:
- if current_unicode_char == sentence_char:
- letter_found = True
- if letter_found:
- continue
- else:
- return False
- return True
+ sentence.lower()
+ letter_counter = 0
+ for unicode_index in range(ord('а'), ord('я')+1):
+ if chr(unicode_index) in sentence:
+ letter_counter += 1
+ return letter_counter == 30
def char_histogram(text):
- result_histogram = {}
+ result_histogram = collections.Counter()
for current_char in text:
- if current_char in result_histogram:
- result_histogram[current_char] += 1
+ result_histogram[current_char] += 1
- else:
+ return dict(result_histogram)
- result_histogram[current_char] = 1
+
- return result_histogram
+
+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 group_by_type(dictionary):
+ result = {}
+ for dict_key, dict_value in dictionary.items():
+ current_type = type(dict_key)
+ if current_type not in result:
+ result[current_type] = dict()
+ result[current_type][dict_key] = dict_value
+ return result
+
+
+def anagrams(words):
+ result = []
+ for word in words:
+ anagrams_found = False
+ for nested_list in result:
+ if char_histogram(word) == char_histogram(nested_list[0]):
+ nested_list.append(word)
+ anagrams_found = True
+ if not anagrams_found:
+ result.append([word])
+ return result