Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework_2 - Савичев Александр #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions homeworks/homework_2/src/main/scala/Exercises.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,31 @@ object Exercises {
/*Реализовать функцию, которая возвращает сумму всех целых чисел в заданном диапазоне (от iForm до iTo), которые делятся
на 3 или на 5.*/
/*Реализовать юнит-тесты в src/test/scala для данной функции.*/
def sumOfDivBy3Or5(iFrom: Int, iTo: Int): Long = ???

def sumOfDivBy3Or5(iFrom: Int, iTo: Int): Long = {
var sum = 0
for {i <- iFrom to iTo
if i % 3 == 0 || i % 5 == 0
} {
sum = sum + i
}
sum
}


/*ЗАДАНИЕ II*/
/*Реализовать функцию, которая вычисляет все различные простые множители целого числа отличные от 1.
Число 80 раскладывается на множители 1 * 2 * 2 * 2 * 2 * 5, результат выполнения функции => Seq(2, 5).
Число 98 можно разложить на множители 1 * 2 * 7 * 7, результат выполнения функции => Seq(2, 7).*/
/*Реализовать юнит-тесты в src/test/scala для данной функции.*/
def primeFactor(number: Int): Seq[Int] = ???
def primeFactor(number: Int): Seq[Int] = {
var n = number
for (i <- 2 to math.sqrt(number).ceil.toInt if n % i == 0) yield {
while (n % i == 0) {
n /= i
}
i
}
}



Expand All @@ -40,15 +55,16 @@ object Exercises {
def abs(vec: Vector2D): Double = java.lang.Math.sqrt(vec.x * vec.x + vec.y * vec.y)
def scalar(vec0: Vector2D, vec1: Vector2D): Double = vec0.x * vec1.x + vec0.y * vec1.y
def cosBetween(vec0: Vector2D, vec1: Vector2D): Double = scalar(vec0, vec1) / abs(vec0) / abs(vec1)
//def sumByFunc(leftVec0: Vector2D, leftVec1: Vector2D, ???, rightVec0: Vector2D, rightVec1: Vector2D) = ???
/*
def sumByFunc(leftVec0: Vector2D, leftVec1: Vector2D, func: (Vector2D, Vector2D) => Double, rightVec0: Vector2D, rightVec1: Vector2D): Double =
func(leftVec0, leftVec1) + func(rightVec0, rightVec1)

def sumScalars(leftVec0: Vector2D, leftVec1: Vector2D, rightVec0: Vector2D, rightVec1: Vector2D): Double =
sumByFunc(leftVec0, leftVec1, scalar, rightVec0, rightVec1)
*/
/*


def sumCosines(leftVec0: Vector2D, leftVec1: Vector2D, rightVec0: Vector2D, rightVec1: Vector2D): Double =
sumByFunc(leftVec0, leftVec1, cosBetween, rightVec0, rightVec1)
*/




Expand All @@ -71,6 +87,18 @@ object Exercises {
"Chrome" -> (3, 7.18), "Cesium" -> (7, 1.873), "Zirconium" -> (3, 6.45)
)

def sortByHeavyweight(ballsArray: Map[String, (Int, Double)] = balls): Seq[String] = ???
def sortByHeavyweight(ballsArray: Map[String, (Int, Double)] = balls): Seq[String] = {
if (ballsArray == null) {
return Seq()
}
ballsArray
.map(e => (e._1, countWeight(e._2._2, e._2._1)))
.toSeq
.sortBy(e => e._2)
.map(e => e._1)
}

def countWeight(density: Double, radius: Int): Double = {
4 / 3 * math.Pi * math.pow(radius, 3) * density
}
}
52 changes: 49 additions & 3 deletions homeworks/homework_2/src/test/scala/Test.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,58 @@
import Exercises.Vector2D
import utest._

object Test extends TestSuite{
object Test extends TestSuite {

val tests = Tests{
val tests: Tests = Tests {
'test_divBy3Or7 - {
assert(Exercises.divBy3Or7(1, 3) == Seq(3))
assert(Exercises.divBy3Or7(5, 9) == Seq(6, 7, 9))
assert(Exercises.divBy3Or7(0, 100) == Seq(0, 3, 6, 7, 9, 12, 14, 15, 18, 21, 24, 27, 28, 30, 33, 35, 36, 39, 42, 45, 48, 49, 51, 54, 56, 57, 60, 63, 66, 69, 70, 72, 75, 77, 78, 81, 84, 87, 90, 91, 93, 96, 98, 99))
}

'test_sumOfDivBy3Or5 - {
assert(Exercises.sumOfDivBy3Or5(7, 2) == 0)
assert(Exercises.sumOfDivBy3Or5(1, 3) == 3)
assert(Exercises.sumOfDivBy3Or5(5, 9) == 20)
assert(Exercises.sumOfDivBy3Or5(0, 100) == 2418)
}

'test_primeFactor - {
assert(Exercises.primeFactor(80) == Seq(2, 5))
assert(Exercises.primeFactor(98) == Seq(2, 7))
assert(Exercises.primeFactor(1) == Seq())
assert(Exercises.primeFactor(-1) == Seq())
assert(Exercises.primeFactor(2) == Seq(2))
}

'test_sumScalars - {
assert(Exercises.sumScalars(Vector2D(0, 1), Vector2D(1, 0), Vector2D(2, 0), Vector2D(0, 2)) == 0)
assert(Exercises.sumScalars(Vector2D(1, 1), Vector2D(1, 1), Vector2D(2, 2), Vector2D(2, 2)) == 10)
assert(Exercises.sumScalars(Vector2D(Double.NaN, 1), Vector2D(1, 1), Vector2D(2, 2), Vector2D(2, 2)).isNaN)
}

'test_sumCosines - {
assert(Exercises.sumCosines(Vector2D(0, 1), Vector2D(1, 0), Vector2D(2, 0), Vector2D(0, 2)) == 0)
assert(math.abs(Exercises.sumCosines(Vector2D(1, 1), Vector2D(1, 1), Vector2D(2, 2), Vector2D(2, 2)) - 2) < 0.00000001)
assert(Exercises.sumCosines(Vector2D(Double.NaN, 1), Vector2D(1, 1), Vector2D(2, 2), Vector2D(2, 2)).isNaN)
}

'test_sortByHeavyweight - {
val checkMap = (map: Map[String, (Int, Double)]) => {
val res = Exercises.sortByHeavyweight(map)
assert(res.size == map.size)

res
.map(name => map(name))
.map(densityAndRadius => Exercises.countWeight(densityAndRadius._2, densityAndRadius._1))
.sliding(2)
.foreach(e => assert(e.size < 2 || e.head <= e(1)))
}

checkMap(Exercises.balls)
checkMap(Map())
checkMap(Map("Aluminum" -> (3, 2.6889)))
assert(Exercises.sortByHeavyweight(null) == Seq())
}
}
}
}