Created
March 4, 2021 08:40
-
-
Save Eladkay/69b8f6a6028bde345d33f27753ec9071 to your computer and use it in GitHub Desktop.
Find average of chosen courses over given semesters, as well as calculate the average of the given semesters over the chosen courses
This file contains 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
import java.net.URL | |
data class Course(val number: String, var avg: Double = 0.0, val avgBySem: MutableMap<String, Double> = mutableMapOf()) | |
val bucket = listOf("104031", "104166", "234114", "234129", | |
"104032", "104134", "114071", "234124", "234141", | |
"044252", "094412", "234218", "234292", "236319") | |
val semesters = listOf("202001", "201902", "201901", "201802", "201801", "201702", "201701", "201602", "201601") | |
fun link(course: String, semester: String) | |
= "https://raw.githubusercontent.com/michael-maltsev/technion-histograms/master/$course/$semester/Finals.json" | |
fun main(args: Array<String>) { | |
val courses = bucket.map { Course(it) } | |
for (course in courses) { | |
semesters@for (semester in semesters) { | |
val link = link(course.number, semester) | |
try { | |
val json = URL(link).readText() | |
val averageLine = json.split("\n").first { "average" in it }.trim() // bad | |
val average = averageLine.split(" ")[1].removePrefix("\"").removeSuffix("\",") | |
course.avgBySem[semester] = average.toDouble() | |
course.avg = course.avgBySem.values.average() | |
} catch (e: Exception) { | |
continue@semesters | |
} | |
} | |
} | |
println("--------------") | |
println("Course Averages:") | |
println("--------------") | |
for(course in courses) println("${course.number}: ${course.avg}") | |
println("--------------") | |
println("Semester Averages:") | |
println("--------------") | |
for(semester in semesters) { | |
var sum = 0.0 | |
var num = 0 | |
courses@for(course in courses) { | |
if(semester !in course.avgBySem) continue@courses | |
num++ | |
sum += course.avgBySem[semester]!! | |
} | |
println("$semester: ${sum / num} ($num)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment