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

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

Към профила на Димитър Желев

Резултати

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

Код

from collections import Counter, defaultdict
CYRILLIC_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
def is_pangram(sentence):
"""checks if sentence is a is pangram"""
alphaset = set(CYRILLIC_ALPHABET)
return alphaset <= set(sentence.lower())
def char_histogram(text):
"""returns char histogram of text"""
histogram = Counter()
for letter in text:
histogram[letter] += 1
return dict(histogram)
def sort_by(func, arguments):
"""returns sorted arguments using func as comparer"""
sorted_arguments = list(arguments)
for i in range(1, len(sorted_arguments)):
save = sorted_arguments[i]
j = i
while j > 0 and func(sorted_arguments[j - 1], save) > 0:
sorted_arguments[j] = sorted_arguments[j - 1]
j -= 1
sorted_arguments[j] = save
return sorted_arguments
def group_by_type(dictionary):
"""returns new dictionary with type grouping of dictionary items"""
type_groups = defaultdict(dict)
for entry in dictionary:
type_groups[type(entry)][entry] = dictionary[entry]
return type_groups
def anagrams(words):
"""returns lists of anagrams of words"""
anagrams_dictionary = defaultdict(list)
for word in words:
key = sum([ord(letter)
for letter in filter(str.isalpha, word.lower())])
anagrams_dictionary[key].append(word)
return [anagrams_dictionary[entry] for entry in anagrams_dictionary]

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.008s

OK

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

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

+from collections import Counter, defaultdict
+from string import punctuation, whitespace
+
+CYRILLIC_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
+
+
+def is_pangram(sentence):
+ """checks if sentence is a is pangram"""
+
+ alphaset = set(CYRILLIC_ALPHABET)
+ return alphaset <= set(sentence.lower())
+
+
+def char_histogram(text):
+ """returns char histogram of text"""
+
+ histogram = Counter()
+ for letter in text:
+ histogram[letter] += 1
+ return dict(histogram)
+
+
+def sort_by(func, arguments):
+ """returns sorted arguments using func as comparer"""
+
+ sorted_list = list(arguments)
+ for i in range(1, len(sorted_list)):
+ save = sorted_list[i]
+ j = i
+ while j > 0 and func(sorted_list[j - 1], save) > 0:
+ sorted_list[j] = sorted_list[j - 1]
+ j -= 1
+ sorted_list[j] = save
+ return sorted_list
+
+
+def group_by_type(dictionary):
+ """returns new dictionary with type grouping of dictionary items"""
+
+ type_dictionary = defaultdict(dict)
+ for entry in dictionary:
+ entry_type = type(entry)
+ type_dictionary[entry_type][entry] = dictionary[entry]
+ return type_dictionary
+
+
+def anagrams(words):
+ """returns lists of anagrams of words"""
+
+ dictionary = defaultdict(list)
+ for word in words:
+ new_word = [letter for letter in word if letter not in punctuation
+ and letter not in whitespace]
+ sorted_word = tuple(sorted(new_word))
+ dictionary[sorted_word].append(word)
+ return [dictionary[entry] for entry in dictionary]
  • Имената sorted_list, type_dictionary, dictionary и new_word са неподходящи. В group_by_type е приемливо да използваш dictionaryв последната функция са
  • Забележи, че в anagrams се интересуваме от буквите, а не от символите и освен това не е case-sensitive

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

from collections import Counter, defaultdict
-from string import punctuation, whitespace
CYRILLIC_ALPHABET = "абвгдежзийклмнопрстуфхцчшщъьюя"
def is_pangram(sentence):
"""checks if sentence is a is pangram"""
alphaset = set(CYRILLIC_ALPHABET)
return alphaset <= set(sentence.lower())
def char_histogram(text):
"""returns char histogram of text"""
histogram = Counter()
for letter in text:
histogram[letter] += 1
return dict(histogram)
def sort_by(func, arguments):
"""returns sorted arguments using func as comparer"""
- sorted_list = list(arguments)
- for i in range(1, len(sorted_list)):
- save = sorted_list[i]
+ sorted_arguments = list(arguments)
+ for i in range(1, len(sorted_arguments)):
+ save = sorted_arguments[i]
j = i
- while j > 0 and func(sorted_list[j - 1], save) > 0:
- sorted_list[j] = sorted_list[j - 1]
+ while j > 0 and func(sorted_arguments[j - 1], save) > 0:
+ sorted_arguments[j] = sorted_arguments[j - 1]
j -= 1
- sorted_list[j] = save
- return sorted_list
+ sorted_arguments[j] = save
+ return sorted_arguments
def group_by_type(dictionary):
"""returns new dictionary with type grouping of dictionary items"""
- type_dictionary = defaultdict(dict)
+ type_groups = defaultdict(dict)
for entry in dictionary:
- entry_type = type(entry)
- type_dictionary[entry_type][entry] = dictionary[entry]
- return type_dictionary
+ type_groups[type(entry)][entry] = dictionary[entry]
+ return type_groups
def anagrams(words):
"""returns lists of anagrams of words"""
- dictionary = defaultdict(list)
+ anagrams_dictionary = defaultdict(list)
for word in words:
- new_word = [letter for letter in word if letter not in punctuation
- and letter not in whitespace]
+ key = sum([ord(letter)
- sorted_word = tuple(sorted(new_word))
+ for letter in filter(str.isalpha, word.lower())])
- dictionary[sorted_word].append(word)
+ anagrams_dictionary[key].append(word)
- return [dictionary[entry] for entry in dictionary]
+ return [anagrams_dictionary[entry] for entry in anagrams_dictionary]