Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mysticBliss/7204210bd9f53299f1ae842b8b134521 to your computer and use it in GitHub Desktop.
Save mysticBliss/7204210bd9f53299f1ae842b8b134521 to your computer and use it in GitHub Desktop.
Python Interview Question #1
# Given a two dictionaries empdesig and empdet containing details about employees and managers in a key value pair - print a hierarchical tree
# empdesig = {'Simha': 'Khan', 'Khan': '', 'Vijay': 'Simha', 'Sai': 'Khan', 'Kiran': 'Kiran'}
# empdet = {'Simha': 'Khan', 'Khan': '', 'Vijay': 'Simha', 'Sai': 'Khan', 'Kiran': 'Kiran'}
# If an employee has manager null(like Khan) or has manager as himself - then they are board members and have no bosses.
for emp,mgr in empdesig.items():
new[emp] = {key:'' for key,value in empdesig.items() if value == emp}
for k,v in new.items():
if v != {}:
for ele in v.keys():
if ele in new.keys():
if ele != k:
new[k][ele] = new[ele]
del new[ele]
else:
new[ele] = {}
def pretty(d, indent=0):
for key, value in d.iteritems():
print '-' * indent + str(key) + ' ' + ' '.join(empdet[key])
if isinstance(value, dict):
pretty(value, indent+1)
else:
print '-' * (indent+1) + str(value) + ' ' + ' '.join(empdet[key])
pretty(new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment