Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.format.DateTimeFormatter
- import java.time.LocalDate
- import java.time.temporal.ChronoUnit
- val formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy")
- def string2Date(s: String): LocalDate =
- LocalDate.parse(s, formatter)
- val zero = LocalDate.of(2013, 01, 01)
- def date2Int(date: LocalDate): Int =
- zero.until(date, ChronoUnit.DAYS).toInt
- def string2Int = string2Date _ andThen date2Int
- val events = Array(
- "27.09.2013", "06.10.2013", "23.10.2013", "06.11.2013", "26.11.2013", "27.11.2013", "21.12.2013", "30.12.2013",
- "06.01.2014", "16.01.2014", "17.01.2014", "21.01.2014", "25.01.2014", "26.01.2014", "05.02.2014", "11.02.2014",
- "21.02.2014", "02.03.2014", "07.03.2014", "30.03.2014", "08.04.2014", "18.04.2014", "23.04.2014", "27.04.2014",
- "02.05.2014", "15.05.2014", "17.05.2014", "18.05.2014", "19.05.2014", "20.05.2014", "25.05.2014", "26.05.2014",
- "28.05.2014") map string2Int
- val start = events.head
- val finish = events.last
- // количество событий на каждый класс в периоде
- def eventCounts(period: Int): Seq[Int] = {
- val groups: Map[Int, Int] = events groupBy (_ % period) mapValues (_.length) withDefaultValue 0
- 0 until period map groups
- }
- // Общее количество дней на каждый класс в периоде
- def dayCounts(period: Int): Seq[Int] =
- 0 until period map (r => (finish - r) / period - (start - r) / period + 1)
- // Вероятности для каждого класса
- def probabilities(period: Int): Seq[Double] =
- eventCounts(period) zip dayCounts(period) map { case (ev, all) => ev.toDouble / all}
- def answerOption: Option[Int] = 2 to (finish - start) find (p => probabilities(p) withFilter (_ >= 0.5))
- println(answerOption.get) //выдает 26
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement