# Create a formula from a set of number ranges

I'm not sure what what to call what I would like help creating so please bear with me.

Basically I have a program which assigns you a number of points(a competition point not a point on a graph or anything) depending on how quickly you answer a query. This is how it looks:

Response Time in Hours | Points Awarded
0 | 10 Points
1-6 | 8 Points
6-12 | 6 Points
12-18 | 4 Points
18-24 | 2 Points
24+ | 0 Points

This can be achieved by making multiple if queries but that could end up being a strain on the server. I would like to know if there is a formula that I could just multiply the response time by which could give me a result in that range? Is this even possible?

Thanks in advance for any help.

-
Little side-note: 6 if-clauses are not really a "strain" on the server. In the code you shold stay at the if's; not sure if the evaluation of given $f$ might even be slower (multiply + subrtract + ceil(double) > 6 if ??) Code: if (t<1) return 10; else if (t<6) return 8; else if (t<12) return 6; else if (t<18) return 4; else if (t<24) return 2; else return 0; (sorry for the bad formatting, it's due to comments) – AlexR Aug 7 '13 at 10:13
Thanks for the comment AlexR. The reason I would like to stay away from the if statements is that we run an e-commerce site and the formula will be used to sort search results so it is very likely that we could have hundreds of these being performed at the same time so any resource reduction is quite important to us. – skr Aug 7 '13 at 10:50
What I tried to point out was that the if-clauses should perform faster than the formula unless you have the formula implemented in a stored procedure in a RDBMS (in this case, if clauses are more expensive). When running a compiled if-clause (say c assembly), it will result in jlt assembly code, i.e. one instruction per case. – AlexR Aug 7 '13 at 11:40

Yes. The trick is to use the ceiling function. Let $x=$ response time. Then we can determine the points awarded by using the piecewise function: $$f(x)=\begin{cases} 10-2\left\lceil\dfrac{x}{6}\right\rceil & \text{if }0\leq x\leq24 \\ 0 & \text{if }x>24 \end{cases}$$

In Python, you could implement this as something like:

import math
def awardPoints(x):
"""
PRE: x is the (positive) response time in hours.
POST: Returns the points to be awarded.
"""
if x <  0: return -1  # ERROR: x must be positive.
if x > 24: return 0   # We don't want to award negative points.

# Else, we know that 0 <= x <= 24.
return 10 - 2*math.ceil(x / 6.0)

-
Thanks very much for the answer Adriano. Do you perhaps have a version of your formula that could work in Javascript as I don;t have any experience in python? – skr Aug 7 '13 at 10:47
I don't have any experience with Javascript, but it seems like its implementation should be very similar. According to this (w3schools.com/jsref/jsref_ceil.asp), the syntax for my last line of code would be identical, except you would capitalize the 'm' in math and end the statement with a semi-colon. – Adriano Aug 7 '13 at 10:54
Thanks for that Adriano, it looks like it will be the same in Javascript. I found I made an error in my explanation. 10 points should be awarded for 0-1 hours, not just for 0. So if they replied in 30min, for example, they would also get 10 points. Is it possible for the formula to accommodate that? – skr Aug 7 '13 at 11:38
No, not without introducing an extra if statement. The formula only works if the ranges are equally spaced (of length $6$). Making the first range have a length of $1$ messes that up unfortunately. – Adriano Aug 7 '13 at 16:53
Ok I understand. This is what I went with in the end: if(x<1){ return 10;}; if else(x>24){ return 0;}; else{ return 10 - 2*Math.ceil(x / 6.0)}; Thanks for your help. – skr Aug 8 '13 at 5:52