Решение на Пет функции от Николай Масларски

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

Към профила на Николай Масларски

Резултати

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

Код

def is_pangram(sentance):
BG_ALPHABET = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
for letter in BG_ALPHABET:
if letter not in sentance.lower():
return False
return True
def char_histogram(text):
histogram = {}
for charecter in text:
if charecter not in histogram:
histogram[charecter] = 0
histogram[charecter] += 1
return histogram
def sort_by(func, arguments):
sorted_arguments = arguments
for i in range(len(sorted_arguments) - 1):
for j in range(len(sorted_arguments) - 1):
if func(sorted_arguments[j], sorted_arguments[j + 1]) \
> 0:
sorted_arguments[j], sorted_arguments[j + 1] = \
sorted_arguments[j + 1], sorted_arguments[j]
return sorted_arguments
def group_by_type(dictionary):
result = {}
for key in dictionary:
if type(key) not in result:
result[type(key)] = {}
result[type(key)][key] = dictionary[key]
return result
def is_anagram(word_1, word_2):
for char in ",.'-? ":
word_1 = word_1.replace(char, "")
word_2 = word_2.replace(char, "")
for letter in word_1:
if letter not in word_2.lower():
return False
return True
def anagrams(words):
result = []
temp_result = []
for word in words:
temp = [anagram for anagram in words if is_anagram(anagram, word)]
temp_result.append(temp)
for item in temp_result:
if item not in result:
result.append(item)
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-1e6ys4a/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()

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

----------------------------------------------------------------------
Ran 16 tests in 0.010s

FAILED (failures=2)

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

Николай обнови решението на 19.03.2014 00:50 (преди над 10 години)

+def is_pangram(sentance):
+ BG_ALPHABET = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
+ 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
+ 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
+
+ for letter in BG_ALPHABET:
+ if letter not in sentance.lower():
+ return False
+ return True
+
+
+def char_histogram(text):
+ histogram = {}
+ for charecter in text:
+ if charecter not in histogram:
+ histogram[charecter] = 0
+ histogram[charecter] += 1
+
+ return histogram
+
+def sort_by(func, arguments):
+ sorted_arguments = arguments
+ for index1 in range(len(sorted_arguments) - 1):
+ for index2 in range(len(sorted_arguments) - 1):
+ if func(sorted_arguments[index2], sorted_arguments[index2+1]) > 0:
+ sorted_arguments[index2], sorted_arguments[index2 + 1] = \
+ sorted_arguments[index2 + 1], sorted_arguments[index2]
+
+ return sorted_arguments
+
+
+
+def group_by_type(dictionary):
+
+ result = {}
+ for key in dictionary:
+ if type(key) not in result:
+ result[type(key)] = {}
+ result[type(key)][key] = dictionary[key]
+ return result
+
+
+def is_anagram(word_1,word_2):
+ for char in ",.'-? ":
+ word_1 = word_1.replace(char, "")
+ word_2 = word_2.replace(char, "")
+
+ for letter in word_1:
+ if letter not in word_2.lower():
+ return False
+ return True
+
+
+def anagrams(words):
+
+ result = []
+ temp_result = []
+
+ for word_1 in words:
+ temp = [word_2 for word_2 in words if is_anagram(word_2,word_1)]
+ temp_result.append(temp)
+
+ for item in temp_result:
+ if item not in result:
+ result.append(item)
+
+ return result

Николай обнови решението на 19.03.2014 15:54 (преди над 10 години)

def is_pangram(sentance):
BG_ALPHABET = ('а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й',
'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у',
'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я')
for letter in BG_ALPHABET:
if letter not in sentance.lower():
return False
return True
def char_histogram(text):
histogram = {}
for charecter in text:
if charecter not in histogram:
histogram[charecter] = 0
histogram[charecter] += 1
return histogram
+
def sort_by(func, arguments):
sorted_arguments = arguments
- for index1 in range(len(sorted_arguments) - 1):
- for index2 in range(len(sorted_arguments) - 1):
- if func(sorted_arguments[index2], sorted_arguments[index2+1]) > 0:
- sorted_arguments[index2], sorted_arguments[index2 + 1] = \
- sorted_arguments[index2 + 1], sorted_arguments[index2]
+ for i in range(len(sorted_arguments) - 1):
+ for j in range(len(sorted_arguments) - 1):
+ if func(sorted_arguments[j], sorted_arguments[j + 1]) \
+ > 0:
+ sorted_arguments[j], sorted_arguments[j + 1] = \
+ sorted_arguments[j + 1], sorted_arguments[j]
return sorted_arguments
-
def group_by_type(dictionary):
-
result = {}
for key in dictionary:
if type(key) not in result:
result[type(key)] = {}
result[type(key)][key] = dictionary[key]
return result
-def is_anagram(word_1,word_2):
+def is_anagram(word_1, word_2):
for char in ",.'-? ":
word_1 = word_1.replace(char, "")
word_2 = word_2.replace(char, "")
for letter in word_1:
if letter not in word_2.lower():
return False
return True
def anagrams(words):
-
result = []
temp_result = []
- for word_1 in words:
- temp = [word_2 for word_2 in words if is_anagram(word_2,word_1)]
+ for word in words:
+ temp = [anagram for anagram in words if is_anagram(anagram, word)]
temp_result.append(temp)
for item in temp_result:
if item not in result:
result.append(item)
- return result
+ return result