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 - Шахтарина Амина #282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
99 changes: 60 additions & 39 deletions homeworks/homework_2/src/main/scala/Exercises.scala
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
object Exercises {

/*ПРИМЕР*/
/*Реализовать функцию, которая возвращает все целые числа в заданном диапазоне (от iForm до iTo), которые делятся
на 3 или на 7.*/
/*Реализовать юнит-тесты в src/test/scala для данной функции.*/
def divBy3Or7(iFrom: Int, iTo: Int): Seq[Int] = {
for {i <- iFrom to iTo
if i % 3 == 0 || i % 7 == 0
} yield i
}


import scala.collection.mutable.ListBuffer
import scala.math._

object Exercises {
/*ЗАДАНИЕ I*/
/*Реализовать функцию, которая возвращает сумму всех целых чисел в заданном диапазоне (от 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 copy = number
var div = 2
val factors = new ListBuffer[Int]

while (copy >= 2) {
if (copy % div == 0) {
factors += div
copy = copy / div
}
else {
div += 1
}
}
factors.toSet
.toSeq
}

/*ЗАДАНИЕ III*/
/*Дано: класс двумерного вектора, а также функции вычисления модуля вектора (abs), вычисления скалярного произведения
Expand All @@ -37,19 +46,24 @@ object Exercises {
Функция sumCosines должна вычислять сумму косинусов углов между парами векторов cosBetween(leftVec0, leftVec1) + cosBetween(rightVec0, rightVec1).*/
/*Реализовать юнит-тесты в src/test/scala для функций sumScalars и sumCosines*/
case class Vector2D(x: Double, y: Double)
def abs(vec: Vector2D): Double = java.lang.Math.sqrt(vec.x * vec.x + vec.y * vec.y)

def abs(vec: Vector2D): Double = 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 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)
*/

def cosBetween( vec0: Vector2D,vec1: Vector2D): Double = scalar(vec0, vec1) / abs(vec0) / abs(vec1)

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)


/*ЗАДАНИЕ IV*/
Expand All @@ -62,15 +76,22 @@ object Exercises {
*/
/*Реализовать юнит-тесты в src/test/scala для данной функции.*/
val balls: Map[String, (Int, Double)] =
Map(
"Aluminum" -> (3, 2.6889), "Tungsten" -> (2, 19.35), "Graphite" -> (12, 2.1), "Iron" -> (3, 7.874),
"Gold" -> (2, 19.32), "Potassium" -> (14, 0.862), "Calcium" -> (8, 1.55), "Cobalt" -> (4, 8.90),
"Lithium" -> (12, 0.534), "Magnesium" -> (10, 1.738), "Copper" -> (3, 8.96), "Sodium" -> (5, 0.971),
"Nickel" -> (2, 8.91), "Tin" -> (1, 7.29), "Platinum" -> (1, 21.45), "Plutonium" -> (3, 19.25),
"Lead" -> (2, 11.336), "Titanium" -> (2, 10.50), "Silver" -> (4, 4.505), "Uranium" -> (2, 19.04),
"Chrome" -> (3, 7.18), "Cesium" -> (7, 1.873), "Zirconium" -> (3, 6.45)
)
Map("Aluminum" -> (3, 2.6889), "Tungsten" -> (2, 19.35),
"Graphite" -> (12, 2.1), "Iron" -> (3, 7.874),
"Gold" -> (2, 19.32), "Potassium" -> (14, 0.862),
"Calcium" -> (8, 1.55), "Cobalt" -> (4, 8.90),
"Lithium" -> (12, 0.534), "Magnesium" -> (10, 1.738),
"Copper" -> (3, 8.96), "Sodium" -> (5, 0.971),
"Nickel" -> (2, 8.91), "Tin" -> (1, 7.29),
"Platinum" -> (1, 21.45), "Plutonium" -> (3, 19.25),
"Lead" -> (2, 11.336), "Titanium" -> (2, 10.50),
"Silver" -> (4, 4.505), "Uranium" -> (2, 19.04),
"Chrome" -> (3, 7.18), "Cesium" -> (7, 1.873),
"Zirconium" -> (3, 6.45))

def getMass(r: Int, density : Double) : Double = 4 / 3 * 3.14 * r * r * r * density

def sortByHeavyweight(ballsArray: Map[String, (Int, Double)] = balls): Seq[String] = ???

}
def sortByHeavyweight(ballsArray: Map[String, (Int, Double)] = balls): Seq[String] =
ballsArray.toSeq.sortBy(ball => getMass(ball._2._1, ball._2._2)).map(_._1)
}