Python exercise question 017: Three table tennis teams in the competition list, python017

Source: Internet
Author: User

Python exercise question 017: Three table tennis teams in the competition list, python017

[Python exercise 017]Two table tennis teams competed, with three members each. Team a is a, B, and c, and Team B is x, y, and z. The game list has been determined by lottery. Someone asked the players about the game list. A said he doesn't compare with x, c said he doesn't compare with x and z. Compile a program to find the three-team player list.

------------------------------------------------------

I really want to break my head. I have read several pieces of other people's code to barely understand this question ...... In the beginning, I just wanted to configure all possibilities (ax, ay, az, bx, by, bz, cx, cy, cz ), then, the impossible pairing is excluded based on the following conditions. This is also incorrect.

Later, I looked at other people's code and thought out the basic ideas as follows: assume that the opponents of a, B, and c are I, j, k, and I, j, k. All possible combinations must be extracted first. At the same time, two conditions must be met: I, j, and k cannot appear at the same time (that is, the opponents of a, B, and c cannot have duplicates ); a is not x, c is not x, z, as long as the two conditions are met. In short: use three for statements and two if statements. The Code is as follows:

For I in range (ord ('x'), ord ('Z') + 1): # assume that the opponent of a, B, and c is I, j, respectively, k for j in range (ord ('x'), ord ('Z') + 1): # use three for example I, j, all possible combinations of k for k in range (ord ('x'), ord ('Z') + 1): if I! = J and j! = K and k! = I: # condition 1: I, j, k cannot appear at the same time if I! = Ord ('x') and k! = Ord ('x') and k! = Ord ('Z'): # condition 2: a is not x, c is not x, z print ('a vs % s, B vs % s, c vs % s' % (chr (I), chr (j), chr (k )))

The output result is as follows:

a vs z, b vs x, c vs y

According to the above code, I think the itertools library used to solve the three-digit non-repeated ones can be simplified as follows:

import itertoolsfor i in itertools.permutations('xyz'):    if i[0] != 'x' and i[2] != 'x' and i[2] != 'z':        print('a vs %s, b vs %s, c vs %s' % (i[0], i[1], i[2]))

The output results are the same. The code is much simpler and the structure is much clearer.

Itertools. permutations () is a good thing. You can directly 'xyz' to generate a list containing all the arranged items. The other two related library methods are: itertools. product () to generate a Cartesian sequence (that is, including all the arrangement methods, itertools. combinations () to generate a list of all the combinations. For the number of items generated, product ()> permutations ()> combinations (). In this question, permutations ('xyz', 3) can generate six groups of permutation modes. If they are replaced by combinations ('xyz', 3 ), there is only one combination method ('x', 'y', 'z '). For more information, see the official documentation.

 

[Updated on ]----------------------------------------------------

Thanks to the patience of rm-rf, there is another way to solve the problem: first, list all possible arrangements (6 groups in total) for Team a, Team B, and Team c ), then, each group matches x, y, and z (using the zip () method) and sets the judgment conditions. It turns out that only one of the six groups meets the conditions. The Code is as follows:

import itertoolsteam_1 = ['a', 'b', 'c']team_2 = ['x', 'y', 'z']for i in itertools.permutations(team_1, 3):    for j in zip(i, team_2):        if j in [('a','x'),('c','x'),('c','z')]:            break    else:        print(i, team_2)

The output result is as follows:

('b', 'c', 'a') ['x', 'y', 'z']

Rm-rf also provides another method, ...... Well, I haven't learned lambda yet, and I haven't learned much about user-defined functions yet ...... But thanks! I hope it will not be long before I can understand it ~~~

import itertoolscheck_list = [('a', 'x'), ('c', 'x'), ('c', 'z')]for i in itertools.permutations(team_1, 3):    f = lambda a,b: len([True for j in zip(a, b) if j not in check_list])    if f(i, team_2) == 3:        print(i, team_2)

 

++

Source: getting started with programming languages: 100 typical examples [Python]

Related Article

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.