| 1 | 1 | def resistor_label(colors): |
| 2 | | pass |
| 2 | # Define color codes for values |
| 3 | color_values = { |
| 4 | "black": 0, |
| 5 | "brown": 1, |
| 6 | "red": 2, |
| 7 | "orange": 3, |
| 8 | "yellow": 4, |
| 9 | "green": 5, |
| 10 | "blue": 6, |
| 11 | "violet": 7, |
| 12 | "grey": 8, |
| 13 | "white": 9 |
| 14 | } |
| 15 | |
| 16 | # Define tolerance values |
| 17 | tolerance_values = { |
| 18 | "grey": 0.05, |
| 19 | "violet": 0.1, |
| 20 | "blue": 0.25, |
| 21 | "green": 0.5, |
| 22 | "brown": 1, |
| 23 | "red": 2, |
| 24 | "gold": 5, |
| 25 | "silver": 10 |
| 26 | } |
| 27 | |
| 28 | # Edge Case: Handle empty input |
| 29 | if not colors: |
| 30 | raise ValueError("At least one color band is required") |
| 31 | |
| 32 | # Edge Case: Handle one-band resistor |
| 33 | if len(colors) == 1: |
| 34 | if colors[0] == "black": |
| 35 | return "0 ohms" |
| 36 | else: |
| 37 | raise ValueError("Invalid single band color") |
| 38 | |
| 39 | # Edge Case: Handle invalid number of bands |
| 40 | if len(colors) not in [4, 5]: |
| 41 | raise ValueError("Invalid number of color bands. Must be 1, 4, or 5 bands.") |
| 42 | |
| 43 | # Edge Case: Check if all colors are valid |
| 44 | for color in colors[:-1]: # All but the last (tolerance) band |
| 45 | if color not in color_values: |
| 46 | raise ValueError(f"Invalid color: {color}") |
| 47 | |
| 48 | # Edge Case: Check if tolerance band is valid |
| 49 | if colors[-1] not in tolerance_values: |
| 50 | raise ValueError(f"Invalid tolerance color: {colors[-1]}") |
| 51 | |
| 52 | # Calculate the resistor value |
| 53 | if len(colors) == 4: |
| 54 | # Four-band resistor |
| 55 | digit1 = color_values[colors[0]] |
| 56 | digit2 = color_values[colors[1]] |
| 57 | multiplier = color_values[colors[2]] |
| 58 | tolerance = tolerance_values[colors[3]] |
| 59 | |
| 60 | value = (digit1 * 10 + digit2) * (10 ** multiplier) |
| 61 | else: # len(colors) == 5 |
| 62 | # Five-band resistor |
| 63 | digit1 = color_values[colors[0]] |
| 64 | digit2 = color_values[colors[1]] |
| 65 | digit3 = color_values[colors[2]] |
| 66 | multiplier = color_values[colors[3]] |
| 67 | tolerance = tolerance_values[colors[4]] |
| 68 | |
| 69 | value = (digit1 * 100 + digit2 * 10 + digit3) * (10 ** multiplier) |
| 70 | |
| 71 | # Format the value with appropriate units |
| 72 | if value >= 1_000_000: |
| 73 | if value % 1_000_000 == 0: |
| 74 | formatted_value = f"{value // 1_000_000} megaohms" |
| 75 | else: |
| 76 | formatted_value = f"{value / 1_000_000:g} megaohms" |
| 77 | elif value >= 1_000: |
| 78 | if value % 1_000 == 0: |
| 79 | formatted_value = f"{value // 1_000} kiloohms" |
| 80 | else: |
| 81 | formatted_value = f"{value / 1_000:g} kiloohms" |
| 82 | else: |
| 83 | formatted_value = f"{value} ohms" |
| 84 | |
| 85 | # Format tolerance |
| 86 | if tolerance == int(tolerance): |
| 87 | tolerance_str = f"±{int(tolerance)}%" |
| 88 | else: |
| 89 | tolerance_str = f"±{tolerance:g}%" |
| 90 | |
| 91 | return f"{formatted_value} {tolerance_str}" |
| 92 | |
| 93 | # Handled Edge Cases: empty input, one-band resistor, invalid number of bands, invalid colors, zero ohm resistor |