Skip to content

Instantly share code, notes, and snippets.

@korniichuk
Last active September 20, 2022 17:27
Show Gist options
  • Save korniichuk/99d0435e97579591e8df58517fcb0b15 to your computer and use it in GitHub Desktop.
Save korniichuk/99d0435e97579591e8df58517fcb0b15 to your computer and use it in GitHub Desktop.
Solution for Brainly's recruitment Python test
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Drug Analyzer\n",
"## Introduction\n",
"You are a member of a biotechnology programming team that is responsible for creating a system for lab technicians, which will assist them with drug analysis.\n",
"\n",
"Your goal is to create the application that will let them input their findings into the system, provide a meaningful analysis and verify the correctness of the data that they have sent.\n",
"## Part 1\n",
"Your goal in this part is to implement the `DrugAnalyzer` class. It will be responsible for analyzing data like the data presented below:\n",
"\n",
"| pill_id | pill_weight | active_substance | impurities |\n",
"|---------|-------------|------------------|------------|\n",
"| L01-10 | 1007.67 | 102.88 | 1.00100 |\n",
"| L01-06 | 996.42 | 99.68 | 2.00087 |\n",
"| G02-03 | 1111.95 | 125.04 | 3.00004 |\n",
"| G03-06 | 989.01 | 119.00 | 4.00062 |\n",
"\n",
"The initialization of the class can be done from Python's list of lists (or nothing) and stored in the instance\n",
"variable called data as per example below:\n",
"```\n",
">>> my_drug_data = [\n",
"... ['L01-10', 1007.67, 102.88, 1.00100],\n",
"... ['L01-06', 996.42, 99.68, 2.00087],\n",
"... ['G02-03', 1111.95, 125.04, 3.00100],\n",
"... ['G03-06', 989.01, 119.00, 4.00004]\n",
"... ]\n",
">>> my_analyzer = DrugAnalyzer(my_drug_data)\n",
">>> my_analyzer.data\n",
"[['L01-10', 1007.67, 102.88, 0.001], ['L01-06', 996.42, 99.68, 0.00087], ['G02-03', 1111.95, 125.04, 0.00100], ['G03-06', 989.01, 119.00, 0.00004]]\n",
">>> DrugAnalyzer().data\n",
"[]\n",
"```\n",
"\n",
"The class should also have an option to add single lists into the object. Adding a list to the `DrugAnalyzer` object\n",
"should return a new instance of this object with an additional element. Adding improper type or a list with improper\n",
"length should raise a `ValueError`. An example of a correct and wrong addition output is shown below:\n",
"```\n",
">>> my_new_analyzer = my_analyzer + ['G03-01', 789.01, 129.00, 0.00008]\n",
">>> my_new_analyzer.data\n",
"[['L01-10', 1007.67, 102.88, 0.001], ['L01-06', 996.42, 99.68, 0.00087], ['G02-03', 1111.95, 125.04, 0.00100], ['G03-06', 989.01, 119.00, 0.00004], ['G03-01', 789.01, 129.00, 0.00008]]\n",
">>> my_new_analyzer = my_analyzer + ['G03-01', 129.00, 0.00008]\n",
"Traceback (the most recent call is displayed as the last one):\n",
" File \"<stdin>\", line 1, in <module>\n",
"ValueError: Improper length of the added list.\n",
"```\n",
"\n",
"## Part 2\n",
"### Implement the `verify_series` method inside the `DrugAnalyzer` class. \n",
"\n",
"The goal of this method is to receive a list of parameters and use them to verify if the pills described inside the instance\n",
"variable data matches the given criteria. It should return a boolean value as a result.\n",
"\n",
"The function would be called as follows:\n",
"```\n",
"verify_series(series_id='L01', act_subst_wgt=100, act_subst_rate=0.05, allowed_imp=0.001)\n",
"```\n",
"\n",
"Where:\n",
"- `series_id` is a 3 characters long string that is present at the beginning of every `pill_id`, before the `-` sign, for example, `L01` is the `series_id` in `pill_id` = `L01-12`.\n",
"- `act_subst_wgt` is the expected weight (mg) of the active substance content in the given series in one pill.\n",
"- `act_subst_rate` is the allowed rate of difference in the active substance weight from the expected one. For example,\n",
"for `100` mg, the accepted values would be between `95` and `105`.\n",
"- `allowed_imp` is the allowed rate of impure substances in the `pill_weight`. For example, for `100` mg `pill_weight`\n",
"and `0.001` rate, the allowed amount of `impurities` is `0.1` mg.\n",
"\n",
"The function should take all pills that are part of the `L01` series, sum their weight and calculate if the\n",
"amount of `active_substance`, as well as `impurities`, match the given rates. It should return `True` if both conditions\n",
"are met and `False` if any of them is not met.\n",
"\n",
"The `False` result should mean that all the passed parameters are proper, but either the `active_substance` amount or the `impurities` amount is improper.\n",
"\n",
"In case of a `series_id` that is not present in the data at all or in case of any improper parameter, the function should throw a `ValueError`.\n",
"\n",
"Please think what could be the possible edge case in such a scenario.\n",
"\n",
"Example:\n",
"```\n",
">>> my_drug_data = [\n",
"... ['L01-10', 1000.02, 102.88, 1.00100],\n",
"... ['L01-06', 999.90, 96.00, 2.00087],\n",
"... ['G02-03', 1000, 96.50, 3.00100],\n",
"... ['G03-06', 989.01, 119.00, 4.00004]\n",
"... ]\n",
">>> my_analyzer = DrugAnalyzer(my_drug_data)\n",
">>> my_analyzer.verify_series(series_id='L01', act_subst_wgt=100, act_subst_rate=0.05, allowed_imp=0.001)\n",
"False\n",
">>> // The overall active_substance weight would be 198.88, which is within the given rate of 0.05 for 200 mg (2 * act_subst_wgt).\n",
">>> // However, the sum of impurities would be 3.00187, which is more than 0.001*1999.92 (allowed_imp_rate * (1000.02 + 999.90).\n",
">>> my_analyzer.verify_series(series_id='L01', act_subst_wgt=100, act_subst_rate=0.05, allowed_imp=0.01)\n",
"True\n",
">>> my_analyzer.verify_series(series_id='B03', act_subst_wgt=100, act_subst_rate=0.05, allowed_imp=0.001)\n",
"Traceback (the most recent call is displayed as the last one):\n",
" File \"<stdin>\", line 1, in <module>\n",
"ValueError: B03 series is not present within the dataset.\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class DrugAnalyzer():\n",
"\n",
" data = []\n",
"\n",
" def __init__(self, data=None):\n",
" if data is not None:\n",
" self.data = data\n",
"\n",
" def __add__(self, value):\n",
" if not isinstance(value, list):\n",
" raise ValueError('Improper type of the added variable.')\n",
" if len(value) != 4:\n",
" raise ValueError('Improper length of the added list.')\n",
" self.data.append(value)\n",
" return self\n",
"\n",
" def verify_series(\n",
" self,\n",
" series_id: str,\n",
" act_subst_wgt: float,\n",
" act_subst_rate: float,\n",
" allowed_imp: float) -> bool:\n",
"\n",
" def filter(self, series_id):\n",
" result = []\n",
" for drug in self.data:\n",
" if drug[0].split('-')[0] == series_id:\n",
" result.append(drug)\n",
" return result\n",
"\n",
" def verify_act_subst_wgt(drugs, act_subst_wgt, act_subst_rate):\n",
" min_subst_wgt = len(drugs) * act_subst_wgt * (1 - act_subst_rate)\n",
" max_subst_wgt = len(drugs) * act_subst_wgt * (1 + act_subst_rate)\n",
" if (total_active_substance < min_subst_wgt or\n",
" total_active_substance > max_subst_wgt):\n",
" return False\n",
" return True\n",
"\n",
" def verify_impurities_wgt(\n",
" total_impurities, total_pill_weight, allowed_imp):\n",
" if total_impurities > total_pill_weight * allowed_imp:\n",
" return False\n",
" return True\n",
"\n",
" # Filter by series_id\n",
" valid_drugs = filter(self, series_id)\n",
" # Raise ValueError if series_id is not present in the data at all\n",
" if not valid_drugs:\n",
" text = '{} series is not present within the dataset.'\n",
" msg = text.format(series_id)\n",
" raise ValueError(msg)\n",
" # Calculate total values\n",
" total_pill_weight, total_active_substance, total_impurities = 0, 0, 0\n",
" for drug in valid_drugs:\n",
" total_pill_weight += drug[1]\n",
" total_active_substance += drug[2]\n",
" total_impurities += drug[3]\n",
" # Verify active substance weight\n",
" if not verify_act_subst_wgt(\n",
" valid_drugs, act_subst_wgt, act_subst_rate):\n",
" return False\n",
" # Verify impurities weight\n",
" if not verify_impurities_wgt(\n",
" total_impurities, total_pill_weight, allowed_imp):\n",
" return False\n",
" return True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Part 1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"my_drug_data = [\n",
" ['L01-10', 1007.67, 102.88, 1.00100],\n",
" ['L01-06', 996.42, 99.68, 2.00087],\n",
" ['G02-03', 1111.95, 125.04, 3.00100],\n",
" ['G03-06', 989.01, 119.00, 4.00004]\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"my_analyzer = DrugAnalyzer(my_drug_data)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['L01-10', 1007.67, 102.88, 1.001],\n",
" ['L01-06', 996.42, 99.68, 2.00087],\n",
" ['G02-03', 1111.95, 125.04, 3.001],\n",
" ['G03-06', 989.01, 119.0, 4.00004]]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_analyzer.data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"DrugAnalyzer().data"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"my_new_analyzer = my_analyzer + ['G03-01', 789.01, 129.00, 0.00008]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['L01-10', 1007.67, 102.88, 1.001],\n",
" ['L01-06', 996.42, 99.68, 2.00087],\n",
" ['G02-03', 1111.95, 125.04, 3.001],\n",
" ['G03-06', 989.01, 119.0, 4.00004],\n",
" ['G03-01', 789.01, 129.0, 8e-05]]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_new_analyzer.data"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "Improper length of the added list.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-8-e972f6c395ab>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_new_analyzer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmy_analyzer\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'G03-01'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m129.00\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.00008\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-1-46bd08a46d33>\u001b[0m in \u001b[0;36m__add__\u001b[0;34m(self, value)\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Improper type of the added variable.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Improper length of the added list.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: Improper length of the added list."
]
}
],
"source": [
"my_new_analyzer = my_analyzer + ['G03-01', 129.00, 0.00008]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "Improper type of the added variable.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-e135109660e1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_analyzer\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-1-46bd08a46d33>\u001b[0m in \u001b[0;36m__add__\u001b[0;34m(self, value)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__add__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Improper type of the added variable.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Improper length of the added list.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: Improper type of the added variable."
]
}
],
"source": [
"my_analyzer + 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Part 2"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"my_drug_data = [\n",
" ['L01-10', 1000.02, 102.88, 1.00100],\n",
" ['L01-06', 999.90, 96.00, 2.00087],\n",
" ['G02-03', 1000, 96.50, 3.00100],\n",
" ['G03-06', 989.01, 119.00, 4.00004]\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"my_analyzer = DrugAnalyzer(my_drug_data)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_analyzer.verify_series(series_id='L01', act_subst_wgt=100, act_subst_rate=0.05, allowed_imp=0.001)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_analyzer.verify_series(series_id='L01', act_subst_wgt=100, act_subst_rate=0.05, allowed_imp=0.01)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "B03 series is not present within the dataset.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-14-bdc8aeb6d1d6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_analyzer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverify_series\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mseries_id\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'B03'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mact_subst_wgt\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mact_subst_rate\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.05\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mallowed_imp\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.001\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-1-46bd08a46d33>\u001b[0m in \u001b[0;36mverify_series\u001b[0;34m(self, series_id, act_subst_wgt, act_subst_rate, allowed_imp)\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[0mtext\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'{} series is not present within the dataset.'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0mmsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mseries_id\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 51\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 52\u001b[0m \u001b[0;31m# Calculate total values\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 53\u001b[0m \u001b[0mtotal_pill_weight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtotal_active_substance\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtotal_impurities\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: B03 series is not present within the dataset."
]
}
],
"source": [
"my_analyzer.verify_series(series_id='B03', act_subst_wgt=100, act_subst_rate=0.05, allowed_imp=0.001)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@xaldoxxx
Copy link

thanks!!!!

@Eliabe-Rocha
Copy link

Thanks!
Help me a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment