Решение на Пет функции от Веляна Димова

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

Към профила на Веляна Димова

Резултати

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

Код

from functools import cmp_to_key
def is_pangram(sentence):
letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for letter in letters:
if not letter in sentence.lower():
return False
return True
def char_histogram(text):
result = {}
for symbol in text:
result[str(symbol)] = text.count(symbol)
return result
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
result = {}
for key in dictionary:
if not type(key) in result:
result[type(key)] = {key: dictionary[key]}
else:
result[type(key)][key] = dictionary[key]
return result
def is_anagram(word1, word2):
return sorted(list(word1)) == sorted(list(word2))
def word_anagram(word, words_list):
result = []
for second_word in words_list:
if is_anagram(word, second_word):
result.append(second_word)
return result
def anagrams(words):
result = []
for word in words:
if not word_anagram(word, words) in result:
result.append(word_anagram(word, words))
return result

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

▸ Покажи лога

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

Веляна обнови решението на 19.03.2014 00:20 (преди около 11 години)

▸ Покажи разликите
+from functools import cmp_to_key
+
+def is_pangram(sentence):
+ letter_list = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ for letter in letter_list:
+ if not letter in sentence.lower():
+ return False
+ return True
+
+def char_histogram(text):
+ symbol_dict = {}
+ for symbol in text:
+ symbol_dict[str(symbol)] = text.count(symbol)
+
+ return symbol_dict
+
+def sort_by(func, arguments):
+ return sorted(arguments, key = cmp_to_key(func))
+
+def group_by_type(dictionary):
+ result = {}
+ for key in dictionary:
+ if not type(key) in result:
+ result[type(key)] = {key:dictionary[key]}
+ else:
+ result[type(key)][key] = dictionary[key]
+ return result
+
+def is_anagram(word1, word2):
+ return (sorted(list(word1)) == sorted(list(word2)))
+
+def word_anagram(word, words_list):
+ result = []
+ for word2 in words_list:
+ if is_anagram(word, word2):
+ result.append(word2)
+ return result
+
+def anagrams(words):
+ result = []
+ for word in words:
+ if not word_anagram(word, words) in result:
+ result.append(word_anagram(word, words))
+ return result

Веляна обнови решението на 19.03.2014 09:36 (преди около 11 години)

▸ Покажи разликите
from functools import cmp_to_key
+
def is_pangram(sentence):
letter_list = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for letter in letter_list:
if not letter in sentence.lower():
return False
return True
+
def char_histogram(text):
symbol_dict = {}
for symbol in text:
symbol_dict[str(symbol)] = text.count(symbol)
-
return symbol_dict
+
def sort_by(func, arguments):
- return sorted(arguments, key = cmp_to_key(func))
+ return sorted(arguments, key=cmp_to_key(func))
+
def group_by_type(dictionary):
result = {}
for key in dictionary:
if not type(key) in result:
- result[type(key)] = {key:dictionary[key]}
+ result[type(key)] = {key: dictionary[key]}
else:
result[type(key)][key] = dictionary[key]
return result
+
def is_anagram(word1, word2):
return (sorted(list(word1)) == sorted(list(word2)))
+
def word_anagram(word, words_list):
result = []
for word2 in words_list:
if is_anagram(word, word2):
result.append(word2)
return result
+
def anagrams(words):
result = []
for word in words:
if not word_anagram(word, words) in result:
result.append(word_anagram(word, words))
- return result
+ return result
  • letter_list не е list, това е объркващо
  • не трябва типа на обекта да присъства в името на променливите: words_list, letter_list, symbol_dict
  • word2 не е добър избор за име на променлива

      if not type(key) in result:
          result[type(key)] = {key: dictionary[key]}
      else:
          result[type(key)][key] = dictionary[key]
    

Разгледай за defaultdict.

return (sorted(list(word1)) == sorted(list(word2))) - скобите в израза са илишни

Веляна обнови решението на 19.03.2014 12:54 (преди около 11 години)

▸ Покажи разликите
from functools import cmp_to_key
def is_pangram(sentence):
- letter_list = 'абвгдежзийклмнопрстуфхцчшщъьюя'
- for letter in letter_list:
+ letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ for letter in letters:
if not letter in sentence.lower():
return False
return True
def char_histogram(text):
- symbol_dict = {}
+ result = {}
for symbol in text:
- symbol_dict[str(symbol)] = text.count(symbol)
- return symbol_dict
+ result[str(symbol)] = text.count(symbol)
+ return result
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
result = {}
for key in dictionary:
if not type(key) in result:
result[type(key)] = {key: dictionary[key]}
else:
result[type(key)][key] = dictionary[key]
return result
def is_anagram(word1, word2):
- return (sorted(list(word1)) == sorted(list(word2)))
+ return sorted(list(word1)) == sorted(list(word2))
def word_anagram(word, words_list):
result = []
- for word2 in words_list:
- if is_anagram(word, word2):
- result.append(word2)
+ for second_word in words_list:
+ if is_anagram(word, second_word):
+ result.append(second_word)
return result
def anagrams(words):
result = []
for word in words:
if not word_anagram(word, words) in result:
result.append(word_anagram(word, words))
return result