From 2abd5aaa1d4a439d52397489133c8b9436595f68 Mon Sep 17 00:00:00 2001 From: norangebit Date: Fri, 22 Mar 2019 17:40:01 +0100 Subject: [PATCH] implement prefix average in recursive way --- .../algorithms/exercises/PrefixAverage.kt | 34 +++++++++++------ .../algorithms/exercises/PrefixAverageTest.kt | 38 +++++++++++++++++++ 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/it/norangeb/algorithms/exercises/PrefixAverage.kt b/src/main/kotlin/it/norangeb/algorithms/exercises/PrefixAverage.kt index 907b2ad..338e54b 100644 --- a/src/main/kotlin/it/norangeb/algorithms/exercises/PrefixAverage.kt +++ b/src/main/kotlin/it/norangeb/algorithms/exercises/PrefixAverage.kt @@ -6,7 +6,7 @@ * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * to use, copy, modify, tryMerge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * @@ -25,19 +25,29 @@ package it.norangeb.algorithms.exercises -class PrefixAverage { - companion object { - fun prefixAverage(arrayIn: IntArray): IntArray { +object PrefixAverage { + fun prefixAverage(arrayIn: IntArray): IntArray { - val arrayOut = IntArray(arrayIn.size) - var currentSum = 0 + val arrayOut = IntArray(arrayIn.size) + var currentSum = 0 - for (i in 0 until arrayIn.size) { - currentSum += arrayIn[i] - arrayOut[i] = currentSum / (i + 1) + for (i in 0 until arrayIn.size) { + currentSum += arrayIn[i] + arrayOut[i] = currentSum / (i + 1) + } + + return arrayOut + } + + fun recursivePrefixAverage(arrayIn: IntArray, position: Int = 0, acc: Int = 0): IntArray { + return when (position) { + arrayIn.size -> IntArray(arrayIn.size) + else -> { + val newAcc = arrayIn[position] + acc + val array = recursivePrefixAverage(arrayIn, position + 1, newAcc) + array[position] = newAcc / (position + 1) + array } - - return arrayOut } } -} \ No newline at end of file +} diff --git a/src/test/kotlin/it/norangeb/algorithms/exercises/PrefixAverageTest.kt b/src/test/kotlin/it/norangeb/algorithms/exercises/PrefixAverageTest.kt index 584ff4e..3531300 100644 --- a/src/test/kotlin/it/norangeb/algorithms/exercises/PrefixAverageTest.kt +++ b/src/test/kotlin/it/norangeb/algorithms/exercises/PrefixAverageTest.kt @@ -67,4 +67,42 @@ class PrefixAverageTest { arrayOut `should equal` intArrayOf(21, 21, 21, 21, 21) } + + @Test + fun testNominalCaseRecursive() { + val arrayIn = intArrayOf(21, 23, 25, 31, 20, 18, 16) + + val arrayOut = PrefixAverage.recursivePrefixAverage(arrayIn) + + arrayOut `should equal` intArrayOf( + 21, 22, 23, 25, 24, 23, 22 + ) + } + + @Test + fun testEmptyArrayRecursive() { + val arrayIn = intArrayOf() + + val arrayOut = PrefixAverage.recursivePrefixAverage(arrayIn) + + arrayOut `should equal` intArrayOf() + } + + @Test + fun testOneElementArrayRecursive() { + val arrayIn = intArrayOf(25) + + val arrayOut = PrefixAverage.recursivePrefixAverage(arrayIn) + + arrayOut `should equal` intArrayOf(25) + } + + @Test + fun testAlwaysSameElementArrayRecursive() { + val arrayIn = intArrayOf(21, 21, 21, 21, 21) + + val arrayOut = PrefixAverage.recursivePrefixAverage(arrayIn) + + arrayOut `should equal` intArrayOf(21, 21, 21, 21, 21) + } } \ No newline at end of file