Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Created May 17, 2019 10:12
Show Gist options
  • Select an option

  • Save jatinsharrma/4c5f99dc55e2392d0845fdd0f39c8242 to your computer and use it in GitHub Desktop.

Select an option

Save jatinsharrma/4c5f99dc55e2392d0845fdd0f39c8242 to your computer and use it in GitHub Desktop.
Care hospital wants to know the medical speciality visited by the maximum number of patients.
# Care hospital wants to know the medical speciality visited by the maximum number of patients. Assume that the patient id of the patient along with the medical speciality visited by the patient is stored in a list. The details of the medical specialities are stored in a dictionary as follows:
# {
# "P":"Pediatrics",
# "O":"Orthopedics",
# "E":"ENT
# }
# Write a function to find the medical speciality visited by the maximum number of patients and return the name of the speciality.
# Also write the pytest test cases to test the program.
def max_visited_speciality(patient_medical_speciality_list,medical_speciality):
result = [0,0,0]
i = 1
while(i< len(patient_medical_speciality_list)):
if patient_medical_speciality_list[i] == 'P': result[0] = result[0] + 1
elif patient_medical_speciality_list[i] == 'O' : result[1] = result[1] + 1
else : result[2] = result[2] + 1
i = i + 2
a = max(result)
a = result.index(a)
if a == 0: speciality = 'Pediatrics'
elif a == 1: speciality ='Orthopedics'
else : speciality = 'ENT'
return speciality
patient_medical_speciality_list=[301,'O',302, 'P' ,305, 'P' ,401, 'E' ,656, 'E']
medical_speciality={"P":"Pediatrics","O":"Orthopedics","E":"ENT"}
speciality=max_visited_speciality(patient_medical_speciality_list,medical_speciality)
print(speciality)
@DannyBoy3310

Copy link
Copy Markdown

def max_visited_speciality(patient_medical_speciality_list,medical_speciality):
# write your logic here
count1 = patient_medical_speciality_list.count('P')
count2 = patient_medical_speciality_list.count('O')
count3 = patient_medical_speciality_list.count('E')
if(count1> count2 and count1> count3):
speciality = medical_speciality["P"]
elif(count2> count1 and count2> count3):
speciality = medical_speciality["O"]
else:
speciality = medical_speciality["E"]

return speciality

#provide different values in the list and test your program
patient_medical_speciality_list=[301,'P',302, 'P' ,305, 'P' ,401, 'E' ,656, 'E']
medical_speciality={"P":"Pediatrics","O":"Orthopedics","E":"ENT"}
speciality=max_visited_speciality(patient_medical_speciality_list,medical_speciality)
print(speciality)

@jatinsharrma

Copy link
Copy Markdown
Author

Nice. You used count method of list. I haven't thought of this at that time. Thanks

@neha1699-ux

Copy link
Copy Markdown

Simpler and easy to understand. Thanks.

@akengi

akengi commented Jan 23, 2022

Copy link
Copy Markdown

these don't work if the sum of specialty visited by patients varies..
example >> [301,'P',302, 'P' ,305, 'P' ,401, 'E' ,656, 'E']
for this, the code will output "Pediatrics" while the answer should be ENT, as the sum of patients visiting 'P" is 908, while the patients visiting 'E' is 1057.

@vikas-beniwal

vikas-beniwal commented Feb 2, 2022

Copy link
Copy Markdown

should not it be like this:

def max_visited_speciality(patient_medical_speciality_list,medical_speciality):

result = [0,0,0]
i = 1 
while(i< len(patient_medical_speciality_list)):
    if patient_medical_speciality_list[i] == 'P':
        result[0] = result[0]+patient_medical_speciality_list[i-1]
    elif patient_medical_speciality_list[i] == 'O' : 
        result[1] =result[1]+patient_medical_speciality_list[i-1]
    else :
        result[2] = result[2]+patient_medical_speciality_list[i-1]
    i = i + 2
a = max(result)
a = result.index(a)
if a == 0: speciality = 'Pediatrics'
elif a == 1: speciality ='Orthopedics'
else : speciality = 'ENT'

return speciality

patient_medical_speciality_list=[301,'O',302, 'P' ,305, 'P' ,401, 'E' ,656, 'E']
#patient_medical_speciality_list=[101,'O',102,'O',302,'P',305,'E',401,'O',656,'O']
#patient_medical_speciality_list=[101,'P',102,'O',302,'P',305,'P']
#patient_medical_speciality_list=[101,'O',102,'E',302,'P',305,'P',401,'E',656,'O',987,'E']

medical_speciality={"P":"Pediatrics","O":"Orthopedics","E":"ENT"}
speciality=max_visited_speciality(patient_medical_speciality_list,medical_speciality)
print(speciality)

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