Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed :D #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions src/main/kotlin/assignment/Assignment.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,20 +16,59 @@ 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)
writeToFile(processedData)
}

fun readResponses(): List<Response> {
TODO()
val responseObj: MutableList<Response> = mutableListOf()

// Read the raw input
val inputFile: String = "src/main/kotlin/assignment/responses.txt"
val inputLines: List<String> = 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<Response>): List<List<Response>> {
TODO()
// Filter covid positive ones out
val noCovid: List<Response> = responses.filter { !it.covidPositive }

// Sort them by branch and then their names
val sortedVal: List<Response> = 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<List<Response>>) {
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")
}
}
28 changes: 28 additions & 0 deletions src/main/kotlin/assignment/batches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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