def innings_to_thirds(score):
score = float(score) # we can accept floats or strings: 5.2 or "5.2"
# 5.2 means 5 times 3 thirds ...
thirds = 3 * int(score)
# plus 0.2 * 10 = 2 thirds
thirds += (score - int(score)) * 10
return int(thirds)
def thirds_to_innings(thirds):
# the parts of the inning notation are the quotient
# and remainder of the division by 3
q, r = divmod(thirds, 3)
inning = str(q)
if r:
inning += '.' + str(r)
return inning
一些测试:
innings = [0, "0.2", 1, 2.1, "2.2"]
for i in innings:
print(f'{i} = {innings_to_thirds(i)} thirds')
thirds = [0, 2, 3, 7, 8]
for t in thirds:
print(f'{t} thirds -> {thirds_to_innings(t)}')
简单的解决方案可能是创建两个实用函数,将传统的表示法转换为三分之一数,反之亦然
一些测试:
输出:
相关问题 更多 >
编程相关推荐