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

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

Към профила на Христо Петков

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 10 успешни тест(а)
  • 6 неуспешни тест(а)

Код

def is_pangram(sentance):
sentance = sentance.lower()
bg_alphabet = "абвгдежзийклмнопрстуфхцчъьюя"
for letter in bg_alphabet:
if letter not in sentance:
return False
return True
def char_histogram(text):
hist={}
for letter in text:
hist[letter] = hist.get(letter, 0) + 1
return hist
def sort_by(func, arg):
arg = sorted(arg, key = lambda x: item_value(x, arg, func))
return arg
def item_value(item, arguments, func):
value = 0
for i in item:
if func(arguments, i) > 0:
value +=1
return value
def group_by_type(dictionary):
grouped = {}
for i in dictionary:
grouped[type(i)] = grouped.get(type(i),{})
grouped[type(i)][i] = dictionary[i]
return grouped
def anagrams(words):
grouped_anagrams = []
for i in words:
if not any(i in temp for temp in grouped_anagrams):
combinations = []
for j in words:
if is_anagram(i,j):
combinations.append(j)
grouped_anagrams.append(combinations)
return grouped_anagrams
def is_anagram(first_word, second_word):
first_word = re.sub('[\W\d_+]', '', first_word)
second_word = re.sub('[\W\d_+]', '', second_word)
first_word = sorted(list(first_word.lower()))
second_word = sorted(list(second_word.lower()))
return first_word == second_word

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

EEEE..........EE
======================================================================
ERROR: test_list_of_latin_anagrams (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-upiqap/test.py", line 109, in test_list_of_latin_anagrams
    set(map(frozenset, solution.anagrams(words))))
  File "/tmp/d20140319-21201-upiqap/solution.py", line 39, in anagrams
    if is_anagram(i,j):
  File "/tmp/d20140319-21201-upiqap/solution.py", line 45, in is_anagram
    first_word = re.sub('[\W\d_+]', '', first_word)
NameError: global name 're' is not defined

======================================================================
ERROR: 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-upiqap/test.py", line 125, in test_with_different_cases
    set(map(frozenset, solution.anagrams(words))))
  File "/tmp/d20140319-21201-upiqap/solution.py", line 39, in anagrams
    if is_anagram(i,j):
  File "/tmp/d20140319-21201-upiqap/solution.py", line 45, in is_anagram
    first_word = re.sub('[\W\d_+]', '', first_word)
NameError: global name 're' is not defined

======================================================================
ERROR: 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-upiqap/test.py", line 135, in test_with_different_symbols
    set(map(frozenset, solution.anagrams(words))))
  File "/tmp/d20140319-21201-upiqap/solution.py", line 39, in anagrams
    if is_anagram(i,j):
  File "/tmp/d20140319-21201-upiqap/solution.py", line 45, in is_anagram
    first_word = re.sub('[\W\d_+]', '', first_word)
NameError: global name 're' is not defined

======================================================================
ERROR: test_with_list_of_cyrilic_anagrams (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-upiqap/test.py", line 117, in test_with_list_of_cyrilic_anagrams
    set(map(frozenset, solution.anagrams(words))))
  File "/tmp/d20140319-21201-upiqap/solution.py", line 39, in anagrams
    if is_anagram(i,j):
  File "/tmp/d20140319-21201-upiqap/solution.py", line 45, in is_anagram
    first_word = re.sub('[\W\d_+]', '', first_word)
NameError: global name 're' is not defined

======================================================================
ERROR: test_sort_by_one_element (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-upiqap/test.py", line 57, in test_sort_by_one_element
    self.assertEqual([1], solution.sort_by(lambda x, y: x - y, [1]))
  File "/tmp/d20140319-21201-upiqap/solution.py", line 16, in sort_by
    arg = sorted(arg, key = lambda x: item_value(x, arg, func))
  File "/tmp/d20140319-21201-upiqap/solution.py", line 16, in <lambda>
    arg = sorted(arg, key = lambda x: item_value(x, arg, func))
  File "/tmp/d20140319-21201-upiqap/solution.py", line 21, in item_value
    for i in item:
TypeError: 'int' object is not iterable

======================================================================
ERROR: 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-upiqap/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]))
  File "/tmp/d20140319-21201-upiqap/solution.py", line 16, in sort_by
    arg = sorted(arg, key = lambda x: item_value(x, arg, func))
  File "/tmp/d20140319-21201-upiqap/solution.py", line 16, in <lambda>
    arg = sorted(arg, key = lambda x: item_value(x, arg, func))
  File "/tmp/d20140319-21201-upiqap/solution.py", line 21, in item_value
    for i in item:
TypeError: 'int' object is not iterable

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

FAILED (errors=6)

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

Христо обнови решението на 15.03.2014 19:29 (преди над 10 години)

+def is_pangram(sentance):
+ bg_alphabet = "абвгдежзийклмнопрстуфхцчъьюя"
+ for letter in bg_alphabet:
+ if letter not in sentance:
+ return False
+ return True
+
+def char_histogram(text):
+ hist_dict={}
+ for letter in text:
+ if letter in hist_dict:
+ hist_dict[letter] += 1
+ else:
+ hist_dict[letter] = 1
+ return hist_dict
+
+def sort_by(func, arguments):
+ switch = 0
+ for j in range(0,len(arguments)):
+ for i in range(j+1,len(arguments)):
+ if func(arguments[j],arguments[i]) > 0:
+ switch = arguments[i]
+ arguments[i] = arguments[j]
+ arguments[j] = switch
+ return arguments
+
+def group_by_type(dictionary):
+ new_dict = {}
+ for i in dictionary:
+ if type(i) in new_dict:
+ new_dict[type(i)][i] = dictionary[i]
+ else:
+ new_dict[type(i)] = {}
+ new_dict[type(i)][i] = dictionary[i]
+ return new_dict
+
+def anagrams(words):
+ temp_list=[]
+ new_list=[]
+ for i in range(0,len(words)):
+ if not any(words[i] in temp for temp in new_list):
+ temp_list.append(words[i])
+ for j in range(i+1,len(words)):
+ if is_anagram(words[i],words[j]):
+ temp_list.append(words[j])
+ new_list.append(temp_list)
+ temp_list=[]
+ return new_list
+
+def is_anagram(word1, word2):
+ for i in word1:
+ if i not in word2:
+ return False
+ for i in word2:
+ if i not in word1:
+ return False
+ return True
  • Имаш доста неописателни имена като new_list, temp_list, word1, word2
  • Не слагай типа на променливата в името ѝ
  • Сортирането ти се дъни в някои от случаите
  • В анаграмите се интересуваме дали има еднакви букви, а не символи
  • is_pangram не е case-sensitive

Христо обнови решението на 17.03.2014 11:10 (преди над 10 години)

+import re
+
def is_pangram(sentance):
+ sentance = lentance.lower()
bg_alphabet = "абвгдежзийклмнопрстуфхцчъьюя"
for letter in bg_alphabet:
if letter not in sentance:
return False
return True
def char_histogram(text):
- hist_dict={}
+ hist={}
for letter in text:
- if letter in hist_dict:
- hist_dict[letter] += 1
- else:
- hist_dict[letter] = 1
- return hist_dict
+ hist[letter] = hist.get(letter, 0) + 1
+ return hist
-def sort_by(func, arguments):
- switch = 0
- for j in range(0,len(arguments)):
- for i in range(j+1,len(arguments)):
- if func(arguments[j],arguments[i]) > 0:
- switch = arguments[i]
- arguments[i] = arguments[j]
- arguments[j] = switch
+def sort_by(func, arg):
+ arg = sorted(arguments, key = lambda x: item_value(x, arg, func))
return arguments
+def item_value(item, arguments, func):
+ value = 0
+ for i in items:
+ if func(arguments, i) > 0:
+ value +=1
+ return value
+
def group_by_type(dictionary):
- new_dict = {}
+ grouped = {}
for i in dictionary:
- if type(i) in new_dict:
- new_dict[type(i)][i] = dictionary[i]
- else:
- new_dict[type(i)] = {}
- new_dict[type(i)][i] = dictionary[i]
- return new_dict
+ grouped[type(i)] = grouped.get(type(i),{})
+ grouped[type(i)][i] = dictionary[i]
+ return grouped
def anagrams(words):
- temp_list=[]
- new_list=[]
- for i in range(0,len(words)):
- if not any(words[i] in temp for temp in new_list):
- temp_list.append(words[i])
- for j in range(i+1,len(words)):
- if is_anagram(words[i],words[j]):
- temp_list.append(words[j])
- new_list.append(temp_list)
- temp_list=[]
- return new_list
+ anagr = []
+ for i in words:
+ if not any(i in temp for temp in anagr):
+ combinations = []
+ for j in words:
+ if is_anagram(i,j):
+ combinations.append(j)
+ anagr.append(combinations)
+ return anagr
-def is_anagram(word1, word2):
- for i in word1:
+def is_anagram(first_word, second_word):
- if i not in word2:
+ first_word = re.sub('[\W\d_+]', '', first_word)
- return False
+ second_word = re.sub('[\W\d_+]', '', second_word)
- for i in word2:
+ first_word = sorted(list(first_word.lower()))
- if i not in word1:
+ second_word = sorted(list(second_word.lower()))
- return False
+ return first_word == second_word
- return True

Христо обнови решението на 19.03.2014 01:55 (преди над 10 години)

-import re
-
def is_pangram(sentance):
- sentance = lentance.lower()
+ sentance = sentance.lower()
bg_alphabet = "абвгдежзийклмнопрстуфхцчъьюя"
for letter in bg_alphabet:
if letter not in sentance:
return False
return True
def char_histogram(text):
hist={}
for letter in text:
hist[letter] = hist.get(letter, 0) + 1
return hist
def sort_by(func, arg):
- arg = sorted(arguments, key = lambda x: item_value(x, arg, func))
- return arguments
+ arg = sorted(arg, key = lambda x: item_value(x, arg, func))
+ return arg
def item_value(item, arguments, func):
value = 0
- for i in items:
+ for i in item:
if func(arguments, i) > 0:
value +=1
return value
def group_by_type(dictionary):
grouped = {}
for i in dictionary:
grouped[type(i)] = grouped.get(type(i),{})
grouped[type(i)][i] = dictionary[i]
return grouped
def anagrams(words):
- anagr = []
+ grouped_anagrams = []
for i in words:
- if not any(i in temp for temp in anagr):
+ if not any(i in temp for temp in grouped_anagrams):
combinations = []
for j in words:
if is_anagram(i,j):
combinations.append(j)
- anagr.append(combinations)
- return anagr
+ grouped_anagrams.append(combinations)
+ return grouped_anagrams
def is_anagram(first_word, second_word):
first_word = re.sub('[\W\d_+]', '', first_word)
second_word = re.sub('[\W\d_+]', '', second_word)
first_word = sorted(list(first_word.lower()))
second_word = sorted(list(second_word.lower()))
return first_word == second_word

Значи 2 точки намаление заради import re?
И още 2 точки намаление защото сменях имената и съм се объркал?

def item_value(item, arguments, func):
  value = 0
  for i in arguments:
   if func(item, i) > 0:
    value +=1
  return value