diff --git a/src/main/kotlin/ru/otus/homework/functions.kt b/src/main/kotlin/ru/otus/homework/functions.kt index 4a7fe1e..d0222cd 100644 --- a/src/main/kotlin/ru/otus/homework/functions.kt +++ b/src/main/kotlin/ru/otus/homework/functions.kt @@ -27,6 +27,18 @@ fun main() { val product = 2 by 2 println("Произведение: $product") + + //Homework--- + val f1result = function1(2, 3, 5, 2) + println(f1result) + + val f2result = function2("Hello", "World", "Nice", "Day", char1 = '/') + println(f2result) + + val list = arrayListOf("Hello", "Good Morning", "Good Evening", "Good Day", "Good Night", "How are you?") + val f3result = function3('e', list, ::function4) + println(f3result) + } infix fun Int.by(other: Int): Int = this * other diff --git a/src/main/kotlin/ru/otus/homework/homework.kt b/src/main/kotlin/ru/otus/homework/homework.kt new file mode 100644 index 0000000..c8a8182 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/homework.kt @@ -0,0 +1,41 @@ +package ru.otus.homework + +import kotlin.system.measureTimeMillis + + +fun function1(int1: Int, int2: Int, vararg n: Int): Int { + var sum = int1 + int2 + + if (!n.isEmpty()) n.forEach { sum += it } else throw IllegalArgumentException() + + return sum +} + +fun function2(vararg s: String, char1: Char = ' '): String { + val sb = StringBuilder() + + s.forEach { string -> sb.append(string).append(char1) } + + return sb.toString() +} + +fun function3(char: Char, list: ArrayList, f: (Char, ArrayList) -> List): String { + + val duration = measureTimeMillis { + f(char, list) + } + + return ("Время выполнения = ${duration / 1000} секунд") +} + +fun function4(char: Char, list: ArrayList): List { + + val newArray = mutableListOf() + + list.forEach { string -> + if (string.contains(char)) newArray.add(string) + Thread.sleep(2000) + } + + return newArray +} \ 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..a682c36 100644 --- a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt +++ b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt @@ -1,6 +1,7 @@ package ru.otus.homework import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test class FunctionsTest { @@ -11,4 +12,10 @@ class FunctionsTest { calculate(1, 2) ) } + + @Test + fun testFunction2(){ + val expected = "Hello/World/Nice/Day/" + assertEquals(expected, function2("Hello", "World", "Nice", "Day", char1 = '/')) + } } \ No newline at end of file