python code error
조회수 759회
def find_locations(matrix, seq):
"""
(Task 2.1) Not as many as stars in the sky (10 points)
Find the occurrences of every elements in the sequence seq,
and return the locations as a Python dictionary (dict).
:param matrix: sequence of sequences
:param seq: sequence of values to find from matrix
:returns: dictionary with keys same as seq and values set of locations
"""
result = {}
position=[]
for e1 in seq:
for j in range(len(matrix)):
e2=matrix[j]
for i in range(len(e2)):
if e2[i]==e1:
position.append((j,i))
if position==[]:
if not type(e1)==str:
result[e1]=set()
else:
result[str(e1)]=set()
continue
position_set=set(position)
#position 튜플 리스트가 {}로 바뀐거임.
if not type(e1)==str:
result[e1]=position_set
else:
result[str(e1)]=position_set
position=[]
print(result)
return result
def compute_trajectory(game_map):
"""
(Task 2.2) And we start traveling (30 points)
Simulate the launch according to the predefined game rules,
then return a dictionary containing the fate of the spaceship ('status')
and the exact path along which the spaceship will travel ('trajectory').
:param game_map: list of strings (str) representing the space map
:returns: dictionary with two keys 'status' and 'trajectory'
"""
result = {'status': 'unknown', 'trajectory': []}
#traveling 해야 하는데 ㅠㅠ simulating
matrix,seq=game_map, '#,*'
star_sharp=find_locations(matrix, seq)
#launch-plan impossible 에 대해 서술
if star_sharp['#']=='set()' or star_sharp['*']=='set()':
result['status']='launch_plan_impossible'
return
position_sharp=list(star_sharp['#'])
position_star=list(star_sharp['*'])
y1,x1=position_sharp[0] ##의 위치
y2,x2=position_star[0] #*의 위치
yd, xd=y2-y1, x2-x1
while 0<=y2+yd<=len(game_map) and 0<=x2+xd<=len(game_map[0]):
if game_map[y2+yd][x2+xd]=='.':
result['trajectory'].append((y2+yd,x2+xd))
y2, x2 = y2+yd,x2+xd
elif game_map[y2+yd][x2+xd] in DIGITS:
for y,x in result['trajectory']:
if game_map[y][x]==game_map[y2+yd][x2+xd]:
result['status']='warp_network_overload'
result['trajectory'].append((y2+yd,x2+xd))
return
result['trajectory'].append((y2+yd,x2+xd))
J1_J2=[]
for j1 in range(len(game_map)):
for j2 in range(len(game_map[0])):
if game_map[j1][j2]==game_map[y2+yd][x2+xd] and (j1!=y2+yd or j2!=x2+xd):
J1_J2.append((j1,j2))
if not len(J1_J2)==1:
print("Oh, Gate is strange!")
result['status']='disappeared'
return
else:
result['trajectory'].append((j1,j2))
y2, x2 = j1, j2
elif game_map[y2+yd][x2+xd]=='/'or game_map[y2+yd][x2+xd]=='\':
if game_map[y2+yd][x2+xd]=='/':
yd, xd =-(xd), -(yd)
elif game_map[y2+yd][x2+xd]=='\':
yd, xd = xd, yd
elif game_map[y2+yd][x2+xd]=='@':
result['status']='destroyed'
result['trajectory'].append((y2+yd, x2+xd))
return
elif game_map[y2+yd][x2+xd]=='#':
result['status']='returned'
return
k=0
while k!=-1:
if result['trajectory'][k:k+2]== result['trajectory'][-2:]:
print("Ooops! Infinite loop!!")
result['trajectory']=result['trajectory'][:-2]
result['trajectory'][k]=list(result['trajectory'][k])
return
k+=1
if not (0<=y2+yd<=len(game_map) and 0<=x2+xd<=len(game_map[0])):
result['status']='disappeared'
return
이런식으로 코드를 만들었는데
File "main.py", line 80
star_sharp=find_locations(matrix, seq)
^
SyntaxError: invalid syntax
이런 Error가 뜹니다 ㅠㅠ find_locations함수에서 error가 난 것 같은데 이해가 안가서요. find_locations 함수를 따로 실행시키면 잘 돌아가거든요. 그런데 왜 오류가 난 걸까요;; 혹시 이것 외에도 오류 있으면 말씀해주시면 정말 감사하겠습니다...ㅜㅜ
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
댓글 입력