Created
April 14, 2017 12:00
-
-
Save onegrx/24e3301e6131a84cba8a55dec5d21d8b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.SimpleDateFormat | |
import java.util.{Calendar, Date} | |
object Sessions extends App { | |
def survey(logs: List[String]): Boolean = { | |
val formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") | |
val dates: List[Date] = logs.map(formatter.parse).sortBy(_.getTime) | |
def getCalendar(d: Date): Calendar = { | |
val c = Calendar.getInstance() | |
c.setTime(d) | |
c | |
} | |
def sameDay(d1: Date, d2: Date): Boolean = { | |
val c1 = getCalendar(d1) | |
val c2 = getCalendar(d2) | |
c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && | |
c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR) | |
} | |
def nextDay(d1: Date, d2: Date): Boolean = { | |
val c1 = getCalendar(d1) | |
val c2 = getCalendar(d2) | |
c1.add(Calendar.DATE, 1) | |
c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && | |
c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR) | |
} | |
def threeDays(dates: List[Date]): Boolean = { | |
var consecutive = 1 | |
for(i <- 0 until dates.length - 1) { | |
val current = dates(i) | |
val next = dates(i + 1) | |
if (nextDay(current, next)) consecutive += 1 | |
else if (!sameDay(current, next)) consecutive = 1 | |
if(consecutive >= 3) return true | |
} | |
false | |
} | |
def sixSessions(dates: List[Date]): Boolean = { | |
var sessions = 1 | |
for(i <- 0 until dates.length - 1) { | |
val c = getCalendar(dates(i)) | |
c.add(Calendar.MINUTE, 10) | |
if(dates(i + 1).after(c.getTime)) sessions += 1 | |
if (sessions >= 6) return true | |
} | |
false | |
} | |
threeDays(dates) && sixSessions(dates) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment