1

In the exercice i´m developing bellow, i´m applying the chain of responsibility pattern. The pattern works correctly but i don´t think it's the best approach. I think the rule design pattern should be a better fit but there´s no much good examples about it using python.

My questions for you are:

  1. Do you think this could be done with other pattern?
  2. Is the chain of responsibility pattern acceptable for this?
  3. Should i study the Rule design (in other languages if i have to) to apply in python?

The exercice is the following:

Supose you have Patients with health conditions such as:

  1. F: Fever
  2. H: Healthy
  3. D: Diabetes
  4. T: Tuberculosis

In this exercice drugs are provided to all patients. It is not possible to target a specific patient. This is the list of available drugs:

  1. As: Aspirin
  2. An: Antibiotic
  3. Insulin
  4. Paracetamol

Drugs can change patients’ states. They can cure, cause side effects or even kill a patient if not properly prescribed. Drugs effects are described by the following rules:

  1. Aspirin cures Fever;
  2. Antibiotic cures Tuberculosis;
  3. A sick patient not receiving the right medicines remains sick, if not explicitly mentioned otherwise;
  4. Insulin prevents diabetic subject from dying, does not cure Diabetes;
  5. If insulin is mixed with antibiotic, healthy people catch Fever;
  6. Paracetamol cures Fever;
  7. Paracetamol kills subject if mixed with aspirin;

Input

Parameter 1

List of patients' health status codes, separated by a comma. e.g. “D,F,F” means we have 3 patients, one with diabetes and two with fever.

Parameter 2

List of drugs codes, separated by a comma, e.g. “As,I” means patients will be treated with Aspirin and Insulin.

Output

The result should be sent to stdout. It should be a comma separated string with number of patients with a given state, following the format: F:NP,H:NP,D:NP,T:NP,X:NP (NP:number of people)

New contributor
Andre Garcia is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

I think the rule design pattern can work here. What you need is a model of the patiences state that doesn't care about the order in which the rules are applied.

For example, if you process the rule that aspirin cures fever before processing a rule that causes a fever you end up with a fever when you shouldn't.

So instead of fever being a binary state (fever/no fever), model it as caught or not and cured or not.

Then only after all other rules are applied do you evaluate if the patient has a fever.

Your Answer

Andre Garcia is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.