Skip to content

Instantly share code, notes, and snippets.

@gustavofranke
Created May 3, 2017 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gustavofranke/205168f6a568daf525761df95e0860ad to your computer and use it in GitHub Desktop.
Save gustavofranke/205168f6a568daf525761df95e0860ad to your computer and use it in GitHub Desktop.
exercise
// Write some code, that will flatten an array of arbitrarily
// nested arrays of integers into a flat array of integers.
// e.g. [[1,2,[3]],4] -> [1,2,3,4].
val a = List(List(1,2,List(3)),4)
val b = List(1,2,List(3))
///////////////////
//implicit class WrapFlatten(val ls: List[Any]) {
def flatten(ls: List[Any]): List[Any] = ls flatMap {
case i: List[_] => flatten(i)
case e => List(e)
}
val k = List(1, List(2, 3), List(List(List(List(4)), List(5)), List(6, 7)), 8)
flatten(k)
flatten(a)
//val z = k.flatten
///////////////////
// We have some customer records in a text file
// (customers.json, attached) one customer per line,
// JSON-encoded. We want to invite any customer within 100km
// of our Dublin office (GPS coordinates 53.3381985, -6.2592576)
// to some food and drinks on us.
// Write a program that will read the full list of customers and
// output the names and user ids of matching customers (within 100km),
// sorted by user id (ascending).
// You can use the first formula from this Wikipedia
// article to calculate distance, don’t forget, you’ll need to
// convert degrees to radians. Your program should be fully
// tested too.
/* https://en.wikipedia.org/wiki/Great-circle_distance
* customer names and userId
* */
import org.json4s._
import org.json4s.jackson.JsonMethods._
//import org.json4s.native.JsonMethods._
implicit val formats = DefaultFormats // Brings in default date formats etc.
////////////////////////////
import math._
def haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double) = {
val dLat = (lat2 - lat1).toRadians
val dLon = (lon2 - lon1).toRadians
val a = pow(sin(dLat / 2), 2) + pow(sin(dLon / 2), 2) * cos(lat1.toRadians) * cos(lat2.toRadians)
val c = 2 * asin(sqrt(a))
6371 * c
}
////////////////////////////
case class Customer(latitude: String, user_id: Int, name: String, longitude: String)
val a1 = """{"latitude": "52.986375", "user_id": 12, "name": "Christina McArdle", "longitude": "-6.043701"}"""
val b1 = """{"latitude": "53", "user_id": 13, "name": "Olive Ahearn", "longitude": "-7"}"""
val pa = parse(a1).extract[Customer]
val pb = parse(b1).extract[Customer]
//////////////////////////////////
val intercomLatitude = 53.3381985
val intercomLongitude = -6.2592576
val path = "/home/gustavo/workspace/funprog-assignements/montecarlo/src/main/resources/customers.txt"
val inputLines = io.Source
.fromFile(path)
.getLines
.toList
inputLines.length
val parsed = inputLines
.map(parse(_).extract[Customer])
.map(cust => (cust.name, cust.user_id, haversine(cust.latitude.toDouble, cust.longitude.toDouble, intercomLatitude, intercomLongitude)))
.filter(_._3 <= 100)
.sortBy(_._2)
parsed.length
parsed.foreach(x => println(s"${x._1} | ${x._2}"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment