Java program for package installer

时间:2015-06-25 18:30:39

标签: java

May you please help in writing a java program which should accept an array of strings defining dependencies. Each string contains the name of a package followed by a colon and space, then any dependencies required by that package. For simplicity we’ll assume a package can have at most one dependency. The program should output a comma separated list of package names in the order of install, such that a package’s dependency will always precede that package. The program should reject as invalid a dependency specification that contains cycles.

Example of valid input KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Leetmeme Ice:

A valid output for the above would be:

KittenService, Ice, Cyberportal, Leetmeme, CamelCaser, Fraudstream

Example of input that should be rejected (contains cycles) KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Ice: Leetmeme

2 个答案:

答案 0 :(得分:0)

First of all: SO is not a coding-service. Show us what you've got and where the problem lies and you'll get help with it. Now for the solution - I won't code anything and I hope noone else does. Since the dependencies don't contain any cycles, the structure is a tree. Just traverse the tree postorder and import each package in this order.

答案 1 :(得分:0)

从输入字符串数组中获取包列表:

public static List<String> getPackages(String[] intputStrings,
        String inputDelimiter, char wordEndChar, String rejectWord,
        String outputDelimiter) {

    List<String> al = new ArrayList<String>();

    for (String inputString : intputStrings) {

        if (!(inputString.indexOf(rejectWord) > 0)) {

            for (String string : inputString.split(inputDelimiter)) {
                int idx = string.indexOf(wordEndChar);
                if (idx > 1)
                    al.add(string.substring(0, idx));
            }
        }
    }

    return al;
}

调用部分(输入和输出):

public static void main(String[] args) {

String intputString[] = {
        "KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Ice: Leetmeme",
        "(contains cycles) KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Ice: Leetmeme",
        "One: Two: x Three: Ice CamelCaser: KittenService Fraudstream: Five: Leetmeme" };

List<String> s = getPackages(intputString, " ", ':', " cycles", ", ");

for (String string : s)
    System.out.print(string + " ,");        
}