Решение на Пет функции от Йосиф Цветков

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

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

Резултати

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

Код

from functools import cmp_to_key
import re
ALPHABET = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ"
def is_pangram(sentence):
return len(set(sentence.upper()).intersection(ALPHABET)) == 30
def char_histogram(text):
return {symbol: text.count(symbol) for symbol in set(text)}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def is_type(key_object, type_x):
return type(key_object) == type_x
def get_by_type(type_i, dictiona):
return {key: val for key, val in dictiona.items() if is_type(key, type_i)}
def group_by_type(dictionary):
group = {}
for type_i in set(map(type, dictionary)):
group[type_i] = get_by_type(type_i, dictionary)
return group
def filt(word):
return sorted(re.sub(r'([^\w]*[\d]*[_]*)+', '', word))
def are_anagrams(word1, word2):
return word1 == word2 or filt(word1.upper()) == filt(word2.upper())
def anagrams(words):
done = []
while words != []:
done.append([word for word in words if are_anagrams(word, words[-1])])
words = [word for word in words if word not in done[-1]]
return done

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.012s

OK

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

Йосиф обнови решението на 13.03.2014 02:20 (преди над 10 години)

+def is_pangram(string):
+ ALPHABET = set("АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЮЯ")
+ letters = set(string.upper()).intersection(ALPHABET)
+ return len(letters) == 30
+
+
+def char_histogram(string):
+ result = {}
+ for i in set(string):
+ result[i] = string.count(i)
+ return result
+
+
+def sort_by(pred, lst):
+ for i in range(len(lst) - 1):
+ for j in range(len(lst) - 1):
+ if pred(lst[j], lst[j + 1]) > 0:
+ lst[j], lst[j + 1] = lst[j + 1], lst[j]
+ return lst
+
+
+def group_by_type(dictionary):
+ sub_dic = {}
+ final_dic = {}
+
+ for s in set(map(type, dictionary.keys())):
+ for i, j in dictionary.items():
+ if s == type(i):
+ sub_dic[i] = j
+ final_dic[s] = sub_dic
+ sub_dic = {}
+
+ return final_dic
+
+
+def anagrams(lst):
+ sub_list = []
+ final_list = []
+
+ while lst != []:
+ word = lst.pop()
+ sub_list = [i for i in lst if sorted(i) == sorted(word)]
+ sub_list.append(word)
+ lst = [i for i in lst if i not in sub_list]
+ final_list.append(sub_list)
+ sub_list = []
+
+ return final_list

Йосиф обнови решението на 13.03.2014 02:28 (преди над 10 години)

def is_pangram(string):
ALPHABET = set("АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЮЯ")
letters = set(string.upper()).intersection(ALPHABET)
return len(letters) == 30
def char_histogram(string):
result = {}
for i in set(string):
result[i] = string.count(i)
return result
-def sort_by(pred, lst):
- for i in range(len(lst) - 1):
- for j in range(len(lst) - 1):
- if pred(lst[j], lst[j + 1]) > 0:
- lst[j], lst[j + 1] = lst[j + 1], lst[j]
- return lst
+def sort_by(pred, to_sort):
+ for i in range(len(to_sort) - 1):
+ for j in range(len(to_sort) - 1):
+ if pred(to_sort[j], to_sort[j + 1]) > 0:
+ to_sort[j], to_sort[j + 1] = to_sort[j + 1], to_sort[j]
+ return to_sort
def group_by_type(dictionary):
sub_dic = {}
final_dic = {}
for s in set(map(type, dictionary.keys())):
for i, j in dictionary.items():
if s == type(i):
sub_dic[i] = j
final_dic[s] = sub_dic
sub_dic = {}
return final_dic
-def anagrams(lst):
+def anagrams(words):
sub_list = []
final_list = []
- while lst != []:
- word = lst.pop()
- sub_list = [i for i in lst if sorted(i) == sorted(word)]
+ while words != []:
+ word = words.pop()
+ sub_list = [i for i in words if sorted(i) == sorted(word)]
sub_list.append(word)
- lst = [i for i in lst if i not in sub_list]
+ words = [i for i in words if i not in sub_list]
final_list.append(sub_list)
sub_list = []
return final_list

Надявам се че не е проблем че съм ползвал Bubble Sort в задачата за сортиране. Наясно съм че има по-бързи алгоритми, но в случая целя да постигна резултата с минимално количество код. Ако е прекалено дебилно(или грешно), моля кажете за да го оправя, независимо дали е за задачата за сортиране или някоя друга. Предполагам че има какво да се оптимизира, но като за два и половина сутринта - толкова мога.

Йосиф обнови решението на 13.03.2014 17:38 (преди над 10 години)

-def is_pangram(string):
- ALPHABET = set("АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЮЯ")
- letters = set(string.upper()).intersection(ALPHABET)
- return len(letters) == 30
+def is_pangram(x):
+ return len(set(x.upper()).intersection("АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ")) == 30
def char_histogram(string):
- result = {}
- for i in set(string):
- result[i] = string.count(i)
- return result
+ return {i: string.count(i) for i in set(string)}
def sort_by(pred, to_sort):
for i in range(len(to_sort) - 1):
for j in range(len(to_sort) - 1):
if pred(to_sort[j], to_sort[j + 1]) > 0:
to_sort[j], to_sort[j + 1] = to_sort[j + 1], to_sort[j]
return to_sort
def group_by_type(dictionary):
- sub_dic = {}
- final_dic = {}
-
+ res = {}
for s in set(map(type, dictionary.keys())):
- for i, j in dictionary.items():
- if s == type(i):
- sub_dic[i] = j
- final_dic[s] = sub_dic
- sub_dic = {}
+ res[s] = {i: j for i, j in dictionary.items() if type(i) == s}
+ return res
- return final_dic
-
-def anagrams(words):
+def anagrams(x):
- sub_list = []
+ res = []
- final_list = []
+ while x != []:
-
+ res.append([i for i in x if sorted(i) == sorted(x[-1]) or i == x[-1]])
- while words != []:
+ x = [i for i in x if i not in res[-1]]
- word = words.pop()
+ return res
- sub_list = [i for i in words if sorted(i) == sorted(word)]
- sub_list.append(word)
- words = [i for i in words if i not in sub_list]
- final_list.append(sub_list)
- sub_list = []
-
- return final_list

Йосиф обнови решението на 14.03.2014 22:40 (преди над 10 години)

+from functools import cmp_to_key
+
+
def is_pangram(x):
return len(set(x.upper()).intersection("АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ")) == 30
-def char_histogram(string):
- return {i: string.count(i) for i in set(string)}
+def char_histogram(text):
+ return {i: text.count(i) for i in set(text)}
-def sort_by(pred, to_sort):
- for i in range(len(to_sort) - 1):
- for j in range(len(to_sort) - 1):
- if pred(to_sort[j], to_sort[j + 1]) > 0:
- to_sort[j], to_sort[j + 1] = to_sort[j + 1], to_sort[j]
- return to_sort
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
- res = {}
+ group = {}
for s in set(map(type, dictionary.keys())):
- res[s] = {i: j for i, j in dictionary.items() if type(i) == s}
- return res
+ group[s] = {i: j for i, j in dictionary.items() if type(i) == s}
+ return group
def anagrams(x):
res = []
while x != []:
res.append([i for i in x if sorted(i) == sorted(x[-1]) or i == x[-1]])
x = [i for i in x if i not in res[-1]]
return res

Проблем ли това че имам редове (по-точно един) с повече от 80 символа и други 3 реда които имат по-малко от 80 символа понеже ползвам променливи като i, j или приемам като аргумент на функция 'х' ?

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

from functools import cmp_to_key
+import re
def is_pangram(x):
return len(set(x.upper()).intersection("АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ")) == 30
def char_histogram(text):
- return {i: text.count(i) for i in set(text)}
+ return {symbol: text.count(symbol) for symbol in set(text)}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
group = {}
- for s in set(map(type, dictionary.keys())):
- group[s] = {i: j for i, j in dictionary.items() if type(i) == s}
+ for s in set(map(type, dictionary)):
+ group[s] = {i: val for i, val in dictionary.items() if type(i) == s}
return group
+def filt(string):
+ return sorted(re.sub(r'((\W+)*(\d+)*)*', '', string))
+
+
def anagrams(x):
- res = []
+ anagram = []
while x != []:
- res.append([i for i in x if sorted(i) == sorted(x[-1]) or i == x[-1]])
- x = [i for i in x if i not in res[-1]]
+ anagram.append([i for i in x if filt(i) == filt(x[-1]) or i == x[-1]])
- return res
+ x = [to_proces for to_proces in x if to_proces not in anagram[-1]]
+ return anagram

Йосиф обнови решението на 16.03.2014 15:53 (преди над 10 години)

from functools import cmp_to_key
import re
def is_pangram(x):
return len(set(x.upper()).intersection("АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ")) == 30
def char_histogram(text):
return {symbol: text.count(symbol) for symbol in set(text)}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
group = {}
for s in set(map(type, dictionary)):
group[s] = {i: val for i, val in dictionary.items() if type(i) == s}
return group
-def filt(string):
- return sorted(re.sub(r'((\W+)*(\d+)*)*', '', string))
+def filt(anagram):
+ return sorted(re.sub(r'((\W+)*(\d+)*)*', '', anagram))
def anagrams(x):
anagram = []
while x != []:
anagram.append([i for i in x if filt(i) == filt(x[-1]) or i == x[-1]])
x = [to_proces for to_proces in x if to_proces not in anagram[-1]]
return anagram

Йосиф обнови решението на 18.03.2014 16:11 (преди над 10 години)

from functools import cmp_to_key
import re
-def is_pangram(x):
- return len(set(x.upper()).intersection("АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ")) == 30
+ALPHABET = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ"
+def is_pangram(sentence):
+ return len(set(sentence.upper()).intersection(ALPHABET)) == 30
+
+
def char_histogram(text):
return {symbol: text.count(symbol) for symbol in set(text)}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
group = {}
for s in set(map(type, dictionary)):
group[s] = {i: val for i, val in dictionary.items() if type(i) == s}
return group
-def filt(anagram):
- return sorted(re.sub(r'((\W+)*(\d+)*)*', '', anagram))
+def filt(word):
+ return sorted(re.sub(r'([^\w]*[\d]*[_]*)+', '', word))
-def anagrams(x):
- anagram = []
+def test(word1, word2):
- while x != []:
+ return word1 == word2 or filt(word1.upper()) == filt(word2.upper())
- anagram.append([i for i in x if filt(i) == filt(x[-1]) or i == x[-1]])
+
- x = [to_proces for to_proces in x if to_proces not in anagram[-1]]
+
- return anagram
+def anagrams(words):
+ sublist = []
+ while words != []:
+ sublist.append([word for word in words if test(word, words[-1])])
+ words = [word for word in words if word not in sublist[-1]]
+ return sublist

Йосиф обнови решението на 18.03.2014 16:32 (преди над 10 години)

from functools import cmp_to_key
import re
ALPHABET = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ"
def is_pangram(sentence):
return len(set(sentence.upper()).intersection(ALPHABET)) == 30
def char_histogram(text):
return {symbol: text.count(symbol) for symbol in set(text)}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
group = {}
for s in set(map(type, dictionary)):
group[s] = {i: val for i, val in dictionary.items() if type(i) == s}
return group
def filt(word):
return sorted(re.sub(r'([^\w]*[\d]*[_]*)+', '', word))
-def test(word1, word2):
+def are_equiv(word1, word2):
return word1 == word2 or filt(word1.upper()) == filt(word2.upper())
def anagrams(words):
sublist = []
while words != []:
- sublist.append([word for word in words if test(word, words[-1])])
+ sublist.append([word for word in words if are_equiv(word, words[-1])])
words = [word for word in words if word not in sublist[-1]]
return sublist

Йосиф обнови решението на 18.03.2014 22:18 (преди над 10 години)

from functools import cmp_to_key
import re
ALPHABET = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ"
def is_pangram(sentence):
return len(set(sentence.upper()).intersection(ALPHABET)) == 30
def char_histogram(text):
return {symbol: text.count(symbol) for symbol in set(text)}
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
+def is_type(key_object, type_x):
+ return type(key_object) == type_x
+
+
+def get_by_type(type_i, dictiona):
+ return {key: val for key, val in dictiona.items() if is_type(key, type_i)}
+
+
def group_by_type(dictionary):
group = {}
- for s in set(map(type, dictionary)):
- group[s] = {i: val for i, val in dictionary.items() if type(i) == s}
+ for type_i in set(map(type, dictionary)):
+ group[type_i] = get_by_type(type_i, dictionary)
return group
def filt(word):
return sorted(re.sub(r'([^\w]*[\d]*[_]*)+', '', word))
-def are_equiv(word1, word2):
+def are_anagrams(word1, word2):
return word1 == word2 or filt(word1.upper()) == filt(word2.upper())
def anagrams(words):
- sublist = []
+ done = []
while words != []:
- sublist.append([word for word in words if are_equiv(word, words[-1])])
- words = [word for word in words if word not in sublist[-1]]
+ done.append([word for word in words if are_anagrams(word, words[-1])])
- return sublist
+ words = [word for word in words if word not in done[-1]]
+ return done