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

坐标移动

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

全部评论

相关推荐

点赞 评论 收藏
分享
fRank1e:吓得我不敢去外包了,但是目前也只有外包这一个实习,我还要继续去吗
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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