Решение на Пет функции от Теодор Халваджиев

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

Към профила на Теодор Халваджиев

Резултати

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

Код

import functools
from collections import Counter
from collections import defaultdict
def is_pangram(pangram):
alphabet = ["а", "б", "в", "г", "д", "е", "ж", "з", "и", "й", "к", "л",
"м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч",
"ш", "щ", "ъ", "ь", "ю", "я"]
for letter in pangram.lower():
if letter in alphabet:
alphabet.remove(letter)
return not len(alphabet)
def char_histogram(sentence):
histogram = Counter(sentence)
return {item: histogram[item] for item in histogram}
def sort_by(criterion, to_sort):
return sorted(to_sort, key=functools.cmp_to_key(criterion))
def group_by_type(mixed_types):
sorted_types = defaultdict(dict)
for item in mixed_types:
sorted_types[type(item)][item] = mixed_types[item]
return sorted_types
def anagrams(words):
anagram_collected = defaultdict(list)
for word in words:
code = "".join(char for char in sorted(word.lower()) if char.isalpha())
anagram_collected[code].append(word)
return [anagram_collected[sublist] for sublist in anagram_collected]

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.009s

OK

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

Теодор обнови решението на 15.03.2014 15:26 (преди над 10 години)

+def is_pangram(pangram):
+ alphabet = ["а", "б", "в", "г", "д", "е", "ж", "з", "и", "й", "к", "л",
+ "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч",
+ "ш", "щ", "ъ", "ь", "ю", "я"]
+ for letter in pangram.lower():
+ if letter in alphabet:
+ alphabet.remove(letter)
+ return not len(alphabet)
+
+
+def char_histogram(sentence):
+ number_of_appearances = {}
+ already_calculated = set()
+ for letter in sentence:
+ appeared = 1
+ if(not letter in already_calculated):
+ already_calculated.add(letter)
+ for i in range(sentence.find(letter) + 1, len(sentence)):
+ if sentence[i] == letter:
+ appeared += 1
+ number_of_appearances[letter] = appeared
+ return number_of_appearances
+
+
+def sort_by(criterion, to_sort):
+ for i in range(0, len(to_sort)):
+ for j in range(0, len(to_sort) - 1 - i):
+ if criterion(to_sort[j], to_sort[j + 1]) > 0:
+ to_sort[j], to_sort[j + 1] = to_sort[j + 1], to_sort[j]
+ return to_sort
+
+
+def group_by_type(mixed_types):
+ sorted_types = {}
+ same_type = {}
+ current_type = ""
+ for item in mixed_types:
+ for item_compare in mixed_types:
+ if type(item) == type(item_compare):
+ same_type[item_compare] = mixed_types.get(item_compare)
+ current_type = type(item_compare)
+ sorted_types[current_type] = same_type.copy()
+ same_type.clear()
+ return sorted_types
+
+
+def are_anagrams(word, anagram):
+ letters_word = [letter for letter in word]
+ letters_anagram = [letter for letter in anagram]
+ for letter in letters_word.copy():
+ if letter in letters_anagram:
+ letters_word.remove(letter)
+ letters_anagram.remove(letter)
+ return not(bool(len(letters_word)) or bool(len(letters_anagram)))
+
+
+def anagrams(words):
+ anagram_collected = []
+ anagram_sublist = []
+ anagrammed = set()
+ for sample in words:
+ for compare in words:
+ if are_anagrams(sample, compare) and not compare in anagrammed:
+ anagram_sublist.append(compare)
+ anagrammed.add(compare)
+ if len(anagram_sublist) != 0:
+ anagram_collected.append(anagram_sublist.copy())
+ anagram_sublist.clear()
+ return anagram_collected

Теодор обнови решението на 16.03.2014 20:35 (преди над 10 години)

+import functools
+from collections import Counter
+from collections import defaultdict
+
+
def is_pangram(pangram):
alphabet = ["а", "б", "в", "г", "д", "е", "ж", "з", "и", "й", "к", "л",
"м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч",
"ш", "щ", "ъ", "ь", "ю", "я"]
for letter in pangram.lower():
if letter in alphabet:
alphabet.remove(letter)
return not len(alphabet)
def char_histogram(sentence):
- number_of_appearances = {}
- already_calculated = set()
- for letter in sentence:
- appeared = 1
- if(not letter in already_calculated):
- already_calculated.add(letter)
- for i in range(sentence.find(letter) + 1, len(sentence)):
- if sentence[i] == letter:
- appeared += 1
- number_of_appearances[letter] = appeared
- return number_of_appearances
+ histogram = Counter(sentence)
+ return {item: histogram[item] for item in histogram}
def sort_by(criterion, to_sort):
- for i in range(0, len(to_sort)):
- for j in range(0, len(to_sort) - 1 - i):
- if criterion(to_sort[j], to_sort[j + 1]) > 0:
- to_sort[j], to_sort[j + 1] = to_sort[j + 1], to_sort[j]
- return to_sort
+ return sorted(to_sort, key=functools.cmp_to_key(criterion))
def group_by_type(mixed_types):
- sorted_types = {}
- same_type = {}
- current_type = ""
+ sorted_types = defaultdict(dict)
for item in mixed_types:
- for item_compare in mixed_types:
- if type(item) == type(item_compare):
- same_type[item_compare] = mixed_types.get(item_compare)
- current_type = type(item_compare)
- sorted_types[current_type] = same_type.copy()
- same_type.clear()
+ sorted_types[type(item)][item] = mixed_types.get(item)
return sorted_types
-def are_anagrams(word, anagram):
- letters_word = [letter for letter in word]
- letters_anagram = [letter for letter in anagram]
- for letter in letters_word.copy():
- if letter in letters_anagram:
- letters_word.remove(letter)
- letters_anagram.remove(letter)
- return not(bool(len(letters_word)) or bool(len(letters_anagram)))
-
-
def anagrams(words):
- anagram_collected = []
- anagram_sublist = []
+ anagram_collected = defaultdict(list)
- anagrammed = set()
+ for word in words:
- for sample in words:
+ code = "".join(char.lower() for char in sorted(word) if char.isalpha())
- for compare in words:
+ anagram_collected[code].append(word)
- if are_anagrams(sample, compare) and not compare in anagrammed:
+ return [anagram_collected[sublist] for sublist in anagram_collected]
- anagram_sublist.append(compare)
- anagrammed.add(compare)
- if len(anagram_sublist) != 0:
- anagram_collected.append(anagram_sublist.copy())
- anagram_sublist.clear()
- return anagram_collected

Всъщност, ако визираш хитринката за get/setdefault тук ти е повече от излишна. Няма как да нямаш ключа в речника след като итерираш през ключовете му ;) Дори е по-добре да итерираш по mixed_types.items() направо вземайки двойките key, value, т.к. ползваш и двете..

Теодор обнови решението на 18.03.2014 21:00 (преди над 10 години)

import functools
from collections import Counter
from collections import defaultdict
def is_pangram(pangram):
alphabet = ["а", "б", "в", "г", "д", "е", "ж", "з", "и", "й", "к", "л",
"м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч",
"ш", "щ", "ъ", "ь", "ю", "я"]
for letter in pangram.lower():
if letter in alphabet:
alphabet.remove(letter)
return not len(alphabet)
def char_histogram(sentence):
histogram = Counter(sentence)
return {item: histogram[item] for item in histogram}
def sort_by(criterion, to_sort):
return sorted(to_sort, key=functools.cmp_to_key(criterion))
def group_by_type(mixed_types):
sorted_types = defaultdict(dict)
for item in mixed_types:
- sorted_types[type(item)][item] = mixed_types.get(item)
+ sorted_types[type(item)][item] = mixed_types[item]
return sorted_types
def anagrams(words):
anagram_collected = defaultdict(list)
for word in words:
code = "".join(char.lower() for char in sorted(word) if char.isalpha())
anagram_collected[code].append(word)
return [anagram_collected[sublist] for sublist in anagram_collected]

Теодор обнови решението на 19.03.2014 15:53 (преди над 10 години)

import functools
from collections import Counter
from collections import defaultdict
def is_pangram(pangram):
alphabet = ["а", "б", "в", "г", "д", "е", "ж", "з", "и", "й", "к", "л",
"м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч",
"ш", "щ", "ъ", "ь", "ю", "я"]
for letter in pangram.lower():
if letter in alphabet:
alphabet.remove(letter)
return not len(alphabet)
def char_histogram(sentence):
histogram = Counter(sentence)
return {item: histogram[item] for item in histogram}
def sort_by(criterion, to_sort):
return sorted(to_sort, key=functools.cmp_to_key(criterion))
def group_by_type(mixed_types):
sorted_types = defaultdict(dict)
for item in mixed_types:
sorted_types[type(item)][item] = mixed_types[item]
return sorted_types
def anagrams(words):
anagram_collected = defaultdict(list)
for word in words:
- code = "".join(char.lower() for char in sorted(word) if char.isalpha())
+ code = "".join(char for char in sorted(word.lower()) if char.isalpha())
anagram_collected[code].append(word)
return [anagram_collected[sublist] for sublist in anagram_collected]