题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
正则匹配
- 对字符串,先以";"进行分割
- 分离方向和坐标:判断方向的字符是否在['A','S','W','D']中,坐标字符是否在['0','1','2','3','4','5','6','7','8','9']中
while True:
try:
# 输入一行字符串
string = input()
lst = string.split(';')
# 初始坐标
x,y = 0,0
for i in lst:
# 分离方向和移动距离
if len(i) < 2:
continue
else:
direction = i[0]
coordinate = i[1:]
# 判断方向是否符合
if direction in ['A','S','W','D']:
# 判断坐标是否符合
flag = True
for j in coordinate:
if j in ['0','1','2','3','4','5','6','7','8','9']:
continue
else:
flag = False
break
# 若坐标符合
if flag:
# 根据不同方向移动
if direction == 'A':
x -= int(coordinate)
elif direction == 'D':
x += int(coordinate)
elif direction == 'W':
y += int(coordinate)
else:
y -= int(coordinate)
print(str(x)+','+str(y))
except:
break