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

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

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

Резултати

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

Код

def is_pangram(sentence):
alphabet = 'абвгдежзийклмнопрстуфхчшщъьюя'
for letter in alphabet:
if letter not in sentence.lower():
return False
return True
def char_histogram(text):
histogram = {}
for letter in text:
if letter in histogram:
histogram[letter] += 1
else:
histogram[letter] = 1
return histogram
def sort_by(func, arguments):
for i in range(0,len(arguments)):
for j in range(i, len(arguments)):
if func(arguments[i], arguments[j]) > 0:
tmp = arguments[i]
arguments[i] = arguments[j]
arguments[j] = tmp
return arguments
def group_by_type(dictionary):
grouped = dict()
for key, value in dictionary.items():
if type(key) in grouped:
grouped[type(key)][key] = value
else:
grouped[type(key)] = dict()
grouped[type(key)][key] = value
return grouped
def is_anagram(first_word, second_word):
word2 = list(second_word.lower())
for letter in first_word.lower():
if letter not in word2:
return False
else:
word2.remove(letter)
return True
def anagrams(words):
grouped_by_anagrams = []
already_added = []
# word1 & word2 iterate through the list so that we can
# compare each word to every other
for word1 in words:
anagrams_for_word1 = []
for word2 in words:
if is_anagram(word1, word2) and word2 not in already_added:
anagrams_for_word1.append(word2)
already_added.append(word2)
if anagrams_for_word1 != []:
grouped_by_anagrams.append(anagrams_for_word1)
return grouped_by_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-3a5xm5/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.010s

FAILED (failures=1)

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

Елена обнови решението на 14.03.2014 21:54 (преди над 10 години)

+def is_pangram(sentence):
+ alphabet = 'абвгдежзийклмнопрстуфхчшщъьюя'
+ for letter in alphabet:
+ if letter not in sentence.lower():
+ return False
+ return True
+
+def char_histogram(text):
+ histogram = {}
+ for letter in text:
+ if letter in histogram:
+ histogram[letter] += 1
+ else:
+ histogram[letter] = 1
+ return histogram
+
+def sort_by(func, arguments):
+ for i in range(0,len(arguments)):
+ for j in range(i, len(arguments)):
+ if func(arguments[i], arguments[j]) > 0:
+ tmp = arguments[i]
+ arguments[i] = arguments[j]
+ arguments[j] = tmp
+ return arguments
+
+def group_by_type(dictionary):
+ grouped = dict()
+ for key, value in dictionary.items():
+ if type(key) in grouped:
+ grouped[type(key)][key] = value
+ else:
+ grouped[type(key)] = dict()
+ grouped[type(key)][key] = value
+ return grouped
+
+def is_anagram(first_word, second_word):
+ word2 = list(second_word)
+ for letter in first_word:
+ if letter not in word2:
+ return False
+ else:
+ word2.remove(letter)
+ return True
+
+def anagrams(words):
+ grouped_by_anagrams = []
+ already_added = []
+ for word1 in words:
+ anagrams_for_word1 = []
+ for word2 in words:
+ if is_anagram(word1, word2) and word2 not in already_added:
+ anagrams_for_word1.append(word2)
+ already_added.append(word2)
+ if anagrams_for_word1 != []:
+ grouped_by_anagrams.append(anagrams_for_word1)
+ return grouped_by_anagrams
  • Какво значат word1 и word2?
  • Прочети пак условието на anagrams и ще видиш, че ние искаме еднакви букви, а не символи. Има коментари по темата и във форумите
  • Помисли дали сортирането ти няма да даде грешен резултат в някои случаи

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

def is_pangram(sentence):
alphabet = 'абвгдежзийклмнопрстуфхчшщъьюя'
for letter in alphabet:
if letter not in sentence.lower():
return False
return True
def char_histogram(text):
histogram = {}
for letter in text:
if letter in histogram:
histogram[letter] += 1
else:
histogram[letter] = 1
return histogram
def sort_by(func, arguments):
for i in range(0,len(arguments)):
for j in range(i, len(arguments)):
if func(arguments[i], arguments[j]) > 0:
tmp = arguments[i]
arguments[i] = arguments[j]
arguments[j] = tmp
return arguments
def group_by_type(dictionary):
grouped = dict()
for key, value in dictionary.items():
if type(key) in grouped:
grouped[type(key)][key] = value
else:
grouped[type(key)] = dict()
grouped[type(key)][key] = value
return grouped
def is_anagram(first_word, second_word):
- word2 = list(second_word)
- for letter in first_word:
+ word2 = list(second_word.lower())
+ for letter in first_word.lower():
if letter not in word2:
return False
else:
word2.remove(letter)
return True
def anagrams(words):
grouped_by_anagrams = []
already_added = []
+ # word1 & word2 iterate through the list so that we can
+ # compare each word to every other
for word1 in words:
anagrams_for_word1 = []
for word2 in words:
if is_anagram(word1, word2) and word2 not in already_added:
anagrams_for_word1.append(word2)
already_added.append(word2)
if anagrams_for_word1 != []:
grouped_by_anagrams.append(anagrams_for_word1)
return grouped_by_anagrams