Advertisement
Guest User

Untitled

a guest
Mar 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.17 KB | None | 0 0
  1. /**
  2.  * You can edit, run, and share this code.
  3.  * play.kotlinlang.org
  4.  */
  5.  
  6. import java.time.LocalDateTime
  7. import java.time.temporal.ChronoUnit
  8. import java.time.format.DateTimeFormatter
  9.  
  10. fun main() {    
  11.     val a = listOf("2017-03-10 08:13:11", "2017-03-10 19:01:27", "2017-03-11 07:35:55", "2017-03-11 16:15:11", "2017-03-12 08:01:41", "2017-03-12 17:19:08")
  12.     val b = listOf("2017-03-10 18:58:11", "2017-03-10 19:01:27", "2017-03-11 07:35:55", "2017-03-11 16:15:11", "2017-03-12 08:01:41", "2017-03-12 17:19:08")
  13.     val c = listOf("2017-03-08 17:11:13", "2017-03-11 17:22:47", "2017-03-11 19:23:51", "2017-03-11 22:03:12", "2017-03-12 08:32:04", "2017-03-12 13:19:08", "2017-03-12 17:19:08")
  14.    
  15.     val requiredLastDaysCount = 3
  16.     val requiredSessionCount = 6
  17.    
  18.     val hasEnoughSessions = hasEnoughSessions(requiredSessionCount, c)
  19.     val wasUsedForEnoughLastDays = wasUsedForLastDays(requiredLastDaysCount, c.asReversed())
  20.    
  21.     if (hasEnoughSessions and wasUsedForEnoughLastDays) {
  22.         println("Ask user for the opinion")
  23.     }
  24. }
  25.  
  26. private fun wasUsedForLastDays(requiredLastDaysCount : Int, dates: List<String>) : Boolean {    
  27.     val format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
  28.     val lastOpenDate = LocalDateTime.parse(dates[0], format)
  29.    
  30.     for (day in 0..requiredLastDaysCount - 1) {
  31.         var usedThatDay = false
  32.         for (dateIndex in 1 until dates.size) {
  33.             val currentDate = LocalDateTime.parse(dates[dateIndex], format)
  34.             val dayDifference = Math.abs(ChronoUnit.DAYS.between(lastOpenDate, currentDate)).toInt()
  35.             if (dayDifference == day){
  36.                 usedThatDay = true
  37.             }
  38.         }
  39.        
  40.         if (!usedThatDay) return false
  41.     }
  42.    
  43.     return true
  44. }
  45.  
  46. private fun hasEnoughSessions(requiredSessionCount: Int, dates: List<String>) : Boolean{
  47.     var sessionCount = 0
  48.     val indexesToExclude = mutableListOf<Int>()
  49.    
  50.     dates.forEachIndexed { index, date ->
  51.         if (!indexesToExclude.contains(index)) {
  52.             indexesToExclude.addAll(session(date, dates))
  53.             sessionCount = sessionCount + 1
  54.         }
  55.     }
  56.    
  57.     return sessionCount >= requiredSessionCount
  58. }
  59.  
  60. /**
  61.  * Checks for the dates associated with the given session
  62.  *
  63.  * @date to check all of the other dates against
  64.  * @dates list of all dates
  65.  *
  66.  * @returns indexes of the dates that are considered to be the same session
  67.  */
  68. private fun session(date: String, dates: List<String>): List<Int>{
  69.     val sessionIndexes = mutableListOf<Int>()
  70.     val format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
  71.    
  72.     val date =  LocalDateTime.parse(date, format)
  73.     dates.forEachIndexed { index, otherDateString ->
  74.         val otherDate = LocalDateTime.parse(otherDateString, format)
  75.         if (date.isSameSession(otherDate)) {
  76.             sessionIndexes.add(index)
  77.         }
  78.     }
  79.    
  80.     return sessionIndexes
  81. }
  82.  
  83. private fun LocalDateTime.isSameSession(otherDate: LocalDateTime) : Boolean {
  84.      if (this != otherDate) {
  85.           val difference = ChronoUnit.MINUTES.between(this, otherDate)  
  86.           if (difference <= 30) return true
  87.         }
  88.      return false
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement