0
votes

I have an algorithm problem to solve. Description: I have a python dictionary that contains following

modules = {"auth_provider_1": [3, **4**, 17, 19],
         "auth_provider_2": [1, 6, **8**, 10, 13, 14, 16, 18],
         "auth_provider_3": [**0**, 7, 11, 12, 15],
         "auth_provider_4": [**2**, 5, 9],
         "cont_provider_1": [**4**, 14],
         "cont_provider_2": [**8**, 9, 13, 15, 16, 17],
         "cont_provider_3": [**2**, 3, 5, 10, 11, 18],
         "cont_provider_4": [**0**, 1, 6, 7, 12, 19]}

There are two types of modules, auth_provider, and cont_provider. Each one has 4 different providers. For example, we have 4 auth_providers; auth_provider_1, auth_provider_2, auth_provider_3 and auth_provider_4. Each provider has a list that contains which user is using that provider. each user is using only one auth provider and only one cont provider. For example; user 3 is using auth_provider_1 and cont_provider_3 user 1 is using auth_provider_2 and cont_provider_4.. and so on.

Problem: We want to check all providers with a minimum of a user group. For example, if we want to check what providers are users 0,2,4,8 using we will be checking all providers available. Same way with the users 0,2,4,16 and 0,2,4,13...etc

What I tried: Making a list from provider names sorted by their list length. For example;

sorted_list = ['cont_provider_1', 'auth_provider_4', 'auth_provider_1', 'auth_provider_3', 'cont_provider_2', 'cont_provider_3', 'cont_provider_4', 'auth_provider_2']

iterating this sorted list, searching each element of its list in the modules dictionary and when its found in a list I removed the key(provider_name) of that list from the sorted list. for example from a sorted list, the first element is cont_provider_1 which has the smallest list length (it has only 4 and 14), I wanted to start from the smallest one because I thought it would make more sense. Then I search cont_provider_1 in the modules dictionary. But when I find the 4 and auth_provider_1 and cont_provider_1 somehow iteration stuck somewhere and give me this answer

sorted_list_after_iteration_is_over = ['auth_provider_4']
min_users = [4, 14, 0, 7, 11, 12, 15]

Question: What would be the best algorithm for this problem? Am I on the right track? Where I am doing wrong? Any suggestions or help?

here is my whole code

modules = {"auth_provider_1": [3, 4, 17, 19],
         "auth_provider_2": [1, 6, 8, 10, 13, 14, 16, 18],
         "auth_provider_3": [0, 7, 11, 12, 15],
         "auth_provider_4": [2, 5, 9],
         "cont_provider_1": [4, 14],
         "cont_provider_2": [8, 9, 13, 15, 16, 17],
         "cont_provider_3": [2, 3, 5, 10, 11, 18],
         "cont_provider_4": [0, 1, 6, 7, 12, 19]}

providers_sorted_list = sorted(modules, key = lambda key: len(modules[key]))
# ['cont_provider_1', 'auth_provider_4', 'auth_provider_1', 'auth_provider_3', 'cont_provider_2', 'cont_provider_3', 'cont_provider_4', 'auth_provider_2']
test_users = []
for provider in providers_sorted_list:
    search_list = modules[provider]
    for user in search_list: 
        for key, val in modules.items():
            if user in val:
                if not user in test_users:
                    test_users.append(user)
                if key in providers_sorted_list:
                    providers_sorted_list.remove(key)
print(providers_sorted_list) 
print(test_users)