Решение на Пет функции от Иван Иванов

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

Към профила на Иван Иванов

Резултати

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

Код

def is_cyrillic_letter(letter):
if letter == 'ы' or letter == 'э':
return False
return letter >= 'а' and letter <= 'я'
def is_pangram(sentence):
letters = set()
for letter in sentence.lower():
if is_cyrillic_letter(letter):
letters.add(letter)
return len(letters) == 30
def char_histogram(text):
symbol_frequency = {}
for symbol in text:
if not (symbol in symbol_frequency):
symbol_frequency[symbol] = 0
symbol_frequency[symbol] += 1
return symbol_frequency
def sort_by(func, arguments):
for x in range(len(arguments)):
for y in range(len(arguments) - 1, x, -1):
if func(arguments[y-1], arguments[y]) > 0:
arguments[y], arguments[y-1] = arguments[y-1], arguments[y]
return arguments
def group_by_type(dictionary):
grouped = {}
for item in dictionary.items():
if not (type(item[0]) in grouped):
grouped[type(item[0])] = {item[0]: item[1]}
else:
grouped[type(item[0])].update({item[0]: item[1]})
return grouped
def is_letter(letter):
return (letter >= 'a' and letter <= 'z') or is_cyrillic_letter(letter)
def only_letters(word):
temp_word = ''
for symbol in word:
if is_letter(symbol):
temp_word += symbol
return temp_word
def is_anagram(word1, word2):
word1 = only_letters(word1.lower())
word2 = only_letters(word2.lower())
return char_histogram(word1) == char_histogram(word2)
def all_anagrams(anagram, words):
return [word for word in words if is_anagram(anagram, word)]
def remove_anagrams(anagram, words):
without_anagrams = []
for word in words:
if not is_anagram(word, anagram):
without_anagrams.append(word)
return without_anagrams
def anagrams(words):
sorted_anagrams = []
while len(words):
sorted_anagrams.append(all_anagrams(words[0], words))
words = remove_anagrams(words[0], words)
return sorted_anagrams

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.010s

OK

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

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

+def is_cyrillic_letter(letter):
+ if letter == 'ы' or letter == 'э':
+ return False
+ return letter >= 'а' and letter <= 'я'
+
+
+def is_pangram(sentence):
+ letters = set()
+ sentence = sentence.lower()
+ for letter in sentence:
+ if is_cyrillic_letter(letter):
+ letters.add(letter)
+ return len(letters) == 30
+
+
+def char_histogram(text):
+ symbol_frequency = {}
+ for symbol in text:
+ if not (symbol in symbol_frequency):
+ symbol_frequency[symbol] = 0
+ symbol_frequency[symbol] += 1
+ return symbol_frequency
+
+
+def sort_by(func, arguments):
+ for x in range(len(arguments)):
+ for y in range(len(arguments) - 1, x, -1):
+ if func(arguments[y-1], arguments[y]) > 0:
+ arguments[y], arguments[y-1] = arguments[y-1], arguments[y]
+ return arguments
+
+
+def group_by_type(dictionary):
+ grouped = {}
+ for item in dictionary.items():
+ if not (type(item[0]) in grouped):
+ grouped[type(item[0])] = {item[0]: item[1]}
+ else:
+ grouped[type(item[0])].update({item[0]: item[1]})
+ return grouped
+
+
+def is_letter(letter):
+ return (letter >= 'a' and letter <= 'z') or is_cyrillic_letter(letter)
+
+
+def only_letters(word):
+ temp_word = ''
+ for symbol in word:
+ if is_letter(symbol):
+ temp_word += symbol
+ return temp_word
+
+
+def is_anagram(word1, word2):
+ word1 = only_letters(word1.lower())
+ word2 = only_letters(word2.lower())
+ return char_histogram(word1) == char_histogram(word2)
+
+
+def all_anagrams(temp_word, words):
+ return [word for word in words if is_anagram(temp_word, word)]
+
+
+def remove_anagrams(temp_word, words):
+ without_anagrams = []
+ for word in words:
+ if not is_anagram(word, temp_word):
+ without_anagrams.append(word)
+ return without_anagrams
+
+
+def anagrams(words):
+ sorted_anagrams = []
+ while len(words):
+ sorted_anagrams.append(all_anagrams(words[0], words))
+ temp_word = words[0]
+ words = remove_anagrams(temp_word, words)
+ return sorted_anagrams
  • char_histogram и group_by_type можеш да ги подобриш, ако си прегледаш хитринките
  • можеш да добавяш ключ-стойност в речник без update така: d[key] = value
  • в is_pangram можеш направо for letter in sentence.lower():
  • temp_word не е гот, в крайна сметка по-добре е result, но в крайна сметка!
  • на 78-78 ред може направо words = remove_anagrams(words[0], words)

Иван обнови решението на 19.03.2014 00:20 (преди над 10 години)

def is_cyrillic_letter(letter):
if letter == 'ы' or letter == 'э':
return False
return letter >= 'а' and letter <= 'я'
def is_pangram(sentence):
letters = set()
- sentence = sentence.lower()
- for letter in sentence:
+ for letter in sentence.lower():
if is_cyrillic_letter(letter):
letters.add(letter)
return len(letters) == 30
def char_histogram(text):
symbol_frequency = {}
for symbol in text:
if not (symbol in symbol_frequency):
symbol_frequency[symbol] = 0
symbol_frequency[symbol] += 1
return symbol_frequency
def sort_by(func, arguments):
for x in range(len(arguments)):
for y in range(len(arguments) - 1, x, -1):
if func(arguments[y-1], arguments[y]) > 0:
arguments[y], arguments[y-1] = arguments[y-1], arguments[y]
return arguments
def group_by_type(dictionary):
grouped = {}
for item in dictionary.items():
if not (type(item[0]) in grouped):
grouped[type(item[0])] = {item[0]: item[1]}
else:
grouped[type(item[0])].update({item[0]: item[1]})
return grouped
def is_letter(letter):
return (letter >= 'a' and letter <= 'z') or is_cyrillic_letter(letter)
def only_letters(word):
temp_word = ''
for symbol in word:
if is_letter(symbol):
temp_word += symbol
return temp_word
def is_anagram(word1, word2):
word1 = only_letters(word1.lower())
word2 = only_letters(word2.lower())
return char_histogram(word1) == char_histogram(word2)
-def all_anagrams(temp_word, words):
- return [word for word in words if is_anagram(temp_word, word)]
+def all_anagrams(anagram, words):
+ return [word for word in words if is_anagram(anagram, word)]
-def remove_anagrams(temp_word, words):
+def remove_anagrams(anagram, words):
without_anagrams = []
for word in words:
- if not is_anagram(word, temp_word):
+ if not is_anagram(word, anagram):
without_anagrams.append(word)
return without_anagrams
def anagrams(words):
sorted_anagrams = []
while len(words):
sorted_anagrams.append(all_anagrams(words[0], words))
- temp_word = words[0]
- words = remove_anagrams(temp_word, words)
+ words = remove_anagrams(words[0], words)
return sorted_anagrams

Благодаря за бързия коментар. Тъй като не съм на моят компютър, промених нещата, за които съм сигурен и без интерпретатор. Утре, ако се добера до компютър с пайтън, ще променя и останалото.