Решение на Пет функции от Евгени Евлогиев

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

Към профила на Евгени Евлогиев

Резултати

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

Код

import functools
def is_pangram(sentence):
found_letters = set()
for letter in sentence.lower():
if (letter >= 'а' and letter <= 'я' and
letter != 'ы' and letter != 'э'):
found_letters.add(letter)
return len(found_letters) == 30
def char_histogram(text):
result = dict()
for symbol in text:
result[symbol] = 1 + result.get(symbol, 0)
return result
def sort_by(func, arguments):
return sorted(arguments, key=functools.cmp_to_key(func))
def group_by_type(dictionary):
result = {}
for key, value in dictionary.items():
result.setdefault(type(key), {}).update({key: value})
return result
def letters_count(word):
result = {}
for symbol in word:
if symbol.islower():
result[symbol] = 1 + result.get(symbol, 0)
return result
def anagrams(words):
result = []
for word in words:
founded_word = False
for item in result:
if letters_count(word.lower()) == letters_count(item[0].lower()):
item.append(word)
founded_word = True
break
if not founded_word:
new_word = []
new_word.append(word)
result.append(new_word)
return result

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.033s

OK

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

Евгени обнови решението на 16.03.2014 11:32 (преди над 10 години)

+def is_pangram(sentence):
+ founded_letters = set()
+ for letter in sentence.lower():
+ if (ord(letter) >= 1072 and ord(letter) <= 1103 and
+ ord(letter) != 1101 and ord(letter) != 1099 and
+ letter not in founded_letters):
+ founded_letters.add(letter)
+ return len(founded_letters) == 30
+
+
+def char_histogram(text):
+ result = dict()
+ for symbol in text:
+ if symbol in result:
+ result[symbol] += 1
+ else:
+ result[symbol] = 1
+ return result
+
+
+def sort_by(func, arguments):
+ length = len(arguments) - 1
+ is_sorted = False
+ while not is_sorted:
+ is_sorted = True
+ for i in range(length):
+ if func(arguments[i], arguments[i + 1]) > 0:
+ arguments[i], arguments[i + 1] = arguments[i + 1], arguments[i]
+ is_sorted = False
+ return arguments
+
+
+def group_by_type(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ if type(key) in result:
+ result[type(key)].update({key: value})
+ else:
+ result[type(key)] = {key: value}
+ return result
+
+
+def string_to_set(word):
+ result = set()
+ for letter in word:
+ result.add(letter)
+ return result
+
+
+def anagrams(words):
+ result = []
+ for word in words:
+ founded_word = False
+ for item in result:
+ if string_to_set(word) == string_to_set(item[0]):
+ item.append(word)
+ founded_word = True
+ break
+ if not founded_word:
+ new_word = []
+ new_word.append(word)
+ result.append(new_word)
+ return result
  • малко правопис founded -> found
  • в is_pangram нали използваш множество, тогава няма смисъл от тази проверка letter not in founded_letters
  • условието на if-а в is_pangram е много нечетимо, защото човек от къде да знае int-а отговарящ на всеки символ; тоест за да ми говори нещо това условие трябва да google-на таблицата и alt-tab-вайки да дешифрирам; по-добре е в условието да използваш самите букви, а не съответните им числа
  • char_histogram и group_by_type можеш да ги подобриш, ако прегледаш хитринките
  • хубаво си си написал сорт, но обикновенно се ползва нещо наготово
  • string_to_set - можеш да пробваш така set(given_string)
  • anagrams трябва да работи и в такива случаи: 'ew' и 'We!' са анаграми

Евгени обнови решението на 16.03.2014 23:04 (преди над 10 години)

+import functools
+
+
def is_pangram(sentence):
- founded_letters = set()
+ found_letters = set()
for letter in sentence.lower():
- if (ord(letter) >= 1072 and ord(letter) <= 1103 and
- ord(letter) != 1101 and ord(letter) != 1099 and
- letter not in founded_letters):
- founded_letters.add(letter)
- return len(founded_letters) == 30
+ if (letter >= 'а' and letter <= 'я' and
+ letter != 'ы' and letter != 'э'):
+ found_letters.add(letter)
+ return len(found_letters) == 30
def char_histogram(text):
result = dict()
for symbol in text:
- if symbol in result:
- result[symbol] += 1
- else:
- result[symbol] = 1
+ result[symbol] = 1 + result.get(symbol, 0)
return result
def sort_by(func, arguments):
- length = len(arguments) - 1
- is_sorted = False
- while not is_sorted:
- is_sorted = True
- for i in range(length):
- if func(arguments[i], arguments[i + 1]) > 0:
- arguments[i], arguments[i + 1] = arguments[i + 1], arguments[i]
- is_sorted = False
- return arguments
+ return sorted(arguments, key=functools.cmp_to_key(func))
def group_by_type(dictionary):
result = {}
for key, value in dictionary.items():
- if type(key) in result:
- result[type(key)].update({key: value})
- else:
- result[type(key)] = {key: value}
+ result.setdefault(type(key), {}).update({key: value})
return result
-def string_to_set(word):
- result = set()
- for letter in word:
- result.add(letter)
- return result
-
-
def anagrams(words):
result = []
for word in words:
founded_word = False
for item in result:
- if string_to_set(word) == string_to_set(item[0]):
+ if set(word.lower()) == set(item[0].lower()):
item.append(word)
founded_word = True
break
if not founded_word:
new_word = []
new_word.append(word)
result.append(new_word)
return result

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

import functools
def is_pangram(sentence):
found_letters = set()
for letter in sentence.lower():
if (letter >= 'а' and letter <= 'я' and
letter != 'ы' and letter != 'э'):
found_letters.add(letter)
return len(found_letters) == 30
def char_histogram(text):
result = dict()
for symbol in text:
result[symbol] = 1 + result.get(symbol, 0)
return result
def sort_by(func, arguments):
return sorted(arguments, key=functools.cmp_to_key(func))
def group_by_type(dictionary):
result = {}
for key, value in dictionary.items():
result.setdefault(type(key), {}).update({key: value})
return result
+def letters_count(word):
+ result = {}
+ for symbol in word:
+ if symbol.islower():
+ result[symbol] = 1 + result.get(symbol, 0)
+ return result
+
+
def anagrams(words):
result = []
for word in words:
founded_word = False
for item in result:
- if set(word.lower()) == set(item[0].lower()):
+ if letters_count(word.lower()) == letters_count(item[0].lower()):
item.append(word)
founded_word = True
break
if not founded_word:
new_word = []
new_word.append(word)
result.append(new_word)
return result

Евгени обнови решението на 18.03.2014 23:59 (преди над 10 години)

import functools
def is_pangram(sentence):
found_letters = set()
for letter in sentence.lower():
if (letter >= 'а' and letter <= 'я' and
letter != 'ы' and letter != 'э'):
found_letters.add(letter)
return len(found_letters) == 30
def char_histogram(text):
result = dict()
for symbol in text:
result[symbol] = 1 + result.get(symbol, 0)
return result
def sort_by(func, arguments):
return sorted(arguments, key=functools.cmp_to_key(func))
def group_by_type(dictionary):
result = {}
for key, value in dictionary.items():
result.setdefault(type(key), {}).update({key: value})
return result
def letters_count(word):
result = {}
for symbol in word:
if symbol.islower():
result[symbol] = 1 + result.get(symbol, 0)
return result
def anagrams(words):
result = []
for word in words:
founded_word = False
for item in result:
if letters_count(word.lower()) == letters_count(item[0].lower()):
item.append(word)
founded_word = True
break
if not founded_word:
new_word = []
new_word.append(word)
result.append(new_word)
- return result
+ return result