开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
输入:
合法坐标为A(或者D或者W或者S) + 数字(两位以内)
坐标之间以;分隔。
非法坐标点需要进行丢弃。如AA10; A1A; ; YAD; 等。
下面是一个简单的例子 如:
A10;S20;W10;D30;X;A1A;B10A11;;A10;
处理过程:
起点(0,0)
A10 = (-10,0)
S20 = (-10,-20)
W10 = (-10,-10)
D30 = (20,-10)
x = 无效
A1A = 无效
B10A11 = 无效
一个空 不影响
A10 = (10,-10)
结果 (10, -10)
python代码实现:
#coding=utf-8
#判断命令格式是否正确
def judge_command(string):
#定义三个变量,分别检测字母、数字、特殊字符的个数
alpha_count = 0
number_count = 0
other_count = 0
alpha_bak = ['A','S','D','W']
#如果长度小于1,代表命令为空,返回false
if len(string) < 1:
return False
#记录字母、数字、特殊非'-'号特殊字符的个数
if string[0].isdigit():
return False
for i in string:
if i.isalpha():
alpha_count += 1
elif i.isdigit():
number_count += 1
elif i != '-':
other_count += 1
#如果格式正确,输出True,否则,输出Flase
if alpha_count == 1 and string[0] in alpha_bak and number_count<=2 and number_count >= 1 and other_count == 0:
return True
else:
return False
#坐标移动
def axis_move(command,axis):
if command[1] == '-':
number = 0 - int(command[2:])
else:
number = int(command[1:])
if command[0] == 'A':
axis[0] -= number
elif command[0] == 'S':
axis[1] -= number
elif command[0] == 'D':
axis[0] += number
elif command[0] == 'W':
axis[1] += number
return axis
#主函数
def main():
axis = [0,0]
inputs = raw_input()
commands = inputs.split(';')
for c in commands:
if judge_command(c):
axis = axis_move(c,axis)
print str(axis[0]) + ',' + str(axis[1])
return
if __name__ == '__main__':
main()

本文介绍如何开发一个坐标计算工具,该工具根据输入的字符指令(A/D/W/S)和数字来移动坐标,从(0,0)点开始。合法输入如A10, S20等,非法输入将被忽略。示例输入包括A10;S20;W10;D30;X;A1A;B10A11;;A10;,最终结果坐标为(10,-10)。提供了一个简单的Python代码实现。"
105140850,9413590,图神经网络入门:计算机视觉中的图模型与卷积难题,"['图神经网络', '计算机视觉', '数据结构', '卷积操作', '机器学习']
3167

被折叠的 条评论
为什么被折叠?



