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

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

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

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 15 успешни тест(а)
  • 1 неуспешни тест(а)

Код

def anagrams(words):
resulting = []
while words != []:
temp_word = words[0]
temp_result = []
for i in words:
if is_anagrams(i, temp_word):
temp_result.append(i)
for i in temp_result:
words.remove(i)
resulting.append(temp_result)
return resulting
def is_anagrams(first, second):
engl_alph = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
bul_alph = 'абвгдежзийклмнопрстуфхчцшщьъюяАБВГДЕЖЗИЙКЛМНОПРСТУФХЧЦШЩЬЪЮЯ'
set_of_letters = [_ for _ in first if _ in engl_alph or _ in bul_alph]
second_word = [l for l in second if l in engl_alph or l in bul_alph]
if len(set_of_letters) != len(second_word):
return False
for letter in second_word:
if letter not in engl_alph or letter not in bul_alph:
pass
elif letter not in set_of_letters:
return False
else:
set_of_letters.remove(letter)
return True
# def sort_by(func, arguments):
# result = arguments.sort(key=func)
def group_by_type(dictionary):
dictionary_types = [type(_[0]) for _ in dictionary.items()]
resulting_dictionary = {}
for i in dictionary_types:
resulting_dictionary[i] = {}
for i in dictionary:
resulting_dictionary[type(i)][i] = dictionary[i]
#resulting_dictionary[type(i)].__setitem__(i, dictionary[i])
return resulting_dictionary
def is_pangram(sentence):
letters = [i for i in sentence.lower()]
sentence_alphabeth = set()
alphabet = 'абвгдежзийклмнопрстуфхчцшщьъюя'
for letter in letters:
if letter in alphabet:
sentence_alphabeth.add(letter)
return 30 == len(sentence_alphabeth)
def sort_by(func, arguments):
for index in range(0, len(arguments) - 1):
for i in range(1, len(arguments)):
if index >= i:
pass
elif int(func(arguments[index], arguments[i])) > 0:
arguments[index], arguments[i] = arguments[i], arguments[index]
else:
pass
return arguments
def char_histogram(text):
letters = [i for i in text]
resulting_dict = dict()
for i in letters:
if i not in resulting_dict:
resulting_dict[i] = 1
else:
resulting_dict[i] = resulting_dict[i] + 1
return resulting_dict

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

...............F
======================================================================
FAIL: test_sort_by_simple_test (test.TestSortBy)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140319-21201-s60mlk/test.py", line 54, in test_sort_by_simple_test
    solution.sort_by(lambda x, y: x % 2 - y % 2, [0, 1, 2, 3, 4, 5]))
AssertionError: Lists differ: [0, 2, 4, 1, 3, 5] != [0, 2, 4, 3, 1, 5]

First differing element 3:
1
3

- [0, 2, 4, 1, 3, 5]
?              ---

+ [0, 2, 4, 3, 1, 5]
?           +++


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

FAILED (failures=1)

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

Кирил обнови решението на 16.03.2014 19:18 (преди над 10 години)

+
+def is_pangram(sentence):
+ letters = [i for i in sentence.lower()]
+ sentence_alphabeth = set()
+ alphabet = 'абвгдежзийклмнопрстуфхчцшщьъюя'
+ for letter in letters:
+ if letter in alphabet:
+ sentence_alphabeth.add(letter)
+ return 30 == len(sentence_alphabeth)
+
+
+
+def char_histogram(text):
+ letters = [i for i in text]
+ resulting_dict = dict()
+ for i in letters:
+ if i not in resulting_dict:
+ resulting_dict[i] = 1
+ else:
+ resulting_dict[i] = resulting_dict[i] + 1
+ return resulting_dict
+
+
+def sort_by_izmislen(func, arguments):
+ for index in range(0, len(arguments) - 1):
+ for i in range(1, len(arguments)):
+ if index >= i:
+ pass
+ elif int(func(arguments[index], arguments[i])) > 0:
+ arguments[index], arguments[i] = arguments[i], arguments[index]
+ else:
+ pass
+ return arguments
+
+
+def anagrams(words):
+ result = [] # rezultata
+ for word in words:
+ words.remove(word)
+ temp_words = [i for i in words]
+ temp_result = [word]
+ for second_word in temp_words:
+ if is_anagrams(word, second_word):
+ temp_result.append(second_word)
+ for w in temp_result:
+ if w != word:
+ words.remove(w)
+ result.append(temp_result)
+ result.append(words)
+ return result
+
+def is_anagrams(first, second):
+ set_of_letters = [_ for _ in first]
+ for letter in second:
+ if letter not in set_of_letters:
+ return False
+ else:
+ set_of_letters.remove(letter)
+ return True
+
+

Кирил обнови решението на 16.03.2014 19:23 (преди над 10 години)

def is_pangram(sentence):
letters = [i for i in sentence.lower()]
sentence_alphabeth = set()
alphabet = 'абвгдежзийклмнопрстуфхчцшщьъюя'
for letter in letters:
if letter in alphabet:
sentence_alphabeth.add(letter)
return 30 == len(sentence_alphabeth)
def char_histogram(text):
letters = [i for i in text]
resulting_dict = dict()
for i in letters:
if i not in resulting_dict:
resulting_dict[i] = 1
else:
resulting_dict[i] = resulting_dict[i] + 1
return resulting_dict
-def sort_by_izmislen(func, arguments):
+def sort_by(func, arguments):
for index in range(0, len(arguments) - 1):
for i in range(1, len(arguments)):
if index >= i:
pass
elif int(func(arguments[index], arguments[i])) > 0:
arguments[index], arguments[i] = arguments[i], arguments[index]
else:
pass
return arguments
def anagrams(words):
result = [] # rezultata
for word in words:
words.remove(word)
temp_words = [i for i in words]
temp_result = [word]
for second_word in temp_words:
if is_anagrams(word, second_word):
temp_result.append(second_word)
for w in temp_result:
if w != word:
words.remove(w)
result.append(temp_result)
result.append(words)
return result
def is_anagrams(first, second):
set_of_letters = [_ for _ in first]
for letter in second:
if letter not in set_of_letters:
return False
else:
set_of_letters.remove(letter)
return True
+
+
+def group_by_type(dictionary):
+ dictionary_types = [type(_[0]) for _ in dictionary.items()]
+ resulting_dictionary = {}
+ for i in dictionary_types:
+ resulting_dictionary[i] = {}
+ for i in dictionary:
+ resulting_dictionary[type(i)][i] = dictionary[i]
+ #resulting_dictionary[type(i)].__setitem__(i, dictionary[i])
+ return resulting_dictionary

Здравейте, anagrams и sort_by предполагам че са ми грешни и имам няколко въпроса: 1) не разбирам какво е условието два съседни елемента от листа да се разместят ( func(1,2) > 0) 2) result.append(words) без този ред в anagrams ми изчезват думи и не мога да разбера защо :) Приятно проверяване :)

Кирил обнови решението на 18.03.2014 17:02 (преди над 10 години)

+def anagrams(words):
+ resulting = []
+ while words != []:
+ temp_word = words[0]
+ temp_result = []
+ for i in words:
+ if is_anagrams(i, temp_word):
+ temp_result.append(i)
+ for i in temp_result:
+ words.remove(i)
+ resulting.append(temp_result)
+ return resulting
+
+def is_anagrams(first, second):
+ engl_alph = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
+ bul_alph = 'абвгдежзийклмнопрстуфхчцшщьъюяАБВГДЕЖЗИЙКЛМНОПРСТУФХЧЦШЩЬЪЮЯ'
+ set_of_letters = [_ for _ in first if _ in engl_alph or _ in bul_alph]
+ second_word = [l for l in second if l in engl_alph or l in bul_alph]
+ if len(set_of_letters) != len(second_word):
+ return False
+ for letter in second_word:
+ if letter not in engl_alph or letter not in bul_alph:
+ pass
+ elif letter not in set_of_letters:
+ return False
+ else:
+ set_of_letters.remove(letter)
+ return True
+
+# def sort_by(func, arguments):
+ # result = arguments.sort(key=func)
+
+
+def group_by_type(dictionary):
+ dictionary_types = [type(_[0]) for _ in dictionary.items()]
+ resulting_dictionary = {}
+ for i in dictionary_types:
+ resulting_dictionary[i] = {}
+ for i in dictionary:
+ resulting_dictionary[type(i)][i] = dictionary[i]
+ #resulting_dictionary[type(i)].__setitem__(i, dictionary[i])
+ return resulting_dictionary
+
+
def is_pangram(sentence):
letters = [i for i in sentence.lower()]
sentence_alphabeth = set()
alphabet = 'абвгдежзийклмнопрстуфхчцшщьъюя'
for letter in letters:
if letter in alphabet:
sentence_alphabeth.add(letter)
return 30 == len(sentence_alphabeth)
-
-def char_histogram(text):
- letters = [i for i in text]
- resulting_dict = dict()
- for i in letters:
- if i not in resulting_dict:
- resulting_dict[i] = 1
- else:
- resulting_dict[i] = resulting_dict[i] + 1
- return resulting_dict
-
-
-def sort_by(func, arguments):
+def sort_by(func, arguments):
for index in range(0, len(arguments) - 1):
for i in range(1, len(arguments)):
if index >= i:
pass
elif int(func(arguments[index], arguments[i])) > 0:
arguments[index], arguments[i] = arguments[i], arguments[index]
else:
pass
- return arguments
+ return arguments
+
-def anagrams(words):
- result = [] # rezultata
- for word in words:
- words.remove(word)
- temp_words = [i for i in words]
- temp_result = [word]
- for second_word in temp_words:
- if is_anagrams(word, second_word):
- temp_result.append(second_word)
- for w in temp_result:
- if w != word:
- words.remove(w)
- result.append(temp_result)
- result.append(words)
- return result
-
-def is_anagrams(first, second):
- set_of_letters = [_ for _ in first]
- for letter in second:
- if letter not in set_of_letters:
- return False
+def char_histogram(text):
+ letters = [i for i in text]
+ resulting_dict = dict()
+ for i in letters:
+ if i not in resulting_dict:
+ resulting_dict[i] = 1
else:
- set_of_letters.remove(letter)
- return True
-
-
-def group_by_type(dictionary):
- dictionary_types = [type(_[0]) for _ in dictionary.items()]
- resulting_dictionary = {}
- for i in dictionary_types:
- resulting_dictionary[i] = {}
- for i in dictionary:
- resulting_dictionary[type(i)][i] = dictionary[i]
- #resulting_dictionary[type(i)].__setitem__(i, dictionary[i])
- return resulting_dictionary
+ resulting_dict[i] = resulting_dict[i] + 1
+ return resulting_dict