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

Kotlin Task #5

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
29 changes: 24 additions & 5 deletions src/main/kotlin/assignment/Assignment.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package assignment
import java.io.File


// INSTRUCTIONS:
// 1. Read a list of student responses from text file
Expand All @@ -19,15 +21,32 @@ fun main() {
val processedData = processResponses(responses)
writeToFile(processedData)
}

fun tobool(s:String):Boolean{
return s.uppercase()=="YES"
}
fun readResponses(): List<Response> {
TODO()
val read: List<String> = File("responses.txt").readLines()
val responses = mutableListOf<Response>()
for (i in 1 until read.size){
var line=read[i]
line =line.replace("\\s+".toRegex()," ")//to delete multiple spaces
var eachColumn =line.split(" ")
responses.add(Response(eachColumn[2], eachColumn[3].toInt(), eachColumn[4], tobool(eachColumn[5])))
}
return responses.toList()
}

fun processResponses(responses: List<Response>): List<List<Response>> {
TODO()
return responses.filter { !it.covidPositive }.sortedWith(compareBy({it.branch},{it.name})).groupBy { it.branch}.values.chunked(3).map { it.flatten() }
}

fun writeToFile(batches: List<List<Response>>) {
TODO()
}
val fileName = File("batches.txt")


batches.forEachIndexed { batchId, batch -> fileName.appendText("Batch ${batchId + 1}:\n")
batch.forEachIndexed { s_no, student -> fileName.appendText("${s_no+1}\t\t${student.name}\t\t${student.rollNumber}\t\t${student.branch}\n")}

fileName.appendText("\n")
}
}
41 changes: 21 additions & 20 deletions src/main/kotlin/assignment/batches.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
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
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
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
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