Skip to content

Instantly share code, notes, and snippets.

@lrlucena
Created November 12, 2012 21:57
Show Gist options
  • Save lrlucena/4062246 to your computer and use it in GitHub Desktop.
Save lrlucena/4062246 to your computer and use it in GitHub Desktop.
Scala Dojo - ThoughtWorks
package main.scala
import math.{ abs, min }
import Int.MaxValue
case class Map(val airports: Seq[(Int, Int)] = Seq(), val clouds: Seq[(Int, Int)] = Seq()) {
def addAirports(coordinates: Seq[(Int, Int)]) = Map(airports ++ coordinates, clouds)
def airportAt(coordinate: (Int, Int)) = airports.exists(_ == coordinate)
def addClouds(coordinates: Seq[(Int, Int)]) = Map(airports, clouds ++ coordinates)
def cloudAt(coordinate: (Int, Int)) = clouds.exists(_ == coordinate)
private lazy val daysToAirport =
for (airport <- airports) yield {
for (
cloud <- clouds;
distanceX = abs(cloud._1 - airport._1);
distanceY = abs(cloud._2 - airport._2);
sumDistance = distanceX + distanceY
) yield sumDistance
}.foldRight(MaxValue)(min)
def daysToFirstAirport = daysToAirport.min
def daysToLastAirport = daysToAirport.max
}
package ashcloud
import org.scalatest.WordSpec
class MapTest extends WordSpec {
"map"should {
"be build with lines and columns" in {
val map = new Map(2, 2)
assert(map.lines == 2)
assert(map.columns == 2)
}
"receive the coordinates for airports" in {
val coordinates = Seq((0, 0), (1, 1))
val map = new Map(2, 2)
map.addAirports(coordinates)
assert(map.getAirportAt((0,0)) != None)
assert(map.getAirportAt((0,1)) == None)
}
"receive the coordinates for clouds" in {
val coordinates = Seq((0, 0), (1, 1))
val map = new Map(2, 2)
map.addClouds(coordinates)
assert(map.getCloudAt((0,0)) != None)
assert(map.getCloudAt((0,1)) == None)
}
"calculate correctly number of days to first airport in the case 1" in{
val coordinatesClouds = Seq((0, 0))
val coordinatesAirports = Seq((2, 1))
val map = new Map(3, 3)
map.addClouds(coordinatesClouds)
map.addAirports(coordinatesAirports)
assert(map.getDistanceInDaysToFirstAirport() == 3)
}
"calculate correctly number of days to first airport in the case 2" in{
val coordinatesClouds = Seq((2, 2))
val coordinatesAirports = Seq((2, 4))
val map = new Map(6, 7)
map.addClouds(coordinatesClouds)
map.addAirports(coordinatesAirports)
assert(map.getDistanceInDaysToFirstAirport() == 2)
}
"calculate correctly number of days to last airport in the case 1" in{
val coordinatesClouds = Seq((0, 0))
val coordinatesAirports = Seq((2, 1), (2, 4))
val map = new Map(6, 7)
map.addClouds(coordinatesClouds)
map.addAirports(coordinatesAirports)
assert(map.getDistanceInDaysToLastAirport() == 6)
}
"calculate correctly number of days to last airport in the case 2" in{
val coordinatesClouds = Seq((0, 0))
val coordinatesAirports = Seq((0, 1), (0, 2))
val map = new Map(6, 7)
map.addClouds(coordinatesClouds)
map.addAirports(coordinatesAirports)
assert(map.getDistanceInDaysToLastAirport() == 2)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment