Skip to content

Instantly share code, notes, and snippets.

@FrancoB411
Created October 1, 2019 00:32
Show Gist options
  • Save FrancoB411/b4aa87dd503c84f7315d26f38d491572 to your computer and use it in GitHub Desktop.
Save FrancoB411/b4aa87dd503c84f7315d26f38d491572 to your computer and use it in GitHub Desktop.
ohms regex
import re
re_string = "(\d+\.\d+|\d+|)(\D*)\s(ohms)"
inputs = [
"10 ohms",
"100 ohms",
"220 ohms",
"330 ohms",
"470 ohms",
"680 ohms",
"1k ohms",
"10k ohms",
"22k ohms",
"47k ohms",
"4.7k ohms",
"100k ohms",
"330k ohms",
"2M ohms"
]
for input in inputs:
i = re.search(re_string, input)
print(i.group(1))
print(i.group(2))
print(i.group(3))
@lfaynerman
Copy link

def encode_resistor_colors(ohms_string):
color_codes = {"0": "black",
"1": "brown",
"2": "red",
"3": "orange",
"4": "yellow",
"5": "green",
"6": "blue",
"7": "violet",
"8": "gray",
"9": "white"}

ohm_value = ohms_string.split()[0]
if ohm_value[-1] == "k":
    ohm_value = ohm_value[0:-1] + "000"
elif ohm_value[-1] == "M":
    ohm_value = ohm_value[0:-1] + "000000"
    
if ohm_value.find(".") > 0:
    ohm_value = ohm_value.replace(".", "")[0:-1]          

return color_codes[ohm_value[0]] + " " + color_codes[ohm_value[1]] + " " + color_codes[str(len(ohm_value)-2)] + " gold"`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment