Решение на Пет функции от Стефан Владков

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

Към профила на Стефан Владков

Резултати

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

Код

CYRILLIC_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def is_pangram(sentence):
for letter in CYRILLIC_ALPHABET:
if letter not in sentence and letter.upper() not in sentence:
return False
return True
def char_histogram(text):
frequencies = {}
for char in text:
if char not in frequencies:
frequencies[char] = 1
else:
frequencies[char] = frequencies[char] + 1
return frequencies
def sort_by(func, arguments):
sorted = arguments
for position in range(len(sorted) - 1, 0, -1):
for counter in range(position):
if func(sorted[counter], sorted[counter + 1]) > 0:
couple = sorted[counter], sorted[counter + 1]
sorted[counter + 1], sorted[counter] = couple
return sorted
def group_by_type(dictionary):
groups = {}
for value in dictionary:
class_name = value.__class__
if class_name not in groups:
groups[class_name] = {}
groups[class_name][value] = dictionary[value]
return groups
def letter_count(string):
words = string.split(' ')
merged_words = ''
for word in words:
merged_words = merged_words + word
return len(merged_words)
def is_letter(symbol):
is_capital_latin = symbol > 'A' and symbol < 'Z'
is_latin = symbol > 'a' and symbol < 'z'
is_cyrillic = symbol.lower() in CYRILLIC_ALPHABET
return is_capital_latin or is_latin or is_cyrillic
def anagram(first_word, second_word):
for symbol in first_word:
if is_letter(symbol) and symbol.lower() not in second_word.lower():
return False
for symbol in second_word:
if is_letter(symbol) and symbol.lower() not in first_word.lower():
return False
return True
def anagrams(words):
result = []
added = False
for word in words:
added = False
for group in result:
if anagram(word, group[0]):
group.append(word)
added = True
if not added:
result.append([word])
return result

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

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

OK

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

Стефан обнови решението на 17.03.2014 01:09 (преди над 10 години)

+CYRILLIC_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+
+def is_pangram(sentence):
+ for letter in CYRILLIC_ALPHABET:
+ if letter not in sentence and letter.upper() not in sentence:
+ return False
+ return True
+
+def char_histogram(text):
+ frequencies = { }
+ for char in text:
+ if char not in frequencies:
+ frequencies[char] = 1
+ else:
+ frequencies[char] = frequencies[char] + 1
+ return frequencies
+
+def sort_by(func, arguments):
+ sorted = arguments
+ for position in range(len(sorted)):
+ for counter in range(position, len(sorted)):
+ if func(sorted[position], sorted[counter]) > 0:
+ temp = sorted[position]
+ sorted[position] = sorted[counter]
+ sorted[counter] = temp
+ return sorted
+
+def group_by_type(dictionary):
+ groups = {}
+ for value in dictionary:
+ class_name = value.__class__
+ if class_name not in groups:
+ groups[class_name] = {}
+ groups[class_name][value] = dictionary[value]
+ return groups
+
+def letter_count(string):
+ words = string.split(' ')
+ merged_words = ''
+ for word in words:
+ merged_words = merged_words + word
+
+ return len(merged_words)
+
+def is_letter(symbol):
+ is_capital_latin = symbol > 'A' and symbol < 'Z'
+ is_latin = symbol > 'a' and symbol < 'z'
+ is_cyrillic = symbol.lower() in CYRILLIC_ALPHABET
+ return is_capital_latin or is_latin or is_cyrillic
+
+def anagram(word_1, word_2):
+ for symbol in word_1:
+ if is_letter(symbol) and symbol.lower() not in word_2.lower():
+ return False
+ for symbol in word_2:
+ if is_letter(symbol) and symbol.lower() not in word_1.lower():
+ return False
+ return True
+
+def anagrams(words):
+ result = []
+ added = False
+ for word in words:
+ added = False
+ for group in result:
+ if anagram(word, group[0]):
+ group.append(word)
+ added = True
+ if not added:
+ result.append([word])
+ return result
  • Виж къде може да се издъни сортирането ти
  • В Python размяната на стойностите става така: a, b = b, a
  • При създаването на празен речник не е нужно да оставяме интервал между скобите
  • Името first_word е по-добро от word_1

Стефан обнови решението на 18.03.2014 01:58 (преди над 10 години)

CYRILLIC_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def is_pangram(sentence):
for letter in CYRILLIC_ALPHABET:
if letter not in sentence and letter.upper() not in sentence:
return False
return True
def char_histogram(text):
- frequencies = { }
+ frequencies = {}
for char in text:
if char not in frequencies:
frequencies[char] = 1
else:
frequencies[char] = frequencies[char] + 1
return frequencies
-
+
def sort_by(func, arguments):
sorted = arguments
- for position in range(len(sorted)):
- for counter in range(position, len(sorted)):
- if func(sorted[position], sorted[counter]) > 0:
- temp = sorted[position]
- sorted[position] = sorted[counter]
- sorted[counter] = temp
+ for position in range(len(sorted) - 1, 0, -1):
+ for counter in range(position):
+ if func(sorted[counter], sorted[counter + 1]) > 0:
+ couple = sorted[counter], sorted[counter + 1]
+ sorted[counter + 1], sorted[counter] = couple
return sorted
def group_by_type(dictionary):
groups = {}
for value in dictionary:
class_name = value.__class__
if class_name not in groups:
groups[class_name] = {}
groups[class_name][value] = dictionary[value]
return groups
def letter_count(string):
words = string.split(' ')
merged_words = ''
for word in words:
merged_words = merged_words + word
return len(merged_words)
def is_letter(symbol):
is_capital_latin = symbol > 'A' and symbol < 'Z'
is_latin = symbol > 'a' and symbol < 'z'
is_cyrillic = symbol.lower() in CYRILLIC_ALPHABET
return is_capital_latin or is_latin or is_cyrillic
-def anagram(word_1, word_2):
- for symbol in word_1:
- if is_letter(symbol) and symbol.lower() not in word_2.lower():
+def anagram(first_word, second_word):
+ for symbol in first_word:
+ if is_letter(symbol) and symbol.lower() not in second_word.lower():
return False
- for symbol in word_2:
- if is_letter(symbol) and symbol.lower() not in word_1.lower():
+ for symbol in second_word:
+ if is_letter(symbol) and symbol.lower() not in first_word.lower():
return False
return True
def anagrams(words):
result = []
added = False
for word in words:
added = False
for group in result:
if anagram(word, group[0]):
group.append(word)
added = True
if not added:
result.append([word])
return result