算法编写-检查数据库中的两种药物是否冲突

问题描述 投票:0回答:1

我正在编写一种算法,用于检查用户输入的药物是否已知发生碰撞。我有一个毒品数据库和一个将要使用的毒品冲突数据库。

用户可以输入任意数量的药物,这些药物存储在名为model的数组中因此,如果用户输入:对乙酰氨基酚,布洛芬和阿斯普林,则模型数组将在索引0、1和2处包含它们。

NB:

-numdrug = the size of the model
-listOfAllMeds2 = all of the medications in the database
-this.getcollsionList(int id) takes in the drug ID and searches the collisions database) returns an array of DrugCollision objects in which there's a drug name(a drug which it collides with), and a description (what happens if you take both of the drugs)
-the interactions store service stores all of the collisions of the drug which you most recently searched for. I.e if paracetamol had id 2, and you had just called getcollisionsList() then the service would hold all of the collisions paracetamol is known to have, in an array named interactionsList

这是我当前的算法:我的伪代码是:

For all of the drugs entered by the user
For all of the known drugs int the database
If the drug name is found in the database then get the list of the drugs it collides with
For all the drugs that it could collide with
for all of the drugs entered by the user
check whether there is a collision and if so, store it in the collisions list

for (let i = 0; i < numDrug; i++) {
            for (let j = 0; j < listOfAllMeds2.length; j++) {
                if (this.model[i] === listOfAllMeds2[j].name) { // if a valid med in the list
                    this.getCollisionsList(listOfAllMeds2[j].id);
                    console.log("Collision list length for" + listOfAllMeds2[j].name + this.interactionsStoreService.interactionList.length);

                            for (let l = 0; l < this.interactionsStoreService.interactionList.length; l++) {

                                for (let k = 0; k < numDrug; k++) {
                                    if(i!==k) {


                                        // console.log(this.model[k], this.interactionsStoreService.interactionList[l].name + numDrug);
                                        if (this.model[k] === this.interactionsStoreService.interactionList[l].name) {


                                            if (!this.checkIfAlreadyInList(this.model[i], this.model[k], drugsClashing)) {
                                                collide = true;
                                                const collision: Collision = new Collision(this.model[i], this.model[k], this.interactionsStoreService.interactionList[l].description);
                                                drugsClashing.push(collision);
                                            }
                                        }
                                    }

                        }
                    }
                    this.interactionsStoreService.clearList();
                }

            }
        }

因此,一个例子就是我们有毒品:醋乙酰胺瘦素阿昔单抗按此顺序输入。

[listOfAllDrugs2是所有已知药物的列表,例如Lepirudin在ID值为1的listOfAllDrugs2列表中。

[在列表中找到药物并找到ID后,将调用this.getCollisionsList(ID),该调用将调用外部springboot应用程序(虽然无关紧要,但仅用于上下文),然后返回Array of '药物相互作用'。此数组存储在交互服务中以供使用,直到必须找到下一个交互列表,然后才擦除该服务。

对于Lepirudin,当在this.getCollisionsList(ID)中输入ID(Lepirudin为1)时,冲突列表中会同时返回Acemetacin和Abciximab。因此,示例输出应为:

Lepirudin & Acemetacin
Lepirudin & Abciximab

因为它与输入的其他两种药物发生冲突。

用于上下文的数据结构和代码:

listOfAllMeds2:
let listOfAllMeds2: Array<Medicine2> = new Array<Medicine2>();

Medicine2:
export class Medicine {
  constructor(
    public name: string,
    public dosage: string,
    public clashesWith: Array<string>,

  ) {  }


}

this.getCollisionsList(int id):
  this.httpClient.getInteractionById(id).subscribe(data => {
            const JSONdata: any = data;

            for (let i = 0; i < JSONdata.length; i++) {
                const int: DrugInteraction = new DrugInteraction(JSONdata[i].drug_id, JSONdata[i].name, JSONdata[i].description);
                this.interactionsStoreService.addNewInteraction(int);
            }
            console.log(this.interactionsStoreService.interactionList.length);
        });

    }


DrugInteraction.ts:
export class DrugInteraction {

    constructor(
        public drug_id: number,
        public name: string,
        public description: string,

    ) {  }
}


DrugsClashing:
var drugsClashing = new Array<Collision>();

Collision.ts:
export class Collision {
    constructor(
        public drug1Name: string,
        public drug2Name: string,
        public description: string,

    ) {  }


}


InteractionsStoreService.InteractionsList:
 public interactionList = new Array<DrugInteraction>();


DrugsClashing:
var drugsClashing = new Array<Collision>();



method checkIfAlreadyIn List (checks whether the 2 drugs known to clash have already been noted):
checkIfAlreadyInList(drugName1: string, drugName2: string, listOfClashes: Array<Collision>) {
        for (let i = 0; i < listOfClashes.length; i++) {
            if ((listOfClashes[i].drug1Name === drugName1 && listOfClashes[i].drug1Name === drugName2) || listOfClashes[i].drug1Name === drugName2 && listOfClashes[i].drug2Name === drugName1) {
                return true;
            }
        }


    }


[有人可以告诉我我要去哪里错吗?我已经为此工作了好几个小时,现在我觉得我的头充满了棉绒!

我正在编写一种算法,用于检查用户输入的药物是否已知发生碰撞。我有一个毒品数据库和一个将要使用的毒品冲突数据库。 ...

javascript arrays algorithm search logic
1个回答
0
投票

您的主要代码似乎是正确的。我在这里复制了它:

© www.soinside.com 2019 - 2024. All rights reserved.