-
-
Save rsrini7/a1e13f8cfd7a397a7802facdcc501e42 to your computer and use it in GitHub Desktop.
Scala Dojo - ThoughtWorks
This file contains 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
package ashcloud | |
class Map(val lines: Int, val columns: Int) { | |
var airports: Seq[(Int,Int)] = Seq() | |
var clouds: Seq[(Int,Int)] = Seq() | |
def addAirports(coordinates: Seq[(Int,Int)]) { | |
airports = airports ++ coordinates | |
} | |
def getAirportAt(coordinate: (Int, Int))= { | |
airports.find(_ == coordinate) | |
} | |
def addClouds(coordinates: Seq[(Int,Int)]) { | |
clouds = clouds ++ coordinates | |
} | |
def getCloudAt(coordinate: (Int, Int))= { | |
clouds.find(_ == coordinate) | |
} | |
def getDistanceInDaysToFirstAirport() = { | |
var nearest : Int = Int.MaxValue | |
for (cloud <- clouds){ | |
for(airport <- airports){ | |
var distanceX : Int = math.abs(cloud._1 - airport._1); | |
var distanceY : Int = math.abs( cloud._2 - airport._2); | |
var sumDistance = distanceX + distanceY | |
if(sumDistance < nearest){ | |
nearest = sumDistance | |
} | |
} | |
} | |
nearest | |
} | |
def getDistanceInDaysToLastAirport() = { | |
var nearest : Int = Int.MinValue | |
for (cloud <- clouds){ | |
for(airport <- airports){ | |
var distanceX : Int = math.abs(cloud._1 - airport._1); | |
var distanceY : Int = math.abs( cloud._2 - airport._2); | |
var sumDistance = distanceX + distanceY | |
if(sumDistance > nearest){ | |
nearest = sumDistance | |
} | |
} | |
} | |
nearest | |
} | |
} |
This file contains 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
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