Вы находитесь на странице: 1из 3

The Apgar Medical Group keeps a patient file

for each doctor in the office. Each record


contains...
Question:
The Apgar Medical Group keeps a patient file for each doctor in the office. Each record contains the
patient's first and last name, home address, and birth year. The records are sorted in ascending
order by birth year. Two doctors, Dr. Best and Dr. Cushing, have formed a partnership. Using
pseudocode, design the logic that produces a merged list of their patients in ascending order by birth
year. Then modify the program so it does not display patients' names but only produces the number
of patients born each year.

Algorithms, Merging two Sorted Lists:


To design the logic asked for in the question, we'll apply a merging algorithm to combine two sorted
lists of records into a single sorted list by visiting the records of both lists, comparing them record by
record and adding the smaller record to the final list each time until all of the records of both lists
have been visited.

Answer and Explanation:


We will represent the person medical record as follows:

struct PersonRecord {
string first_name;
string last_name;
int date_of_birth;
};
/*

The input will be two lists that are already sorted based on date of birth.
In most modern programming languages, a sorted list is usually represented
as an array or a vector where each element can be referenced by an index.
The algorithm below is a straightforward way to combine the two sorted lists.

*/
int i = 0; // index to track Dr A's list
int j = 0; // index to track Dr B's list
int k = 0; // index to track combined list
PersonRecord combined_list[drbest.length + drcushing.length];
while (i < drbest.length and j<drcushing.length)
{
if (drbest[i].date_of_birth < drcushing[j].date_of_birth)
{
combined_list[k] = drbest[i];
i = i + 1;
}
else
{
combined_list[k] = drcushing[j];
j = j + 1;
}
k = k + 1;
}

This loop will add the remaining records from Dr. Best's list:

while (i < drbest.length)


{
combined_list[k] = drbest[i];
i = i + 1;
k = k + 1;
}

This loop will add the remaining records from Dr. Cushing's list:

while (j < drcushing.length)


{
combined_list[k] = drcushing[j];
j = j + 1;
k = k + 1;
}
/*

At this point both list were merged into one. To output the records,
just iterate on every record and print the names and date of birth:

*/
for (i = 0; i < combined_list.length; i=i+1)
{
print first_name, last_name, date_of_birth
}
count = 1; // count of people date of birth
date_of_birth = combined_list[0].date_of_birth
for (i = 1; i < combined_list.length; i=i+1)
{
if (combined_list[i].date_of_birth != date_of_birth)
{
print date_of_birth, count
count = 1 // reset the count
}
else
{
count = count + 1 // same date of birth as previous record
}
}
print date_of_birth, count

Вам также может понравиться