gpt4 book ai didi

python - 将号码拆分为可变费率计费的频段,即 "tiered pricing"

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:17:16 24 4
gpt4 key购买 nike

我有一个在纸面上很简单的问题,但我很难用代码解决。在我继续之前,这不是字符串拆分问题

我有一个本质上是带状时间表应用程序。比例因客户而异,但一个例子是一个月的前三个小时为 80,接下来的三个小时为 70,其余为 50。我目前在代码中表示为:

scale = [80, 80, 80, 70, 70, 70, 50]

...但我也愿意接受那里的建议。

步骤的规模和数量是​​——而且必须是——可变的。对于某些客户,我的一些计费要简单得多,但我希望能够提供这种高使用率计划。

但是我如何计算工作小时数(例如 15.2)并计算他们应该支付多少?我如何将这个大数字分成乐队?正如我所说,纸上谈兵很容易,但随着我的客户越来越多,计划越来越复杂,这就变得很无聊了。以下是我将如何计算 15.2 小时:

3 hours at 80 = 240
3 hours at 70 = 210
9.2 hours at 50 = 460
total = 910

当我在做这件事的时候,我很感激对我试图描述的内容的专有名称的评论。还有 Oli,如果你在 2023 年回到这里,下次选择一个更简单的计费方案,伙计。

最佳答案

首先,我会把这个:

scale = [80, 80, 80, 70, 70, 70, 50]

进入这个:

import math

scale = {(0, 3): 80, (3, 6): 70, (6, math.inf): 50}

然后我的算法的其余部分如下:

# Total hours worked
hours_worked = 15.2

# Handle the decimal (if any) to begin with... First find the "max" rate
decimal_rate = next(rate for (lower, upper), rate in scale.items()
if lower <= hours_worked and upper >= hours_worked)

# Then calculate the last "sliver" of pay
decimal_end = hours_worked - int(hours_worked)
end_pay = decimal_end * decimal_rate

# Use an integer for ease of calculation
hours_worked = int(hours_worked)

hours_paid_for = 0

# Beginning total pay is just the decimal "ending"
total_pay = end_pay

while hours_paid_for < hours_worked:
# Find the rate for the current bucket of hours
rate_filter = (rate for (lower, upper), rate in scale.items() if lower <= hours_paid_for and hours_paid_for < upper)
current_level = next(rate_filter)

print('Hour: {}'.format(hours_paid_for))
print('Pay rate: ${}'.format(current_level))

total_pay += current_level

hours_paid_for += 1

print('Total earned: ${}'.format(total_pay))

输出如下:

Hour: 0
Pay rate: $80
Hour: 1
Pay rate: $80
Hour: 2
Pay rate: $80
Hour: 3
Pay rate: $70
Hour: 4
Pay rate: $70
Hour: 5
Pay rate: $70
Hour: 6
Pay rate: $50
Hour: 7
Pay rate: $50
Hour: 8
Pay rate: $50
Hour: 9
Pay rate: $50
Hour: 10
Pay rate: $50
Hour: 11
Pay rate: $50
Hour: 12
Pay rate: $50
Hour: 13
Pay rate: $50
Hour: 14
Pay rate: $50
Total earned: $910.0

这里还有一个简洁的函数:

def calculate_pay(scale, hours_worked):

# Handle the decimal (if any) to begin with... First find the "max" rate
decimal_rate = next(rate for (lower, upper), rate in scale.items()
if lower <= hours_worked and upper >= hours_worked)

# Then calculate the last "sliver" of pay
decimal_end = hours_worked - int(hours_worked)
end_pay = decimal_end * decimal_rate

# Use an integer for ease of calculation
hours_worked = int(hours_worked)

# Hours already paid for (int)
hours_paid_for = 0

# Beginning 'total pay' can be the decimal end, if any
total_pay = end_pay

while hours_paid_for < hours_worked:
# Find the rate for the current bucket of hours
rate_filter = (rate for (lower, upper), rate in scale.items()
if lower <= hours_paid_for and hours_paid_for < upper)
current_level = next(rate_filter)

total_pay += current_level

hours_paid_for += 1

return total_pay

关于python - 将号码拆分为可变费率计费的频段,即 "tiered pricing",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46427634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com