题解 | #坐标移动#

坐标移动

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

import re

s_arr = input().split(";")
zb = [0, 0]
# def pd(s):
#     # c_arr = s.split()
#     wz = ['A', 'D', 'W', 'S']

#     if len(s) > 0 and s[0] in wz:
#         #   print()
#         if len(s) == 2 and s[1].isdigit():
#             return True
#         elif len(s) == 3 and s[1].isdigit() and s[2].isdigit():
#             return True
#         else:
#             return False
#         # return True
#     else:
#         return False
# arr = [v for v in s_arr if pd(v)]


pattern = r"^[ADSW][0-9]{1,2}$"
arr = [v for v in s_arr if re.match(pattern, v)]

for a in arr:
    num = int(a[1::])
    # print(num)
    if a[0] == "A":
        zb[0] = zb[0] - num
    if a[0] == "D":
        zb[0] = zb[0] + num
    if a[0] == "W":
        zb[1] = zb[1] + num
    if a[0] == "S":
        zb[1] = zb[1] - num
print(",".join(str(num) for num in zb))

全部评论

相关推荐

10-30 22:18
已编辑
毛坦厂中学 C++
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务