Python ADT question

Joined
Oct 21, 2024
Messages
3
Reaction score
0
Part 3 – Patient score ADT
In this section we will develop our patient score ADT that accepts a list of patients and returns a list of patients with their respective scores.

The constructor has the following interface:

makePscore : [patients] -> ["PS",[]]
A Score has the following format:

["PS", [(Patient, score), (Patient, score)]]
A list with a “PS” tag in the first position and a list in the first index position that contains the Patient score tuples.

Calculate the Patient score based on the points assigned to each factor below:

The Patient is hypertensive – 4 Points
The Patient has hight cholesterol – 3 Points
The Patient is physically inactive – 2 Points
The Patient is a male – 1.5 Points
The Patient is female – 1 Point
.

calPScore : Patient -> Float
Implement the following functions to complete the Score ADT

Function Name Type Description
makePscore(ptLst) Constructor Takes a List of Patients and returns a score list, where the first part of the list is a tag ‘PS’
addPatient(PSLst,pt) Mutator Takes a Score List and a Patient as input and adds the patient to the score list. this function calculates the patient score before adding it to the list.
getCritical(Score) Selector Takes a Score as an input and returns a list of all critical Patients. PSLst is the Global SCORE LIST (which contains tuples of patients and scores).
getNonCrit(Score) Selector Takes a Score as an input and returns a list of all non-critical patients. PSLst is the Global SCORE LIST (which contains tuples of patients and scores).
isScore(Score) Predicate Checks to see if a given list, is a valid Score
isEmptyScore(Score) Predicate Checks to see if a given Score list is empty. PSLst is the Global SCORE LIST (which contains tuples of patients and scores).
Input Format

The number of patients, p

p lines of patient information (Age, Sex, TrestBPS, Chol, FBS, Thalach, OldPeak)

Constraints

None

Output Format

Usage of the Constructor, Selector or Accessor, Mutator and Predicate functions.

Sample Input 0

1
27 M 145 233 0 150 2.3
Sample Output 0

[('HDPT', {'age': 27, 'sex': 'M', 'trestbps': 145, 'chol': 233, 'fbs': 0, 'thalach': 150, 'oldpeak': 2.3})]
['PS', []]
Empty Patient Score List: True
['PS', [(('HDPT', {'age': 27, 'sex': 'M', 'trestbps': 145, 'chol': 233, 'fbs': 0, 'thalach': 150, 'oldpeak': 2.3}), 8.5)]]
Empty Patient Score List: False
Critical Patients: [('HDPT', {'age': 27, 'sex': 'M', 'trestbps': 145, 'chol': 233, 'fbs': 0, 'thalach': 150, 'oldpeak': 2.3})]
Non Critical Patients: []
Invalid Score of 12: False
Valid Score of 8.5: True
Explanation 0

Patient with age 27, sex M, trestbps 145, chol 233, fbs 0, thalach 150, and oldpeak': 2.3 is first added to the previously empty list of patients.

An empty patient score list, PSLst is created and tested to ensure that the list is empty. Empty Patient Score List: True.

The patient score is calculated and the patient and corresponding score is added as a tuple to PSLst.The updated patient score list is ['PS', [(('HDPT', {'age': 27, 'sex': 'M', 'trestbps': 145, 'chol': 233, 'fbs': 0, 'thalach': 150, 'oldpeak': 2.3}), 8.5)]]

A test of empty patient score list then generates Empty Patient Score List: False

The list of Critical Patients: [('HDPT', {'age': 27, 'sex': 'M', 'trestbps': 145, 'chol': 233, 'fbs': 0, 'thalach': 150, 'oldpeak': 2.3})]

The List of Non Critical Patients: []

Functions isScore is tested with Invalid Score of 12 which generates False and Valid Score of 8.5 which generates True.

Sample Input 1

2
47 M 145 233 0 150 2.3
52 F 165 253 1 180 1.8
Sample Output 1

[('HDPT', {'age': 47, 'sex': 'M', 'trestbps': 145, 'chol': 233, 'fbs': 0, 'thalach': 150, 'oldpeak': 2.3}), ('HDPT', {'age': 52, 'sex': 'F', 'trestbps': 165, 'chol': 253, 'fbs': 1, 'thalach': 180, 'oldpeak': 1.8})]
['PS', []]
Empty Patient Score List: True
['PS', [(('HDPT', {'age': 47, 'sex': 'M', 'trestbps': 145, 'chol': 233, 'fbs': 0, 'thalach': 150, 'oldpeak': 2.3}), 8.5), (('HDPT', {'age': 52, 'sex': 'F', 'trestbps': 165, 'chol': 253, 'fbs': 1, 'thalach': 180, 'oldpeak': 1.8}), 8.0)]]
Empty Patient Score List: False
Critical Patients: [('HDPT', {'age': 47, 'sex': 'M', 'trestbps': 145, 'chol': 233, 'fbs': 0, 'thalach': 150, 'oldpeak': 2.3}), ('HDPT', {'age': 52, 'sex': 'F', 'trestbps': 165, 'chol': 253, 'fbs': 1, 'thalach': 180, 'oldpeak': 1.8})]
Non Critical Patients: []
Invalid Score of 12: False
Valid Score of 8.0: True


Here's the code:

#!/bin/python3

import os
import random
import re
import sys

# Function to create a patient
def makePatient(age, sex, trestbps, chol, fbs, thalach, oldpeak):
return ('HDPT', {
'age': age,
'sex': sex,
'trestbps': trestbps,
'chol': chol,
'fbs': fbs,
'thalach': thalach,
'oldpeak': oldpeak
})

# Constructor for the Patient Score List
def makePscore(patient_list):
return ['PS', []]

# Function to calculate the Patient Score
def calPScore(patient):
score = 0
if patient[1]['trestbps'] > 140:
score += 4
if patient[1]['chol'] > 200:
score += 3
if patient[1]['thalach'] < 150:
score += 2
score += 1.5 if patient[1]['sex'] == 'M' else 1
return float(score) # Ensure score is a float

# Function to add a patient to the Patient Score List
def addPatient(score_list, patient):
score = calPScore(patient)
score_list[1].append((patient, score))

# Function to get critical patients
def getCritical(score_list):
return [patient for patient, score in score_list[1] if score > 5.0]

# Function to get non-critical patients
def getNonCrit(score_list):
return [patient for patient, score in score_list[1] if score <= 5.0]

# Predicate to check if a given list is a valid Score
def isScore(score_list):
return (
isinstance(score_list, list)
and len(score_list) > 0
and score_list[0] == 'PS'
and all(isinstance(score, (int, float)) and 0 <= score <= 10 for _, score in score_list[1])
)

# Predicate to check if the Patient Score List is empty
def isEmptyScore(score_list):
return len(score_list[1]) == 0

# Selector to get patient scores from the list
def ptScores(score_list):
return score_list[1]

if name == 'main':
no_of_patients = int(input())
patient_list = []
for i in range(no_of_patients):
p_info = input().strip().split()
patient_list.append(makePatient(
int(p_info[0]), p_info[1], int(p_info[2]),
int(p_info[3]), int(p_info[4]), int(p_info[5]), float(p_info[6])
))
print(patient_list)
PSLst = makePscore(patient_list)
print(PSLst)
print("Empty Patient Score List: " + str(isEmptyScore(PSLst)))
for p in patient_list:
addPatient(PSLst, p)
print(PSLst)
print("Empty Patient Score List: " + str(isEmptyScore(PSLst)))
print("Critical Patients: " + str(getCritical(PSLst)))
print("Non Critical Patients: " + str(getNonCrit(PSLst)))
print("Invalid Score of 12: " + str(isScore(['PS', [(None, 12)]])))
print("Valid Score of " + str(ptScores(PSLst)[-1][1]) + ": " + str(isScore(PSLst)))
 
Joined
Sep 21, 2022
Messages
186
Reaction score
26
Code:
def getCritical(score_list):
  return [patient for patient, score in score_list[1] if score > 5.0]
This kind of syntax puts me off Python. It reads like gibberish.
 
Joined
Dec 10, 2022
Messages
100
Reaction score
26
It's a list generator. Both are the same

Python:
alist = [0,1,2,3,4,5]
alist2 = [n for n in range(6)]

print(f'alist -> {alist}')
print(f'alist2 -> {alist2}')

Output
Code:
alist -> [0, 1, 2, 3, 4, 5]
alist2 -> [0, 1, 2, 3, 4, 5]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,032
Latest member
OdellBerg4

Latest Threads

Top