Python实现按天分段计费的费用计算方法
最编程
2024-08-06 18:15:38
...
1 # coding:utf-8
2 import sys
3
4 MAX_INT = sys.maxsize
5 pricelist = []
6 priceIdx = 0
7
8
9 # 价格区间对象
10 class PriceRange(object):
11 def __init__(self, price, min, max=MAX_INT):
12 self.price = price
13 self.min = min
14 self.max = max
15
16
17 # 计算单日花费
18 def getOnedayCost(pre, total):
19 global priceIdx
20 if priceIdx == len(pricelist):
21 priceIdx = priceIdx - 1
22 price = pricelist[priceIdx].price
23 max = pricelist[priceIdx].max
24
25 if total > max:
26 part1 = (max - pre) * price
27 pre = max
28 priceIdx = priceIdx + 1
29 if priceIdx == len(pricelist):
30 return part1 + (total - max) * price
31 else:
32 return part1 + getOnedayCost(pre, total)
33 else:
34 return (total - pre) * price
35
36
37 # 计算总花费
38 def caculate(usedlist):
39 total = 0
40 cost = []
41 for used in usedlist:
42 pre = total
43 total = used + total
44 cost.append(getOnedayCost(pre, total))
45 return cost
46
47
48 # 校验价格区间
49 def verifyPriceBrand():
50 if pricelist is None or len(pricelist) == 0:
51 raise Exception("the price range can not be null!")
52 tmp = pricelist[0].max
53 for idx in range(1, len(pricelist)):
54 if not pricelist[idx].min == tmp:
55 raise Exception("the price range is not continuous!")
56 tmp = pricelist[idx].max
57 if tmp < MAX_INT:
58 raise Exception("the price range is Incomplete !")
59 print("pricelist is qualified.")
60 return True
61
62
63 # 初始化价格区间
64 def initPriceBrand():
65 pricelist.append(PriceRange(5, 0, 10))
66 pricelist.append(PriceRange(6, 10, 20))
67 pricelist.append(PriceRange(7, 20, 30))
68 pricelist.append(PriceRange(8, 30, 40))
69 pricelist.append(PriceRange(9, 40, 50))
70 pricelist.append(PriceRange(10, 50))
71
72
73 if __name__ == "__main__":
74 initPriceBrand()
75 verifyPriceBrand()
76 costlist = [5, 20, 80, 30, 10]
77 print(caculate(costlist))
推荐阅读
-
Python实现按天分段计费的费用计算方法
-
固定资产折旧代码 python 固定资产折旧代码逻辑-1.1 概述 固定资产折旧方法有多种,基本可分为直线法(包括年平均法和工作量法)和加速折 旧法(包括年数总和法和双倍余额递减法),企业应根据固定资产所含经济利益的预计 实现情况选择不同的折旧方法。我国会计准则规定,固定资产折旧按月计提;当月增加的固定资产,当月不提折旧,从次月起计提折旧;当月减少的固定资产,当月仍应计提折旧,从次月起停止计提折旧;计提折旧后,无论能否使用,均不得提取折旧;提前报废的固定资产,不再计提折旧。折旧计算方法举例如下: 年平均折旧额=(资产购置价值-残值)/预计使用年限 年总折旧额=(资产购置价值-残值)*使用年限/预计使用总年限 例如,价值 1 万元的电脑,残值为零,预计使用 5 年,各年折旧计算公式如下。按以下公式计算各年折旧 第一年:10,000.00 * 5/(5+4+3+2+1) 第二年:10,000.00 * 4/(5+4+3+2+1) 第三年:10,000.00 * 3/(5+4+3+2+1) 第四年:10,000.00 * 2/(5+4+3+2+1) 第五年:10,000.00 * 1/(5+4+3+2+1) 1.2 +2+1) 1.2 相关配置