正则表达式-字典类-坐标移动

坐标移动

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

全部评论

相关推荐

asdasdasda...:19岁,不容易啊可能升个本会好点,现在学历歧视太严重了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务