-1

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
    did you try something? – barunsthakur Jun 25 '15 at 18:31
  • I am planning to take the input in a map. May you please explain me what logic should I apply. – Sunshine Jun 25 '15 at 18:31
  • 1
    Stackoverflow is not a site, where you post a problem and get code in return. Please show us your attempt and we may help you understanding the concepts, you do not understand. – Turing85 Jun 25 '15 at 18:35
  • possible duplicate of [Sample Directed Graph and Topological Sort Code](http://stackoverflow.com/questions/2739392/sample-directed-graph-and-topological-sort-code) – fabian Jun 25 '15 at 18:37

2 Answers2

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.

  • Thanks for the response. I need help in logic : – Sunshine Jun 26 '15 at 01:43
  • I have a doubt , I can not make a tree, because as per input , there will be many independent trees as 1) kittenservice -> CamelCaser 2) ice->Cyberportal -> leetmeme -> camelcaser -> fraudstream – Sunshine Jun 26 '15 at 02:00
  • class Solution { Map input=new HashMap(); List output=new LinkedList(); public void getInput() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = in.readLine()) != null && s.length() != 0) { if(s.split(":").length>1) input.put(s.split(":")[0].trim(),s.split((":"))[1].trim()); else input.put(s.split(":")[0].trim(),null); } } } – Sunshine Jun 26 '15 at 03:14
  • public class PackageInstaller { public static void main(String args[]) throws IOException { Solution solution=new Solution(); solution.getInput(); } } – Sunshine Jun 26 '15 at 03:14
  • I have taken the input and stored in a map. I will now apply the logic in another function which will return the list of string as output. – Sunshine Jun 26 '15 at 03:15
  • @Sunshine for the multiple trees: my mistake, should've read the constraints more precise, though this shouldn't be a problem. Just generate multiple trees - actually you could aswell implicitly use the map for the structure - iterate over the trees and apply the method to each of the trees. For the code: pls don't put code into comments, thats just cruel (looks correct so far though). –  Jun 26 '15 at 07:26
0

Get Package List from input String Array:

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;
}

Invocation Part (Input and output) :

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 + " ,");        
}
Rajesh
  • 2,135
  • 1
  • 12
  • 14
  • Hi Rajesh, Thanks for the above solution. Unfortunately it is not giving the solution as expected. I can code my self, if someone could explain me the logic. – Sunshine Jun 26 '15 at 16:12
  • I have updated code..go through it and run it. In case, I have missed something to capture from your inputs, let me know. – Rajesh Jun 26 '15 at 18:00