Skip to content
Merged
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
44 changes: 44 additions & 0 deletions solutions/python/resistor-color/1/resistor_color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Utilities for working with the 10-color resistor code.

Mnemonics map the colors to the numbers, that, when stored
as an array, happen to map to their index in the array:
Better Be Right Or Your Great Big Values Go Wrong.
"""

COLORS: tuple = (
"black",
"brown",
"red",
"orange",
"yellow",
"green",
"blue",
"violet",
"grey",
"white",
)


def color_code(color: str) -> int:
"""
Return the numeric code (0-9) for a resistor color.

:param color: Color name.
:type color: str
:returns: Index in the standard 10-color sequence
(0 for 'black' through 9 for 'white').
:rtype: int
:raises ValueError: If color is not a valid resistor color.
"""
return COLORS.index(color)


def colors() -> list:
"""
Return all valid resistor colors in ascending code order.

:returns: List of color names in order from 'black' (0) to 'white' (9).
:rtype: list
"""
return list(COLORS)