Reputation: 135
from decimal import Decimal
prices = {
"2": [Decimal(2), Decimal(37.00)],
"5": [Decimal(5), Decimal(42.00)],
"10": [Decimal(10), Decimal(53.00)],
"15": [Decimal(15), Decimal(64.00)],
"20": [Decimal(20), Decimal(74.00)],
"25": [Decimal(25), Decimal(90.00)],
"31.5": [Decimal(31.5), Decimal(101.00)]
}
This table is interpreted thusly: If the total mass is lesser than or equal to 2kg, the shipping price is 37. If the total mass is greater than 2kg and lesser than or equal to 5kg, the shipping price is 42. If the total mass is is greater than 5kg and lesser than or equal to 10kg, the shipping price is 53 ... This is written like so:
def calculate(mass):
if mass <= prices["2"][0]:
print(prices["2"][1])
elif mass > prices["2"][0] and mass <= prices["5"][0]:
print(prices["5"][1])
elif mass > prices["5"][0] and mass <= prices["10"][0]:
print(prices["10"][1])
elif mass> prices["10"][0] and mass <= prices["15"][0]:
print(prices["15"][1])
elif mass > prices["15"][0] and mass <= prices["20"][0]:
print(prices["20"][1])
elif mass > prices["20"][0] and mass <= prices["25"][0]:
print(prices["25"][1])
elif mass > prices["25"][0] and mass <= prices["31.5"][0]:
print(prices["31.5"][1])
elif mass > prices["31.5"][0]:
remainder = Decimal(mass) % Decimal(31.5)
However, if the total mass is greater than the maximum package weight of 31.5kg, then a new package has to be made and the excess mass has to be distributed among packages not exceeding 31.5 kg.
So, for instance, if the total mass is 32 kg, the total shipping price would be:
package 1 (31.5kg) + package 2 (0.5kg) = 101 + 37 = 138
I think this should be somehow implemented using modulo, or recursion or both but I don't know how to put it in code.
Thank you in advance.
Upvotes: 0
Views: 57
Reputation: 13051
Here, I checked all the result of comparison and which one is first one to be True
, then find relative price.
def get_price(weight):
if weight < 0:
return None
div = int(weight/package)
mod = weight - div*package
mod_price = prices[list(map(lambda x:mod<=x, weights)).index(True)]
return div*package_price + mod_price
weights = [0.00, 2.00, 5.00, 10.00, 15.00, 20.00, 25.00, 31.50]
prices = [0.00, 37.00, 42.00, 53.00, 64.00, 74.00, 90.00, 101.00]
package = max(weights)
package_price = max(prices)
print(get_price(300)) # weight 300 = 31.50*9 + 16.5, price 983.0 (101.00*9 + 74.00)
Upvotes: 1
Reputation: 614
You can use a recursive call and change the prints to returns
def calculate(mass):
if mass <= prices["2"][0]:
return prices["2"][1]
elif mass > prices["2"][0] and mass <= prices["5"][0]:
return(prices["5"][1])
elif mass > prices["5"][0] and mass <= prices["10"][0]:
return(prices["10"][1])
elif mass> prices["10"][0] and mass <= prices["15"][0]:
return(prices["15"][1])
elif mass > prices["15"][0] and mass <= prices["20"][0]:
return(prices["20"][1])
elif mass > prices["20"][0] and mass <= prices["25"][0]:
return(prices["25"][1])
elif mass > prices["25"][0] and mass <= prices["31.5"][0]:
return(prices["31.5"][1])
elif mass > prices["31.5"][0]:
remainder = Decimal(mass) % Decimal(31.5)
remainder_price=calculate(remainder)
return(prices["31.5"][1]+remainder_price)
print(calculate(32))
Upvotes: 1