Advertisement
nigredo

FP 06 2014

Jun 6th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.78 KB | None | 0 0
  1. import java.time.format.DateTimeFormatter
  2. import java.time.LocalDate
  3. import java.time.temporal.ChronoUnit
  4.  
  5. val formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy")
  6.  
  7. def string2Date(s: String): LocalDate =
  8.   LocalDate.parse(s, formatter)
  9.  
  10. val zero = LocalDate.of(2013, 01, 01)
  11.  
  12. def date2Int(date: LocalDate): Int =
  13.   zero.until(date, ChronoUnit.DAYS).toInt
  14.  
  15. def string2Int = string2Date _ andThen date2Int
  16.  
  17. val events = Array(
  18.     "27.09.2013", "06.10.2013", "23.10.2013", "06.11.2013", "26.11.2013", "27.11.2013", "21.12.2013", "30.12.2013",
  19.     "06.01.2014", "16.01.2014", "17.01.2014", "21.01.2014", "25.01.2014", "26.01.2014", "05.02.2014", "11.02.2014",
  20.     "21.02.2014", "02.03.2014", "07.03.2014", "30.03.2014", "08.04.2014", "18.04.2014", "23.04.2014", "27.04.2014",
  21.     "02.05.2014", "15.05.2014", "17.05.2014", "18.05.2014", "19.05.2014", "20.05.2014", "25.05.2014", "26.05.2014",
  22.     "28.05.2014") map string2Int
  23. val start = events.head
  24. val finish = events.last
  25.  
  26. // количество событий на каждый класс в периоде
  27. def eventCounts(period: Int): Seq[Int] = {
  28.   val groups: Map[Int, Int] = events groupBy (_ % period) mapValues (_.length) withDefaultValue 0
  29.   0 until period map groups
  30. }
  31. // Общее количество дней на каждый класс в периоде
  32. def dayCounts(period: Int): Seq[Int] =
  33.   0 until period map (r => (finish - r) / period - (start - r) / period + 1)
  34.  
  35. // Вероятности для каждого класса
  36. def probabilities(period: Int): Seq[Double] =
  37.   eventCounts(period) zip dayCounts(period) map { case (ev, all) => ev.toDouble / all}
  38.  
  39. def answerOption: Option[Int] = 2 to (finish - start) find (p => probabilities(p) withFilter (_ >= 0.5))
  40.  
  41. println(answerOption.get) //выдает 26
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement