Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.86 KB | None | 0 0
  1. import scala.annotation.tailrec
  2.  
  3.  
  4. object td3 extends App {
  5.     val nbOfStudents = 150
  6.     val nbOfMark = 5
  7.  
  8.     val r = scala.util.Random
  9.  
  10.  
  11.     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")
  12.  
  13.     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")
  14.  
  15.     /* From a list of name and first name generate a tuple which represent a student
  16.     scala>studentGenerator(names,firstnames)
  17.     res0: (String, String) = (Vincent,Ludovic)
  18.    */
  19.     def studentGenerator(names: List[String], firstNames: List[String]): (String, String) = (names(r.nextInt(names.size)),firstnames(r.nextInt(firstnames.size)))
  20.  
  21.     /*
  22.     theStudents : a list of nbOfStudents of Students
  23.    */
  24.     lazy val theStudents: List[(String, String)] = List.fill(nbOfStudents)(studentGenerator(names,firstnames))
  25.  
  26.     /*
  27.     marksGenerator : generates a list of mark, with a maximum nbOfMarks of mark
  28.    */
  29.     def marksGenerator(nbOfMarks: Int): List[Double] = List.fill(r.nextInt(nbOfMark)+1)(r.nextDouble()*20)
  30.  
  31.     /*
  32.     marksForClass : generates a list of Students with marks
  33.    */
  34.     def marksForClass(nb: Int, aClass: List[(String, String)]): List[((String, String), List[Double])] = aClass.map(x => (x,marksGenerator(nb)))
  35.  
  36.     /*
  37.     theClassMark : the list of the same students than theStudent with marks
  38.    */
  39.     lazy val theClassMark: List[((String, String), List[Double])] = marksForClass(nbOfMark,theStudents)
  40.  
  41.     /*
  42.     average : From a list of mark, generates the average
  43.    */
  44.     def average(marks: List[Double]): Double = marks.sum / marks.size
  45.  
  46.     /*
  47.     averageClass : from a list of students (with marks) generates the average marks for each students (for those who have mark)
  48.    */
  49.     def averageClass(aClass: List[((String, String), List[Double])]): List[((String, String), Double)] = aClass.map{case (x,y) => (x,average(y))}
  50.  
  51.     lazy val theClassResult = averageClass(theClassMark)
  52.  
  53.     /*
  54.     betterStudent : which student is better than the other
  55.    */
  56.     def betterStudent(aStudent: ((String, String), Double), anotherStudent: ((String, String), Double)): ((String, String), Double) = if(aStudent._2 > anotherStudent._2) aStudent else anotherStudent
  57.  
  58.     /*
  59.     worstStudent : which student is better than the other
  60.    */
  61.     def worstStudent(aStudent: ((String, String), Double), anotherStudent: ((String, String), Double)): ((String, String), Double) = if(aStudent._2 < anotherStudent._2) aStudent else anotherStudent
  62.  
  63.     /*
  64.     theBestStudent is the student which has the highest average mark.
  65.    */
  66.     lazy val theBestStudent = theClassResult.reduce(betterStudent)
  67.  
  68.     /*
  69.     theWorstStudent is the student which has the highest average mark.
  70.    */
  71.     lazy val theWorstStudent = theClassResult.reduce(worstStudent)
  72.  
  73.  
  74.     /*
  75.     qualifiedStudents are all students that have an average gretter (or equal) to 10
  76.    */
  77.     lazy val qualifiedStudents = theClassResult.filter(_._2 >= 10)
  78.  
  79.     /*
  80.     prettyrPrintResult : a pretty printer for final result of a student
  81.    */
  82.     def prettyrPrintResult(aStudent: ((String, String), Double)): Unit = println(s"${aStudent._1._2} ${aStudent._1._1} : ${aStudent._2}")
  83.  
  84.     /*
  85.     pretty print the class results, the qualified students and the best one !
  86.    */
  87.     def printInformations(): Unit = {
  88.         println("----------    La classe :    ----------")
  89.         theClassResult.foreach(prettyrPrintResult)
  90.         println()
  91.         println("----------    Ceux qui passent :    ----------")
  92.         qualifiedStudents.foreach(prettyrPrintResult)
  93.         println()
  94.         println("----------    Le meilleur :    ----------")
  95.         prettyrPrintResult(theBestStudent)
  96.         println()
  97.         println("----------    Thomas Lavaud :    ----------")
  98.         prettyrPrintResult(theWorstStudent)
  99.     }
  100.  
  101.     // remove comments after to print
  102.     printInformations()
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement