Created
October 1, 2019 03:39
-
-
Save alacret/9d82b51e4d33e88a0c8e808a86105546 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
COLOR_CODE = { 0: 'black', 1: 'brown', 2: 'red', 3: 'orange', | |
4: 'yellow', 5: 'green', 6: 'blue', 7: 'violet', 8: 'gray', | |
9: 'white'} | |
def encode_resistor_colors(ohms_string): | |
# your code here | |
ohms = ohms_string.split(' ')[0] | |
trailing_zeros = 0 | |
last_index = len(ohms) - 1 | |
last = ohms[last_index] | |
if last == 'k': | |
trailing_zeros = 3 | |
ohms = ohms[:last_index] | |
if last == 'M': | |
trailing_zeros = 6 | |
ohms = ohms[:last_index] | |
if ohms.find('.') != -1: | |
digits = ohms.replace('.','') | |
trailing_zeros = trailing_zeros - 1 | |
else: | |
digits = ohms | |
colors = [] | |
colors.append(COLOR_CODE[int(digits[0])]) | |
if len(digits) > 1: | |
colors.append(COLOR_CODE[int(digits[1])]) | |
else: | |
colors.append('black') | |
trailing_zeros = trailing_zeros - 1 | |
if len(digits) > 2: | |
if digits[2] == '0' and trailing_zeros == 0: | |
colors.append('brown') | |
else: | |
if digits[2] == '0': | |
colors.append(COLOR_CODE[trailing_zeros + 1]) | |
else: | |
colors.append(COLOR_CODE[int(digits[2])]) | |
else: | |
if trailing_zeros == 0: | |
colors.append('black') | |
else: | |
colors.append(COLOR_CODE[int(trailing_zeros)]) | |
colors.append('gold') | |
print(ohms_string) | |
print(colors) | |
return ' '.join(colors) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment