Clinic roster merge
MediumOnline Assessment
Merge two exports of a clinic's patient roster into one clean CSV.
Each file is a list of lines; the first line is a header. Cells are separated by commas. A cell may be wrapped in double quotes, in which case it can contain commas and a doubled "" stands for one " character. A quote that does not open a cell is an ordinary character. Strip leading and trailing spaces from every cell value after unquoting.
The four fields, in output order, are id, name, phone and site. Identify a header cell by deleting its spaces, underscores and hyphens and lowercasing it:
id:id,patientid,patientnumber,mrnname:name,fullname,patientnamephone:phone,tel,telephone,mobilesite:site,clinic,location
Other columns are ignored. A file with no id column contributes nothing. A field a file lacks, or a cell missing because its line is short, is empty; extra cells beyond the header are ignored.
Phone numbers keep digits only. Patients are identified by id (compared exactly as text, so 007 and 7 differ); lines whose id is empty are dropped.
Process all lines of csv_a, then all lines of csv_b, in order. Each line is laid over the patient's record: every non-empty cell replaces what the record has for that field, and empty cells change nothing.
Input: csv_a and csv_b, lists of strings.
Output: a list of CSV lines: the header id,name,phone,site, then one line per patient, sorted by id in ascending character order. Write a cell inside double quotes (doubling any " it contains) only if it contains a comma or a double quote; always write all four cells.
Example 1
- Input:
- [["id,name,phone,clinic","101,Ana Ruiz,555-0101,North","102,\"Bell, Tom\",555 0102,South"],["Patient Number,Phone,Full Name,Site","102,(555) 0199,,","103,555.0103,Cy Dunn,East"]]
- Output:
- ["id,name,phone,site","101,Ana Ruiz,5550101,North","102,\"Bell, Tom\",5550199,South","103,Cy Dunn,5550103,East"]
- Explanation
- The two files list the columns in different orders and with different names (
clinicvsSite,Patient Number,Full Name). Patient 102 appears in both: the name stays "Bell, Tom" because B's name is empty, and the phone becomes B's 5550199 because B has a value. Phones keep only digits. The name containing a comma is written in quotes in the output.
Example 2
- Input:
- [["MRN, Name ,Tel,Notes,Location","007,Zoe,12-3,xx,Lab","7,Yan,,,","007,,999,,",",Ghost,1,,","10,\"Quote \"\"Q\"\" Lee\",5"],["id,site","10,Annex","007,"]]
- Output:
- ["id,name,phone,site","007,Zoe,999,Lab","10,\"Quote \"\"Q\"\" Lee\",5,Annex","7,Yan,,"]
- Explanation
- The header
NameandMRNmatch after ignoring spaces and case;Notesis not one of the fields and is ignored. Patient 007 is on two lines in A, the later one adds the phone 999; B's empty site does not erase "Lab". The row without an id is dropped. A short line leaves the missing fields empty. Ids are sorted as text: "007", "10", "7". The doubled quotes in the input name become a quote character, which is doubled again in the output.
Example 3
- Input:
- [["site,ID,name","N,5,Al","N,5,Bo","S,6,\"\"","E,,Cy"],["Name,Phone","Dee,555"]]
- Output:
- ["id,name,phone,site","5,Bo,,N","6,,,S"]
- Explanation
- Columns are in a different order here. For patient 5 the later line replaces the name "Al" with "Bo". The line without an id is dropped. File B has no id column, so it contributes nothing. Patient 6 has no name or phone, so those cells are empty.
Constraints
0 <= csv_a.length, csv_b.length <= 300lines per file; when a file is not empty its first line is the header- Each line is at most 120 characters, printable ASCII, with no line breaks inside a field
- A header contains at most one column for each of the four fields