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

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

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

Резултати

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

Код

def is_pangram(sentance):
sentance_upper_case = sentance.upper()
letters_count = 0
letters = []
for letter in sentance_upper_case:
if (ord(letter) in range(1040, 1072) and
letter not in letters):
letters.append(letter)
return len(letters) == 30
def char_histogram(text):
chars_in_text = {}
for letter in text:
if letter not in chars_in_text:
chars_in_text[str(letter)] = 1
else:
chars_in_text[str(letter)] += 1
return chars_in_text
def group_by_type(dictionary):
grouped_by_type = {}
for x in dictionary.keys():
if type(x) not in grouped_by_type:
grouped_by_type[type(x)] = {x:dictionary[x]}
else:
grouped_by_type[type(x)].update({x:dictionary[x]})
return grouped_by_type

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

EEEE.........EEE
======================================================================
ERROR: test_list_of_latin_anagrams (test.TestAnagrams)
----------------------------------------------------------------------
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-cffned/test.py", line 109, in test_list_of_latin_anagrams
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: test_with_different_cases (test.TestAnagrams)
----------------------------------------------------------------------
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-cffned/test.py", line 125, in test_with_different_cases
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: test_with_different_symbols (test.TestAnagrams)
----------------------------------------------------------------------
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-cffned/test.py", line 135, in test_with_different_symbols
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: test_with_list_of_cyrilic_anagrams (test.TestAnagrams)
----------------------------------------------------------------------
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-cffned/test.py", line 117, in test_with_list_of_cyrilic_anagrams
    set(map(frozenset, solution.anagrams(words))))
AttributeError: 'module' object has no attribute 'anagrams'

======================================================================
ERROR: test_sort_by_empty (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-cffned/test.py", line 44, in test_sort_by_empty
    self.assertEqual([], solution.sort_by(lambda x, y: x - y, []))
AttributeError: 'module' object has no attribute 'sort_by'

======================================================================
ERROR: test_sort_by_one_element (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-cffned/test.py", line 57, in test_sort_by_one_element
    self.assertEqual([1], solution.sort_by(lambda x, y: x - y, [1]))
AttributeError: 'module' object has no attribute 'sort_by'

======================================================================
ERROR: 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-cffned/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]))
AttributeError: 'module' object has no attribute 'sort_by'

----------------------------------------------------------------------
Ran 16 tests in 0.014s

FAILED (errors=7)

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

Марианна обнови решението на 17.03.2014 14:36 (преди над 10 години)

+def is_pangram(sentance):
+ sentance_length = len(sentance)
+ new_sentance = sentance.upper()
+ letters_count = 0
+ i = 0
+ letters = []
+ while i < sentance_length:
+ if (ord(new_sentance[i]) in range(1040, 1072) and
+ new_sentance[i] not in letters):
+ letters.append(new_sentance[i])
+ i += 1
+ if len(letters) == 30:
+ return True
+ else:
+ return False

Марианна обнови решението на 17.03.2014 15:51 (преди над 10 години)

def is_pangram(sentance):
sentance_length = len(sentance)
new_sentance = sentance.upper()
letters_count = 0
i = 0
letters = []
while i < sentance_length:
if (ord(new_sentance[i]) in range(1040, 1072) and
new_sentance[i] not in letters):
letters.append(new_sentance[i])
i += 1
if len(letters) == 30:
return True
else:
- return False
+ return False
+
+def char_histogram(text):
+ text_length = len(text)
+ chars_in_text = {}
+ i = 0
+ while i < text_length:
+ if text[i] not in chars_in_text:
+ chars_in_text[str(text[i])] = 1
+ else:
+ chars_in_text[str(text[i])] += 1
+ i += 1
+ return chars_in_text

Марианна обнови решението на 18.03.2014 15:47 (преди над 10 години)

def is_pangram(sentance):
sentance_length = len(sentance)
- new_sentance = sentance.upper()
+ sentance_upper_case = sentance.upper()
letters_count = 0
i = 0
letters = []
while i < sentance_length:
- if (ord(new_sentance[i]) in range(1040, 1072) and
- new_sentance[i] not in letters):
- letters.append(new_sentance[i])
+ if (ord(sentance_upper_case[i]) in range(1040, 1072) and
+ sentance_upper_case[i] not in letters):
+ letters.append(sentance_upper_case[i])
i += 1
if len(letters) == 30:
return True
else:
return False
def char_histogram(text):
text_length = len(text)
chars_in_text = {}
i = 0
while i < text_length:
if text[i] not in chars_in_text:
chars_in_text[str(text[i])] = 1
else:
chars_in_text[str(text[i])] += 1
i += 1
return chars_in_text

Марианна обнови решението на 18.03.2014 17:44 (преди над 10 години)

-def is_pangram(sentance):
+def is_pangram(sentance):
sentance_length = len(sentance)
sentance_upper_case = sentance.upper()
letters_count = 0
- i = 0
- letters = []
- while i < sentance_length:
- if (ord(sentance_upper_case[i]) in range(1040, 1072) and
- sentance_upper_case[i] not in letters):
- letters.append(sentance_upper_case[i])
- i += 1
- if len(letters) == 30:
+ letters = {str(sentance_upper_case[i]):i for i in range (sentance_length)
+ if ord(sentance_upper_case[i]) in range (1040,1072)}
+ if len(letters) == 30:
return True
else:
return False
def char_histogram(text):
text_length = len(text)
chars_in_text = {}
i = 0
while i < text_length:
if text[i] not in chars_in_text:
chars_in_text[str(text[i])] = 1
else:
chars_in_text[str(text[i])] += 1
i += 1
return chars_in_text

Марианна обнови решението на 18.03.2014 17:45 (преди над 10 години)

def is_pangram(sentance):
sentance_length = len(sentance)
sentance_upper_case = sentance.upper()
letters_count = 0
- letters = {str(sentance_upper_case[i]):i for i in range (sentance_length)
+ letters = {str(sentance_upper_case[i]):i for i in range (0, 31)
if ord(sentance_upper_case[i]) in range (1040,1072)}
if len(letters) == 30:
return True
else:
return False
def char_histogram(text):
text_length = len(text)
chars_in_text = {}
i = 0
while i < text_length:
if text[i] not in chars_in_text:
chars_in_text[str(text[i])] = 1
else:
chars_in_text[str(text[i])] += 1
i += 1
return chars_in_text

Марианна обнови решението на 18.03.2014 17:51 (преди над 10 години)

def is_pangram(sentance):
sentance_length = len(sentance)
sentance_upper_case = sentance.upper()
letters_count = 0
letters = {str(sentance_upper_case[i]):i for i in range (0, 31)
if ord(sentance_upper_case[i]) in range (1040,1072)}
if len(letters) == 30:
return True
else:
return False
def char_histogram(text):
text_length = len(text)
chars_in_text = {}
- i = 0
- while i < text_length:
+ for i in range(text_length):
if text[i] not in chars_in_text:
chars_in_text[str(text[i])] = 1
else:
chars_in_text[str(text[i])] += 1
- i += 1
return chars_in_text

Марианна обнови решението на 18.03.2014 18:56 (преди над 10 години)

def is_pangram(sentance):
sentance_length = len(sentance)
sentance_upper_case = sentance.upper()
- letters_count = 0
letters = {str(sentance_upper_case[i]):i for i in range (0, 31)
if ord(sentance_upper_case[i]) in range (1040,1072)}
if len(letters) == 30:
return True
else:
return False
def char_histogram(text):
text_length = len(text)
chars_in_text = {}
for i in range(text_length):
if text[i] not in chars_in_text:
chars_in_text[str(text[i])] = 1
else:
chars_in_text[str(text[i])] += 1
return chars_in_text

Марианна обнови решението на 18.03.2014 19:43 (преди над 10 години)

def is_pangram(sentance):
sentance_length = len(sentance)
sentance_upper_case = sentance.upper()
letters = {str(sentance_upper_case[i]):i for i in range (0, 31)
if ord(sentance_upper_case[i]) in range (1040,1072)}
if len(letters) == 30:
return True
else:
return False
def char_histogram(text):
text_length = len(text)
chars_in_text = {}
for i in range(text_length):
if text[i] not in chars_in_text:
chars_in_text[str(text[i])] = 1
else:
chars_in_text[str(text[i])] += 1
- return chars_in_text
+ return chars_in_text
+
+def group_by_type(dictionary):
+ grouped_by_type = {type(x):{x:dictionary[x]} for x in dictionary.keys()}
+ return grouped_by_type
  • ами това е нормално по начина, по който си го написала. (за всеки елемент създаваш речник {original_key: original_value} като стойност зад ключ original_type на външния. Няма да се акумулират така.

    • вариант 1: конструираш вътрешните речници така, че да обхваща всички двойки, които се очаква да са в него. Може би по-трудния и тромав вариант
    • вариант 2: правиш го по малко по-процедурен начин, който ще ти е по-лесно да проследиш и да напишеш правилно :)
  • Не прави така:

    if len(letters) == 30: 
        return True
    else:
        return False
    

    това е лошия вариант на return len(letters) == 30

  • letters речника изглежда излишно сложен, зле подравнен и съм сигурен, че можеш да измислиш нещо по-простичко, по-подходящо и по-лесно четимо на негово място. note: не говоря за името

  • В python предпочитаме "for each"-like итериране по колекции, когато не се налага нещо друго. Пример: for item in my_collection: е по-добре от for index in range(len(my_collection)). Случват се и изключения. Твоя случай от кои е?

Марианна обнови решението на 19.03.2014 09:58 (преди над 10 години)

-def is_pangram(sentance):
- sentance_length = len(sentance)
+def is_pangram(sentance):
sentance_upper_case = sentance.upper()
- letters = {str(sentance_upper_case[i]):i for i in range (0, 31)
- if ord(sentance_upper_case[i]) in range (1040,1072)}
- if len(letters) == 30:
- return True
- else:
- return False
+ letters_count = 0
+ letters = []
+ for letter in sentance_upper_case:
+ if (ord(letter) in range(1040, 1072) and
+ letter not in letters):
+ letters.append(letter)
+ return len(letters) == 30
def char_histogram(text):
- text_length = len(text)
chars_in_text = {}
- for i in range(text_length):
- if text[i] not in chars_in_text:
- chars_in_text[str(text[i])] = 1
+ for letter in text:
+ if letter not in chars_in_text:
+ chars_in_text[str(letter)] = 1
else:
- chars_in_text[str(text[i])] += 1
+ chars_in_text[str(letter)] += 1
return chars_in_text
def group_by_type(dictionary):
grouped_by_type = {type(x):{x:dictionary[x]} for x in dictionary.keys()}
return grouped_by_type

Марианна обнови решението на 19.03.2014 10:25 (преди над 10 години)

def is_pangram(sentance):
sentance_upper_case = sentance.upper()
letters_count = 0
letters = []
for letter in sentance_upper_case:
if (ord(letter) in range(1040, 1072) and
letter not in letters):
letters.append(letter)
return len(letters) == 30
def char_histogram(text):
chars_in_text = {}
for letter in text:
if letter not in chars_in_text:
chars_in_text[str(letter)] = 1
else:
chars_in_text[str(letter)] += 1
return chars_in_text
def group_by_type(dictionary):
- grouped_by_type = {type(x):{x:dictionary[x]} for x in dictionary.keys()}
+ grouped_by_type = {}
+ for x in dictionary.keys():
+ if type(x) not in grouped_by_type:
+ grouped_by_type[type(x)] = {x:dictionary[x]}
+ else:
+ grouped_by_type[type(x)].update({x:dictionary[x]})
return grouped_by_type