diff --git a/gradle/gradle-daemon-jvm.properties b/gradle/gradle-daemon-jvm.properties new file mode 100644 index 0000000..382c699 --- /dev/null +++ b/gradle/gradle-daemon-jvm.properties @@ -0,0 +1,3 @@ +#This file is generated by updateDaemonJvm +toolchainVendor=jetbrains +toolchainVersion=21 diff --git a/settings.gradle b/settings.gradle index 995211b..0171928 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,5 @@ +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.10.0' +} rootProject.name = 'homework01' diff --git a/src/main/kotlin/ru/otus/homework/fizzbuzz.kt b/src/main/kotlin/ru/otus/homework/fizzbuzz.kt index 6e04be1..33e6c51 100644 --- a/src/main/kotlin/ru/otus/homework/fizzbuzz.kt +++ b/src/main/kotlin/ru/otus/homework/fizzbuzz.kt @@ -2,5 +2,22 @@ package ru.otus.homework fun fizzbuzz(n: Int): Array { - TODO("Выполните задание") + if(n <= 0){ + return emptyArray() + } + + val tempArray: Array = Array(size = n) { it } + + val resArray: Array = Array(size = n){""} + + for(i in tempArray){ + resArray[i] = when{ + (i % 15 == 0) -> "FizzBuzz" + (i % 3 == 0) -> "Fizz" + (i % 5 == 0) -> "Buzz" + else -> i.toString() + } + } + + return resArray } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/sumoftwo.kt b/src/main/kotlin/ru/otus/homework/sumoftwo.kt index 70d72e5..579f96d 100644 --- a/src/main/kotlin/ru/otus/homework/sumoftwo.kt +++ b/src/main/kotlin/ru/otus/homework/sumoftwo.kt @@ -2,5 +2,42 @@ package ru.otus.homework fun sumOfTwo(numbers: IntArray, target: Int): IntArray { - TODO("Выполните задание") + if(numbers.isEmpty()){ + return IntArray(size = 0) + } + + for(i in 1..numbers.size){ + val indexes = IntArray(size = i){it} + + do{ + // calculate sum of current elements + var sum = 0 + for(j in indexes){ + sum += numbers[j] + } + + // if sum equal target then return array of indexes + if(sum == target){ + return indexes + } + + var endOfCycle = true + // check if we can create new combination of the indexes + findIndexLoop@ for(index in indexes.lastIndex downTo 0){ + if(indexes[index] < numbers.lastIndex - (indexes.lastIndex - index)){ + // index not in the end + // create new combination - move this index one step right + // follow indexes set right behind it + indexes[index] ++ + for(k in index + 1 .. indexes.lastIndex){ + indexes[k] = indexes[k-1] + 1 + endOfCycle = false + break@findIndexLoop + } + } + } + }while(!endOfCycle) + } + + throw IllegalArgumentException("Target is impossible") } \ No newline at end of file