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

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

Към профила на Никола Димитров

Резултати

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

Код

from itertools import groupby
from collections import defaultdict
from functools import cmp_to_key
ALPHABET_SIZE = 30
def is_pangram(sentence):
chars_in_text = [char_code for char_code in range(ord('а'), ord('я') + 1)
if chr(char_code) in sentence.lower()]
return len(chars_in_text) == ALPHABET_SIZE
def char_histogram(text):
return { letter: text.count(letter) for letter in text }
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
# Since itertools.groupby requires a pre-sorted list:
sorted_keys = sorted(dictionary.keys(), key=lambda x: str(type(x)))
entries_by_type = [(class_type, list(keys)) for class_type, keys in
groupby(sorted_keys, type)]
result = defaultdict(dict)
for class_type, key_list in entries_by_type:
for key in key_list:
result[class_type][key] = dictionary[key]
return dict(result)
def case_insensitive_histogram(word):
insensitive = char_histogram(word)
lower_dict_keys = { key.lower() for key in insensitive.keys() }
for lower in lower_dict_keys:
upper = lower.upper()
upper_count = insensitive[upper] if upper in insensitive else 0
lower_count = insensitive[lower] if lower in insensitive else 0
insensitive[lower] = insensitive[upper] = lower_count + upper_count
return insensitive
def extract_letters(word):
return "".join([letter for letter in word if letter.isalpha()])
def is_anagram_to(word, other):
word = extract_letters(word)
other = extract_letters(other)
return case_insensitive_histogram(word) == case_insensitive_histogram(other)
def anagrams(words):
grouped_by_anagrams = dict()
for word in words:
grouped_by_anagrams[word] = [other for other in words
if is_anagram_to(word, other)]
# Join the strings and convert to set to remove duplicates
# (Can't convert to set right away since lists are mutable)
result = {",".join(value) for value in grouped_by_anagrams.values()}
result = [value.split(",") for value in result]
return result

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.014s

OK

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

Никола обнови решението на 14.03.2014 00:27 (преди около 10 години)

+from itertools import groupby
+from collections import defaultdict
+from functools import cmp_to_key
+
+ALPHABET_SIZE = 30
+def is_pangram(sentence):
+ chars_in_text = [char_code for char_code in range(ord('а'), ord('я') + 1)
+ if chr(char_code) in sentence.lower()]
+ return len(chars_in_text) == ALPHABET_SIZE
+
+
+def char_histogram(text):
+ return { letter: text.count(letter) for letter in text }
+
+
+def sort_by(func, arguments):
+ return sorted(arguments, key=cmp_to_key(func))
+
+
+def group_by_type(dictionary):
+ # Since itertools.groupby requires a pre-sorted list:
+ sorted_keys = sorted(dictionary.keys(), key=lambda x: str(type(x)))
+ entries_by_type = [(class_type, list(keys)) for class_type, keys in
+ groupby(sorted_keys, type)]
+
+ result = defaultdict(dict)
+ for class_type, key_list in entries_by_type:
+ for key in key_list:
+ result[class_type][key] = dictionary[key]
+
+ return dict(result)
+
+
+def is_anagram_to(word, possible_anagram):
+ return char_histogram(word) == char_histogram(possible_anagram)
+
+
+def anagrams(words):
+ grouped_by_anagrams = dict()
+ for word in words:
+ grouped_by_anagrams[word] = [other for other in words
+ if is_anagram_to(word, other)]
+ # Join the strings and convert to set to remove duplicates
+ # (Can't convert to set right away since lists are mutable)
+ result = {",".join(value) for value in grouped_by_anagrams.values()}
+ result = [value.split(",") for value in result]
+ return result

Никола обнови решението на 16.03.2014 10:06 (преди около 10 години)

from itertools import groupby
from collections import defaultdict
from functools import cmp_to_key
ALPHABET_SIZE = 30
def is_pangram(sentence):
chars_in_text = [char_code for char_code in range(ord('а'), ord('я') + 1)
if chr(char_code) in sentence.lower()]
return len(chars_in_text) == ALPHABET_SIZE
def char_histogram(text):
return { letter: text.count(letter) for letter in text }
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
# Since itertools.groupby requires a pre-sorted list:
sorted_keys = sorted(dictionary.keys(), key=lambda x: str(type(x)))
entries_by_type = [(class_type, list(keys)) for class_type, keys in
groupby(sorted_keys, type)]
result = defaultdict(dict)
for class_type, key_list in entries_by_type:
for key in key_list:
result[class_type][key] = dictionary[key]
return dict(result)
-def is_anagram_to(word, possible_anagram):
- return char_histogram(word) == char_histogram(possible_anagram)
+def case_insensitive_histogram(word):
+ insensitive = char_histogram(word)
+ lower_dict_keys = { key.lower() for key in insensitive.keys() }
+ for lower in lower_dict_keys:
+ upper = lower.upper()
+ upper_count = insensitive[upper] if upper in insensitive else 0
+ lower_count = insensitive[lower] if lower in insensitive else 0
+ insensitive[lower] = insensitive[upper] = lower_count + upper_count
+ return insensitive
+
+def is_anagram_to(word, other):
+ return case_insensitive_histogram(word) == case_insensitive_histogram(other)
+
+
def anagrams(words):
grouped_by_anagrams = dict()
for word in words:
grouped_by_anagrams[word] = [other for other in words
if is_anagram_to(word, other)]
# Join the strings and convert to set to remove duplicates
# (Can't convert to set right away since lists are mutable)
result = {",".join(value) for value in grouped_by_anagrams.values()}
result = [value.split(",") for value in result]
- return result
+ return result

Никола обнови решението на 19.03.2014 01:20 (преди около 10 години)

from itertools import groupby
from collections import defaultdict
from functools import cmp_to_key
ALPHABET_SIZE = 30
def is_pangram(sentence):
chars_in_text = [char_code for char_code in range(ord('а'), ord('я') + 1)
if chr(char_code) in sentence.lower()]
return len(chars_in_text) == ALPHABET_SIZE
def char_histogram(text):
return { letter: text.count(letter) for letter in text }
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
# Since itertools.groupby requires a pre-sorted list:
sorted_keys = sorted(dictionary.keys(), key=lambda x: str(type(x)))
entries_by_type = [(class_type, list(keys)) for class_type, keys in
groupby(sorted_keys, type)]
result = defaultdict(dict)
for class_type, key_list in entries_by_type:
for key in key_list:
result[class_type][key] = dictionary[key]
return dict(result)
def case_insensitive_histogram(word):
insensitive = char_histogram(word)
lower_dict_keys = { key.lower() for key in insensitive.keys() }
for lower in lower_dict_keys:
upper = lower.upper()
upper_count = insensitive[upper] if upper in insensitive else 0
lower_count = insensitive[lower] if lower in insensitive else 0
insensitive[lower] = insensitive[upper] = lower_count + upper_count
return insensitive
+def extract_letters(word):
+ return "".join([letter for letter in word if letter.isalpha()])
+
def is_anagram_to(word, other):
- return case_insensitive_histogram(word) == case_insensitive_histogram(other)
+ return case_insensitive_histogram(extract_letters(word)) == case_insensitive_histogram(extract_letters(other))
def anagrams(words):
grouped_by_anagrams = dict()
for word in words:
grouped_by_anagrams[word] = [other for other in words
if is_anagram_to(word, other)]
# Join the strings and convert to set to remove duplicates
# (Can't convert to set right away since lists are mutable)
result = {",".join(value) for value in grouped_by_anagrams.values()}
result = [value.split(",") for value in result]
return result

Никола обнови решението на 19.03.2014 01:23 (преди около 10 години)

from itertools import groupby
from collections import defaultdict
from functools import cmp_to_key
ALPHABET_SIZE = 30
def is_pangram(sentence):
chars_in_text = [char_code for char_code in range(ord('а'), ord('я') + 1)
if chr(char_code) in sentence.lower()]
return len(chars_in_text) == ALPHABET_SIZE
def char_histogram(text):
return { letter: text.count(letter) for letter in text }
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def group_by_type(dictionary):
# Since itertools.groupby requires a pre-sorted list:
sorted_keys = sorted(dictionary.keys(), key=lambda x: str(type(x)))
entries_by_type = [(class_type, list(keys)) for class_type, keys in
groupby(sorted_keys, type)]
result = defaultdict(dict)
for class_type, key_list in entries_by_type:
for key in key_list:
result[class_type][key] = dictionary[key]
return dict(result)
def case_insensitive_histogram(word):
insensitive = char_histogram(word)
lower_dict_keys = { key.lower() for key in insensitive.keys() }
for lower in lower_dict_keys:
upper = lower.upper()
upper_count = insensitive[upper] if upper in insensitive else 0
lower_count = insensitive[lower] if lower in insensitive else 0
insensitive[lower] = insensitive[upper] = lower_count + upper_count
return insensitive
def extract_letters(word):
return "".join([letter for letter in word if letter.isalpha()])
def is_anagram_to(word, other):
- return case_insensitive_histogram(extract_letters(word)) == case_insensitive_histogram(extract_letters(other))
+ word = extract_letters(word)
+ other = extract_letters(other)
+ return case_insensitive_histogram(word) == case_insensitive_histogram(other)
def anagrams(words):
grouped_by_anagrams = dict()
for word in words:
grouped_by_anagrams[word] = [other for other in words
if is_anagram_to(word, other)]
# Join the strings and convert to set to remove duplicates
# (Can't convert to set right away since lists are mutable)
result = {",".join(value) for value in grouped_by_anagrams.values()}
result = [value.split(",") for value in result]
return result