From a611720334b199ab21ba6ae39cd0c68cc7d78ac3 Mon Sep 17 00:00:00 2001 From: ivanb Date: Sat, 19 Oct 2024 21:16:35 +0300 Subject: [PATCH 1/3] Added fun FizzBuzz and sumOfTwo --- src/main/kotlin/ru/otus/homework/fizzbuzz.kt | 27 +++++++++++++++++++- src/main/kotlin/ru/otus/homework/sumoftwo.kt | 27 ++++++++++++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/fizzbuzz.kt b/src/main/kotlin/ru/otus/homework/fizzbuzz.kt index 6e04be1..dd9c970 100644 --- a/src/main/kotlin/ru/otus/homework/fizzbuzz.kt +++ b/src/main/kotlin/ru/otus/homework/fizzbuzz.kt @@ -1,6 +1,31 @@ package ru.otus.homework +fun main() { + val inpN = 6 + val resultTextArray = fizzbuzz(inpN) + println(resultTextArray.contentToString()) +} fun fizzbuzz(n: Int): Array { - TODO("Выполните задание") + var i = 0 + val numbArray = Array(n) { i++ } + val textArray = Array(n) { "textString" } + + for(index in numbArray.indices) { + val number = numbArray[index] + + if(number % 3 == 0 && number % 5 == 0) { + textArray[index] = "FizzBuzz" + } else if(number % 3 == 0) { + textArray[index] = "Fizz" + } else { + if (number % 5 == 0) { + textArray[index] = "Buzz" + } else { + textArray[index] = number.toString() + } + } + } + + return textArray } \ 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..1a1be76 100644 --- a/src/main/kotlin/ru/otus/homework/sumoftwo.kt +++ b/src/main/kotlin/ru/otus/homework/sumoftwo.kt @@ -1,6 +1,29 @@ package ru.otus.homework +fun main(args: Array) { + val numbersArray = arrayOf(10, 7, 11, 15, 2) + val target = 9 + println(sumOfTwo(numbersArray, target).contentToString()) + +} + +fun sumOfTwo(inpArray: Array, target: Int): Array { + val indexcesArray = arrayOf(0, 0) + inpArray.forEachIndexed { index, value -> + + for (nextIndex in index + 1 until inpArray.size) { + val nextValue = inpArray[nextIndex] + val sumOfTwoValues = value + nextValue + + if (sumOfTwoValues == target) { + indexcesArray[0] = index + indexcesArray[1] = nextIndex + } + + } + + } + if (indexcesArray[0] != 0 && indexcesArray[1] != 0) return indexcesArray else throw IllegalArgumentException("not values") + -fun sumOfTwo(numbers: IntArray, target: Int): IntArray { - TODO("Выполните задание") } \ No newline at end of file From 431ea12eafa720afddd5e657f820e4a9cb4562a8 Mon Sep 17 00:00:00 2001 From: ivanb Date: Sat, 19 Oct 2024 21:26:01 +0300 Subject: [PATCH 2/3] Change intArray to numbers Change arrayOf to intArrayOf --- src/main/kotlin/ru/otus/homework/sumoftwo.kt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/sumoftwo.kt b/src/main/kotlin/ru/otus/homework/sumoftwo.kt index 1a1be76..b5c12f8 100644 --- a/src/main/kotlin/ru/otus/homework/sumoftwo.kt +++ b/src/main/kotlin/ru/otus/homework/sumoftwo.kt @@ -1,18 +1,19 @@ package ru.otus.homework fun main(args: Array) { - val numbersArray = arrayOf(10, 7, 11, 15, 2) + val numbersArray = intArrayOf(10, 7, 11, 15, 2) val target = 9 println(sumOfTwo(numbersArray, target).contentToString()) } -fun sumOfTwo(inpArray: Array, target: Int): Array { - val indexcesArray = arrayOf(0, 0) - inpArray.forEachIndexed { index, value -> +fun sumOfTwo(numbers: IntArray, target: Int): IntArray { - for (nextIndex in index + 1 until inpArray.size) { - val nextValue = inpArray[nextIndex] + val indexcesArray = intArrayOf(0, 0) + numbers.forEachIndexed { index, value -> + + for (nextIndex in index + 1 until numbers.size) { + val nextValue = numbers[nextIndex] val sumOfTwoValues = value + nextValue if (sumOfTwoValues == target) { From 783811f10e52fcccab6e77b2d15734bc61d972e7 Mon Sep 17 00:00:00 2001 From: ivanb Date: Sat, 19 Oct 2024 22:26:22 +0300 Subject: [PATCH 3/3] Change condition in If - && to || --- src/main/kotlin/ru/otus/homework/sumoftwo.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/sumoftwo.kt b/src/main/kotlin/ru/otus/homework/sumoftwo.kt index b5c12f8..7b3f2a3 100644 --- a/src/main/kotlin/ru/otus/homework/sumoftwo.kt +++ b/src/main/kotlin/ru/otus/homework/sumoftwo.kt @@ -1,10 +1,9 @@ package ru.otus.homework fun main(args: Array) { - val numbersArray = intArrayOf(10, 7, 11, 15, 2) + val numbersArray = intArrayOf(0, 9, 11, 15, 2) val target = 9 println(sumOfTwo(numbersArray, target).contentToString()) - } fun sumOfTwo(numbers: IntArray, target: Int): IntArray { @@ -20,11 +19,10 @@ fun sumOfTwo(numbers: IntArray, target: Int): IntArray { indexcesArray[0] = index indexcesArray[1] = nextIndex } - } - } - if (indexcesArray[0] != 0 && indexcesArray[1] != 0) return indexcesArray else throw IllegalArgumentException("not values") - + if (indexcesArray[0] != 0 || indexcesArray[1] != 0) + return indexcesArray + else throw IllegalArgumentException("not values") } \ No newline at end of file