Build a Medical Data Validator - Step 44

Tell us what’s happening:

I’ve tried both the index of the key in the dictionary and of the dictionary in medical_records, the expected print is shown but the code is not considered right, what’s wrong?

Your code so far

import re


medical_records = [
    {
        'patient_id': '1001',
        'age': 34,
        'gender': 'Female',
        'diagnosis': 'Hypertension',
        'medications': ['Lisinopril'],
        'last_visit_id': 'v2301',
    },
    {
        'patient_id': 'p1002',
        'age': 47,
        'gender': 'male',
        'diagnosis': 'Type 2 Diabetes',
        'medications': ['Metformin', 'Insulin'],
        'last_visit_id': 'v2302',
    },
    {
        'patient_id': 'P1003',
        'age': 29,
        'gender': 'female',
        'diagnosis': 'Asthma',
        'medications': ['Albuterol'],
        'last_visit_id': 'v2303',
    },
    {
        'patient_id': 'p1004',
        'age': 52,
        'gender': 'Male',
        'diagnosis': 'Chronic Back Pain',
        'medications': ['Ibuprofen', 'Physical Therapy'],
        'last_visit_id': 'V2304',
    }
]


def find_invalid_records(
    patient_id, age, gender, diagnosis, medications, last_visit_id
):

    constraints = {
        'patient_id': isinstance(patient_id, str)
        and re.fullmatch('p\d+', patient_id, re.IGNORECASE),
        'age': isinstance(age, int) and age >= 18,
        'gender': isinstance(gender, str) and gender.lower() in ('male', 'female'),
        'diagnosis': isinstance(diagnosis, str) or diagnosis is None,
        'medications': isinstance(medications, list)
        and all([isinstance(i, str) for i in medications]),
        'last_visit_id': isinstance(last_visit_id, str)
        and re.fullmatch('v\d+', last_visit_id, re.IGNORECASE)
    }

    return [key for key, value in constraints.items() if not value]


def validate(data):
    is_sequence = isinstance(data, (list, tuple))

    if not is_sequence:
        print('Invalid format: expected a list or tuple.')
        return False
        
    is_invalid = False
    key_set = set(
        ['patient_id', 'age', 'gender', 'diagnosis', 'medications', 'last_visit_id']
    )

    for index, dictionary in enumerate(data):
        if not isinstance(dictionary, dict):
            print(f'Invalid format: expected a dictionary at position {index}.')
            is_invalid = True
            continue

        if set(dictionary.keys()) != key_set:
            print(
                f'Invalid format: {dictionary} at position {index} has missing and/or invalid keys.'
            )
            is_invalid = True
            continue

        invalid_records = find_invalid_records(**dictionary)

# User Editable Region

        for key in invalid_records:
            print(f"Unexpected format '{key}: {dictionary[key]}' at position {list(dictionary.keys()).index(key)}.")
            is_invalid = True

# User Editable Region

    if is_invalid:
        return False
    print('Valid format.')
    return True

validate(medical_records)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15

Challenge Information:

Build a Medical Data Validator - Step 44

try to change something in the medical records object so you have something invalid, and you can see what your code is doing. Note that the index is the one of the object in the input data

If I change something it correctly gives

Unexpected format ‘patient_id: 1001’ at position 0.
Unexpected format ‘gender: Feale’ at position 2.
Unexpected format ‘last_visit_id: 2304’ at position 5.

that does not seem correct, with 4 objects in medical_records there can’t be something at position 5

Considering medical records gives this, but it still doesn’t work

Unexpected format 'gender: Feale' at position 0.
Unexpected format 'diagnosis: 3' at position 0.
Unexpected format 'medications: Lisinopril' at position 0.
Unexpected format 'age: purple' at position 1.
Unexpected format 'gender: feale' at position 2.
Unexpected format 'last_visit_id: 2303' at position 2.
Unexpected format 'last_visit_id: 2304' at position 3.

Can you share the changes that you made to the medical_records ?

Hard to evaluate from just this output.

Are the positions correct? Or not?

This the changed medical_records

medical_records = [
    {
        'patient_id': 'p1001',
        'age': 34,
        'gender': 'Feale',
        'diagnosis': 3,
        'medications': ('Lisinopril'),
        'last_visit_id': 'v2301',
    },
    {
        'patient_id': 'p1002',
        'age': 'purple',
        'gender': 'male',
        'diagnosis': 'Type 2 Diabetes',
        'medications': ['Metformin', 'Insulin'],
        'last_visit_id': 'v2302',
    },
    {
        'patient_id': 'P1003',
        'age': 29,
        'gender': 'feale',
        'diagnosis': 'Asthma',
        'medications': ['Albuterol'],
        'last_visit_id': '2303',
    },
    {
        'patient_id': 'p1004',
        'age': 52,
        'gender': 'Male',
        'diagnosis': 'Chronic Back Pain',
        'medications': ['Ibuprofen', 'Physical Therapy'],
        'last_visit_id': '2304',
    }
]

You’ll need to share your most recent code as well, these results don’t match the code you’ve given.

import re


medical_records = [
    {
        'patient_id': 'p1001',
        'age': 34,
        'gender': 'Feale',
        'diagnosis': 3,
        'medications': ('Lisinopril'),
        'last_visit_id': 'v2301',
    },
    {
        'patient_id': 'p1002',
        'age': 'purple',
        'gender': 'male',
        'diagnosis': 'Type 2 Diabetes',
        'medications': ['Metformin', 'Insulin'],
        'last_visit_id': 'v2302',
    },
    {
        'patient_id': 'P1003',
        'age': 29,
        'gender': 'feale',
        'diagnosis': 'Asthma',
        'medications': ['Albuterol'],
        'last_visit_id': '2303',
    },
    {
        'patient_id': 'p1004',
        'age': 52,
        'gender': 'Male',
        'diagnosis': 'Chronic Back Pain',
        'medications': ['Ibuprofen', 'Physical Therapy'],
        'last_visit_id': '2304',
    }
]


def find_invalid_records(
    patient_id, age, gender, diagnosis, medications, last_visit_id
):

    constraints = {
        'patient_id': isinstance(patient_id, str)
        and re.fullmatch('p\d+', patient_id, re.IGNORECASE),
        'age': isinstance(age, int) and age >= 18,
        'gender': isinstance(gender, str) and gender.lower() in ('male', 'female'),
        'diagnosis': isinstance(diagnosis, str) or diagnosis is None,
        'medications': isinstance(medications, list)
        and all([isinstance(i, str) for i in medications]),
        'last_visit_id': isinstance(last_visit_id, str)
        and re.fullmatch('v\d+', last_visit_id, re.IGNORECASE)
    }

    return [key for key, value in constraints.items() if not value]


def validate(data):
    is_sequence = isinstance(data, (list, tuple))

    if not is_sequence:
        print('Invalid format: expected a list or tuple.')
        return False
        
    is_invalid = False
    key_set = set(
        ['patient_id', 'age', 'gender', 'diagnosis', 'medications', 'last_visit_id']
    )

    for index, dictionary in enumerate(data):
        if not isinstance(dictionary, dict):
            print(f'Invalid format: expected a dictionary at position {index}.')
            is_invalid = True
            continue

        if set(dictionary.keys()) != key_set:
            print(
                f'Invalid format: {dictionary} at position {index} has missing and/or invalid keys.'
            )
            is_invalid = True
            continue

        invalid_records = find_invalid_records(**dictionary)
        for key in invalid_records:
            print(f"Unexpected format '{key}: {dictionary[key]}' at position {medical_records.index(dictionary)}.")
            is_invalid = True
    if is_invalid:
        return False
    print('Valid format.')
    return True

validate(medical_records)

Good solution. You are finding the index of the dictionary in the medical records list.

I think this gives the correct result. However, look at the larger context of your code. Where does the dictionary variable come from?

It’s the variable of the for iterable at line 71

Any other variables created there?

1 Like