简体   繁体   English

如何使用我的使用量和量对成本的括号动态计算水费成本?

[英]How do I dynamically calculate a water bill cost using my usage volume and volume-to-cost brackets?

I'm trying to predict my water bill using a flowmeter connected to my mainline.我正在尝试使用连接到我的干线的流量计来预测我的水费。

I have the price rates used for my bill which are: 27.54kL @ $2.392 91.80kL @ $3.413 300.00kL @ $3.699我的账单使用的价格为: 27.54kL @ $2.392 91.80kL @ $3.413 300.00kL @ $3.699

So for example, if I used 250kL, my bill would be broken down as:例如,如果我使用 250kL,我的账单将被分解为:

1st rate: There are more than 27.54kL in 250kL, so cost of this rate is: 27.54*$2.392 = $65.88 (250 - 27.54 = 222.46kL remaining for the next rate)
2nd rate: There are more than 91.80kL in 222.46kL, so cost of this rate is = 91.80*$3.413 = $313.31 (222.46 - 91.80 = 130.66kL remaining for next rate)
3rd rate: There are less than 300kL in 130.66kL, but more than 0, so cost of this rate is: 130.66*$3.699 = $483.31 (130.66 - 130.66 = 0L remaining for next rate (if there was a 4th))

Total = sum of all rate costs = $862.50

How can I calculate this in a dynamic way, so that if the rates or even the number of rates change, it still works?我怎样才能以动态的方式计算它,以便如果费率甚至费率的数量发生变化,它仍然有效? I can easily reproduce the above, but I'd like to use lists and list comprehension or other loops to produce the final cost.我可以轻松地重现上述内容,但我想使用列表和列表理解或其他循环来产生最终成本。

This will calculate what I need, but it's not dynamic:这将计算我需要什么,但它不是动态的:

volumeCostDict = [[27.54,2.392], [91.80,3.413], [350.00,3.699]]
volume = 271 # kilolitres

rate1Volume = volumeCostDict[0][0] if volume > volumeCostDict[0][0] else volume
rate1Cost = rate1Volume * volumeCostDict[0][1]

rate2Volume = volumeCostDict[1][0] if volume > (volumeCostDict[0][0] + volumeCostDict[1][0]) else volume - volumeCostDict[0][0]
rate2Cost = rate2Volume * volumeCostDict[1][1]

rate3Volume = volumeCostDict[2][0] if volume > (volumeCostDict[0][0] + volumeCostDict[1][0] + volumeCostDict[2][0]) else volume - volumeCostDict[0][0] - volumeCostDict[1][0]
rate3Cost = rate3Volume * volumeCostDict[2][1]

print(rate1Cost)
print(rate2Cost)
print(rate3Cost)

totalCost = rate1Cost + rate2Cost + rate3Cost

Thanks!谢谢!

I got it, just needed to code it statically first and then work out the pattern to apply it dynamically.我明白了,只需要先对其进行静态编码,然后制定模式以动态应用它。 There's still probably a simpler way, but this works EDIT: to work for all cases可能还有一种更简单的方法,但这有效 编辑:适用于所有情况

volumeCostDict = [[27.54,2.392], [91.80,3.413], [350.00,3.699]]
volume = 271 # kilolitres

rateVolumes = [volume_kL \
                 if volume_kL < volumeCostDict[i][0] and i == 0 \
                 else volumeCostDict[i][0] \
                 if volume_kL > sum([volumeCostDict[j][0] for j in range(i+1)]) \
                 else volume_kL - sum([volumeCostDict[k][0] for k in range(i)]) \
                 if (volume_kL - sum([volumeCostDict[k][0] for k in range(i)])) > 0 \
                 else 0 \
                 for i in range(len(volumeCostDict))]
totalCost = sum([rateVolumes[i]*volumeCostDict[i][1] for i in range(len(rateVolumes))])
print(rateVolumes)
print(totalCost)

I have found a way out, if you say that, the range would be same, it is only data change thing.我找到了一条出路,如果你这么说,范围是一样的,只是数据变化的事情。 So what we can do, we can make use of while loop and follow this algo所以我们能做什么,我们可以利用while循环并遵循这个算法

1. While volume != 0
2. Two extreme cases and two normal cases are there:
   a) {If the volume becomes less the least value `volumeCostDict[0][0]`} and {If volume > `volumeCostDict[0][0]` and volume > `volumeCostDict[0][0]`}
   b) {If volume >= `volumeCostDict[0][0]`} and {If volume >= `volumeCostDict[1][0]`}
3. Deal with the both the cases, in case one do the computation for both the extreme cases. 
4. For normal cases, computation is there, which you have shown already
5. Print after coming from the while loop 

FINAL SOLUTION最终解决方案

volumeCostDict = [[27.54,2.392], [91.80,3.413], [350.00,3.699]]
volume = 250 # kilolitres
totalCost = 0

# this will run until volume becomes 0
while volume != 0 :
    # extreme case, what if the value becomes less than the least
    # in our case 27.54, so we calculate the same for 27.54
    # and make the value go to 0 
    # this is required, since for Volume = 200, you won't get any result
    if volume < volumeCostDict[0][0]:
        totalCost += (volume * volumeCostDict[0][1])
        volume = 0
      
    # this is normal case, which will check if the volume is
    # greater than equals least value  
    if volume >= volumeCostDict[0][0]:
        volume -= volumeCostDict[0][0]
        totalCost += (volumeCostDict[0][0] * volumeCostDict[0][1])
        
    # this is normal case, which will check if the volume is
    # greater than equals second rate value 
    if volume >= volumeCostDict[1][0]:
        volume -= volumeCostDict[1][0]
        totalCost += (volumeCostDict[1][0] * volumeCostDict[1][1])
      
    # extreme case again, if the volume after deduction  is still 
    # greater than the normal cases, that is 27.54 and 91.80
    if volume > volumeCostDict[0][0] and volume > volumeCostDict[1][0]:
        totalCost += (volume * volumeCostDict[2][1])
        volume = 0

print("${:.2f}".format(totalCost))
# OUTPUT for volume = 250 will be 
>>> $862.50

You can check this with other volumes as well.您也可以使用其他volumes检查这一点。 I know this solution will sound layman to you.我知道这个解决方案对你来说听起来很外行。 But tell me how much useful it was for you:) For any suggestions, I am open to learn.但是告诉我它对你有多大用处:) 对于任何建议,我愿意学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM