I want to find shareholder relationships between companies.
in the below example ‘Person 1’ have directly 50% of ‘Company 1’ shares then need to check if ‘Company 1’ also have share’s of other companies. ‘Company 1’ have 50% of ‘Company 2’ share and 20% of ‘Company 3’. it means ‘Person 1’ indirectly have some share in ‘Company 2’ and ‘Company 3’.
The desired output in this example is:
Company 1 : Person 1 , 0.5
Company 2 : Company 1 , 0.5
Company 3 : Company 2 , 0.2
This is my code:
companies = {
'Company 1': {
'Person 1': 0.5,
'Person 2': 0.3,
'Person 3': 0.2,
},
'Company 2': {
'Person 4': 0.25,
'Company 1': 0.5,
'Person 5': 0.25,
},
'Company 3': {
'Person 5': 0.6,
'Person 6': 0.2,
'Company 2': 0.2,
},
}
def is_shareholder_in_company(company, companies, shareholder_name):
company_shareholders = companies[company]
for shareholder, ownership_percentage in company_shareholders.items():
if shareholder_name == shareholder:
print(company, ':',shareholder, ',', ownership_percentage)
return True
for i in companies:
if is_shareholder_in_company(i, companies, 'Person 1'):
temp_name = i
for j in companies:
if is_shareholder_in_company(j, companies, i):
temp_name = j
for x in companies:
if is_shareholder_in_company(x, companies, j):
temp_name = x
how to refactor nested if’s? the count of nested if’s is dependent on ‘companies’ data and it is unknown.
thanks.