Решение на Пет функции от Константин Тодоров

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

Към профила на Константин Тодоров

Резултати

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

Код

from collections import defaultdict
def is_pangram(sentence):
alfabeth = set(list("ахчуднабългарсказемьополюшвайцъфтящижита"))
counter = 0
sentence = sentence.lower()
for letter in alfabeth:
if letter in sentence:
counter += 1
return counter == 30
def char_histogram(text):
letters = list(text)
histogram = dict()
for letter in letters:
if letter in histogram:
histogram[letter] += 1
else:
histogram[letter] = 1
return histogram
def sort_by(func, arguments):
swapped = True
while swapped == True:
swapped = False
for i in range(1, len(arguments)):
if func(arguments[i - 1], arguments[i]) > 0:
arguments[i - 1], arguments[i] = arguments[i], arguments[i - 1]
swapped = True
return arguments
def group_by_type(dictionary):
types = defaultdict(dict)
for key in dictionary:
if type(key) not in types:
types[type(key)] = {k: dictionary[k] for k in dictionary
if type(k) == type(key)}
return types
def is_anagram(x, y):
x = x.lower()
y = y.lower()
for letter in list(x):
if letter.isalpha() and list(x).count(letter) != list(y).count(letter):
return False
for letter in list(y):
if letter.isalpha() and list(x).count(letter) != list(y).count(letter):
return False
return True
def anagrams(words):
anagram_classes = list()
for word in words:
word_anagrams = [
x for x in words if is_anagram(word.lower(), x.lower())]
if word_anagrams not in anagram_classes:
anagram_classes.append(word_anagrams)
return anagram_classes

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.015s

OK

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

Константин обнови решението на 14.03.2014 15:28 (преди над 10 години)

+from collections import defaultdict
+
+
+def is_pangram(sentence):
+ alfabeth = set(list("ахчуднабългарсказемьополюшвайцъфтящижита"))
+ counter = 0
+
+ for letter in alfabeth:
+ if letter in sentence:
+ counter += 1
+
+ return counter == 30
+
+
+def char_histogram(text):
+ letters = list(text)
+ histogram = dict()
+
+ for letter in letters:
+ if letter in histogram:
+ histogram[letter] += 1
+ else:
+ histogram[letter] = 1
+
+ return histogram
+
+
+def sort_by(func, arguments):
+ swapped = True
+ while swapped == True:
+ swapped = False
+ for i in range(1, len(arguments)):
+ if func(arguments[i - 1], arguments[i]) > 0:
+ arguments[i - 1], arguments[i] = arguments[i], arguments[i - 1]
+ swapped = True
+
+ return arguments
+
+
+def group_by_type(dictionary):
+ type_dict = defaultdict(dict)
+
+ for key in dictionary:
+ k_type = type(key)
+ if k_type not in type_dict:
+ type_dict[k_type] = {k: dictionary[k] for k in dictionary
+ if type(k) == k_type}
+
+ return type_dict
+
+
+def anagrams(words):
+ anagram_classes = list()
+ for word in words:
+ word_anagrams = [x for x in words if sorted(word) == sorted(x)]
+ if word_anagrams not in anagram_classes:
+ anagram_classes.append(word_anagrams)
+
+ return anagram_classes

Константин обнови решението на 14.03.2014 23:25 (преди над 10 години)

from collections import defaultdict
def is_pangram(sentence):
alfabeth = set(list("ахчуднабългарсказемьополюшвайцъфтящижита"))
counter = 0
-
+ sentence = sentence.lower()
for letter in alfabeth:
if letter in sentence:
counter += 1
return counter == 30
def char_histogram(text):
letters = list(text)
histogram = dict()
for letter in letters:
if letter in histogram:
histogram[letter] += 1
else:
histogram[letter] = 1
return histogram
def sort_by(func, arguments):
swapped = True
while swapped == True:
swapped = False
for i in range(1, len(arguments)):
if func(arguments[i - 1], arguments[i]) > 0:
arguments[i - 1], arguments[i] = arguments[i], arguments[i - 1]
swapped = True
return arguments
def group_by_type(dictionary):
type_dict = defaultdict(dict)
for key in dictionary:
k_type = type(key)
if k_type not in type_dict:
type_dict[k_type] = {k: dictionary[k] for k in dictionary
if type(k) == k_type}
return type_dict
def anagrams(words):
anagram_classes = list()
for word in words:
word_anagrams = [x for x in words if sorted(word) == sorted(x)]
if word_anagrams not in anagram_classes:
anagram_classes.append(word_anagrams)
return anagram_classes

Константин обнови решението на 17.03.2014 17:54 (преди над 10 години)

from collections import defaultdict
def is_pangram(sentence):
alfabeth = set(list("ахчуднабългарсказемьополюшвайцъфтящижита"))
counter = 0
sentence = sentence.lower()
for letter in alfabeth:
if letter in sentence:
counter += 1
return counter == 30
def char_histogram(text):
letters = list(text)
histogram = dict()
for letter in letters:
if letter in histogram:
histogram[letter] += 1
else:
histogram[letter] = 1
return histogram
def sort_by(func, arguments):
swapped = True
while swapped == True:
swapped = False
for i in range(1, len(arguments)):
if func(arguments[i - 1], arguments[i]) > 0:
arguments[i - 1], arguments[i] = arguments[i], arguments[i - 1]
swapped = True
return arguments
def group_by_type(dictionary):
- type_dict = defaultdict(dict)
+ types = defaultdict(dict)
for key in dictionary:
- k_type = type(key)
- if k_type not in type_dict:
- type_dict[k_type] = {k: dictionary[k] for k in dictionary
- if type(k) == k_type}
+ if type(key) not in types:
+ types[type(key)] = {k: dictionary[k] for k in dictionary
+ if type(k) == type(key)}
- return type_dict
+ return types
+def is_anagram(x, y):
+ for z in list(x):
+ if z.isalpha() and z not in list(y):
+ return False
+
+ return True
+
+
def anagrams(words):
anagram_classes = list()
for word in words:
- word_anagrams = [x for x in words if sorted(word) == sorted(x)]
+ word_anagrams = [x for x in words if is_anagram(word, x)]
if word_anagrams not in anagram_classes:
anagram_classes.append(word_anagrams)
return anagram_classes

Константин обнови решението на 17.03.2014 18:13 (преди над 10 години)

from collections import defaultdict
def is_pangram(sentence):
alfabeth = set(list("ахчуднабългарсказемьополюшвайцъфтящижита"))
counter = 0
sentence = sentence.lower()
for letter in alfabeth:
if letter in sentence:
counter += 1
return counter == 30
def char_histogram(text):
letters = list(text)
histogram = dict()
for letter in letters:
if letter in histogram:
histogram[letter] += 1
else:
histogram[letter] = 1
return histogram
def sort_by(func, arguments):
swapped = True
while swapped == True:
swapped = False
for i in range(1, len(arguments)):
if func(arguments[i - 1], arguments[i]) > 0:
arguments[i - 1], arguments[i] = arguments[i], arguments[i - 1]
swapped = True
return arguments
def group_by_type(dictionary):
types = defaultdict(dict)
for key in dictionary:
if type(key) not in types:
types[type(key)] = {k: dictionary[k] for k in dictionary
if type(k) == type(key)}
return types
def is_anagram(x, y):
for z in list(x):
- if z.isalpha() and z not in list(y):
+ if z.isalpha() and list(x).count(z) != list(y).count(z):
return False
return True
def anagrams(words):
anagram_classes = list()
for word in words:
word_anagrams = [x for x in words if is_anagram(word, x)]
if word_anagrams not in anagram_classes:
anagram_classes.append(word_anagrams)
return anagram_classes

Константин обнови решението на 17.03.2014 18:15 (преди над 10 години)

from collections import defaultdict
def is_pangram(sentence):
alfabeth = set(list("ахчуднабългарсказемьополюшвайцъфтящижита"))
counter = 0
sentence = sentence.lower()
for letter in alfabeth:
if letter in sentence:
counter += 1
return counter == 30
def char_histogram(text):
letters = list(text)
histogram = dict()
for letter in letters:
if letter in histogram:
histogram[letter] += 1
else:
histogram[letter] = 1
return histogram
def sort_by(func, arguments):
swapped = True
while swapped == True:
swapped = False
for i in range(1, len(arguments)):
if func(arguments[i - 1], arguments[i]) > 0:
arguments[i - 1], arguments[i] = arguments[i], arguments[i - 1]
swapped = True
return arguments
def group_by_type(dictionary):
types = defaultdict(dict)
for key in dictionary:
if type(key) not in types:
types[type(key)] = {k: dictionary[k] for k in dictionary
if type(k) == type(key)}
return types
def is_anagram(x, y):
- for z in list(x):
- if z.isalpha() and list(x).count(z) != list(y).count(z):
+ for letter in list(x):
+ if letter.isalpha() and list(x).count(letter) != list(y).count(letter):
return False
return True
def anagrams(words):
anagram_classes = list()
for word in words:
word_anagrams = [x for x in words if is_anagram(word, x)]
if word_anagrams not in anagram_classes:
anagram_classes.append(word_anagrams)
return anagram_classes

Все още не отчиташ, че малките и главните букви не бива да ги различаваме и не ни интересуват символите, т.е. A! и a са анаграми. Имаш повече цикли отколкото са нужни, за да се реши конкретната задача на Python :smile:

Константин обнови решението на 19.03.2014 03:03 (преди над 10 години)

from collections import defaultdict
def is_pangram(sentence):
alfabeth = set(list("ахчуднабългарсказемьополюшвайцъфтящижита"))
counter = 0
sentence = sentence.lower()
for letter in alfabeth:
if letter in sentence:
counter += 1
return counter == 30
def char_histogram(text):
letters = list(text)
histogram = dict()
for letter in letters:
if letter in histogram:
histogram[letter] += 1
else:
histogram[letter] = 1
return histogram
def sort_by(func, arguments):
swapped = True
while swapped == True:
swapped = False
for i in range(1, len(arguments)):
if func(arguments[i - 1], arguments[i]) > 0:
arguments[i - 1], arguments[i] = arguments[i], arguments[i - 1]
swapped = True
return arguments
def group_by_type(dictionary):
types = defaultdict(dict)
for key in dictionary:
if type(key) not in types:
types[type(key)] = {k: dictionary[k] for k in dictionary
if type(k) == type(key)}
return types
def is_anagram(x, y):
+ x = x.lower()
+ y = y.lower()
for letter in list(x):
if letter.isalpha() and list(x).count(letter) != list(y).count(letter):
return False
+ for letter in list(y):
+ if letter.isalpha() and list(x).count(letter) != list(y).count(letter):
+ return False
return True
+
def anagrams(words):
anagram_classes = list()
for word in words:
- word_anagrams = [x for x in words if is_anagram(word, x)]
+ word_anagrams = [
+ x for x in words if is_anagram(word.lower(), x.lower())]
if word_anagrams not in anagram_classes:
anagram_classes.append(word_anagrams)
return anagram_classes

Константин обнови решението на 19.03.2014 03:08 (преди над 10 години)

from collections import defaultdict
def is_pangram(sentence):
alfabeth = set(list("ахчуднабългарсказемьополюшвайцъфтящижита"))
counter = 0
sentence = sentence.lower()
for letter in alfabeth:
if letter in sentence:
counter += 1
return counter == 30
def char_histogram(text):
letters = list(text)
histogram = dict()
for letter in letters:
if letter in histogram:
histogram[letter] += 1
else:
histogram[letter] = 1
return histogram
def sort_by(func, arguments):
swapped = True
while swapped == True:
swapped = False
for i in range(1, len(arguments)):
if func(arguments[i - 1], arguments[i]) > 0:
arguments[i - 1], arguments[i] = arguments[i], arguments[i - 1]
swapped = True
return arguments
def group_by_type(dictionary):
types = defaultdict(dict)
for key in dictionary:
if type(key) not in types:
types[type(key)] = {k: dictionary[k] for k in dictionary
if type(k) == type(key)}
return types
def is_anagram(x, y):
x = x.lower()
y = y.lower()
for letter in list(x):
if letter.isalpha() and list(x).count(letter) != list(y).count(letter):
return False
for letter in list(y):
if letter.isalpha() and list(x).count(letter) != list(y).count(letter):
return False
return True
-
def anagrams(words):
anagram_classes = list()
for word in words:
word_anagrams = [
x for x in words if is_anagram(word.lower(), x.lower())]
if word_anagrams not in anagram_classes:
anagram_classes.append(word_anagrams)
return anagram_classes