Решение на Пет функции от Диана Генева

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

Към профила на Диана Генева

Резултати

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

Код

def is_pangram(sentance):
sentance = sentance.lower()
alphabet = list("явертъуиопасдфгхйклзьцжбнм")
for letter in sentance:
if letter in alphabet:
alphabet.remove(letter)
return not alphabet
def char_histogram(text):
histogram = {}
for symbol in text:
if symbol in histogram:
histogram[symbol] += 1
else:
histogram[symbol] = 1
return histogram
def sort_by(func, arguments):
for i in range(len(arguments)):
for j in range(i):
if func(arguments[i], arguments[j]) <= 0:
arguments[i], arguments[j] = arguments[j], arguments[i]
return arguments
def group_by_type(dictionary):
types = {}
for key, value in dictionary.items():
if type(key) in types:
types[type(key)][key] = value
else:
types[type(key)] = {key: value}
return types
def is_anagram(list_a, list_b):
for letter in list(list_a):
if letter in list_b:
list_a.remove(letter)
list_b.remove(letter)
return (not list_a) and (not list_b)
def anagrams(words):
whole_list = []
while words:
word = words.pop()
analist = [word]
for i in list(words):
if is_anagram(list(word), list(i)):
analist.append(i)
words.remove(i)
whole_list.append(analist)
return whole_list

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

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

======================================================================
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-ki5xgd/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] != [4, 2, 0, 5, 3, 1]

First differing element 0:
0
4

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

----------------------------------------------------------------------
Ran 16 tests in 0.011s

FAILED (failures=3)

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

Диана обнови решението на 17.03.2014 19:52 (преди над 10 години)

+def is_pangram(sentance):
+ sentance = sentance.lower()
+ alphabet = list("явертъуиопасдфгхйклзьцжбнм")
+ for letter in sentance:
+ if letter in alphabet:
+ alphabet.remove(letter)
+ return not alphabet
+
+
+def char_histogram(text):
+ histogram = {}
+ for symbol in text:
+ if symbol in histogram:
+ histogram[symbol] += 1
+ else:
+ histogram[symbol] = 1
+ return histogram
+
+
+def sort_by(func, arguments):
+ for i in range(len(arguments)):
+ for j in range(i):
+ if func(arguments[i], arguments[j]) <= 0:
+ arguments[i], arguments[j] = arguments[j], arguments[i]
+ return arguments
+
+
+def group_by_type(dictionary):
+ types = {}
+ for key, value in dictionary.items():
+ if type(key) in types:
+ types[type(key)][key] = value
+ else:
+ types[type(key)] = {key: value}
+ return types
+
+
+def is_anagram(list_a, list_b):
+ for letter in list(list_a):
+ if letter in list_b:
+ list_a.remove(letter)
+ list_b.remove(letter)
+ return (not list_a) and (not list_b)
+
+
+def anagrams(words):
+ whole_list = []
+ while words:
+ word = words.pop()
+ analist = [word]
+ for i in list(words):
+ if is_anagram(list(word), list(i)):
+ analist.append(i)
+ words.remove(i)
+ whole_list.append(analist)
+ return whole_list
  • Постарай се в имената на променливите ти да няма типа им
  • Сортирането ти не е стабилно
  • Анаграмите се интересуват само от букви, не от символи, освен това не правят разлика между малки и големи букви