Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#@title ` AND _ OR _ Scheduler ` implementation class AND _ OR _ Scheduler ( object ) : def _ _ init _ _
#@title ANDORScheduler implementation
class ANDORSchedulerobject:
def initself:
# It is up to you to implement the initialization.
### YOUR SOLUTION HERE
self.tasks # Dictionary to store tasks and their dependencies
self.completed set # Set to store completed tasks
self.available set # Set to store tasks available for execution
def addandtaskself t dependencies:
Adds an AND task t with given dependencies."""
### YOUR SOLUTION HERE
self.taskstsetdependencies 'and' # Store task with its dependencies and type
self.available.updatedependencies # Update available tasks with dependencies
def addortaskself t dependencies:
Adds an OR task t with given dependencies."""
### YOUR SOLUTION HERE
self.taskstsetdependenciesor # Store task with its dependencies and type
self.available.updatedependencies # Update available tasks with dependencies
@property
def doneself:
### YOUR SOLUTION HERE
return lenselftasks lenselfcompleted
@property
def availabletasksself:
Returns the set of tasks that can be done in parallel.
A task can be done if:
It is an AND task, and all its predecessors have been completed, or
It is an OR task, and at least one of its predecessors has been completed.
And of course, we don't return any task that has already been
completed."""
### YOUR SOLUTION HERE
return self.available self.completed
def markcompletedself t:
Marks the task t as completed, and returns the additional
set of tasks that can be done and that could not be
previously done once t is completed."""
### YOUR SOLUTION HERE
self.completed.addt # Mark task as completed
addedtasks set
for task, details in self.tasks.items: # Iterate through tasks
deps, tasktype details
if tasktype 'and' and alld in self.completed for d in deps or
tasktype or and anyd in self.completed for d in deps:
self.available.addtask # Add task to available if all dependencies are completed
addedtasks.addtask
return addedtasks
def showself:
You can use the nx graph to display the graph. You may want to ensure
that you display AND and OR nodes differently."""
### YOUR SOLUTION HERE
andtasks k for k v in self.tasks.items if v 'and' # Extract AND tasks
ortasks k for k v in self.tasks.items if vor # Extract OR tasks
printAND Tasks:", andtasks
printOR Tasks:", ortasks
s ANDORScheduler
saddandtaskabc
assert savailabletasks bc
r smarkcompletedb
assert r set
assert savailabletasks c
r smarkcompletedc
assert r a
assert savailabletasks a
r smarkcompleteda
assert r set
assert savailabletasks set
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started