题解 | #坐标移动#

坐标移动

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

python初学,确实如果思路到就不难,就只是筛选跟判断的问题,早上起来就给他干了

coordinate = list(input().split(";"))  # 使用“;”进行切片分割出每一个坐标信息,转换成列表
list1 = ["A", "W", "S", "D"]
list2 = []
for strings in coordinate:
    counts = len(strings)
    section = strings[1:counts:]  # 动态切片,将后1/2位字符切出判断是否是数字
    if 2 <= counts <= 3 and strings[0] in list1 and section.isdecimal():
        list2.append(strings)  # 将得到的最终结果追加到列表中
answer = [(0, 0)]
n = 0
for strings in list2:
    counts = len(strings)
    if strings[0] == "A":  # 判断是否为A,x-
        nums = int(strings[1:counts:])  #将数字切出
        tup = (answer[n][0] - nums, answer[n][1])
        answer.append(tup)
        n += 1
    elif strings[0] == "D":  # 判断是否为D,x+
        nums = int(strings[1:counts:])
        tup = (answer[n][0] + nums, answer[n][1])
        answer.append(tup)
        n += 1
    elif strings[0] == "S":  # 判断是否为S,y-
        nums = int(strings[1:counts:])
        tup = (answer[n][0], answer[n][1] - nums)
        answer.append(tup)
        n += 1
    elif strings[0] == "W":  # 判断是否为W,y+
        nums = int(strings[1:counts:])
        tup = (answer[n][0], answer[n][1] + nums)
        answer.append(tup)
        n += 1
print("{},{}".format(answer[-1][0], answer[-1][1]))
全部评论

相关推荐

06-12 16:23
已编辑
小米_软件开发(准入职员工)
点赞 评论 收藏
分享
下北澤大天使:你是我见过最美的牛客女孩😍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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