Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.annotation.tailrec
- object td3 extends App {
- val nbOfStudents = 150
- val nbOfMark = 5
- val r = scala.util.Random
- val names = List("Martin", "Bernard", "Dubois", "Thomas", "Robert", "Richard", "Petit", "Durand", "Leroy", "Moreau", "Simon", "Laurent", "Lefebvre", "Michel", "Garcia", "David", "Bertrand", "Roux", "Vincent", "Fournier", "Morel", "Girard", "Andre", "Lefevre", "Mercier", "Dupont", "Lambert", "Bonnet", "Francois", "Martinez", "Garnier", "Faure", "Rousseau", "Blanc", "Guerin", "Muller", "Henry", "Roussel", "Nicolas", "Perrin", "Morin", "Mathieu", "Clement", "Gauthier", "Dumont", "Lopez", "Fontaine", "Chevalier", "Robin", "Masson", "Sanchez", "Gerard", "Nguyen", "Boyer", "Denis", "Lemaire", "Duval", "Joly", "Gautier")
- val firstnames = List("Adam", "Alex", "Alexandre", "Alexis", "Anthony", "Antoine", "Benjamin", "Cédric", "Charles", "Christopher", "David", "Dylan", "Édouard", "Elliot", "Émile", "Étienne", "Félix", "Gabriel", "Guillaume", "Hugo", "Isaac", "Jacob", "Jérémy", "Jonathan", "Julien", "Justin", "Léo", "Logan", "Loïc", "Louis", "Lucas", "Ludovic", "Malik", "Mathieu", "Mathis", "Maxime", "Michaël", "Nathan", "Nicolas", "Noah", "Olivier", "Philippe", "Raphaël", "Samuel", "Simon", "Thomas", "Tommy", "Tristan", "Victor", "Vincent")
- /* From a list of name and first name generate a tuple which represent a student
- scala>studentGenerator(names,firstnames)
- res0: (String, String) = (Vincent,Ludovic)
- */
- def studentGenerator(names: List[String], firstNames: List[String]): (String, String) = (names(r.nextInt(names.size)),firstnames(r.nextInt(firstnames.size)))
- /*
- theStudents : a list of nbOfStudents of Students
- */
- lazy val theStudents: List[(String, String)] = List.fill(nbOfStudents)(studentGenerator(names,firstnames))
- /*
- marksGenerator : generates a list of mark, with a maximum nbOfMarks of mark
- */
- def marksGenerator(nbOfMarks: Int): List[Double] = List.fill(r.nextInt(nbOfMark)+1)(r.nextDouble()*20)
- /*
- marksForClass : generates a list of Students with marks
- */
- def marksForClass(nb: Int, aClass: List[(String, String)]): List[((String, String), List[Double])] = aClass.map(x => (x,marksGenerator(nb)))
- /*
- theClassMark : the list of the same students than theStudent with marks
- */
- lazy val theClassMark: List[((String, String), List[Double])] = marksForClass(nbOfMark,theStudents)
- /*
- average : From a list of mark, generates the average
- */
- def average(marks: List[Double]): Double = marks.sum / marks.size
- /*
- averageClass : from a list of students (with marks) generates the average marks for each students (for those who have mark)
- */
- def averageClass(aClass: List[((String, String), List[Double])]): List[((String, String), Double)] = aClass.map{case (x,y) => (x,average(y))}
- lazy val theClassResult = averageClass(theClassMark)
- /*
- betterStudent : which student is better than the other
- */
- def betterStudent(aStudent: ((String, String), Double), anotherStudent: ((String, String), Double)): ((String, String), Double) = if(aStudent._2 > anotherStudent._2) aStudent else anotherStudent
- /*
- worstStudent : which student is better than the other
- */
- def worstStudent(aStudent: ((String, String), Double), anotherStudent: ((String, String), Double)): ((String, String), Double) = if(aStudent._2 < anotherStudent._2) aStudent else anotherStudent
- /*
- theBestStudent is the student which has the highest average mark.
- */
- lazy val theBestStudent = theClassResult.reduce(betterStudent)
- /*
- theWorstStudent is the student which has the highest average mark.
- */
- lazy val theWorstStudent = theClassResult.reduce(worstStudent)
- /*
- qualifiedStudents are all students that have an average gretter (or equal) to 10
- */
- lazy val qualifiedStudents = theClassResult.filter(_._2 >= 10)
- /*
- prettyrPrintResult : a pretty printer for final result of a student
- */
- def prettyrPrintResult(aStudent: ((String, String), Double)): Unit = println(s"${aStudent._1._2} ${aStudent._1._1} : ${aStudent._2}")
- /*
- pretty print the class results, the qualified students and the best one !
- */
- def printInformations(): Unit = {
- println("---------- La classe : ----------")
- theClassResult.foreach(prettyrPrintResult)
- println()
- println("---------- Ceux qui passent : ----------")
- qualifiedStudents.foreach(prettyrPrintResult)
- println()
- println("---------- Le meilleur : ----------")
- prettyrPrintResult(theBestStudent)
- println()
- println("---------- Thomas Lavaud : ----------")
- prettyrPrintResult(theWorstStudent)
- }
- // remove comments after to print
- printInformations()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement