From 88f1f23000fcefa9024bd2023ba86dd0b2044ed1 Mon Sep 17 00:00:00 2001 From: Dmitriy Bulygin Date: Sun, 17 Aug 2025 14:04:34 +0300 Subject: [PATCH] added tasks solutions --- src/main/kotlin/ru/otus/homework/functions.kt | 28 +++++++++++++++++ .../kotlin/ru/otus/homework/FunctionsTest.kt | 31 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/main/kotlin/ru/otus/homework/functions.kt b/src/main/kotlin/ru/otus/homework/functions.kt index 4a7fe1e..7c559f8 100644 --- a/src/main/kotlin/ru/otus/homework/functions.kt +++ b/src/main/kotlin/ru/otus/homework/functions.kt @@ -1,6 +1,7 @@ package ru.otus.homework import java.time.LocalDate +import kotlin.random.Random fun main() { println(calculate(10, 20)) @@ -27,6 +28,13 @@ fun main() { val product = 2 by 2 println("Произведение: $product") + + println("Время выполнения функции sumOfValues = ${funExecTime { + sumOfValues(1, 2, 3, 4, 5) + }} ms") + println("Время выполнения вынкции concatWithSep = ${funExecTime { + concatWithSep("str1", "str2", "str3", c = ',') + }} ms") } infix fun Int.by(other: Int): Int = this * other @@ -78,3 +86,23 @@ fun calculate(n1: Int, n2: Int, op: (Int, Int) -> Int): String { fun add(a: Int, b: Int): Int = a + b fun subtract(a: Int, b: Int): Int = a - b + +// Домашка тут + +// ## 1. Функция с обязательными и необязательными позиционными параметрами +// Используем "Элвис" оператор +fun sumOfValues(n1: Int? = null, n2: Int? = null, vararg n: Int) = + (n1 ?: throw IllegalArgumentException()) + + (n2 ?: throw IllegalArgumentException()) + + n.sum() + +// ## 2. Функция с необязательным параметром и позиционными параметрами +fun concatWithSep(vararg s: String, c: Char = ' ') = s.joinToString("$c") + +// ## 4. Функция, измеряющая время выполнения другой функции +fun funExecTime(f: () -> Unit): Long { + val start = System.currentTimeMillis() + f() + Thread.sleep(Random.nextLong(200, 1000)) // имитация долгой операции + return System.currentTimeMillis() - start +} \ No newline at end of file diff --git a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt index 93a36cf..1f8ba35 100644 --- a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt +++ b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt @@ -2,6 +2,7 @@ package ru.otus.homework import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows class FunctionsTest { @Test @@ -11,4 +12,34 @@ class FunctionsTest { calculate(1, 2) ) } + @Test + fun `test of sumOfValues with OK result`() { + Assertions.assertEquals( + 15, + sumOfValues(1, 2, 3, 4, 5) + ) + } + + @Test + fun `test of sumOfValues with throw exceptions result`() { + assertThrows { + sumOfValues(1) + } + } + + @Test + fun `test of concatWithSep with NON separator`() { + Assertions.assertEquals( + "str1 str2 str3", + concatWithSep("str1", "str2", "str3") + ) + } + + @Test + fun `test of concatWithSep with separator`() { + Assertions.assertEquals( + "str1,str2,str3", + concatWithSep("str1", "str2", "str3", c=',') + ) + } } \ No newline at end of file