Created
April 29, 2025 13:39
-
-
Save Ammar-Ishfaq/6c3060fb0e2c428ffac312acfb1b4ac8 to your computer and use it in GitHub Desktop.
Kotlin program to estimate patient waiting time based on doctor availability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Doctor( | |
val id: Int, | |
val name: String, | |
val avgConsultationTime: Int, | |
var nextAvailableTime: Int = 0 | |
) | |
fun calculateWaitingTime(doctors: List<Doctor>, patientPosition: Int): Int { | |
val doctorList = doctors.map { it.copy() } | |
var currentTime = 0 | |
var patientCount = 0 | |
while (true) { | |
for (doctor in doctorList.sortedWith( | |
compareBy({ it.nextAvailableTime }, { it.avgConsultationTime }, { it.id }) | |
)) { | |
if (doctor.nextAvailableTime <= currentTime) { | |
patientCount++ | |
if (patientCount == patientPosition) { | |
println("Assigning patient $patientCount to ${doctor.name} at $currentTime") | |
return currentTime | |
} else { | |
println("Assigning patient $patientCount to ${doctor.name} from $currentTime to ${currentTime + doctor.avgConsultationTime}") | |
doctor.nextAvailableTime = currentTime + doctor.avgConsultationTime | |
} | |
} | |
} | |
currentTime++ | |
} | |
} | |
fun main() { | |
val doctors = listOf( | |
Doctor(id = 1, name = "Doctor A", avgConsultationTime = 3), | |
Doctor(id = 2, name = "Doctor B", avgConsultationTime = 4), | |
) | |
val patientPosition = 11 | |
val estimatedTime = calculateWaitingTime(doctors, patientPosition) | |
println("Estimated waiting time for patient $patientPosition: $estimatedTime minutes") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output of the code:
Assigning patient 1 to Doctor A from 0 to 3
Assigning patient 2 to Doctor B from 0 to 4
Assigning patient 3 to Doctor A from 3 to 6
Assigning patient 4 to Doctor B from 4 to 8
Assigning patient 5 to Doctor A from 6 to 9
Assigning patient 6 to Doctor B from 8 to 12
Assigning patient 7 to Doctor A from 9 to 12
Assigning patient 8 to Doctor A from 12 to 15
Assigning patient 9 to Doctor B from 12 to 16
Assigning patient 10 to Doctor A from 15 to 18
Assigning patient 11 to Doctor B at 16
Estimated waiting time for patient 11: 16 minutes