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

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

Към профила на Спасимир Тупаров

Резултати

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

Код

def is_pangram(sentence):
bulgarian_alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
encountered_symbols = {}
for symbol in bulgarian_alphabet:
if symbol in sentence.lower():
encountered_symbols[symbol] = 1
return len(encountered_symbols) == len(bulgarian_alphabet)
def char_histogram(text):
symbols_and_encounters = {}
for symbol in text:
if symbol in symbols_and_encounters:
symbols_and_encounters[symbol] += 1
else:
symbols_and_encounters[symbol] = 1
return symbols_and_encounters
def group_by_type(dictionary):
result = {}
for e in dictionary:
if type(e) not in result:
result[type(e)] = {e: dictionary[e]}
else: # type(e) in result:
result[type(e)][e] = dictionary[e]
return result
def sort_by(func, arguments):
result = arguments
for i in range(1, len(result)):
val = result[i]
j = i - 1
while (j >= 0) and func(result[j], val) > 0:
result[j+1] = result[j]
j -= 1
result[j+1] = val
return result
def should_be_added(first_word, nth_word):
first_word_histogramed = char_histogram(first_word.upper())
nth_word_histogramed = char_histogram(nth_word.upper())
result = False
for symbol in first_word_histogramed:
if not symbol.isalpha():
continue
if (symbol in nth_word_histogramed and
nth_word_histogramed[symbol] ==
first_word_histogramed[symbol]):
result = True
else:
return False
return result
def anagrams(words):
result = []
anagram_lists = []
for first_word in words:
anagram_lists = []
for nth_word in words:
if (should_be_added(first_word, nth_word) and
should_be_added(nth_word, first_word)):
anagram_lists.append(nth_word)
if anagram_lists != [] and anagram_lists not in result:
result.append(anagram_lists)
return result

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.010s

OK

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

Спасимир обнови решението на 19.03.2014 02:00 (преди около 10 години)

+def is_pangram(sentence):
+
+ copy_sentence = sentence.lower()
+ bulgarian_charss = [chr(x) for x in range(ord('а'), ord('я')+1)]
+ bulgarian_charss.pop(-3)
+ bulgarian_charss.pop(-4)
+
+ encountered_symbols = {}
+
+ for symbol in bulgarian_charss:
+ if symbol in copy_sentence:
+ encountered_symbols[symbol] = 1
+
+ return len(encountered_symbols) == len(bulgarian_charss)
+
+
+def char_histogram(text):
+
+ symbols_and_encounters = {}
+ for symbol in text:
+ if symbol in symbols_and_encounters:
+ symbols_and_encounters[symbol] += 1
+ else:
+ symbols_and_encounters[symbol] = 1
+
+ return symbols_and_encounters
+
+
+def group_by_type(dictionary):
+ result = {}
+ for e in dictionary:
+ if type(e) not in result:
+ result[type(e)] = {e: dictionary[e]}
+
+ elif type(e) in result:
+ result[type(e)][e] = dictionary[e]
+
+ return result
+
+
+def sort_by(func, arguments):
+ result = arguments
+ for i in range(1, len(result)):
+ val = result[i]
+ j = i - 1
+
+ while (j >= 0) and func(result[j], val) > 0:
+ result[j+1] = result[j]
+ j = j - 1
+ result[j+1] = val
+ return result
+
+
+def anagrams(words):
+
+ result = []
+ temp = []
+
+ for firs_word in words:
+ temp = []
+ firs_word_histogramed = char_histogram(firs_word.upper())
+ for nth_word in words:
+ nth_word_histogramed = char_histogram(nth_word.upper())
+ flag = False
+ for symbol in firs_word_histogramed:
+ if not symbol.isalpha():
+ continue
+ if symbol in nth_word_histogramed and\
+ nth_word_histogramed[symbol] ==\
+ firs_word_histogramed[symbol]:
+ flag = True
+ else:
+ flag = False
+ break
+ if flag:
+ for symbol_two in firs_word_histogramed:
+ if not symbol_two.isalpha():
+ continue
+ if symbol_two in nth_word_histogramed and\
+ firs_word_histogramed[symbol_two]\
+ == nth_word_histogramed[symbol_two]:
+ flag = True
+ else:
+ flag = False
+ break
+ if flag:
+ temp.append(nth_word)
+ if temp != [] and temp not in result:
+ result.append(temp)
+ return result
  • не оставяй празни редове след заглавието на функция
  • copy_sentence в случая не е много удачно, защото ти не правиш копие на изречението, а lowercase копие; а и ползваш copy_sentence само на едно място, така че просто на това място използвай sentence.lower()
  • bulgarian_charss има правописна грешка -> chars; дори и да я нямаше пак по-добре е bulgarian_characters; това, което си направил на тези 3 реда с това име става по-разбираемо на 1: bulgarian_alphabet = 'абвгде ...'; можеш и да го cast-неш към list, ако толкова искаш
  • в is_pangram не трябва да има значение дали буквите са малки или големи - ако имаш A трябва да го броиш като срещане на a
  • прегледай хитринктие и char_histogram и group_by_type ще ги направиш по-добре
  • temp е кофти име
  • anagrams е прекалено тлъста функция, опитай се да я разбиеш на по-малки функции; като цяло много прилича на C решение с тези флагове и break-ове
  • ако условието ти в if, for или while стане дълго за един ред не използвай \, a просто обвий условието със скоби (); например
if (symbol_two in nth_word_histogramed and
        firs_word_histogramed[symbol_two] ==
        nth_word_histogramed[symbol_two]):
    ...
  • firs_word_histogramed first

Спасимир обнови решението на 19.03.2014 15:50 (преди около 10 години)

def is_pangram(sentence):
-
- copy_sentence = sentence.lower()
- bulgarian_charss = [chr(x) for x in range(ord('а'), ord('я')+1)]
- bulgarian_charss.pop(-3)
- bulgarian_charss.pop(-4)
-
+ bulgarian_alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ bulgarian_alphabet = list(bulgarian_alphabet)
encountered_symbols = {}
-
- for symbol in bulgarian_charss:
- if symbol in copy_sentence:
+ for symbol in bulgarian_alphabet:
+ if symbol in sentence.lower():
encountered_symbols[symbol] = 1
- return len(encountered_symbols) == len(bulgarian_charss)
+ return len(encountered_symbols) == len(bulgarian_alphabet)
def char_histogram(text):
-
symbols_and_encounters = {}
for symbol in text:
if symbol in symbols_and_encounters:
symbols_and_encounters[symbol] += 1
else:
symbols_and_encounters[symbol] = 1
return symbols_and_encounters
def group_by_type(dictionary):
result = {}
for e in dictionary:
if type(e) not in result:
result[type(e)] = {e: dictionary[e]}
-
- elif type(e) in result:
+ else: # type(e) in result:
result[type(e)][e] = dictionary[e]
return result
def sort_by(func, arguments):
result = arguments
for i in range(1, len(result)):
val = result[i]
j = i - 1
-
while (j >= 0) and func(result[j], val) > 0:
result[j+1] = result[j]
- j = j - 1
+ j -= 1
result[j+1] = val
return result
def anagrams(words):
-
result = []
- temp = []
+ anagram_lists = []
for firs_word in words:
- temp = []
- firs_word_histogramed = char_histogram(firs_word.upper())
+ anagram_lists = []
+ first_word_histogramed = char_histogram(firs_word.upper())
for nth_word in words:
nth_word_histogramed = char_histogram(nth_word.upper())
flag = False
- for symbol in firs_word_histogramed:
+ for symbol in first_word_histogramed:
if not symbol.isalpha():
continue
- if symbol in nth_word_histogramed and\
- nth_word_histogramed[symbol] ==\
- firs_word_histogramed[symbol]:
+ if (symbol in nth_word_histogramed and
+ nth_word_histogramed[symbol] ==
+ first_word_histogramed[symbol]):
flag = True
else:
flag = False
break
if flag:
- for symbol_two in firs_word_histogramed:
+ for symbol_two in first_word_histogramed:
if not symbol_two.isalpha():
continue
- if symbol_two in nth_word_histogramed and\
- firs_word_histogramed[symbol_two]\
- == nth_word_histogramed[symbol_two]:
+ if (symbol_two in nth_word_histogramed and
+ first_word_histogramed[symbol_two]
+ == nth_word_histogramed[symbol_two]):
flag = True
else:
flag = False
break
if flag:
- temp.append(nth_word)
- if temp != [] and temp not in result:
- result.append(temp)
+ anagram_lists.append(nth_word)
+ if anagram_lists != [] and anagram_lists not in result:
+ result.append(anagram_lists)
return result

Изчистих някой неща ... сега ще довърша и другите.

"в is_pangram не трябва да има значение дали буквите са малки или големи - ако имаш A трябва да го броиш като срещане на a"

Ами мисля, че функцията работи точно така. Затова използвам sentence.lower() така всички вече ще са малки и не би трябвало да има проблем.

Благодаря за отделеното внимание! :)

Спасимир обнови решението на 19.03.2014 16:46 (преди около 10 години)

def is_pangram(sentence):
bulgarian_alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
- bulgarian_alphabet = list(bulgarian_alphabet)
encountered_symbols = {}
for symbol in bulgarian_alphabet:
if symbol in sentence.lower():
encountered_symbols[symbol] = 1
return len(encountered_symbols) == len(bulgarian_alphabet)
def char_histogram(text):
symbols_and_encounters = {}
for symbol in text:
if symbol in symbols_and_encounters:
symbols_and_encounters[symbol] += 1
else:
symbols_and_encounters[symbol] = 1
return symbols_and_encounters
def group_by_type(dictionary):
result = {}
for e in dictionary:
if type(e) not in result:
result[type(e)] = {e: dictionary[e]}
else: # type(e) in result:
result[type(e)][e] = dictionary[e]
return result
def sort_by(func, arguments):
result = arguments
for i in range(1, len(result)):
val = result[i]
j = i - 1
while (j >= 0) and func(result[j], val) > 0:
result[j+1] = result[j]
j -= 1
result[j+1] = val
return result
+def should_be_added(first_word_histogramed, nth_word):
+ nth_word_histogramed = char_histogram(nth_word.upper())
+ result = False
+ for symbol in first_word_histogramed:
+ if not symbol.isalpha():
+ continue
+ if (symbol in nth_word_histogramed and
+ nth_word_histogramed[symbol] ==
+ first_word_histogramed[symbol]):
+ result = True
+ else:
+ return False
+ if result:
+ for symbol_two in first_word_histogramed:
+ if not symbol_two.isalpha():
+ continue
+ if (symbol_two in nth_word_histogramed and
+ first_word_histogramed[symbol_two]
+ == nth_word_histogramed[symbol_two]):
+ result = True
+ else:
+ return False
+
+ return result
+
+
def anagrams(words):
result = []
anagram_lists = []
for firs_word in words:
anagram_lists = []
first_word_histogramed = char_histogram(firs_word.upper())
for nth_word in words:
- nth_word_histogramed = char_histogram(nth_word.upper())
- flag = False
- for symbol in first_word_histogramed:
- if not symbol.isalpha():
- continue
- if (symbol in nth_word_histogramed and
- nth_word_histogramed[symbol] ==
- first_word_histogramed[symbol]):
- flag = True
- else:
- flag = False
- break
- if flag:
- for symbol_two in first_word_histogramed:
- if not symbol_two.isalpha():
- continue
- if (symbol_two in nth_word_histogramed and
- first_word_histogramed[symbol_two]
- == nth_word_histogramed[symbol_two]):
- flag = True
- else:
- flag = False
- break
- if flag:
- anagram_lists.append(nth_word)
+ if should_be_added(first_word_histogramed, nth_word):
+ anagram_lists.append(nth_word)
if anagram_lists != [] and anagram_lists not in result:
result.append(anagram_lists)
return result

Спасимир обнови решението на 19.03.2014 16:59 (преди около 10 години)

def is_pangram(sentence):
bulgarian_alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
encountered_symbols = {}
for symbol in bulgarian_alphabet:
if symbol in sentence.lower():
encountered_symbols[symbol] = 1
return len(encountered_symbols) == len(bulgarian_alphabet)
def char_histogram(text):
symbols_and_encounters = {}
for symbol in text:
if symbol in symbols_and_encounters:
symbols_and_encounters[symbol] += 1
else:
symbols_and_encounters[symbol] = 1
return symbols_and_encounters
def group_by_type(dictionary):
result = {}
for e in dictionary:
if type(e) not in result:
result[type(e)] = {e: dictionary[e]}
else: # type(e) in result:
result[type(e)][e] = dictionary[e]
return result
def sort_by(func, arguments):
result = arguments
for i in range(1, len(result)):
val = result[i]
j = i - 1
while (j >= 0) and func(result[j], val) > 0:
result[j+1] = result[j]
j -= 1
result[j+1] = val
return result
-def should_be_added(first_word_histogramed, nth_word):
+def should_be_added(first_word, nth_word):
+ first_word_histogramed = char_histogram(first_word.upper())
nth_word_histogramed = char_histogram(nth_word.upper())
result = False
for symbol in first_word_histogramed:
if not symbol.isalpha():
continue
if (symbol in nth_word_histogramed and
nth_word_histogramed[symbol] ==
first_word_histogramed[symbol]):
result = True
else:
return False
- if result:
- for symbol_two in first_word_histogramed:
- if not symbol_two.isalpha():
- continue
- if (symbol_two in nth_word_histogramed and
- first_word_histogramed[symbol_two]
- == nth_word_histogramed[symbol_two]):
- result = True
- else:
- return False
-
return result
def anagrams(words):
result = []
anagram_lists = []
- for firs_word in words:
+ for first_word in words:
anagram_lists = []
- first_word_histogramed = char_histogram(firs_word.upper())
for nth_word in words:
- if should_be_added(first_word_histogramed, nth_word):
- anagram_lists.append(nth_word)
+ if (should_be_added(first_word, nth_word) and
+ should_be_added(nth_word, first_word)):
+ anagram_lists.append(nth_word)
if anagram_lists != [] and anagram_lists not in result:
result.append(anagram_lists)
return result