Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * You can edit, run, and share this code.
- * play.kotlinlang.org
- */
- import java.time.LocalDateTime
- import java.time.temporal.ChronoUnit
- import java.time.format.DateTimeFormatter
- fun main() {
- 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")
- 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")
- 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")
- val requiredLastDaysCount = 3
- val requiredSessionCount = 6
- val hasEnoughSessions = hasEnoughSessions(requiredSessionCount, c)
- val wasUsedForEnoughLastDays = wasUsedForLastDays(requiredLastDaysCount, c.asReversed())
- if (hasEnoughSessions and wasUsedForEnoughLastDays) {
- println("Ask user for the opinion")
- }
- }
- private fun wasUsedForLastDays(requiredLastDaysCount : Int, dates: List<String>) : Boolean {
- val format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
- val lastOpenDate = LocalDateTime.parse(dates[0], format)
- for (day in 0..requiredLastDaysCount - 1) {
- var usedThatDay = false
- for (dateIndex in 1 until dates.size) {
- val currentDate = LocalDateTime.parse(dates[dateIndex], format)
- val dayDifference = Math.abs(ChronoUnit.DAYS.between(lastOpenDate, currentDate)).toInt()
- if (dayDifference == day){
- usedThatDay = true
- }
- }
- if (!usedThatDay) return false
- }
- return true
- }
- private fun hasEnoughSessions(requiredSessionCount: Int, dates: List<String>) : Boolean{
- var sessionCount = 0
- val indexesToExclude = mutableListOf<Int>()
- dates.forEachIndexed { index, date ->
- if (!indexesToExclude.contains(index)) {
- indexesToExclude.addAll(session(date, dates))
- sessionCount = sessionCount + 1
- }
- }
- return sessionCount >= requiredSessionCount
- }
- /**
- * Checks for the dates associated with the given session
- *
- * @date to check all of the other dates against
- * @dates list of all dates
- *
- * @returns indexes of the dates that are considered to be the same session
- */
- private fun session(date: String, dates: List<String>): List<Int>{
- val sessionIndexes = mutableListOf<Int>()
- val format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
- val date = LocalDateTime.parse(date, format)
- dates.forEachIndexed { index, otherDateString ->
- val otherDate = LocalDateTime.parse(otherDateString, format)
- if (date.isSameSession(otherDate)) {
- sessionIndexes.add(index)
- }
- }
- return sessionIndexes
- }
- private fun LocalDateTime.isSameSession(otherDate: LocalDateTime) : Boolean {
- if (this != otherDate) {
- val difference = ChronoUnit.MINUTES.between(this, otherDate)
- if (difference <= 30) return true
- }
- return false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement