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))
相关推荐
ZhengEnCi3 小时前
M3-markconv库找不到wkhtmltopdf问题
python
2301_764441336 小时前
LISA时空跃迁分析,地理时空分析
数据结构·python·算法
chushiyunen7 小时前
python rest请求、requests
开发语言·python
cTz6FE7gA7 小时前
Python异步编程:从协程到Asyncio的底层揭秘
python
baidu_huihui7 小时前
在 CentOS 9 上安装 pip(Python 的包管理工具)
开发语言·python·pip
南 阳7 小时前
Python从入门到精通day63
开发语言·python
lbb 小魔仙7 小时前
Python_RAG知识库问答系统实战指南
开发语言·python
FreakStudio7 小时前
MicroPython LVGL基础知识和概念:底层渲染与性能优化
python·单片机·嵌入式·电子diy
素玥8 小时前
实训5 python连接mysql数据库
数据库·python·mysql