Skip to content

Instantly share code, notes, and snippets.

@rodchile
Created March 6, 2015 19:34
Show Gist options
  • Save rodchile/826df9cdef6d94d79b2f to your computer and use it in GitHub Desktop.
Save rodchile/826df9cdef6d94d79b2f to your computer and use it in GitHub Desktop.
package net.rodrigogarcia.net.Trees;
import java.io.*;
import java.util.*;
public class Solution {
static class Part {
public String getName() { return "X"; }
}
static class Operation {
private String name_;
public Operation(final String name) { name_ = name; }
public String getName() { return name_; }
public void operate(Part p) {
System.out.println("Operation " + name_ + " on part " + p.getName());
}
public boolean equals(Operation op)
{
return getName().equals(op.getName());
}
}
static class StepManager {
// Declare the interface in the property and hide its implementation
static Set<Operation> operations = new HashSet<Solution.Operation>();
static Map<Operation, ArrayList<String>> dependencies = new HashMap<Operation, ArrayList<String>>();
public void addOperation(final Operation operation, final String[] prerequisites) {
//TimeComplexity: BigO(1)
//The set protect us to add repeat operations
if (operations.add(operation))
{
//Create ArrayList of dependencies
ArrayList<String> operatationDependencies = new ArrayList<String>(Arrays.asList(prerequisites));
dependencies.put(operation, operatationDependencies);
}
}
public void performOperation(String operationName, Part p) {
// Add your implementation here. When this method is called,
// you must call the 'operate()' method for the named operation,
// as well as all prerequisites for that operation.
Operation tempOperation = new Operation(operationName);
if (!operations.contains(tempOperation))
{
System.out.println("The operation doesn't exists");
}
//My idea was that to call navigateTree but ran out of time - My brain was specially rusty today
//My initial estimation for the algorithm was BigO(LogN) because its a tree
}
private void navigateTree(String opName, ArrayList<String>localDependencies)
{
}
}
public static void main(String[] args) throws Exception {
StepManager manager = new StepManager();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = in.readLine()) != null && s.length() != 0) {
if (s.startsWith("#")) {
continue;
}
String[] parts = s.split(",");
manager.addOperation(new Operation(parts[0]), Arrays.copyOfRange(parts, 1, parts.length));
}
manager.performOperation("final", new Part());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment