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

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

Към профила на Илиян Влахов

Резултати

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

Код

from collections import OrderedDict
BG_ALPHABET = [
"а", "б", "в", "г", "д", "е", "ж", "з", "и", "й", "к", "л", "м", "н", "о",
"п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ь", "ю", "я"
]
def is_pangram(sentence):
alphabet_holder = BG_ALPHABET[:]
letters = set(_.lower() for _ in sentence)
for letter in letters:
if letter in alphabet_holder:
alphabet_holder.remove(letter)
if not alphabet_holder:
return True
else:
return False
def char_histogram(text):
char_holder = {i: text.count(i) for i in text}
return char_holder
def sort_by(func, arguments):
for i in range(len(arguments)):
for j in range(i, len(arguments)):
if func(arguments[i], arguments[j]) > 0:
arguments[i], arguments[j] = arguments[j], arguments[i]
return arguments
def group_by_type(dictionary):
grouped_dictionary = {}
for key, value in dictionary.items():
old_value = grouped_dictionary.setdefault(type(key), {key: value})
old_value[key] = value
#grouped_dictionary[type(key)] = old_value
return grouped_dictionary
def anagrams(words):
anagrams_list = []
while len(words) != 0:
word = word_copy = words.pop()
flag = None
for word_list in anagrams_list:
if word in word_list:
flag = 1
if flag:
continue
anagrams_list.append([word])
for item in words:
print(item)
for char in item:
word = word.replace(char, "", 1)
if not word:
anagrams_list[-1].append(item)
word = word_copy
return(anagrams_list)

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

.F
Stdout:
Dave Barry
F
Stdout:
Tom Marvolo Riddle
I am Lord Voldemort
Tom Cruise
Tom Marvolo Riddle
I am Lord Voldemort
Tom Marvolo Riddle
............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-1tcx3eh/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({'Ray Adverb'})
frozenset({'Dave Barry'})

Stdout:
Dave Barry

======================================================================
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-1tcx3eh/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({'Tom Cruise'})
frozenset({"So I'm cuter"})
frozenset({'Tom Marvolo Riddle'})
frozenset({'I am Lord Voldemort'})

Stdout:
Tom Marvolo Riddle
I am Lord Voldemort
Tom Cruise
Tom Marvolo Riddle
I am Lord Voldemort
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-1tcx3eh/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.011s

FAILED (failures=3)

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

Илиян обнови решението на 19.03.2014 01:35 (преди над 10 години)

+from collections import OrderedDict
+
+BG_ALP = [
+ "а", "б", "в", "г", "д", "е", "ж", "з", "и", "й", "к", "л", "м", "н", "о",
+ "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ь", "ю", "я"
+]
+
+
+def is_pangram(sentence):
+ alp_holder = BG_ALP[:]
+ letters = set(_.lower() for _ in sentence)
+ for letter in letters:
+ if letter in alp_holder:
+ alp_holder.remove(letter)
+ if not alp_holder:
+ return True
+ else:
+ return False
+
+
+def char_histogram(text):
+ char_dic = {i: text.count(i) for i in text}
+ return char_dic
+print(char_histogram('This is an example!'))
+
+
+def sort_by(func, arguments):
+ for i in range(len(arguments)):
+ for j in range(i, len(arguments)):
+ if func(arguments[i], arguments[j]) > 0:
+ temp = arguments[i]
+ arguments[i] = arguments[j]
+ arguments[j] = temp
+ return arguments
+
+
+def group_by_type(d):
+ new_dict = {}
+ for key, value in d.items():
+ old_value = new_dict.setdefault(type(key), {key: value})
+ old_value[key] = value
+ print(new_dict)
+ #new_dict[type(key)] = old_value
+ return new_dict
  • Разкарай print-a
  • alp_holder можеш да го кръстиш alphabet_holder и ще е по-ясно какво държи
  • char_dic, типа не трябва да присъства в името на променливата, освен това е написан неправилно
  • temp, всички променливи са временни, но размяната на стойности на двете променливи в Python става така: first, second = second, first
  • Като цяло повече променливи са именовани не много добре: old_value, new_dict, BG_ALP, d. Помисли за по-добри имена.

Илиян обнови решението на 19.03.2014 14:16 (преди над 10 години)

from collections import OrderedDict
-BG_ALP = [
+BG_ALPHABET = [
"а", "б", "в", "г", "д", "е", "ж", "з", "и", "й", "к", "л", "м", "н", "о",
"п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ь", "ю", "я"
]
def is_pangram(sentence):
- alp_holder = BG_ALP[:]
+ alphabet_holder = BG_ALPHABET[:]
letters = set(_.lower() for _ in sentence)
for letter in letters:
- if letter in alp_holder:
- alp_holder.remove(letter)
- if not alp_holder:
+ if letter in alphabet_holder:
+ alphabet_holder.remove(letter)
+ if not alphabet_holder:
return True
else:
return False
def char_histogram(text):
- char_dic = {i: text.count(i) for i in text}
- return char_dic
-print(char_histogram('This is an example!'))
+ char_holder = {i: text.count(i) for i in text}
+ return char_holder
def sort_by(func, arguments):
for i in range(len(arguments)):
for j in range(i, len(arguments)):
if func(arguments[i], arguments[j]) > 0:
- temp = arguments[i]
- arguments[i] = arguments[j]
- arguments[j] = temp
+ arguments[i], arguments[j] = arguments[j], arguments[i]
return arguments
-def group_by_type(d):
- new_dict = {}
- for key, value in d.items():
- old_value = new_dict.setdefault(type(key), {key: value})
+def group_by_type(dictionary):
+ grouped_dictionary = {}
+ for key, value in dictionary.items():
+ old_value = grouped_dictionary.setdefault(type(key), {key: value})
old_value[key] = value
- print(new_dict)
- #new_dict[type(key)] = old_value
+ #grouped_dictionary[type(key)] = old_value
- return new_dict
+ return grouped_dictionary

Илиян обнови решението на 19.03.2014 16:21 (преди над 10 години)

from collections import OrderedDict
BG_ALPHABET = [
"а", "б", "в", "г", "д", "е", "ж", "з", "и", "й", "к", "л", "м", "н", "о",
"п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ь", "ю", "я"
]
def is_pangram(sentence):
alphabet_holder = BG_ALPHABET[:]
letters = set(_.lower() for _ in sentence)
for letter in letters:
if letter in alphabet_holder:
alphabet_holder.remove(letter)
if not alphabet_holder:
return True
else:
return False
def char_histogram(text):
char_holder = {i: text.count(i) for i in text}
return char_holder
def sort_by(func, arguments):
for i in range(len(arguments)):
for j in range(i, len(arguments)):
if func(arguments[i], arguments[j]) > 0:
arguments[i], arguments[j] = arguments[j], arguments[i]
return arguments
def group_by_type(dictionary):
grouped_dictionary = {}
for key, value in dictionary.items():
old_value = grouped_dictionary.setdefault(type(key), {key: value})
old_value[key] = value
#grouped_dictionary[type(key)] = old_value
return grouped_dictionary
+
+
+def anagrams(words):
+ anagrams_list = []
+ while len(words) != 0:
+ word = word_copy = words.pop()
+ flag = None
+ for word_list in anagrams_list:
+ if word in word_list:
+ flag = 1
+ if flag:
+ continue
+ anagrams_list.append([word])
+ for item in words:
+ print(item)
+ for char in item:
+ word = word.replace(char, "", 1)
+ if not word:
+ anagrams_list[-1].append(item)
+ word = word_copy
+ return(anagrams_list)