Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import kotlin.math.log10
import kotlin.math.pow

object ResistorColorTrio {

fun text(vararg input: Color): String {
val duoValue = 10 * input[0].ordinal + input[1].ordinal
val trioValue = duoValue * 10.0.pow(input[2].ordinal)
val (first, second, third) = input.take(3).map { it.ordinal.toLong() }

val thousandPowers = (log10(trioValue) / 3).toInt()
val resultValue = (trioValue / 1000.0.pow(thousandPowers)).toInt()
val resultUnit = Unit.values()[thousandPowers].name.lowercase()
val value: Long = ((first * 10L) + second) * 10L.pow(third.toInt())

return "$resultValue $resultUnit"
return when {
value >= 1e9 -> "${value / 1_000_000_000L} gigaohms"
value >= 1e6 -> "${value / 1_000_000L} megaohms"
value >= 1e3 -> "${value / 1_000L} kiloohms"
else -> "$value ohms"
}
}

}

fun Long.pow(exp: Int): Long = this.toDouble().pow(exp).toLong()
46 changes: 35 additions & 11 deletions exercises/practice/resistor-color-trio/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
[canonical-tests]
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

# Orange and orange and black
"d6863355-15b7-40bb-abe0-bfb1a25512ed" = true
[d6863355-15b7-40bb-abe0-bfb1a25512ed]
description = "Orange and orange and black"

# Blue and grey and brown
"1224a3a9-8c8e-4032-843a-5224e04647d6" = true
[1224a3a9-8c8e-4032-843a-5224e04647d6]
description = "Blue and grey and brown"

# Red and black and red
"b8bda7dc-6b95-4539-abb2-2ad51d66a207" = true
[b8bda7dc-6b95-4539-abb2-2ad51d66a207]
description = "Red and black and red"

# Green and brown and orange
"5b1e74bc-d838-4eda-bbb3-eaba988e733b" = true
[5b1e74bc-d838-4eda-bbb3-eaba988e733b]
description = "Green and brown and orange"

# Yellow and violet and yellow
"f5d37ef9-1919-4719-a90d-a33c5a6934c9" = true
[f5d37ef9-1919-4719-a90d-a33c5a6934c9]
description = "Yellow and violet and yellow"

[5f6404a7-5bb3-4283-877d-3d39bcc33854]
description = "Blue and violet and blue"

[7d3a6ab8-e40e-46c3-98b1-91639fff2344]
description = "Minimum possible value"

[ca0aa0ac-3825-42de-9f07-dac68cc580fd]
description = "Maximum possible value"

[0061a76c-903a-4714-8ce2-f26ce23b0e09]
description = "First two colors make an invalid octal number"

[30872c92-f567-4b69-a105-8455611c10c4]
description = "Ignore extra colors"
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,24 @@ class ResistorColorTrioTest {
@Ignore
@Test
fun `yellow violet violet`() = assertEquals("470 megaohms", ResistorColorTrio.text(YELLOW, VIOLET, VIOLET))

@Ignore
@Test
fun `blue and violet and blue`() = assertEquals("67 megaohms", ResistorColorTrio.text(BLUE, VIOLET, BLUE))

@Ignore
@Test
fun `minimum possible value`() = assertEquals("0 ohms", ResistorColorTrio.text(BLACK, BLACK, BLACK))

@Ignore
@Test
fun `maximum possible value`() = assertEquals("99 gigaohms", ResistorColorTrio.text(WHITE, WHITE, WHITE))

@Ignore
@Test
fun `first two colors make an invalid octal number`() = assertEquals("8 ohms", ResistorColorTrio.text(BLACK, GREY, BLACK))

@Ignore
@Test
fun `ignore extra colors`() = assertEquals("650 kiloohms", ResistorColorTrio.text(BLUE, GREEN, YELLOW, ORANGE))
}