Using Python to solve permutation and composition problems

Source: Internet
Author: User

Encountered a permutation of the following problems, did not calculate their own, and finally in Python in the permutation library function to obtain the answer. The total number of 10-bit students should be 10!. However, it is not clear how the number of male students at any time in the car is less than the number of female students. But it can be calculated using the following code.

Use the Python3 program to find the answers for the library combinations and permutations functions that have permutations in the A:python.    1 from itertools import permutations  2 res=0                         3 s=[ 1,1,1,1,1,2,2,2,2,2]   # 1 means boys, 2 Menas girls     4 for item in Permutations (s,10):   5     for i in range (1,10):     6          if not Item[:i].count (1) >=item[:i].count (2):   7             break  8      else:          9          res+=1             10 print ("The num is {}". Format (res))  [email protected]:~# python3 permutation_girls_boys.pythe num is 604800

Using Python to solve permutation and composition problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.