Find compilation order


  • 3

    Input:

    target0.go: [target1.go, target2.go, target3.go, target4.go]
    target1.go [ target5.go, target6.go, target4.go]
    target2.go: [target9.go, target7.go, target4.go]
    target3.go [target6.go, target4.go]

    Find the order in which a compiler will build these files

    Rule:

    1. If two files that are not dependent on each other then the order does not matter.
    2. Before building a file, all its dependent modules should be built

    Output:
    A order of compilation given the list of dependent modules


  • 0
    def compileOrder(targets):
        stack = []
        visited = {}
        def defOrder(key):       
            visited[key] = True
            if key in targets:
                for item in targets[key]:
                    if item not in visited :
                        defOrder(item)    
            stack.append(key)
               
        for key in targets.keys():
            if key not in visited :
                defOrder(key)
       
        print (stack[0:])
        
    
    def testcompileOrder():
        targets  =  {'target0.go': ['target1.go', 'target2.go','target3.go','target4.go'],'target1.go': ['target5.go', 'target6.go','target4.go'],
                  'target2.go': ['target9.go', 'target7.go','target4.go'],'target3.go': ['target6.go', 'target4.go']}
     
        compileOrder(targets)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.