Решение на Пет функции от Герасим Станчев

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

Към профила на Герасим Станчев

Резултати

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

Код

#is_pangram
#####################################
def is_pangram(sentence):
all_chars = []
sentence = sentence.lower()
compare_chars = [chr(i) for i in range(ord('а'), ord('ъ') + 1)]
compare_chars.append('ь')
compare_chars.append('ю')
compare_chars.append('я')
for char in compare_chars:
if char in sentence:
all_chars.append(char)
all_chars.sort()
if len(all_chars) != 30:
return False
return all_chars == compare_chars
#####################################
#char_histogram
#####################################
def char_histogram(text):
result = {}
for char in text:
if char not in result:
result[char] = text.count(char)
return result
#####################################
#sort_by
#####################################
def sort_by(func, arguments):
args = arguments #Because of PEP8 at row 6.
for left in range(len(args)):
for right in range(left + 1, len(args)):
if func(args[left], args[right]) > 0:
x, y = args.index(args[left]), args.index(args[right])
args[y], args[x] = args[x], args[y]
return args
#####################################
#group_by_type
#####################################
def group_by_type(dictionary):
result, temp_results = {}, {}
for key in iter(dictionary):
if type(key) not in result:
result[type(key)] = {}
for key in iter(result.copy()):
for val in iter(dictionary):
if key == type(val):
temp_results[val] = dictionary[val]
result[key] = temp_results
temp_results = {}
return result
#####################################
#anagrams
#####################################
def check_equality (word_1, word_2):
import re
#Cleans non-alphabetic symbols, ".lower()"'s, and sorts them.
word_1 = sorted(''.join(re.sub(r'[^a-zA-Z]', '', word_1)).lower())
word_2 = sorted(''.join(re.sub(r'[^a-zA-Z]', '', word_2)).lower())
return word_1 == word_2
def anagrams(words):
result_holder, temporary_results = [], []
for word_1 in words:
for word_2 in words:
if check_equality(word_1, word_2):
temporary_results.append(word_2)
result_holder.append(temporary_results)
temporary_results = []
results = []
for word in result_holder:
if word not in results:
results.append(word)
return results
#####################################

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

...............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-nl561d/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.027s

FAILED (failures=1)

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

Герасим обнови решението на 14.03.2014 16:17 (преди над 10 години)

+def is_pangram(sentence):
+ all_chars = []
+ sentence = sentence.lower()
+ compare_chars = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
+ for char in compare_chars:
+ if char in sentence:
+ all_chars.append(char)
+ all_chars.sort()
+ if len(all_chars) != 30:
+ return False
+ return all_chars == compare_chars
+
+
+def char_histogram(text):
+ result = {}
+ for char in range(len(text)):
+ if text[char] not in result:
+ result[text[char]] = text.count(text[char])
+ return result
+
+def sort_by(func, arguments):
+ for left in range(len(arguments)):
+ for right in range(left + 1, len(arguments)):
+ if func(arguments[left], arguments[right]) > 0:
+ x, y = arguments.index(arguments[left]), arguments.index(arguments[right])
+ arguments[y], arguments[x] = arguments[x], arguments[y]
+ return arguments
+
+
+def anagrams(words):
+ result_dups, temp_results = [], []
+ for word_comp in words:
+ for word_search in words:
+ if ''.join(sorted(word_comp)) == ''.join(sorted(word_search)):
+ temp_results.append(word_search)
+ result_dups.append(temp_results)
+ temp_results = []
+
+ results = []
+ for word in result_dups:
+ if word not in results:
+ results.append(word)
+
+ return results

Герасим обнови решението на 15.03.2014 13:18 (преди над 10 години)

+#is_pangram
+#####################################
def is_pangram(sentence):
all_chars = []
sentence = sentence.lower()
compare_chars = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
for char in compare_chars:
if char in sentence:
all_chars.append(char)
all_chars.sort()
if len(all_chars) != 30:
return False
return all_chars == compare_chars
-
+#####################################
+
+#char_histogram
+#####################################
def char_histogram(text):
result = {}
for char in range(len(text)):
if text[char] not in result:
result[text[char]] = text.count(text[char])
return result
def sort_by(func, arguments):
for left in range(len(arguments)):
for right in range(left + 1, len(arguments)):
if func(arguments[left], arguments[right]) > 0:
x, y = arguments.index(arguments[left]), arguments.index(arguments[right])
arguments[y], arguments[x] = arguments[x], arguments[y]
return arguments
+#####################################
+#group_by_type
+#####################################
+def group_by_type(dictionary):
+ result, temp_results = {}, {}
+ for key in iter(dictionary):
+ if type(key) not in result:
+ result[type(key)] = {}
+
+ for key in iter(result.copy()):
+ for val in iter(dictionary):
+ if key == type(val):
+ temp_results[val] = dictionary[val]
+ result[key] = temp_results
+ temp_results = {}
+
+ return result
+
+#####################################
+
+#anagrams
+#####################################
def anagrams(words):
result_dups, temp_results = [], []
for word_comp in words:
for word_search in words:
if ''.join(sorted(word_comp)) == ''.join(sorted(word_search)):
temp_results.append(word_search)
result_dups.append(temp_results)
temp_results = []
results = []
for word in result_dups:
if word not in results:
results.append(word)
return results
+
+#####################################
  • Имаш редове по-дълги от 80 символа
  • Обърни внимание, че в anagrams се иска да има еднакви букви, не се интересуваме от символите
  • Имената result_dups, temp_results и word_comp нищо не значат
  • Дали няма случаи, в които сортирането ти ще върне грешен резултат
  1. Ще го оправя.
  2. По презумпция не се ли подават само низове със символи? Трябва ли да "изчиствам" низовете от други символи?
  3. И това ще го оправя.
  4. Ако са подадени големи букви. Но дори на примера е написано с малка буква името mary. Трябва ли да разглеждам този случай?

Герасим обнови решението на 18.03.2014 11:59 (преди над 10 години)

#is_pangram
#####################################
def is_pangram(sentence):
all_chars = []
sentence = sentence.lower()
- compare_chars = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
+ compare_chars = [chr(i) for i in range(ord('а'), ord('ъ') + 1)]
+ compare_chars.append('ь')
+ compare_chars.append('ю')
+ compare_chars.append('я')
for char in compare_chars:
if char in sentence:
all_chars.append(char)
all_chars.sort()
if len(all_chars) != 30:
return False
return all_chars == compare_chars
#####################################
#char_histogram
#####################################
def char_histogram(text):
result = {}
- for char in range(len(text)):
- if text[char] not in result:
- result[text[char]] = text.count(text[char])
+ for char in text:
+ if char not in result:
+ result[char] = text.count(char)
return result
+#####################################
+
+#sort_by
+#####################################
def sort_by(func, arguments):
- for left in range(len(arguments)):
- for right in range(left + 1, len(arguments)):
- if func(arguments[left], arguments[right]) > 0:
- x, y = arguments.index(arguments[left]), arguments.index(arguments[right])
- arguments[y], arguments[x] = arguments[x], arguments[y]
- return arguments
+ args = arguments #Because of PEP8 at row 6.
+ for left in range(len(args)):
+ for right in range(left + 1, len(args)):
+ if func(args[left], args[right]) > 0:
+ x, y = args.index(args[left]), args.index(args[right])
+ args[y], args[x] = args[x], args[y]
+ return args
#####################################
#group_by_type
#####################################
def group_by_type(dictionary):
result, temp_results = {}, {}
for key in iter(dictionary):
if type(key) not in result:
result[type(key)] = {}
for key in iter(result.copy()):
for val in iter(dictionary):
if key == type(val):
temp_results[val] = dictionary[val]
result[key] = temp_results
temp_results = {}
return result
#####################################
#anagrams
#####################################
+def check_equality (word_1, word_2):
+ import re
+ #Cleans non-alphabetic symbols, ".lower()"'s, and sorts them.
+ word_1 = sorted(''.join(re.sub(r'[^a-zA-Z]', '', word_1)).lower())
+ word_2 = sorted(''.join(re.sub(r'[^a-zA-Z]', '', word_2)).lower())
+ return word_1 == word_2
+
def anagrams(words):
result_dups, temp_results = [], []
+
for word_comp in words:
for word_search in words:
- if ''.join(sorted(word_comp)) == ''.join(sorted(word_search)):
+ if check_equality(word_comp, word_search):
temp_results.append(word_search)
result_dups.append(temp_results)
temp_results = []
results = []
for word in result_dups:
if word not in results:
results.append(word)
return results
#####################################

Не би трябвало да имам. Дори да го пусна като: word_1 = ''.join(re.sub(r'[^a-zA-Z]', '', word_1)).lower() word_1 = sorted(word_1) word_2 = ''.join(re.sub(r'[^a-zA-Z]', '', word_2)).lower() word_2 = sorted(word_2), ще сортира коректно, защото двата низа са изчистени от "небуквени" символи, приведени са към lower() и след това са сортирани. Сортировката се извършва само върху малки букви, така че винаги трябва да е коректна. Преди return на word_1 и на word_2 е присвоен списък със сортираните букви от подадения низ. Мисля, че вече се повтарям, така че спирам с обясненията. :)

Герасим обнови решението на 18.03.2014 23:57 (преди над 10 години)

#is_pangram
#####################################
def is_pangram(sentence):
all_chars = []
sentence = sentence.lower()
compare_chars = [chr(i) for i in range(ord('а'), ord('ъ') + 1)]
compare_chars.append('ь')
compare_chars.append('ю')
compare_chars.append('я')
for char in compare_chars:
if char in sentence:
all_chars.append(char)
all_chars.sort()
if len(all_chars) != 30:
return False
return all_chars == compare_chars
#####################################
#char_histogram
#####################################
def char_histogram(text):
result = {}
for char in text:
if char not in result:
result[char] = text.count(char)
return result
#####################################
#sort_by
#####################################
def sort_by(func, arguments):
args = arguments #Because of PEP8 at row 6.
for left in range(len(args)):
for right in range(left + 1, len(args)):
if func(args[left], args[right]) > 0:
x, y = args.index(args[left]), args.index(args[right])
args[y], args[x] = args[x], args[y]
return args
#####################################
#group_by_type
#####################################
def group_by_type(dictionary):
result, temp_results = {}, {}
for key in iter(dictionary):
if type(key) not in result:
result[type(key)] = {}
for key in iter(result.copy()):
for val in iter(dictionary):
if key == type(val):
temp_results[val] = dictionary[val]
result[key] = temp_results
temp_results = {}
return result
#####################################
#anagrams
#####################################
def check_equality (word_1, word_2):
import re
#Cleans non-alphabetic symbols, ".lower()"'s, and sorts them.
word_1 = sorted(''.join(re.sub(r'[^a-zA-Z]', '', word_1)).lower())
word_2 = sorted(''.join(re.sub(r'[^a-zA-Z]', '', word_2)).lower())
return word_1 == word_2
def anagrams(words):
- result_dups, temp_results = [], []
+ result_holder, temporary_results = [], []
- for word_comp in words:
- for word_search in words:
- if check_equality(word_comp, word_search):
- temp_results.append(word_search)
- result_dups.append(temp_results)
- temp_results = []
+ for word_1 in words:
+ for word_2 in words:
+ if check_equality(word_1, word_2):
+ temporary_results.append(word_2)
+ result_holder.append(temporary_results)
+ temporary_results = []
results = []
- for word in result_dups:
+ for word in result_holder:
if word not in results:
results.append(word)
return results
#####################################