python 中的优化方法。类似于排列

发布于 2024-11-10 14:21:59 字数 1505 浏览 1 评论 0原文

我有一种方法可以生成预订过程中的乘客数量。参数是最小和最大乘客数量。

只有 2 个条件:

  • number_of_adults 必须大于 0
  • number_of_infants 必须小于或等于 number_of_adults

这是代码:

    def generate_numbers_of_paxes(min_number=1,max_number=9):

    number_of_passengers=random.randint(min_number, max_number)
    print(number_of_passengers)
    number_of_adults=random.randint(min_number, number_of_passengers)
    number_of_youths=random.randint(0, number_of_passengers-number_of_adults)
    number_of_children=random.randint(0, number_of_passengers-number_of_adults-number_of_youths)
    number_of_infants=random.randint(0, number_of_passengers-number_of_adults-number_of_youths-number_of_children)
    number_of_adults+=number_of_passengers-number_of_adults-number_of_youths-number_of_children-number_of_infants
    if(number_of_adults<number_of_infants):
        difference=number_of_infants-number_of_adults
        number_of_infants-=difference
        number_of_adults+=difference

    return [number_of_adults,number_of_youths,number_of_children,number_of_infants]

numbers_of_paxes=generate_numbers_of_paxes()
for (x,y) in enumerate(numbers_of_paxes):
    print(x,y)

此代码效果很好,但正如您所看到的,行中存在一些问题

number_of_adults+=number_of_passengers-number_of_adults-number_of_youths-number_of_children-number_of_infants

,因为每个年龄段的人数总和并不总是等于 number_of_passegers,并且我将差值添加到 number_of_adult

I have a method which generates me number of passengers to reservation process. The parameters are the minimum and maximum numbers of passengers.

There are only 2 conditions:

  • number_of_adults must be greater than 0
  • number_of_infants must be less than or equal to number_of_adults

This is code:

    def generate_numbers_of_paxes(min_number=1,max_number=9):

    number_of_passengers=random.randint(min_number, max_number)
    print(number_of_passengers)
    number_of_adults=random.randint(min_number, number_of_passengers)
    number_of_youths=random.randint(0, number_of_passengers-number_of_adults)
    number_of_children=random.randint(0, number_of_passengers-number_of_adults-number_of_youths)
    number_of_infants=random.randint(0, number_of_passengers-number_of_adults-number_of_youths-number_of_children)
    number_of_adults+=number_of_passengers-number_of_adults-number_of_youths-number_of_children-number_of_infants
    if(number_of_adults<number_of_infants):
        difference=number_of_infants-number_of_adults
        number_of_infants-=difference
        number_of_adults+=difference

    return [number_of_adults,number_of_youths,number_of_children,number_of_infants]

numbers_of_paxes=generate_numbers_of_paxes()
for (x,y) in enumerate(numbers_of_paxes):
    print(x,y)

This code works good, but as you can see there is some problem in line

number_of_adults+=number_of_passengers-number_of_adults-number_of_youths-number_of_children-number_of_infants

because sum of number for every age group isn't always equals number_of_passegers, and I add difference to number_of_adult.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腻橙味 2024-11-17 14:22:00

您的 number_of_infants 计算应该很简单:

number_of_infants = number_of_passengers - number_of_adults - number_of_youths - number_of_children

否则,它很可能低于满足乘客要求所需的数量(0 之间的随机数字)以及乘客人数 - 成人人数 - 青少年人数 - 儿童人数)。

但是...您最好在成人之前计算婴儿数量,然后计算成人数量,并将 number_of_infants 作为 random() 的最小值代码>,考虑到您列出的约束。将孩子的数量留到最后并应用相同的逻辑。由于这看起来像是家庭作业,因此我不会提供相关代码。

也就是说:

  1. 生成随机数量的乘客

  2. 生成随机数量的婴儿
    (不能超过数量的一半
    乘客)

  3. 生成随机数量的成年人
    (必须至少有婴儿数量)

  4. 生成随机数量的青少年
    (不能多于乘客少
    婴儿减去成人)

  5. 儿童数量是
    乘客减去前三名
    组。

另外,你为什么要这样做?

for (x,y) in enumerate(numbers_of_paxes):
    print(y)

要么做:

for (x,y) in enumerate(numbers_of_paxes):
    print(x, y)

要么:

for y in numbers_of_paxes:
    print(y)

Your number_of_infants calculation should be simply:

number_of_infants = number_of_passengers - number_of_adults - number_of_youths - number_of_children

Otherwise it is likely to be lower than the required number to meet your passenger requirements (a random number between 0 and number_of_passengers - number_of_adults - number_of_youths - number_of_children).

However... you are better calculating the number of infants before adults and then calculating the number of adults with number_of_infants as the minimum for the random(), given the constraints you listed. Leave the number of children until last and apply the same logic. Since this looks like homework, I'll not provide the code for this.

That is to say:

  1. Generate random number of passengers

  2. Generate random number of infants
    (can't be more than half of number
    of passengers)

  3. Generate random number of adults
    (must be at least number of infants)

  4. Generate random number of youths
    (can't be more than passengers less
    infants less adults)

  5. Number of children is number of
    passengers less previous three
    groups.

Also, why are you doing this?

for (x,y) in enumerate(numbers_of_paxes):
    print(y)

Either do:

for (x,y) in enumerate(numbers_of_paxes):
    print(x, y)

Or:

for y in numbers_of_paxes:
    print(y)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文