正则表达式-字典类-坐标移动
坐标移动
http://www.nowcoder.com/questionTerminal/119bcca3befb405fbe58abe9c532eb29
Python考察正则表达式和字典
匹配pattern为 ^[AWSD][0-9]{1,2}$
后利用字典类简化位移 代码如下
import re while True: try: filter_out=[re.match(r'^[AWDS][0-9]{1,2}$',i) for i in input().split(';')] while None in filter_out: filter_out.remove(None) pos=[0,0] movement={'A':(-1,0),'D':(1,0),'W':(0,1),'S':(0,-1)} for i in filter_out: pos[0]+=movement[i.string[0]][0]*int(i.string[1:]) pos[1]+=movement[i.string[0]][1]*int(i.string[1:]) print('%d,%d'%tuple(pos)) except: break