При компиляции выдаёт ошибку:
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xc2 in position 0: invalid continuation byte
Программа "python.exe" завершилась с кодом 4294967295 (0xffffffff).
Что не так в коде? Помогите.
Возможно я неправильно написал функции
macth и findTaskHavingMaxPriorityInGroup
По идее они должны выводить в консоль
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xc2 in position 0: invalid continuation byte
Программа "python.exe" завершилась с кодом 4294967295 (0xffffffff).
Что не так в коде? Помогите.
Python:
import traceback
class TestRunner(object):
def __init__(self, name):
self.name = name
self.testNo = 1
def expectTrue(self, cond):
try:
if cond():
self._pass()
else:
self._fail()
except Exception as e:
self._fail(e)
def expectFalse(self, cond):
self.expectTrue(lambda: not cond())
def expectException(self, block):
try:
block()
self._fail()
except:
self._pass()
def _fail(self, e=None):
print(f'FAILED: Test # {self.testNo} of {self.name}')
self.testNo += 1
if e is not None:
traceback.print_tb(e.__traceback__)
def _pass(self):
print(f'PASSED: Test # {self.testNo} of {self.name}')
self.testNo += 1
def match(string, pattern):
if [type(string),type(pattern)]!=[str]*2:raise TypeError
if len(string)!=len(pattern)or set(pattern)-set('ad* '):raise ValueError
m,r=[0]*len(string),1
for i in range(len(string)):m[i]=bool((pattern[i]=='a'and string[i]in[chr(i)for i in range(97,123)])or(pattern[i]=='d'and string[i]in[chr(i)for i in range(48,58)])or(pattern[i]==string[i]==' ')or(pattern[i]=='*'and string[i]in([' ']+[chr(i)for i in range(48,58)]+[chr(i)for i in range(97,123)])))
for i in m:r*=i
return bool(r)
def testMatch():
runner = TestRunner('match')
runner.expectFalse(lambda: match('xy', 'a'))
runner.expectFalse(lambda: match('x', 'd'))
runner.expectFalse(lambda: match('0', 'a'))
runner.expectFalse(lambda: match('*', ' '))
runner.expectFalse(lambda: match(' ', 'a'))
runner.expectTrue(lambda: match('01 xy', 'dd aa'))
runner.expectTrue(lambda: match('1x', '**'))
runner.expectException(lambda: match('x', 'w'))
tasks = {
'id': 0,
'name': 'Все задачи',
'children': [
{
'id': 1,
'name': 'Разработка',
'children': [
{'id': 2, 'name': 'Планирование разработок', 'priority': 1},
{'id': 3, 'name': 'Подготовка релиза', 'priority': 4},
{'id': 4, 'name': 'Оптимизация', 'priority': 2},
],
},
{
'id': 5,
'name': 'Тестирование',
'children': [
{
'id': 6,
'name': 'Ручное тестирование',
'children': [
{'id': 7, 'name': 'Составление тест-планов', 'priority': 3},
{'id': 8, 'name': 'Выполнение тестов', 'priority': 6},
],
},
{
'id': 9,
'name': 'Автоматическое тестирование',
'children': [
{'id': 10, 'name': 'Составление тест-планов', 'priority': 3},
{'id': 11, 'name': 'Написание тестов', 'priority': 3},
],
},
],
},
{'id': 12, 'name': 'Аналитика', 'children': []},
],
}
def findTaskHavingMaxPriorityInGroup(tasks, groupId):
# ------------------------------------------------------------------------------------------------
def search_group(id, tasks):
if tasks['id'] == id:
search_max(tasks)
return 1
search_group2(id, tasks)
def search_group2(id, tasks):
if 'children' in tasks:
_ = tasks['children']
for pot in _:
if pot['id'] == id:
search_max(pot)
return 0
if 'children' in pot:
search_group2(id, pot)
def search_max(tasks):
global flag
if 'priority' in tasks:
raise Exception("not a group")
if tasks['children'] == []:
flag = None
return 0
search_max2(tasks)
def search_max2(tasks):
if 'children' in tasks:
_ = tasks['children']
for pot in _:
if 'priority' in pot:
if pot['priority'] > max['priority']:
max['priority'] = pot['priority']
max['name'] = pot['name']
max['id'] = pot['id']
if 'children' in pot:
search_max2(pot)
max = {'id': 0, 'name':'', 'priority':0,}
global flag
flag = 1
search_group(groupId, tasks)
if flag == None:
return None
if max['name'] == '':
raise Exception("not a group")
return max
# ------------------------------------------------------------------------------------------------
def taskEquals(a, b):
return (
not 'children' in a and
not 'children' in b and
a['id'] == b['id'] and
a['name'] == b['name'] and
a['priority'] == b['priority']
)
def testFindTaskHavingMaxPriorityInGroup():
runner = TestRunner('findTaskHavingMaxPriorityInGroup')
runner.expectException(lambda: findTaskHavingMaxPriorityInGroup(tasks, 13))
runner.expectException(lambda: findTaskHavingMaxPriorityInGroup(tasks, 2))
runner.expectTrue(lambda: findTaskHavingMaxPriorityInGroup(tasks, 12) is None)
runner.expectTrue(lambda: taskEquals(findTaskHavingMaxPriorityInGroup(tasks, 0), {
'id': 8,
'name': 'Выполнение тестов',
'priority': 6,
}))
runner.expectTrue(lambda: taskEquals(findTaskHavingMaxPriorityInGroup(tasks, 1), {
'id': 3,
'name': 'Подготовка релиза',
'priority': 4,
}))
runner.expectTrue(lambda: findTaskHavingMaxPriorityInGroup(tasks, 9)['priority'] == 3)
testMatch()
testFindTaskHavingMaxPriorityInGroup()
Возможно я неправильно написал функции
macth и findTaskHavingMaxPriorityInGroup
По идее они должны выводить в консоль
Код:
PASSED: Test #1 of match
PASSED: Test #2 of match
PASSED: Test #3 of match
PASSED: Test #4 of match
PASSED: Test #5 of match
PASSED: Test #6 of match
PASSED: Test #7 of match
PASSED: Test #8 of match
PASSED: Test #1 of findTaskHavingMaxPriorityInGroup
PASSED: Test #2 of findTaskHavingMaxPriorityInGroup
PASSED: Test #3 of findTaskHavingMaxPriorityInGroup
PASSED: Test #4 of findTaskHavingMaxPriorityInGroup
PASSED: Test #5 of findTaskHavingMaxPriorityInGroup
PASSED: Test #6 of findTaskHavingMaxPriorityInGroup