From bdeb6b81da383b196d756c91888be8ffadeb98f1 Mon Sep 17 00:00:00 2001 From: Gena Date: Mon, 18 Aug 2025 18:35:36 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=BE=D0=BC=D0=B0=D1=88=D0=BD=D0=B5?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=B7=D0=B0=D0=BD=D1=8F=D1=82=D0=B8=D1=8E=20=20Kotlin?= =?UTF-8?q?=20#3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/ru/otus/homework/functions.kt | 47 +++++++++++++++++++ .../kotlin/ru/otus/homework/FunctionsTest.kt | 12 +++++ 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..54674df 100644 --- a/src/main/kotlin/ru/otus/homework/functions.kt +++ b/src/main/kotlin/ru/otus/homework/functions.kt @@ -1,6 +1,9 @@ package ru.otus.homework +import java.lang.System.* import java.time.LocalDate +import kotlin.time.Duration +import kotlin.time.toDuration fun main() { println(calculate(10, 20)) @@ -27,6 +30,12 @@ fun main() { val product = 2 by 2 println("Произведение: $product") + + val sum = variableAdd(1, 2, 3, 4) + println("1 + 2 + 3 + 4 = $sum") + + val ms = measure(::longLoop) + println("longLoop takes $ms milliseconds") } infix fun Int.by(other: Int): Int = this * other @@ -78,3 +87,41 @@ 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 + +fun variableAdd(a: Int, b: Int, vararg values: Int): Int { + var result = a + b + + for(i in values) { + result += i + } + + return result +} + +fun sumStrings(vararg strings: String, separator: Char = ' '): String { + var result: String = "" + + for(i in strings) { + if(result.isEmpty()) + result += i + else + result += ("$separator" + i) + } + + return result +} + +fun longLoop() { + for(i in 1..100) { + println("i = $i") + Thread.sleep(10) + } +} + +fun measure( oper: () -> Unit ): Long { + val start = currentTimeMillis() + + oper() + + return (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..edb5a6d 100644 --- a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt +++ b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt @@ -11,4 +11,16 @@ class FunctionsTest { calculate(1, 2) ) } + fun sumStringsTest() { + Assertions.assertEquals( + "str1 str2 str3", + sumStrings("str1", "str2", "str3") + ) + } + fun sumStringsTest2() { + Assertions.assertEquals( + "str1,str2,str3", + sumStrings("str1", "str2", "str3", separator = ',') + ) + } } \ No newline at end of file From 7b139a848263e8304ad0b05802502225107c1be5 Mon Sep 17 00:00:00 2001 From: Gena Date: Mon, 18 Aug 2025 18:36:48 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=BC=D0=B0=D1=88=D0=BD=D0=B5?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=B7=D0=B0=D0=BD=D1=8F=D1=82=D0=B8=D1=8E=20=20Kotlin?= =?UTF-8?q?=20#3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/ru/otus/homework/functions.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/functions.kt b/src/main/kotlin/ru/otus/homework/functions.kt index 54674df..388b3b2 100644 --- a/src/main/kotlin/ru/otus/homework/functions.kt +++ b/src/main/kotlin/ru/otus/homework/functions.kt @@ -2,8 +2,6 @@ package ru.otus.homework import java.lang.System.* import java.time.LocalDate -import kotlin.time.Duration -import kotlin.time.toDuration fun main() { println(calculate(10, 20))