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

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

Към профила на Димитър Чаушев

Резултати

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

Код

from functools import cmp_to_key
chars = [chr(i) for i in range(97, 123)]
def is_pangram(sentence):
sentence = sentence.lower()
for letter in range(1072, 1099):
if chr(letter) not in sentence:
return False
if chr(1100) not in sentence:
return False
for letter in range(1102, 1104):
if chr(letter) not in sentence:
return False
return True
def char_histogram(text):
dictionary = {}
for element in text:
if element not in dictionary:
dictionary[element] = 1
else: dictionary[element] += 1
return dictionary
def sort_by(func, arguments):
arguments.sort(key = cmp_to_key(func))
return arguments
def group_by_type(dictionary):
result = {}
for element in dictionary.keys():
result[type(element)] = dict([(second_element,\
dictionary[second_element]) for second_element in dictionary.keys()\
if type(second_element) == type(element)])
return result
def anagrams(words):
temporary_answer = []
for word in words:
temporary = []
temporary.append(word)
for word2 in words:
if word is not word2:
check = ''
check2 = ''
for element in word:
if element in chars:
check += element
for element in word2:
if element in chars:
check2 += element
if sorted(check) == sorted(check2):
temporary.append(word2)
temporary_answer.append(temporary)
for i in range(0, len(temporary_answer)):
temporary_answer[i] = sorted(temporary_answer[i])
answer = []
for i in temporary_answer:
if i not in answer:
answer.append(i)
else: temporary_answer.remove(i)
return answer

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

.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-14tj6z4/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'})

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

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

FAILED (failures=2)

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

Димитър обнови решението на 14.03.2014 01:06 (преди над 10 години)

+# 1--------------------
+
+def is_pangram(sentance):
+ sentance = sentance.lower()
+ for letter in range(1072, 1099):
+ if chr(letter) not in sentance:
+ return False
+ if chr(1100) not in sentance:
+ return False
+ for letter in range(1102, 1104):
+ if chr(letter) not in sentance:
+ return False
+
+ return True
+
+
+
+
+#2-----------------------
+
+def char_histogram(text):
+ dictionary = {}
+ for char in text:
+ if char not in dictionary:
+ dictionary[char] = 1
+ else: dictionary[char] += 1
+
+ return dictionary
+
+
+
+
+#3------------------------
+
+from functools import cmp_to_key
+
+def sort_by(func, arguments):
+ arguments.sort(key = cmp_to_key(func))
+
+ return arguments
+
+
+
+
+#4-----------------------
+
+def group_by_type(dictionary):
+ new_dict = {}
+ for element in dictionary.keys():
+ new_dict[type(element)] = dict([(second_element,\
+ dictionary[second_element]) for second_element in dictionary.keys()\
+ if type(second_element) == type(element)])
+ return new_dict
+
+
+
+
+#5------------------------
+
+def anagrams(words):
+ anagram_list = []
+
+ for word in words:
+ temp_list = []
+ temp_list.append(word)
+
+ for second_word in words:
+ if (word != second_word) and (sorted(word) == sorted(second_word)):
+ temp_list.append(second_word)
+
+ anagram_list.append(temp_list)
+
+ for list1 in anagram_list:
+ for list2 in anagram_list:
+ if (list1 != list2) and (sorted(list1) == sorted(list2)):
+ anagram_list.remove(list2)
+
+ return anagram_list

Анаграма е дума или фраза образувана от буквите на друга дума или фраза, чрез пермутация.

  • Твоето решение не прави разлика между буква и символ.
  • Разкарай тези коментари, които не помагат, но за сметка на това пречатю
  • Не оставяй толкова много празни реда между функциите. 2 стигат.
  • Не слагай типа на промнеливата в името ѝ. Вземахме по точка за това на предишното домашно.

Димитър обнови решението на 16.03.2014 00:17 (преди над 10 години)

-# 1--------------------
-
def is_pangram(sentance):
sentance = sentance.lower()
for letter in range(1072, 1099):
if chr(letter) not in sentance:
return False
if chr(1100) not in sentance:
return False
for letter in range(1102, 1104):
if chr(letter) not in sentance:
return False
return True
-
-
-#2-----------------------
-
def char_histogram(text):
dictionary = {}
- for char in text:
- if char not in dictionary:
- dictionary[char] = 1
- else: dictionary[char] += 1
+ for element in text:
+ if element not in dictionary:
+ dictionary[element] = 1
+ else: dictionary[element] += 1
return dictionary
-
-
-#3------------------------
-
from functools import cmp_to_key
def sort_by(func, arguments):
arguments.sort(key = cmp_to_key(func))
return arguments
-
-
-#4-----------------------
-
def group_by_type(dictionary):
- new_dict = {}
+ result = {}
for element in dictionary.keys():
- new_dict[type(element)] = dict([(second_element,\
+ result[type(element)] = dict([(second_element,\
dictionary[second_element]) for second_element in dictionary.keys()\
if type(second_element) == type(element)])
- return new_dict
+ return result
+chars = [chr(i) for i in range(97, 123)]
+def all_perms(element):
+ if len(element) <=1:
+ return element
+ else:
+ tmp = []
+ for perm in all_perms(element[1:]):
+ for i in range(len(element)):
+ tmp.append(perm[:i] + element[0:1] + perm[i:])
+ return tmp
-#5------------------------
+chars = [chr(i) for i in range(97, 123)]
-def anagrams(words):
- anagram_list = []
+def isOk(word):
+ word = word.lower()
+ for i in word:
+ if i not in chars:
+ return False
+ return True
+def anagrams(words):
+ answer = []
for word in words:
- temp_list = []
- temp_list.append(word)
+ temp = []
+ if isOk(word):
+ perms = all_perms(word)
+ for element in perms:
+ if element in words:
+ temp.append(element)
+ words.remove(element)
+ answer.append(temp)
- for second_word in words:
- if (word != second_word) and (sorted(word) == sorted(second_word)):
+ return answer
- temp_list.append(second_word)
-
- anagram_list.append(temp_list)
-
- for list1 in anagram_list:
- for list2 in anagram_list:
- if (list1 != list2) and (sorted(list1) == sorted(list2)):
- anagram_list.remove(list2)
-
- return anagram_list

Не знам само дали правилно разбирам това за буквите и символите :/
Иначе решението на последната функция с анаграмите стана малко бавно, но това е най-доброто, което мога да измисля за сега.

Димитър обнови решението на 17.03.2014 13:01 (преди над 10 години)

def is_pangram(sentance):
sentance = sentance.lower()
for letter in range(1072, 1099):
if chr(letter) not in sentance:
return False
if chr(1100) not in sentance:
return False
for letter in range(1102, 1104):
if chr(letter) not in sentance:
return False
return True
def char_histogram(text):
dictionary = {}
for element in text:
if element not in dictionary:
dictionary[element] = 1
else: dictionary[element] += 1
return dictionary
from functools import cmp_to_key
def sort_by(func, arguments):
arguments.sort(key = cmp_to_key(func))
return arguments
def group_by_type(dictionary):
result = {}
for element in dictionary.keys():
result[type(element)] = dict([(second_element,\
dictionary[second_element]) for second_element in dictionary.keys()\
if type(second_element) == type(element)])
return result
chars = [chr(i) for i in range(97, 123)]
def all_perms(element):
if len(element) <=1:
return element
else:
tmp = []
for perm in all_perms(element[1:]):
for i in range(len(element)):
tmp.append(perm[:i] + element[0:1] + perm[i:])
return tmp
chars = [chr(i) for i in range(97, 123)]
-def isOk(word):
- word = word.lower()
- for i in word:
- if i not in chars:
- return False
- return True
-
def anagrams(words):
answer = []
+
for word in words:
- temp = []
- if isOk(word):
- perms = all_perms(word)
- for element in perms:
- if element in words:
- temp.append(element)
- words.remove(element)
- answer.append(temp)
+ tmp = []
+ dictt = {}
+ tmp.append(word)
+ for word2 in words:
+ if word is not word2:
+ check = ''
+ check2 = ''
+ for el in word:
+ if el in chars:
+ check += el
+ for el in word2:
+ if el in chars:
+ check2 += el
- return answer
+ if sorted(check) == sorted(check2):
+ tmp.append(word2)
+
+ answer.append(tmp)
+
+ for i in range(0, len(answer)):
+ answer[i] = sorted(answer[i])
+
+ temp = []
+ for i in answer:
+ if i not in temp:
+ temp.append(i)
+ else: answer.remove(i)
+
+ return temp

Именоваш си нещата супер лошо. Какво значат tmp, temp, el,, dictt и perm!?

Не импортвай разни неща по средата на файл, само защото можеш. Изнеси ги най-отгоре.

Не знам само дали правилно разбирам това за буквите и символите :/

Ами значи, че "Баба" и "Абаб" са анаграми. Т.е. считаме 'Б' и 'б' за една и съща буква. Освен това игнорираме символите. Демек "Баба" и "Ба-ба" също са анаграми.

П.П. Пише се sentence :)

Да, да, за буквите и символите го разбрах вчера след като видях един от тестовете, който една колежка е постнала във форума(да е жива и здрава, дайте й точка! :D ) Сега ще оправя и именуаването на променливите. Мерси :)

Димитър обнови решението на 18.03.2014 21:00 (преди над 10 години)

+from functools import cmp_to_key
+chars = [chr(i) for i in range(97, 123)]
+
def is_pangram(sentance):
sentance = sentance.lower()
for letter in range(1072, 1099):
if chr(letter) not in sentance:
return False
if chr(1100) not in sentance:
return False
for letter in range(1102, 1104):
if chr(letter) not in sentance:
return False
return True
def char_histogram(text):
dictionary = {}
for element in text:
if element not in dictionary:
dictionary[element] = 1
else: dictionary[element] += 1
return dictionary
-from functools import cmp_to_key
-
def sort_by(func, arguments):
arguments.sort(key = cmp_to_key(func))
return arguments
def group_by_type(dictionary):
result = {}
for element in dictionary.keys():
result[type(element)] = dict([(second_element,\
dictionary[second_element]) for second_element in dictionary.keys()\
if type(second_element) == type(element)])
return result
-chars = [chr(i) for i in range(97, 123)]
-
-def all_perms(element):
- if len(element) <=1:
- return element
- else:
- tmp = []
- for perm in all_perms(element[1:]):
- for i in range(len(element)):
- tmp.append(perm[:i] + element[0:1] + perm[i:])
- return tmp
-
-chars = [chr(i) for i in range(97, 123)]
-
def anagrams(words):
- answer = []
+ temporary_answer = []
for word in words:
- tmp = []
- dictt = {}
- tmp.append(word)
+ temporary = []
+ temporary.append(word)
for word2 in words:
if word is not word2:
check = ''
check2 = ''
- for el in word:
- if el in chars:
- check += el
- for el in word2:
- if el in chars:
- check2 += el
+ for element in word:
+ if element in chars:
+ check += element
+ for element in word2:
+ if element in chars:
+ check2 += element
if sorted(check) == sorted(check2):
- tmp.append(word2)
+ temporary.append(word2)
- answer.append(tmp)
+ temporary_answer.append(temporary)
- for i in range(0, len(answer)):
- answer[i] = sorted(answer[i])
+ for i in range(0, len(temporary_answer)):
+ temporary_answer[i] = sorted(temporary_answer[i])
- temp = []
- for i in answer:
- if i not in temp:
- temp.append(i)
- else: answer.remove(i)
+ answer = []
+ for i in temporary_answer:
+ if i not in answer:
+ answer.append(i)
+ else: temporary_answer.remove(i)
- return temp
+ return answer

Димитър обнови решението на 19.03.2014 07:52 (преди над 10 години)

from functools import cmp_to_key
chars = [chr(i) for i in range(97, 123)]
-def is_pangram(sentance):
- sentance = sentance.lower()
+def is_pangram(sentence):
+ sentence = sentence.lower()
for letter in range(1072, 1099):
- if chr(letter) not in sentance:
+ if chr(letter) not in sentence:
return False
- if chr(1100) not in sentance:
+ if chr(1100) not in sentence:
return False
for letter in range(1102, 1104):
- if chr(letter) not in sentance:
+ if chr(letter) not in sentence:
return False
return True
def char_histogram(text):
dictionary = {}
for element in text:
if element not in dictionary:
dictionary[element] = 1
else: dictionary[element] += 1
return dictionary
def sort_by(func, arguments):
arguments.sort(key = cmp_to_key(func))
return arguments
def group_by_type(dictionary):
result = {}
for element in dictionary.keys():
result[type(element)] = dict([(second_element,\
dictionary[second_element]) for second_element in dictionary.keys()\
if type(second_element) == type(element)])
return result
def anagrams(words):
temporary_answer = []
for word in words:
temporary = []
temporary.append(word)
for word2 in words:
if word is not word2:
check = ''
check2 = ''
for element in word:
if element in chars:
check += element
for element in word2:
if element in chars:
check2 += element
if sorted(check) == sorted(check2):
temporary.append(word2)
temporary_answer.append(temporary)
for i in range(0, len(temporary_answer)):
temporary_answer[i] = sorted(temporary_answer[i])
answer = []
for i in temporary_answer:
if i not in answer:
answer.append(i)
else: temporary_answer.remove(i)
return answer