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))
相关推荐
2501_916007473 分钟前
Python实现HTTPS爬虫的完整指南:使用requests、BeautifulSoup、Selenium和Scrapy
爬虫·python·ios·小程序·https·uni-app·iphone
gptAI_plus11 分钟前
别把整个仓库塞给 AI:用 Python 生成安全的代码上下文清单
python·chatgpt
吃饱了得干活12 分钟前
Agent 记忆系统:从短期记忆到长期记忆
python·langchain·agent
大鱼>29 分钟前
DSPy:LLM程序自动编译与提示词优化
开发语言·人工智能·python·深度学习
2401_8432537034 分钟前
金融智能:AI如何重构银行业未来
人工智能·python·金融
uncle_ll35 分钟前
服务器选型、微调范式、训练优化与环境搭建
服务器·python·gpt·llm·nlp
久久学姐1 小时前
Python开发爬虫的常用技术架构
爬虫·python·http·框架·数据存储
circuitsosk1 小时前
大规模离线数据管道构建:样本获取、清洗、加工与合成
人工智能·python·机器学习·搜索引擎
tang777891 小时前
分布式爬虫优化指南:如何用代理IP把采集效率提升300%
分布式·爬虫·python·tcp/ip·分布式爬虫·爬虫代理·代理ip
gwf2161 小时前
Soft-RoCE与Soft-iWARP深度解析:无硬件RDMA学习环境搭建(零基础必知必会)
人工智能·python·tcp/ip·tcp·tcpdump