需要考虑顺时针与逆时针转换的数字大小,并要在输入的string前加上"a"
________________________________________________________________
需要使用python的ord函数——
ord()函数是chr()函数(对于8位的ASCII字符串)或unichr()函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返回对应的ASCII数值,或者Unicode数值,如果所给的Unicode字符超出了你的Python定义范围,则会引发一个TypeError的异常。line = raw_input()
line = "a" + line
real_distance = 0
total_distance = 0
for i in range(1,len(line)):
distance1 = abs(ord(line[i]) - ord(line[i-1]))
distance2 = 26 - distance1
if distance1 < distance2:
real_distance = distance1
else:
real_distance = distance2
total_distance += real_distance
print total_distance
本文介绍了一个使用Python ord函数的应用实例。该实例通过计算字符串中相邻字符间的距离,实现了特定场景下字符距离的计算。文章提供了完整的代码示例,帮助读者理解ord函数的工作原理。
4255

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



