Skip to content

Commit 88e480e

Browse files
author
Jarvis@LNMIIT
committed
Scala Implementation of Euclidean Norm Algorithn
1 parent 26b61ea commit 88e480e

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* SCALA Implementation of Euclidean Norm.
2+
Though the corner cases are covered. But still if you find any additions to it,
3+
please do add a test for it.
4+
Any improvements/tests in the code is highly appreciated.
5+
*/
6+
7+
8+
9+
import scala.math
10+
11+
class EuclideanNorm {
12+
13+
def calculateEuclideanNorm(listofIntegers: List[Int]):Int = {
14+
15+
val squaredElements = listofIntegers.map(x => x*x).sum
16+
math.sqrt(squaredElements).toInt
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import org.scalatest.{Matchers, FunSuite}
2+
3+
class calculateEuclideanNormTest extends FunSuite with Matchers {
4+
5+
test("Euclidean Norm should give") {
6+
val objectForEuclideanNorm = new EuclideanNorm
7+
8+
objectForEuclideanNorm.calculateEuclideanNorm(List(1,2,3)) should be(3)
9+
objectForEuclideanNorm.calculateEuclideanNorm(List(3,4)) should be(5)
10+
objectForEuclideanNorm.calculateEuclideanNorm(List(1,1,1,1)) should be(2)
11+
}
12+
}

0 commit comments

Comments
 (0)