-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
...esentation/src/commonMain/kotlin/dev/ohoussein/cryptoapp/crypto/presentation/core/Math.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dev.ohoussein.cryptoapp.crypto.presentation.core | ||
|
||
fun List<Double>.averageValues(maxValues: Int = 20): List<Double> { | ||
if (size <= maxValues) { | ||
return this | ||
} | ||
|
||
val groupSize = size / maxValues | ||
val averagedPrices = mutableListOf<Double>() | ||
|
||
for (i in indices step groupSize) { | ||
val group = subList(i, minOf(i + groupSize, size)) | ||
val avgPrice = group.average() | ||
averagedPrices.add(avgPrice) | ||
} | ||
|
||
return averagedPrices | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...tation/src/commonTest/kotlin/dev/ohoussein/cryptoapp/crypto/presentation/core/MathTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package dev.ohoussein.cryptoapp.crypto.presentation.core | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class MathTest { | ||
|
||
@Test | ||
fun `Given values less than max value When call averageValues it it should return the same list`() { | ||
val prices = listOf(100.0, 110.0, 105.0) | ||
val result = prices.averageValues(maxValues = 20) | ||
val expected = listOf(100.0, 110.0, 105.0) | ||
|
||
assertEquals(expected, result) | ||
} | ||
|
||
@Test | ||
fun `Given values When call averageValues it it should return the average values`() { | ||
val prices = (1..40).map { it.toDouble() } | ||
val result = prices.averageValues(maxValues = 20) | ||
val expected = listOf( | ||
1.5, 3.5, 5.5, 7.5, 9.5, 11.5, 13.5, 15.5, 17.5, 19.5, | ||
21.5, 23.5, 25.5, 27.5, 29.5, 31.5, 33.5, 35.5, 37.5, 39.5 | ||
) | ||
|
||
assertEquals(expected, result) | ||
} | ||
|
||
@Test | ||
fun `Given empty values When call averageValues it it should return an empty list`() { | ||
val prices = emptyList<Double>() | ||
val result = prices.averageValues(maxValues = 20) | ||
val expected = emptyList<Double>() | ||
|
||
assertEquals(expected, result) | ||
} | ||
} |