From bdbe6633bfdf64717352ec7d20ff80bac3fd1f6d Mon Sep 17 00:00:00 2001 From: Abhinav Jha Date: Tue, 22 Feb 2022 15:14:07 +0530 Subject: [PATCH] Completed :D --- src/main/kotlin/assignment/Assignment.kt | 47 ++++++++++++++++++++++-- src/main/kotlin/assignment/batches.txt | 28 ++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/assignment/Assignment.kt b/src/main/kotlin/assignment/Assignment.kt index d11b7c9..5b003ae 100644 --- a/src/main/kotlin/assignment/Assignment.kt +++ b/src/main/kotlin/assignment/Assignment.kt @@ -1,5 +1,7 @@ package assignment +import java.io.File + // INSTRUCTIONS: // 1. Read a list of student responses from text file // 2. Filter students who are not covid positive @@ -14,6 +16,8 @@ data class Response( val covidPositive: Boolean ) +// NOTE: I LIKE EXPLICITLY STATING TYPES AT MOST PLACES EXCEPT RANGED FOR LOOPS. ALTHOUGH INTELLIJ HELPS. + fun main() { val responses = readResponses() val processedData = processResponses(responses) @@ -21,13 +25,50 @@ fun main() { } fun readResponses(): List { - TODO() + val responseObj: MutableList = mutableListOf() + + // Read the raw input + val inputFile: String = "src/main/kotlin/assignment/responses.txt" + val inputLines: List = File(inputFile).readLines() + + // Return a Response instance list after working with the input lines + for (line in inputLines.drop(1)) { + val str = line.split(" ") + + val finalStr = str.filter { it != "" } + + // Finally, add a complete Response object to the list + responseObj.add(Response(finalStr[1], finalStr[2].toInt(), finalStr[3], finalStr[4] == "yes")) + } + + // return as a List + return responseObj.toList() } fun processResponses(responses: List): List> { - TODO() + // Filter covid positive ones out + val noCovid: List = responses.filter { !it.covidPositive } + + // Sort them by branch and then their names + val sortedVal: List = noCovid.sortedWith(compareBy({ it.branch }, { it.name })) + val branchGroup = sortedVal.groupBy { it.branch } // It's a Map. Too long and intellij helps + + // 3 Groups + return branchGroup.values.chunked(3).map { it.flatten() } } fun writeToFile(batches: List>) { - TODO() + // Get the output file ready + val outputFile: File = File("src/main/kotlin/assignment/batches.txt") + outputFile.appendText("\n--Text added by Abhinav--\n") + + // append the contents to the text file + batches.forEachIndexed { batchIndex, batch -> + outputFile.appendText("Batch ${batchIndex + 1}:\n") + + batch.forEachIndexed { studentIndex, student -> + outputFile.appendText("${studentIndex + 1}.\t${student.name}\t\t${student.rollNumber}\t\t${student.branch}\n") } + + outputFile.appendText("\n") + } } diff --git a/src/main/kotlin/assignment/batches.txt b/src/main/kotlin/assignment/batches.txt index 7fcf244..80b0546 100644 --- a/src/main/kotlin/assignment/batches.txt +++ b/src/main/kotlin/assignment/batches.txt @@ -23,3 +23,31 @@ Batch 3: 5. Nora 4123894 PHE 6. Owen 4898291 PHE 7. Ryan 9071271 PHE + +--Text added by Abhinav-- +Batch 1: +1. Chloe 4283661 CHE +2. Jack 5672351 CHE +3. Emily 4736471 CIV +4. Jacob 3246617 CSE +5. Lucy 3248462 CSE +6. Sophia 1263612 CSE + +Batch 2: +1. Harry 1278934 ECE +2. Hazel 2361718 ECE +3. Angel 1317382 EEE +4. Jasper 2366236 EEE +5. Clara 4328992 MAT +6. Eliana 9090341 MAT +7. Sarah 1334921 MAT + +Batch 3: +1. Kevin 4271786 MEC +2. Daisy 7364891 MIN +3. Daniel 8783261 MIN +4. William 4662376 MIN +5. Nora 4123894 PHE +6. Owen 4898291 PHE +7. Ryan 9071271 PHE +