From 492534b74caccd8a6b5519a369b00d25d7336680 Mon Sep 17 00:00:00 2001 From: Karavaev Date: Tue, 26 Aug 2025 14:33:21 +0300 Subject: [PATCH] Homework_Kotlin#3 --- src/main/kotlin/ru/otus/homework/functions.kt | 93 ++++++------------- .../kotlin/ru/otus/homework/FunctionsTest.kt | 13 ++- 2 files changed, 40 insertions(+), 66 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/functions.kt b/src/main/kotlin/ru/otus/homework/functions.kt index 4a7fe1e..0fa8491 100644 --- a/src/main/kotlin/ru/otus/homework/functions.kt +++ b/src/main/kotlin/ru/otus/homework/functions.kt @@ -1,80 +1,47 @@ package ru.otus.homework +import java.lang.Thread.sleep +import java.sql.Time import java.time.LocalDate +import java.time.LocalTime -fun main() { - println(calculate(10, 20)) - println(calculate(10, 20.5F)) - println(calculate(30.1F, 40.2F, 50.3F, 60.4F)) - println(calculate(3, 2, ::add)) - println(calculate(3, 2, ::subtract)) - println(calculate(3, 2) { n1, n2 -> n1 * n2 }) +fun main() { + var result1: Int = task_1(1,2, 3, 4, 5, 6, 7, 8, 9, 10, 111 ) + println("result1 = $result1") - sign( - lastName = "Иванов", - firstName = "Вася" - ) + var result2 : String = task_2("one","two","three","four","five") + println("result2 = $result2") - translate(calculate(1.1F, 2.2F, 3.3F)) { - "In english: ${it.replace("+", "plus").replace("=", "equals")}" - } - println( - calculate(1.1F, 2.2F, 3.3F) { - "%.4f (с точностью до четырех знаков)".format(this) - } - ) + result2 = task_2("one","two","three","four","five", separ = '$') + println("result2 = $result2") - val product = 2 by 2 - println("Произведение: $product") + var result4 : Int = task_4(::slowFunc) + println("duration = $result4 seconds") } -infix fun Int.by(other: Int): Int = this * other - -fun translate(what: String, translator: (String) -> String) { - println(translator(what)) +fun task_1(n1: Int, n2: Int, vararg n3: Int): Int { + var sum = n1 + n2 + n3.forEach { sum += it } + return sum } -fun sign(firstName: String, lastName: String, date: LocalDate = LocalDate.now()) { - println("Работу выполнил: $firstName $lastName, ${date.russian()}") +fun task_2(vararg str: String, separ: Char = ' '): String { + var strRes: String = str.joinToString("$separ") + return strRes } -internal fun LocalDate.russian(): String { - return "${this.dayOfMonth}.${monthValue}.${year}" +fun task_4(paramFunc: () -> Unit) : Int { + val startTime = LocalTime.now() + paramFunc() + val stopTime = LocalTime.now() + val duration = stopTime.toSecondOfDay() - startTime.toSecondOfDay() + return duration } -fun what(): String = "Огурцов" - -fun calculate(n1: Int, n2: Int): String = "$n1 + $n2 = ${ n1 + n2 } ${ what() }" - -fun calculate(n1: Int, n2: Float): String { - fun add(): String { - val s: Float - - while (true) { - // Пример блока. Вычисляем, и сразу выходим - val s1 = n1 + n2 - s = s1 - break - } - - return "$n1 + $n2 = $s" +fun slowFunc(): Unit { + for(i in 0 until 10) { + println("Number = $i") + Thread.sleep(1000) } - return "${ add() } ${ what() }" -} - -fun Float.formatWithDot(): String = "%.2f".format(this) - -fun calculate(vararg n: Float, format: Float.() -> String = Float::formatWithDot): String { - var sum = 0F - n.forEach { sum += it } - return "${n.joinToString(" + ")} = ${sum.format()}" -} - -fun calculate(n1: Int, n2: Int, op: (Int, Int) -> Int): String { - val result = op(n1, n2) - return "Результат операции $n1 и $n2 равен: $result" -} - -fun add(a: Int, b: Int): Int = a + b -fun subtract(a: Int, b: Int): Int = a - b +} \ 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..6dd35b0 100644 --- a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt +++ b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt @@ -2,13 +2,20 @@ 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 - fun calculationTest() { + fun task2Test() { + + Assertions.assertEquals( + "str1 str2 str3", + task_2("str1", "str2", "str3") + ) + Assertions.assertEquals( - "1 + 2 = 3 Огурцов", - calculate(1, 2) + "str1,str2,str3", + task_2("str1", "str2", "str3", separ = ',') ) } } \ No newline at end of file