# Find if it is possible to get a ratio from given ranges of costs and quantities in Python

PythonServer Side ProgrammingProgramming

Suppose we have a range of cost from lowCost to upCost and another range of quantity from lowQuant to upQuant, we have to check whether we can find the given ratio r where r=cost/quantity, and lowCost ⇐ cost ⇐ upCost and lowQuant ⇐ quantity ⇐ upQuant.

So, if the input is like lowCost = 2, upCost = 10, lowQuant = 3, upQuant = 9 and r = 3, then the output will be True as the cost = r * quantity = 3 * 3 = 9 where cost is in range [1, 10] and quantity is in [2, 8]

To solve this, we will follow these steps −

• for i in range l_quant to u_quant, do

• res := i * ratio

• if l_cost ⇐ res and res ⇐ u_cost, then

• return True

• return False

## Example

Let us see the following implementation to get better understanding −

Live Demo

def can_we_find_r(l_cost, u_cost, l_quant, u_quant, ratio) :
for i in range(l_quant, u_quant + 1) :
res = i * ratio
if (l_cost <= res and res <= u_cost) :
return True
return False

l_cost = 2
u_cost = 10
l_quant = 3
u_quant = 9
ratio = 3

print(can_we_find_r(l_cost, u_cost,l_quant,u_quant, ratio))

## Input

2, 10, 3, 9, 3

## Output

True