Created
March 10, 2015 19:02
-
-
Save vijaysharmay/520c3c4187ce4fb08d68 to your computer and use it in GitHub Desktop.
Python Interview Question #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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