How to find friends

Source: Internet
Author: User
How to find friends

 

Simple thinking and hard Coding

 1 def check_connection(network, first, second): 2     link_dictionary = dict() 3  4     for link in network: 5         drones = link.split(‘-‘) 6  7         link_dictionary.setdefault(drones[0], []).append(drones[1]) 8         link_dictionary.setdefault(drones[1], []).append(drones[0]) 9 10     future = []11     visited = []12     future.append(first)13 14     while future:15         current = future.pop()16         visited.append(current)17 18         extend = link_dictionary[current]19 20         if second in extend:21             return True22 23         for each in extend:24             if each not in visited:25                 future.append(each)26 27     return False

Use dict to store the direct relationship of each person, such as {lyly: [Lala, Gege]}. Use two lists, one of which indicates the person who has already traversed, and the other that is going to traverse;

In addition, two-dimensional arrays in python can be defined as follows: Connection = [[false for col in range (5)] For row in range (5)], which is a 5*5 array, I tried to define error = [[false] * 5] * 5 in this way. I found that I only needed to modify it.

Error [0] [0], then error [1] [0], error [2] [0]... because [false] * 5 generates a one-dimensional array, and * 5 generates only five references. Therefore, no matter which one is modified, the remaining one changes.

 

Measure the test taker's knowledge about the code.

 1 def check_connection(network, first, second): 2     setlist = [] 3     for connection in network: 4         s = ab = set(connection.split(‘-‘)) 5         # unify all set related to a, b 6         for t in setlist[:]: # we need to use copy 7             if t & ab:       # check t include a, b 8                 s |= t 9                 setlist.remove(t)10         setlist.append(s)    # only s include a, b11 12     return any(set([first, second]) <= s for s in setlist)

Use each link as a set. If the intersection between links is not empty, calculate the Union of links, that is, the link circle;

Finally, check whether first and second are in a link circle;

Make full use of the set operation, & intersection, | and, <= subset;

There are also 12th rows of syntax features

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.