HJ17坐标移动

题目

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29?tpId=37\&tags=\&title=\&difficulty=0\&judgeStatus=0\&rp=1\&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37

思路

朴素遍历判断字符串是否合法,合法就对x,y进行加或减。

字符串处理的题目。感觉是个简单题,但是它标为中等了。

代码

bash 复制代码
import sys
# print(ord("0"))
# print(ord("9"))
# 48
# 57

def isdigit(v):
    # v: str
    # 两位以内
    for e in v:
        # valid [48,57]
        o = ord(e)
        if o < 48 or o > 57:
            return False
    return True

def valid(x_y):
    # x_y: str
    # [2,3] valid len
    if len(x_y) < 2 or len(x_y)>3:
        return False
    d, v = x_y[0], x_y[1:]
    if d not in "ADWS":
        return False
    if not isdigit(v):
        return False 
    return True 
def get_direction_value(x_y):
    d = x_y[0]
    v = int(x_y[1:])
    return d, v



for line in sys.stdin:
    a = line.split()[0].split(";")
    x, y = 0, 0
    for x_y in a:
        if not valid(x_y):
            continue
        d, v = get_direction_value(x_y)
        if d == "A":
            x -= v
        elif d == "D":
            x += v 
        elif d == "W":
            y += v
        else:
            y -= v 
    print("{},{}".format(x,y))
相关推荐
hmbbcsm10 分钟前
练习python题目小记(五)
开发语言·python
蓝桉~MLGT11 分钟前
Python学习历程——文件
python·学习·策略模式
循环过三天12 分钟前
7.5、Python-匿名函数lambda
笔记·python·学习
仟濹26 分钟前
【Java 基础】3 面向对象 - this
java·开发语言·python
Dxy123931021631 分钟前
Python一个类的特殊方法有哪些
开发语言·python
梅花141 小时前
基于Django的博客系统
后端·python·django·毕业设计·博客·博客系统·毕设
烤汉堡2 小时前
Python入门到实战:网络请求与数据获取
python
rimoyee2 小时前
[python探本] 内存数据存储机制
python
LiLiYuan.2 小时前
Arrays类和List接口的关联
java·开发语言·windows·python