专业编程基础技术教程

网站首页 > 基础教程 正文

Python最远足迹(python最短跑道长度)

ccvgpt 2025-03-23 17:41:54 基础教程 13 ℃
import re

def is_valid_coordinate(coord):
    """判断坐标是否合法"""
    # 合法的坐标格式为 (x,y),其中 0 < x < 1000,0 < y < 1000,且不能有前导零
    pattern = r'^\((\d{1,3}|[1-9]\d{3}),(\d{1,3}|[1-9]\d{3})\)
if re.match(pattern, coord): x, y = map(int, coord.strip('()').split(',')) return 0 < x < 1000 and 0 < y < 1000 return false def get_farthest_footprintdata: coordinates='re.findall(r'\(\d+,\d+\)',' data farthest_coord='None' max_distance='0' for coord in coordinates: if is_valid_coordinatecoord: x y='map(int,' coord.strip.split distance='x' x y y if distance> max_distance: max_distance = distance farthest_coord = coord # 如果没有合法坐标,返回总部坐标 (0,0) return farthest_coord if farthest_coord else "(0,0)" # 自定义输入 input_data = input("请输入记录仪中的数据:") # 获取最远足迹坐标 result = get_farthest_footprint(input_data) # 输出结果 print("最远足迹坐标:", result)


Python最远足迹(python最短跑道长度)

代码说明:

  1. 正则表达式匹配

使用 re.findall(r'\(\d+,\d+\)', data) 提取所有可能的坐标格式。

使用 is_valid_coordinate 函数进一步验证坐标是否合法。

  1. 合法性检查

坐标格式必须为 (x,y),且 x 和 y 不能有前导零。

x 和 y 的取值范围为 0 < x < 1000 和 0 < y < 1000。

  1. 距离计算

相对总部的距离计算公式为 x * x + y * y。

遍历所有合法坐标,找到距离最大的坐标。

  1. 输出结果

如果没有任何合法坐标,返回总部坐标 (0,0)。

否则,返回最远足迹的坐标。

最近发表
标签列表