Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gradle/gradle-daemon-jvm.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#This file is generated by updateDaemonJvm
toolchainVendor=jetbrains
toolchainVersion=21
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.10.0'
}
rootProject.name = 'homework01'

19 changes: 18 additions & 1 deletion src/main/kotlin/ru/otus/homework/fizzbuzz.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,22 @@ package ru.otus.homework


fun fizzbuzz(n: Int): Array<String> {
TODO("Выполните задание")
if(n <= 0){
return emptyArray<String>()
}

val tempArray: Array<Int> = Array<Int>(size = n) { it }

val resArray: Array<String> = Array<String>(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
}
39 changes: 38 additions & 1 deletion src/main/kotlin/ru/otus/homework/sumoftwo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}