Решение на Пет функции от Таня Христова

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

Към профила на Таня Христова

Резултати

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

Код

from functools import cmp_to_key
def is_pangram(sentence):
alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
sentence = sentence.lower()
for letter in alphabet:
if letter not in sentence:
return False
return True
def count_char(text, char):
count = 0
for letter in text:
if letter == char:
count += 1
return count
def char_histogram(text):
histogram = {}
for char in text:
histogram[char] = histogram.get(char, 0) + 1
return histogram
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
grouped = {}
for key in dictionary.keys():
type_of_key = type(key)
grouped[type_of_key] = {k: dictionary[k] for k in dictionary if type(k) == type_of_key}
return grouped
def get_set_of_letters(word):
return {letter for letter in word if letter.isalpha()}
def is_anagram(first_word, second_word):
return get_set_of_letters(first_word.lower()) == get_set_of_letters(second_word.lower())
def anagrams(words):
anagrams = []
for word in words:
anagram = [w for w in words if is_anagram(w, word)]
if anagram not in anagrams:
anagrams.append(anagram)
return anagrams

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

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

OK

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

Таня обнови решението на 17.03.2014 15:31 (преди над 10 години)

+from functools import cmp_to_key
+
+
+def is_pangram(sentence):
+ alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ sentence = sentence.lower()
+ for letter in alphabet:
+ if letter not in sentence:
+ return False
+ return True
+
+
+def count_char(text, char):
+ count = 0
+ for letter in text:
+ if letter == char:
+ count += 1
+ return count
+
+
+def char_histogram(text):
+ histogram = {}
+ for char in text:
+ histogram[char] = histogram.get(char, 0) + 1
+ return histogram
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ grouped = {}
+ for key in dictionary.keys():
+ type_k = type(key)
+ grouped[type_k] = {k: dictionary[k] for k in dictionary if type(k) == type_k}
+ return grouped
+
+
+def get_set_of_letters(word):
+ return {char for char in word if char.isalpha()}
+
+
+def is_anagram(first_word, second_word):
+ return get_set_of_letters(first_word) == get_set_of_letters(second_word)
+
+
+def anagrams(words):
+ anagrams = []
+ for word in words:
+ anagram = [w for w in words if is_anagram(w, word)]
+ if anagram not in anagrams:
+ anagrams.append(anagram)
+ return anagrams

Таня обнови решението на 18.03.2014 19:33 (преди над 10 години)

from functools import cmp_to_key
def is_pangram(sentence):
alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
sentence = sentence.lower()
for letter in alphabet:
if letter not in sentence:
return False
return True
def count_char(text, char):
count = 0
for letter in text:
if letter == char:
count += 1
return count
def char_histogram(text):
histogram = {}
for char in text:
histogram[char] = histogram.get(char, 0) + 1
return histogram
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
grouped = {}
for key in dictionary.keys():
- type_k = type(key)
- grouped[type_k] = {k: dictionary[k] for k in dictionary if type(k) == type_k}
+ type_of_key = type(key)
+ grouped[type_of_key] = {k: dictionary[k] for k in dictionary if type(k) == type_of_key}
return grouped
def get_set_of_letters(word):
- return {char for char in word if char.isalpha()}
+ return {letter for letter in word if letter.isalpha()}
def is_anagram(first_word, second_word):
- return get_set_of_letters(first_word) == get_set_of_letters(second_word)
+ return get_set_of_letters(first_word.lower()) == get_set_of_letters(second_word.lower())
def anagrams(words):
anagrams = []
for word in words:
anagram = [w for w in words if is_anagram(w, word)]
if anagram not in anagrams:
anagrams.append(anagram)
return anagrams