Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save albertogiunta/db900ed80215a3af1cadfe4a0dbe4996 to your computer and use it in GitHub Desktop.
Save albertogiunta/db900ed80215a3af1cadfe4a0dbe4996 to your computer and use it in GitHub Desktop.
This code was used for the Hackerrank Twitter challenge. The code works well on 6 of the 9 test cases, but they don't tell you what's wrong in the wrong test cases, hence it's quite difficult to know what's wrong.
import java.text.SimpleDateFormat
import java.util.{Calendar, Date}
import scala.collection.mutable
object Solution {
def main(args: Array[String]) {
val rawLogs = mutable.MutableList[String](
"10.10.10.10 - - [27/Sep/2016:05:22:00 +0000] \"GET /1.1/friendships/list.json?user_id=123 HTTP/1.1\" 500 563 19 \"Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)\" 177.177.177.177",
"10.10.10.10 - - [27/Sep/2016:05:22:08 +0000] \"GET /1.1/friendships/list.json?user_id=123 HTTP/1.1\" 200 563 19 \"Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)\" 177.177.177.177",
"10.10.10.10 - - [27/Sep/2016:05:22:31 +0000] \"GET /1.1/friendships/list.json HTTP/1.1\" 200 563 19 \"Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)\" 177.177.177.177",
"10.10.10.10 - - [27/Sep/2016:05:22:59 +0000] \"GET /1.1/friendships/list.json HTTP/1.1\" 200 94 6 \"Twitter-iPhone/6.63 iOS/10.0.1 (Apple;iPhone7,2;;;;;1)\" 177.177.177.177",
"10.10.10.10 - - [27/Sep/2016:05:23:01 +0000] \"GET /1.1/users/show.json?include_entities=1&user_id=321 HTTP/1.1\" 200 4160 51 \"Twitter-iPhone/6.63 iOS/9.3.5 (Apple;iPhone7,2;;;;;0)\" 177.177.177.177",
"10.10.10.10 - - [27/Sep/2016:22:45:33 +0000] \"GET /1.1/friendships/list.json?user_id=234 HTTP/1.1\" 200 563 19 \"Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)\" 177.177.177.177",
"10.10.10.10 - - [27/Sep/2016:22:45:51 +0000] \"POST /1.1/friendships/create.json HTTP/1.1\" 200 4193 120 \"Twitter-iPhone/6.62.1 iOS/9.3.5 (Apple;iPhone7,2;;;;;0)\" 177.177.177.177"
)
// val rawLogs = mutable.MutableList[String]()
// var line = ""
// while ({line = StdIn.readLine(); line != null}) {
// rawLogs += line
// }
val logs = mutable.MutableList[Log]()
val cal = Calendar.getInstance()
rawLogs.foreach(it => {
val date = """(\d\d)\/([A-Z]\w+)\/(\d\d\d\d):(\d\d):(\d\d)""".r
val uri = """\s+(?:\/[A-Za-z0-9$.+!*'(){},~:;=@#%_\-]*)+""".r
val code = """\s(\d+)\s""".r
cal
.setTime(new java.text.SimpleDateFormat("dd/MMM/yyyy:HH:mm")
.parse(date.findFirstIn(it).getOrElse("")))
val log = Log(
cal.getTime,
uri.findFirstIn(it).getOrElse("").trim,
code.findFirstIn(it).getOrElse("").split("\" ")(0).trim)
logs += log
})
var currentLog = logs.head
val listFFS = mutable.MutableList[Log](currentLog)
logs.foreach(log => {
if (log.date.after(currentLog.date) || log.url != currentLog.url) {
currentLog = log
listFFS += log
}
listFFS.last.total += 1
if (log.code != "500") listFFS.last.not500 += 1
})
listFFS
.sortBy(it => (it.date, it.url))
.foreach(it => println(f"${new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").format(it.date)} ${it.url} ${(100 * it.not500 / it.total).toFloat}%.2f"))
}
case class Log(date: Date, url: String, code: String, var total: Int = 0, var not500: Int = 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment