N
N
Nonoxy2021-03-21 14:08:03
Python
Nonoxy, 2021-03-21 14:08:03

How to calculate the total cost?

In general, I decided to simplify my life a bit and make a boost price calculator on the fanpay, the cost is calculated by intervals (I will attach a photo below), I ran into such a problem: how to teach the program to understand intervals, i.e.
1500 (initial) - 3500 (final)
Using the mathematics of the second grade, we understand that we need to raise the rating by 2000, but the prices on the site are indicated for the ranges, i.e. 1-2000 (n-th price), 2000-3000 (n-th price), 3000-3500 (n-th price), I feel that the answer to my question is banal, and I'm walking somewhere on the surface.
PS
I enter the initial and final rating into the program605728fbc9a81881312034.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
o5a, 2021-03-21
@Nonoxy

How can you calculate it.
Create a dictionary of consecutive price ranges.

prices = {2000: 70, 3000: 100, 3500: 150, 4000: 200, 4500: 250, 5000: 300}

Then gradually go through the dictionary, comparing the current initial rating with the key.
If the rating is below the range, then we add the calculated amount from this range to the final amount, i.e. (border - rating)*price, after which the current rating is set to this border.
And so on until we reach the desired rating.

G
Grigory Boev, 2021-03-21
@ProgrammerForever

Yes, that's how it's supposed to be. If the range is included completely - count it as a whole, if only its part - then count the fraction of the range.
Here is a demo table
605734c168526238238062.png

V
Vindicar, 2021-03-21
@Vindicar

You have two possible cases.
a. The initial rating and the target rating are in the same range. You calculate the cost for the price of this range.
b. Initial rating and target rating in different ranges.
Then the total cost will be the sum of three values.
1) Cost of leaving the initial range: (upper limit - initial rating) * range price
2) Cost of passing through all intermediate ranges: Sum ((upper - lower) * range price). May be null if the target rating is in the adjacent range.
3) Cost inside target range: (target rating - bottom) * range price
Differences may need to be rounded up to the minimum amount of rating to buy at a time.
In general, are you sure that within the same transaction the price for the rating is different? Maybe it depends only on the starting rating? Then a boost with large volumes from low ratings will be more profitable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question