利用re包匹配所有合法的正则,遍历多组输入,对每行做分割并确定单条移动数据的正确性,如果正确按照对应方式移动即可
import sys
import re
pattern = re.compile('^[ADWS][0-9]{1,2}$')
for line in sys.stdin:
location = [0, 0]
line = line.strip()
for action in line.split(';'):
if pattern.search(action):
move, step = action[0], int(action[1:])
if move == 'A':
location[0] -= step
elif move == 'D':
location[0] += step
elif move == 'S':
location[1] -= step
elif move == 'W':
location[1] += step
print(location[0], end=',')
print(location[1])