Решение на Пет функции от Александър Златков

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

Към профила на Александър Златков

Резултати

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

Код

from collections import defaultdict
LETTERS = 'абвгдежзийклмнопрстуфхкцчшщьюя'
def is_pangram(text):
text_lowercase = text.lower()
for letter in LETTERS:
if letter not in text_lowercase:
return False
return True
def char_histogram(text):
histogram = defaultdict(int)
for letter in text:
histogram[letter] += 1
return histogram
def sort_by(func, arguments):
arguments_count = len(arguments)
sorted = list(arguments)
for i in range(1, arguments_count):
val = sorted[i]
j = i - 1
while (j >= 0) and func(sorted[j], val) > 0:
sorted[j+1] = sorted[j]
j = j-1
sorted[j+1] = val
return sorted
def group_by_type(dictionary):
types = defaultdict(dict)
for key in dictionary:
types[type(key)][key] = dictionary[key]
return dict(types)
def anagrams(words):
matched_anagrams = defaultdict(list)
for word in words:
characters = set()
for letter in word:
if letter.isalpha():
characters.add(letter.lower())
key = frozenset(characters)
matched_anagrams[key].append(word)
anagrams = []
for key in matched_anagrams:
anagrams.append(matched_anagrams[key])
return anagrams

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

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

OK

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

Александър обнови решението на 19.03.2014 02:01 (преди над 10 години)

+from collections import defaultdict
+
+LETTERS = 'абвгдежзийклмнопрстуфхкцчшщьюя'
+
+def is_pangram(text):
+ text_lowercase = text.lower()
+ for letter in LETTERS:
+ if letter not in text_lowercase:
+ return False
+ return True
+
+def char_histogram(text):
+ histogram = defaultdict(int)
+ for letter in text:
+ histogram[letter] += 1
+
+ return histogram
+
+def sort_by(func, arguments):
+ arguments_count = len(arguments)
+ sorted = list(arguments)
+ for i in range(1, arguments_count):
+ val = sorted[i]
+ j = i - 1
+ while (j >= 0) and func(sorted[j], val) > 0:
+ sorted[j+1] = sorted[j]
+ j = j - 1
+ sorted[j+1] = val
+
+ return sorted
+
+def group_by_type(dictionary):
+ types = defaultdict(dict)
+ for key in dictionary:
+ types[type(key)][key] = dictionary[key]
+
+ return dict(types)
+
+def anagrams(words):
+ matched_anagrams = defaultdict(list)
+ for word in words:
+ characters = set()
+ for letter in word:
+ if letter.isalpha():
+ characters.add(letter)
+ key = frozenset(characters)
+ matched_anagrams[key].append(word)
+
+ anagrams = []
+ for key in matched_anagrams:
+ anagrams.append(matched_anagrams[key])
+
+ return anagrams

Александър обнови решението на 19.03.2014 02:04 (преди над 10 години)

from collections import defaultdict
LETTERS = 'абвгдежзийклмнопрстуфхкцчшщьюя'
def is_pangram(text):
text_lowercase = text.lower()
for letter in LETTERS:
if letter not in text_lowercase:
return False
return True
def char_histogram(text):
histogram = defaultdict(int)
for letter in text:
histogram[letter] += 1
return histogram
def sort_by(func, arguments):
arguments_count = len(arguments)
sorted = list(arguments)
for i in range(1, arguments_count):
val = sorted[i]
j = i - 1
while (j >= 0) and func(sorted[j], val) > 0:
sorted[j+1] = sorted[j]
j = j - 1
sorted[j+1] = val
return sorted
def group_by_type(dictionary):
types = defaultdict(dict)
for key in dictionary:
types[type(key)][key] = dictionary[key]
return dict(types)
def anagrams(words):
matched_anagrams = defaultdict(list)
for word in words:
characters = set()
for letter in word:
if letter.isalpha():
- characters.add(letter)
+ characters.add(letter.lower())
key = frozenset(characters)
matched_anagrams[key].append(word)
anagrams = []
for key in matched_anagrams:
anagrams.append(matched_anagrams[key])
return anagrams

Александър обнови решението на 19.03.2014 02:09 (преди над 10 години)

from collections import defaultdict
LETTERS = 'абвгдежзийклмнопрстуфхкцчшщьюя'
+
def is_pangram(text):
text_lowercase = text.lower()
for letter in LETTERS:
if letter not in text_lowercase:
return False
return True
+
def char_histogram(text):
histogram = defaultdict(int)
for letter in text:
histogram[letter] += 1
return histogram
+
def sort_by(func, arguments):
arguments_count = len(arguments)
sorted = list(arguments)
for i in range(1, arguments_count):
val = sorted[i]
j = i - 1
while (j >= 0) and func(sorted[j], val) > 0:
sorted[j+1] = sorted[j]
- j = j - 1
+ j = j-1
sorted[j+1] = val
return sorted
+
def group_by_type(dictionary):
types = defaultdict(dict)
for key in dictionary:
types[type(key)][key] = dictionary[key]
return dict(types)
+
def anagrams(words):
matched_anagrams = defaultdict(list)
for word in words:
characters = set()
for letter in word:
if letter.isalpha():
characters.add(letter.lower())
key = frozenset(characters)
matched_anagrams[key].append(word)
anagrams = []
for key in matched_anagrams:
anagrams.append(matched_anagrams[key])
- return anagrams
+ return anagrams