Please find an expert in algorithms to solve this problem
import itertools
from pystachio import Empty, List
from twitter.common.lang import Compatibility
from .schema_base import GB, Constraint, Process, Resources, Task
_all_ = (
# shorthand for process ordering constraint
'order',
# task combinators
'combine_tasks', # merge N tasks in parallel
'concat_tasks', # serialize N tasks
# options helpers
'java_options',
'python_options',
# the automatically-sequential version of a task
'SequentialTask',
# create a simple task from a command line + name
'SimpleTask',
# helper classes
'Options',
'Processes',
'Tasks',
'Units',
)
class Units(object):
"""Helpers for base units of Tasks and Processes."""
@classmethod
def safe_get(cls, unit):
return 0 if unit is Empty else unit.get()
@classmethod
def optional_resources(cls, resources):
return Resources() if resources is Empty else resources
@classmethod
def resources_sum(cls, *resources):
"""Add two Resources objects together."""
def add_unit(f1, f2):
return cls.safe_get(f1) + cls.safe_get(f2)
def add(r1, r2):
return Resources(cpu=add_unit(r1.cpu(), r2.cpu()),
ram=add_unit(r1.ram(), r2.ram()),
disk=add_unit(r1.disk(), r2.disk()),
gpu=add_unit(r1.gpu(), r2.gpu()))
return reduce(
add,
map(cls.optional_resources, resources),
Resources(cpu=0, ram=0, disk=0, gpu=0))
@classmethod
def finalization_wait_sum(cls, waits):
"""Return a finalization_wait that is the sum of the
inputs"""
return sum(map(cls.safe_get, waits))
@classmethod
def resources_max(cls, resources):
"""Return a Resource object that is the maximum of the inputs along
each
resource dimension."""
def max_unit(f1, f2):
return max(cls.safe_get(f1), cls.safe_get(f2))
def resource_max(r1, r2):
return Resources(cpu=max_unit(r1.cpu(), r2.cpu()),
ram=max_unit(r1.ram(), r2.ram()),
disk=max_unit(r1.disk(), r2.disk()),
gpu=max_unit(r1.gpu(), r2.gpu()))
return reduce(
resource_max,
map(cls.optional_resources, resources),
Resources(cpu=0, ram=0, disk=0, gpu=0))
@classmethod
def finalization_wait_max(cls, waits):
"""Return a finalization_wait that is the maximum of the
inputs"""
return max([0] + map(cls.safe_get, waits))
@classmethod
def processes_merge(cls, tasks):
"""Return a deduped list of the processes from all tasks."""
return list(set(itertools.chain.from_iterable(task.processes() for
task in tasks)))
@classmethod
def constraints_merge(cls, tasks):
"""Return a deduped list of the constraints from all
tasks."""
return list(set(itertools.chain.from_iterable(task.constraints()
for task in tasks)))
class Processes(object):
"""Helper class for Process objects."""
@classmethod
def _process_name(cls, process):
if isinstance(process, Process):
return process.name()
elif isinstance(process, Compatibility.string):
return process
raise ValueError("Unknown value for process order: %s" %
repr(process))
@classmethod
def order(cls, *processes):
"""Given a list of processes, return the list of constraints that
keeps them in order, e.g.
order(p1, p2, p3) => [Constraint(order=[p1.name(), p2.name(),
p3.name()])].
Similarly, concatenation operations are valid, i.e.
order(p1, p2) + order(p2, p3) <=> order(p1, p2, p3)
"""
return [Constraint(order=[cls._process_name(p) for p in
processes])]
class Tasks(object):
"""Helper class for Task objects."""
SIMPLE_CPU = 1.0
SIMPLE_RAM = 1 * GB
SIMPLE_DISK = 1 * GB
@classmethod
def _combine_processes(cls, *tasks):
"""Given multiple tasks, merge their processes together, retaining
the identity of the first
task."""
if len(tasks) == 0:
return Task()
head_task = tasks[-1]
return head_task(processes=Units.processes_merge(tasks))
@classmethod
def combine(cls, *tasks, **kw):
"""Given multiple tasks, return a Task that runs all processes in
parallel."""
if len(tasks) == 0:
return Task()
base = cls._combine_processes(*tasks)
return base(
resources=Units.resources_sum(*(task.resources() for task in
tasks)),
constraints=Units.constraints_merge(tasks),
finalization_wait=Units.finalization_wait_max(task.finalization_wait()
for task in tasks),
**kw
)
@classmethod
def concat(cls, *tasks, **kw):
"""Given tasks T1...TN, return a single Task that runs all
processes such that
all processes in Tk run before any process in Tk+1."""
if len(tasks) == 0:
return Task()
base = cls._combine_processes(*tasks)
base_constraints = Units.constraints_merge(tasks)
# TODO(wickman) be smarter about this in light of existing
constraints
for (t1, t2) in zip(tasks[0:-1], tasks[1:]):
for p1 in t1.processes():
for p2 in t2.processes():
if p1 != p2:
base_constraints.extend(Processes.order(p1, p2))
return base(
resources=Units.resources_max(task.resources() for task in
tasks),
constraints=base_constraints,
finalization_wait=Units.finalization_wait_sum(task.finalization_wait()
for task in tasks),
**kw)
@classmethod
def simple(cls, name, command):
"""Create a usable Task from a provided name + command line and a
default set of resources"""
return Task(
name=name,
processes=[Process(name=name, cmdline=command)],
resources=Resources(cpu=cls.SIMPLE_CPU,
ram=cls.SIMPLE_RAM,
disk=cls.SIMPLE_DISK))
@classmethod
def sequential(cls, task):
"""Add a constraint that makes all processes within a task run
sequentially."""
def maybe_constrain(task):
return {'constraints': order(*task.processes())} if
task.processes() is not Empty else {}
if task.constraints() is Empty or task.constraints() ==
List(Constraint)([]):
return task(**maybe_constrain(task))
raise ValueError('Cannot turn a Task with existing constraints into
a SequentialTask!')
class Options(object):
"""Helper class for constructing command-line arguments."""
@classmethod
def render_option(cls, short_prefix, long_prefix, option,
value=None):
option = '%s%s' % (short_prefix if len(option) == 1 else
long_prefix, option)
return '%s %s' % (option, value) if value else option
@classmethod
def render_options(cls, short_prefix, long_prefix, *options,
**kw_options):
renders = []
for option in options:
if isinstance(option, Compatibility.string):
renders.append(cls.render_option(short_prefix, long_prefix,
option))
elif isinstance(option, dict):
# preserve order in case option is an OrderedDict, rather than
recursing with **option
for argument, value in option.items():
renders.append(cls.render_option(short_prefix, long_prefix,
argument, value))
else:
raise ValueError('Got an unexpected argument to render_options: %s'
% repr(option))
for argument, value in kw_options.items():
renders.append(cls.render_option(short_prefix, long_prefix,
argument, value))
return renders
@classmethod
def java(cls, *options, **kw_options):
"""
Given a set of arguments, keyword arguments or dictionaries,
render
command-line parameters accordingly. For example:
java_options('a', 'b') == '-a -b'
java_options({
'a': 23,
'b': 'foo'
}) == '-a 23 -b foo'
java_options(a=23, b='foo') == '-a 23 -b foo'
"""
return ' '.join(cls.render_options('-', '-', *options,
**kw_options))
@classmethod
def python(cls, *options, **kw_options):
"""
Given a set of arguments, keyword arguments or dictionaries,
render
command-line parameters accordingly. Single letter parameters
are
rendered with single '-'. For example:
python_options('a', 'boo') == '-a --boo'
python_options({
'a': 23,
'boo': 'foo'
}) == '-a 23 --boo foo'
python_options(a=23, boo='foo') == '-a 23 --boo foo'
"""
return ' '.join(cls.render_options('-', '--', *options,
**kw_options))
def SimpleTask(name, command):
"""A simple command-line Task with default resources"""
return Tasks.simple(name, command)
def SequentialTask(*args, **kw):
"""A Task whose processes are always sequential."""
return Tasks.sequential(Task(*args, **kw))
python_options = Options.python
java_options = Options.java
combine_tasks = Tasks.combine
concat_tasks = Tasks.concat
order = Processes.order
HHackerRank Spirinkle: Unfulfilled Orders Ms. Sugar has decided to use her spirinkle idea as a re...
4-54. Special Orders (QLO 4-1, © 2) Sherene Nili manages a company that produces wedding gowns. She produces both a custom product that is made to order and a standard product that is sold in bridal salons. Her accountant prepared the following forecasted income statement for March, which is a busy month. Custom Dresses Standard Dresses 10 20 $50,000 $30,000 $10,000 $ 8,000 20,000 9,000 600 4,200 2,800 1,000 600 Number of dresses Sales revenue Materials Labor Machine depreciation Rent...
Wally's Widget Warehouse takes orders from 7 A.M. to 7 P.M. The manager wants to analyze the process and has provided the process flow diagram shown below. There are three steps required to ship a customer order. The first step is to take the order from a customer. The second step is to pick the order for the customer and then they have to pack the order ready for shipping. Wally promises that every order placed today gets shipped tomorrow....
[for Python 3 class; must use stdin and stdout for IO; for libraries, must import by "from x import y"] The Task Feeling sorry for all the mischief he has caused recently, Hugh Manatee has agreed to help Professor Smith stock pile bundles of seagrass at one of several docks along the Indian River Lagoon. There are N (1 <= N <= 1,000,000, N) docks conveniently numbered 1..N, initially all of them have no bundles. Professor Smith then gives Hugh...
Sherene Nili manages a company that produces wedding gowns. She
produces both a custom product that is made to order and a standard
product that is sold in bridal salons. Her accountant prepared the
following forecasted income statement for March, which is a busy
month:
Ms. Nili already has orders for the 10 custom dresses reflected
in the March forecasted income statement. The depreciation charges
are for machines used in the respective product lines. Machines
depreciate at the rate of...
Inventory management. A computer store sells and maintains an
inventory of fax machines (units). On average, one unit is sold per
day, but the store experiences a burst of customers on some days. A
marketing survey suggests that customer interarrival times are iid
exponentially distributed with rate 1 per day.
When
the inventory on hand drops down to 5 units, an order of 20 units
is placed with the supplier, and the lead time is uniformly
distributed between 5 and...
Sherene Nili manages a company that produces wedding gowns. She produces both a custom product that is made to order and a standard product that is sold in bridal salons. Her accountant prepared the following forecasted income statement for March, which is a busy month: Custom Dresses Standard Dresses Total Number of dresses 10 20 30 Sales revenue $ 52,500 $ 32,500 $ 85,000 Materials $ 10,500 $ 8,500 $ 19,000 Labor 20,500 9,500 30,000 Machine depreciation 650 350 1,000...
Sherene Nili manages a company that produces wedding gowns. She produces both a custom product that is made to order and a standard product that is sold in bridal salons. Her accountant prepared the following forecasted income statement for March, which is a busy month: Custom Dresses Standard Dresses Total Number of dresses 10 20 30 Sales revenue $ 51,000 $ 31,000 $ 82,000 Materials $ 10,200 $ 8,200 $ 18,400 Labor 20,200 9,200 29,400 Machine depreciation 620 320 940...
Sherene Nili manages a company that produces wedding gowns. She produces both a custom product that is made to order and a standard product that is sold in bridal salons. Her accountant prepared the following forecasted income statement for March, which is a busy month: Custom Dresses Standard Dresses Total Number of dresses S55.000 S 11.000 21.000 Walerials Labar Machine depreciation Rent Heal and light Other production costs Marketing and administration Total costs Operating profit $35,000 $ 9,000 10,000 400...
Sherene Nili manages a company that produces wedding gowns. She produces both a custom product that is made to order and a standard product that is sold in bridal salons. Her accountant prepared the following forecasted income statement for March, which is a busy month: Custom Dresses Standard Dresses Total Number of dresses 10 20 30 Sales revenue $ 47,500 $ 27,500 $ 75,000 Materials $ 9,500 $ 7,500 $ 17,000 Labor 19,500 8,500 28,000 Machine depreciation 550 250 800...
- $30.000 Sherene Nil manages a company that produces wedding gowns. She produces both a custom product that is made to order and a standard product that is sold in bridal salons. Her accountant prepared the following forecasted income statement for March which is a busy month Custom Dresses Standard Dresses Number of dresses $50.000 $120.000 $10.000 S8000 $ 18,000 20,000 Machine depreciation 4.200 2.800 Heat and light Other production con Marketing and administration 7.700 Total $ 67000 Operating profit...