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))
相关推荐
czhc11400756631 分钟前
Linux 76 rsync
linux·运维·python
悠悠小茉莉32 分钟前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)
c++·ide·vscode·python·visualstudio·visual studio
m0_625686551 小时前
day53
python
Real_man1 小时前
告别 requirements.txt,拥抱 pyproject.toml和uv的现代Python工作流
python
站大爷IP2 小时前
Python文件操作的"保险箱":with语句深度实战指南
python
运器1232 小时前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程
巴里巴气4 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
19894 小时前
【零基础学AI】第26讲:循环神经网络(RNN)与LSTM - 文本生成
人工智能·python·rnn·神经网络·机器学习·tensorflow·lstm
JavaEdge在掘金5 小时前
Redis 数据倾斜?别慌!从成因到解决方案,一文帮你搞定
python
ansurfen5 小时前
我的第一个AI项目:从零搭建RAG知识库的踩坑之旅
python·llm