Решение на Пет функции от Гено Чипилски

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

Към профила на Гено Чипилски

Резултати

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

Код

from collections import Counter
from functools import cmp_to_key
ALL_LETTERS_BG = 'абвгдежзийклмнопрстуфхцчшщъьюя'
ALL_LETTERS_BG_EN = ALL_LETTERS_BG + "abcdefghijklmnopqrstuvwxyz"
def is_pangram(sentence):
for letter in ALL_LETTERS_BG:
if letter.lower() not in sentence.lower():
return False
return True
def char_histogram(text):
return Counter(text)
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
def anagrams(words):
anagram_words = []
for word1 in words:
word_anagrams = [word1]
couter_word_1 = Counter(x for x in word1.lower() if x in ALL_LETTERS_BG_EN)
for word2 in words:
couter_word_2 = Counter(x for x in word2.lower() if x in ALL_LETTERS_BG_EN)
if couter_word_1 == couter_word_2:
word_anagrams.append(word2)
if word_anagrams not in anagram_words:
anagram_words.append(word_anagrams)
return anagram_words
def group_by_type(dictionary):
new_dict = {}
for key in dictionary:
group = new_dict.setdefault(type(key), {}) # key might exist already
group[key] = dictionary[key]
return new_dict

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.017s

OK

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

Гено обнови решението на 19.03.2014 10:16 (преди над 10 години)

+from collections import Counter
+
+def is_pangram(sentence):
+ all_letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ for letter in all_letters:
+ if letter.lower() not in sentence.lower():
+ return False
+ return True
+
+def char_histogram(text):
+ histogram = Counter()
+ for char in text:
+ histogram[char] += 1
+ return histogram
+
+def sort_by(func, arguments):
+ b_unsorted = True
+ while b_unsorted:
+ b_unsorted = False
+ for i in range(0,len(arguments)-1):
+ if func(arguments[i], arguments[i+1]) >= 0:
+ tmp = arguments[i+1]
+ arguments[i+1] = arguments[i]
+ arguments[i] = tmp
+ b_unsorted = True
+ if not b_unsorted:
+ return arguments
+
+def anagrams(words):
+ output = []
+ histograms = map(char_histogram, words)
+ already_found_histograms = []
+ histogram_words = []
+ for word_index_1 in range(0,len(words)):
+ if words[word_index_1] in already_found_histograms:
+ continue
+ histogram_words = [(words[word_index_1])]
+ already_found_histograms.append(words[word_index_1])
+ for word_index_2 in range(0,len(words)):
+ if words[word_index_2] in already_found_histograms:
+ continue
+ if histograms[word_index_1] == histograms[word_index_2]:
+ histogram_words.append(words[word_index_2])
+ already_found_histograms.append(words[word_index_2])
+ output.append(histogram_words)
+ return output
+
+def group_by_type(dictionary):
+ output = {}
+ for key in dictionary:
+ if type(key) not in output:
+ output[type(key)] = {}
+ output[type(key)][key] = dictionary[key]
+ return output

Гено обнови решението на 19.03.2014 12:40 (преди над 10 години)

from collections import Counter
+from functools import cmp_to_key
+ALL_LETTERS = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+
+
def is_pangram(sentence):
- all_letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
- for letter in all_letters:
+ for letter in ALL_LETTERS:
if letter.lower() not in sentence.lower():
return False
return True
+
def char_histogram(text):
- histogram = Counter()
- for char in text:
- histogram[char] += 1
- return histogram
+ return Counter(text)
+
def sort_by(func, arguments):
- b_unsorted = True
- while b_unsorted:
- b_unsorted = False
- for i in range(0,len(arguments)-1):
- if func(arguments[i], arguments[i+1]) >= 0:
- tmp = arguments[i+1]
- arguments[i+1] = arguments[i]
- arguments[i] = tmp
- b_unsorted = True
- if not b_unsorted:
- return arguments
+ return sorted(arguments, key=cmp_to_key(func))
+
def anagrams(words):
- output = []
- histograms = map(char_histogram, words)
- already_found_histograms = []
- histogram_words = []
- for word_index_1 in range(0,len(words)):
- if words[word_index_1] in already_found_histograms:
- continue
- histogram_words = [(words[word_index_1])]
- already_found_histograms.append(words[word_index_1])
- for word_index_2 in range(0,len(words)):
- if words[word_index_2] in already_found_histograms:
- continue
- if histograms[word_index_1] == histograms[word_index_2]:
- histogram_words.append(words[word_index_2])
- already_found_histograms.append(words[word_index_2])
- output.append(histogram_words)
- return output
+ anagram_words = []
+ for word1 in words:
+ word_anagrams = [word1]
+ for word2 in words:
+ if Counter(word1) == Counter(word2):
+ word_anagrams.append(word2)
+ if word_anagrams not in anagram_words:
+ anagram_words.append(word_anagrams)
+ return anagram_words
+
def group_by_type(dictionary):
- output = {}
+ new_dict = {}
for key in dictionary:
- if type(key) not in output:
- output[type(key)] = {}
+ group = new_dict.setdefault(type(key), {}) # key might exist already
- output[type(key)][key] = dictionary[key]
+ group[key] = dictionary[key]
- return output
+ return new_dict
  • в ALL_LETTERS всичко е lowercase, защо ти е да правиш letter.lower()?
  • group_by_type next step е да използваш defaultdict
  • word1, word2; знам, че и на двете места итерираш думи, но можеше да са word и another_word
  • при анаграмите гледамe само буквите(без препинателните знаци) и няма значение case-а на буквите(това дали са малки или главни); тоест тези двете са анаграми 'a,b,C', 'cBA'

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

from collections import Counter
from functools import cmp_to_key
-ALL_LETTERS = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ALL_LETTERS_BG = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ALL_LETTERS_BG_EN = ALL_LETTERS_BG + "abcdefghijklmnopqrstuvwxyz"
-
def is_pangram(sentence):
- for letter in ALL_LETTERS:
+ for letter in ALL_LETTERS_BG:
if letter.lower() not in sentence.lower():
return False
return True
-
def char_histogram(text):
return Counter(text)
-
def sort_by(func, arguments):
return sorted(arguments, key=cmp_to_key(func))
-
def anagrams(words):
anagram_words = []
for word1 in words:
word_anagrams = [word1]
+ couter_word_1 = Counter(x for x in word1.lower() if x in ALL_LETTERS_BG_EN)
for word2 in words:
- if Counter(word1) == Counter(word2):
+ couter_word_2 = Counter(x for x in word2.lower() if x in ALL_LETTERS_BG_EN)
+ if couter_word_1 == couter_word_2:
word_anagrams.append(word2)
if word_anagrams not in anagram_words:
anagram_words.append(word_anagrams)
return anagram_words
-
def group_by_type(dictionary):
new_dict = {}
for key in dictionary:
group = new_dict.setdefault(type(key), {}) # key might exist already
group[key] = dictionary[key]
return new_dict