I am given a question as follows:
B = C, D
A = B
C = E
meaning B is dependent on variable C and D, while A is dependent on B, and so on. Variable E and D is independent.
Input of 'A' should then return:
E, C, D, B, A
listing all dependent variables. To solve this problem, I first started off by defining a dictionary to iterate this condition easily:
letter = {'A' : ['B'], 'B' : ['C', 'D'], 'C' : ['E']}
However, I am now stuck on how I should loop this in order to print all the children efficiently. I believe this is wrong but I think I may be going in the right direction:
def problem(value):
letter = {'A' : ['B'], 'B' : ['C', 'D'], 'C' : ['E']}
for i in letter:
if i != value:
continue
if len(letter[i]) > 1:
for k in letter:
print("".join(letter[k]), k)
print("".join(letter[i]), i)
Please help!