| 1 | 1 | def label(colors): |
| 2 | | pass |
| 2 | # Edge Case: Invalid number of colors |
| 3 | if len(colors) < 3: |
| 4 | raise ValueError("At least three colors are required") |
| 5 | |
| 6 | # Define color code mapping |
| 7 | color_codes = { |
| 8 | "black": 0, |
| 9 | "brown": 1, |
| 10 | "red": 2, |
| 11 | "orange": 3, |
| 12 | "yellow": 4, |
| 13 | "green": 5, |
| 14 | "blue": 6, |
| 15 | "violet": 7, |
| 16 | "grey": 8, |
| 17 | "white": 9 |
| 18 | } |
| 19 | |
| 20 | # Edge Case: Invalid color inputs not in the defined color set |
| 21 | for color in colors[:3]: |
| 22 | if color not in color_codes: |
| 23 | raise ValueError(f"Invalid color: {color}") |
| 24 | |
| 25 | # Calculate the main value from first two colors |
| 26 | main_value = color_codes[colors[0]] * 10 + color_codes[colors[1]] |
| 27 | |
| 28 | # Edge Case: Zero value resistors (black-black-black) |
| 29 | if main_value == 0 and color_codes[colors[2]] == 0: |
| 30 | return "0 ohms" |
| 31 | |
| 32 | # Calculate the total ohms value |
| 33 | zeros = color_codes[colors[2]] |
| 34 | total_ohms = main_value * (10 ** zeros) |
| 35 | |
| 36 | # Define metric prefixes |
| 37 | prefixes = [ |
| 38 | ("", 1), # no prefix for values less than 1000 |
| 39 | ("kilo", 10**3), # kiloohms |
| 40 | ("mega", 10**6), # megaohms |
| 41 | ("giga", 10**9) # gigaohms |
| 42 | ] |
| 43 | |
| 44 | # Find the appropriate prefix |
| 45 | prefix = "" |
| 46 | divisor = 1 |
| 47 | |
| 48 | # Edge Case: Maximum value resistors handling |
| 49 | for i in range(len(prefixes)-1, -1, -1): |
| 50 | if total_ohms >= prefixes[i][1]: |
| 51 | prefix, divisor = prefixes[i] |
| 52 | break |
| 53 | |
| 54 | # Calculate the value with the appropriate prefix |
| 55 | if divisor == 1: |
| 56 | formatted_value = str(total_ohms) |
| 57 | else: |
| 58 | # Edge Case: Handling integer division to avoid decimal points when possible |
| 59 | if total_ohms % divisor == 0: |
| 60 | formatted_value = str(total_ohms // divisor) |
| 61 | else: |
| 62 | formatted_value = str(total_ohms / divisor) |
| 63 | |
| 64 | # Edge Case: Properly format the output string |
| 65 | if prefix: |
| 66 | return f"{formatted_value} {prefix}ohms" |
| 67 | else: |
| 68 | return f"{formatted_value} ohms" |
| 69 | |
| 70 | # Handled Edge Cases: Invalid number of colors, Invalid color inputs not in the defined color set, Zero value resistors (black-black-black), Maximum value resistors handling, Handling integer division to avoid decimal points when possible, Properly format the output string |