题解 | #坐标移动#

坐标移动

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

import re

def move_coordinates(commands):
    x, y = 0, 0
    pattern = re.compile(r'([ADWS])(\d{1,2})')
    moves = commands.split(';')
    for move in moves:
        result = pattern.fullmatch(move)
        if result:
            direction, magnitude = result.groups()
            magnitude = int(magnitude)
            if direction == 'A':
                x -= magnitude
            elif direction == 'D':
                x += magnitude
            elif direction == 'W':
                y += magnitude
            elif direction == 'S':
                y -= magnitude
    return f"{x},{y}"
input_commands = input()
output = move_coordinates(input_commands)
print(output)

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务