Credit Card Recommended User List Data Post 01

Title Description
Credit cards now carry out marketing activities, and customers holding our credit cards recommend new customers to run cards. After successful opening, they will receive credit awards.Provides that each customer can recommend up to two new households and that a new household can only be recommended once.However, the link effect is allowed, that is, if customer A recommends new house B and new house B recommends new house C, the customer belongs to the recommended list of both A and B.For simplicity, consider only one old customer A as a starting point recommendation.Programmatic calculations recommend a list of customers with no less than n new customers.
Input description of ^
The first behavior of the input is a space-separated two positive integers, the first representing the number m of the original recommended list and the second representing the value of n.
Each subsequent m-line is a space-separated list of original recommendations, with the first column as the referee and the second two as the referee. If the referee only recommends a new home, the third column is replaced by *.The recommended person and the recommended person are all in capital letters, and different letters represent different people.
Output Description:
Output a list of eligible customers on the same line, out of order, separated by spaces.If the customer list is empty, output None.See auspicious examples for details.

Example 1

Input:

5 2

A B C

C F *

B D E

D G *

E H I

output

A B E

Idea: Traverse in python using dict.Don't forget n=0.

line1 = input()
line_num = int(line1.split()[0])
n = int(line1.split()[1])
#print(n)
line_list = []
for i in range(line_num):
    line = input()
    line_list.append(line)

if n == 0:
    result = []
    for user_list in line_list:
        user1, user2, user3 = user_list.split()
        if user1 not in result:
            result.append(user1)
        if user2 != "*":
            result.append(user2)
        if user3 != "*":
            result.append(user3)
    if result == []:
        outline = "None"
    else:
        outline = " ".join(result)
    print(outline)

else:
    dict = {}
    for user_list in line_list:
        user1, user2, user3 = user_list.split()
        dict.setdefault(user1, [])
        if user2 != "*":
            dict[user1].append(user2)
        if user3 != "*":
            dict[user1].append(user3)
    #print(dict)
    keys = dict.keys()
    for user1,list in dict.items():
        for item in list:
            if item in keys:
                dict[user1].extend(dict[item])
    #print(dict)

    result = []
    for user1 in dict:
        if len(dict[user1]) >= n:
            result.append(user1)
    #print(result)
    if result == []:
        outline = "None"
    else:
        outline = " ".join(result)
    print(outline)

Tags: less Python

Posted on Sun, 01 Dec 2019 10:11:10 -0800 by edcaru