题解 | #坐标移动#

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

import re

str = input().split(";")
pattern = r"([A,S,D,W])(\d+)(?![a-zA-Z])"
x = 0
y = 0
for s in str:

    match = re.match(pattern, s)
    if match:
        if match.group(1) == "A":
            x = x - int(match.group(2))
        elif match.group(1) == "D":
            x = x + int(match.group(2))
        elif match.group(1) == "W":
            y = y + int(match.group(2))
        elif match.group(1) == "S":
            y = y - int(match.group(2))
print("{},{}".format(x, y))

全部评论
?![a-zA-Z] 和?!/[a-zA-Z] 区别:当前者没有/时会自动将不匹配的字符串整个排除,后者会将A1W 中单个W排除 剩下A1.
点赞 回复 分享
发布于 2023-03-28 23:11 四川

相关推荐

是秃子总会发光_:科软的还被排序?不是大雪深埋吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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