python - Ruby on Rails : Dependencies and Nodes -


i have simple problem needs solving. have code sorts out tasks dependant on other tasks. examaple tasks a, b, c, , d. these need sorting in order of dependancies. example, task b can completed after task d done. therefore order of tasks :

  • a
  • d
  • b
  • c

i have code in python, there way make code work in rails (as in correct syntax/method)

class node: def __init__(self, name):   self.name = name   self.edges = []  def addedge(self, node):   self.edges.append(node) 

require 'set'  class circulardependencieserror < exception end  class task   attr_reader :name, :depends    def initialize(task_name, dependencies=[])     @name = task_name     @depends = set.new dependencies   end    def add_dependencies(*d)     @depends.merge d    end    def <=>(rhs)     if depends.include?(rhs.name) , rhs.depends.include?(name)        raise circulardependencieserror,             "#{name} , #{rhs.name} depend on each other"     elsif rhs.depends.include? name        -1     elsif depends.include? rhs.name       1     else        0     end   end end  tasks = [   task.new("a", ["b", "c"]),   task.new("b", ["c", "d"]), ]  t = task.new("c") t.add_dependencies("d", "e") tasks << t  p tasks p tasks.sort  --output:-- [#<task:0x000001018c0530 @name="a", @depends=#<set: {"b", "c"}>>,  #<task:0x000001018c0350 @name="b", @depends=#<set: {"c", "d"}>>,  #<task:0x000001018c01c0 @name="c", @depends=#<set: {"d", "e"}>>]  [#<task:0x000001018c01c0 @name="c", @depends=#<set: {"d", "e"}>>,  #<task:0x000001018c0350 @name="b", @depends=#<set: {"c", "d"}>>,  #<task:0x000001018c0530 @name="a", @depends=#<set: {"b", "c"}>>] 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -