题解 | #坐标移动#

坐标移动

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

# 坐标类
class Point():
    # 初始化 0,0
    def __init__(self):
        self.x = 0
        self.y = 0
    # 坐标可以移动
    def move(self, direction, length):
        if direction == "A":
            self.x -= length
        elif direction == "S":
            self.y -= length
        elif direction == "W":
            self.y += length
        elif direction == "D":
            self.x += length
        else:
            pass
    # 打印坐标
    def get_position(self):
        print(str(self.x) + "," + str(self.y))
    
input_list = input().split(';')
mypoint = Point()
for item in input_list:
    # 对输出校验,长度不符合直接跳过
    if len(item) >3 or len(item)<2:
        continue
    # 只对长度符合的进行处理,
    if item[0].isalpha() and item[1:].isdigit():
        mypoint.move(item[0], int(item[1:]))
mypoint.get_position()

#坐标移动#
全部评论

相关推荐

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