开发者

Optimize method in python. Similar to permutation

开发者 https://www.devze.com 2023-03-09 00:37 出处:网络
I have a method which generates me number of passengers to reservation process. The parameters are the minimum and maximum numbers of passengers.

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开发者_JS百科.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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消