Is there a nicer, more cohesive way to achieve what function pace_points
does?
Pseudo code would be: with every x number lower of pace
return value higher by y.
Next step pseudo code would be: with every x number lower of pace
return value higher according to some distribution (e.g. exponential).
def pace_points(pace: float):
if pace >= 6.5:
return 0.5
elif 6 <= pace < 6.5:
return 1
elif 5.5 <= pace < 6:
return 2
elif 5 <= pace < 5.5:
return 3
elif 4.5 <= pace < 5:
return 4
elif 4.0 <= pace < 4.5:
return 5
else:
return 10
The following expresses you logic in a more compact form:
import math
def pace_points(pace):
p = math.ceil(2*(6.5-pace))
return 0.5 if p <= 0 else 10 if p > 5 else p
FOLLOW UP: following @DanielHao comment below, just wanted to articulate on what the code does.
math.ceil
returns the closest integer higher that the passed argument. It is different from round
, which simply returns the closest integer. For example, math.ceil(2.3) will be 3, round(2.3) is 2. (the "sister" function to math.ceil
is math.floor
, which returns the closest integer lower than the passed argument.math.ceil(2 * (6.5 - pace))
is a function that returns the desired values within the (0, 4.5] interval.if...else...else
takes care of the returned value outside the interval.