## Overview
Resistors are electrical components marked with colorful stripes/bands to indicate both their resistance value in ohms and how tight a tolerance that value has. If you did my Resistor Color Codes kata, you wrote a function which took a string containing a resistor's band colors, and returned a string identifying the resistor's ohms and tolerance values.


Well, now you need that in reverse: The previous owner of your "Beyond-Ultimate Raspberry Pi Starter Kit" (as featured in my Fizz Buzz Cuckoo Clock kata) had emptied all the tiny labeled zip-lock bags of components into the box, so that for each resistor you need for a project, instead of looking for text on a label, you need to find one with the sequence of band colors that matches the ohms value you need.

## The resistor color codes
You can see this Wikipedia page for a colorful chart, but the basic resistor color codes are:

0: black, 1: brown, 2: red, 3: orange, 4: yellow, 5: green, 6: blue, 7: violet, 8: gray, 9: white

All resistors have at least three bands, with the first and second bands indicating the first two digits of the ohms value, and the third indicating the power of ten to multiply them by, for example a resistor with a value of 47 ohms, which equals 47 * 10^0 ohms, would have the three bands "yellow violet black".

Most resistors also have a fourth band indicating tolerance -- in an electronics kit like yours, the tolerance will always be 5%, which is indicated by a gold band. So in your kit, the 47-ohm resistor in the above paragraph would have the four bands "yellow violet black gold".

## Your mission
Your function will receive a string containing the ohms value you need, followed by a space and the word "ohms" (to avoid Codewars unicode headaches I'm just using the word instead of the ohms unicode symbol). The way an ohms value is formatted depends on the magnitude of the value:

* For resistors less than 1000 ohms, the ohms value is just formatted as the plain number. For example, with the 47-ohm resistor above, your function would receive the string `"47 ohms"`, and return the string `"yellow violet black gold".

* For resistors greater than or equal to 1000 ohms, but less than 1000000 ohms, the ohms value is divided by 1000, and has a lower-case "k" after it. For example, if your function received the string `"4.7k ohms"`, it would need to return the string `"yellow violet red gold"`.

* For resistors of 1000000 ohms or greater, the ohms value is divided by 1000000, and has an upper-case "M" after it. For example, if your function received the string `"1M ohms"`, it would need to return the string `"brown black green gold"`. 

Test case resistor values will all be between 10 ohms and 990M ohms.

## More examples, featuring some common resistor values from your kit
```
"10 ohms"        "brown black black gold"
"100 ohms"       "brown black brown gold"
"220 ohms"       "red red brown gold"
"330 ohms"       "orange orange brown gold"
"470 ohms"       "yellow violet brown gold"
"680 ohms"       "blue gray brown gold"
"1k ohms"        "brown black red gold"
"10k ohms"       "brown black orange gold"
"22k ohms"       "red red orange gold"
"47k ohms"       "yellow violet orange gold"
"100k ohms"      "brown black yellow gold"
"330k ohms"      "orange orange yellow gold"
"2M ohms"        "red black green gold"
```

Have fun!

Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code:
def encode_resistor_colors(ohms_string):
	

Solutions:
c = 'black brown red orange yellow green blue violet gray white'.split()

def encode_resistor_colors(ohms_string):
	ohms = str(int(eval(ohms_string.replace('k', '*1000').replace('M', '*1000000').split()[0])))
	return '%s %s %s gold' % (c[int(ohms[0])], c[int(ohms[1])], c[len(ohms[2:])])


COLORS = {'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):
	ohmS = str(int(eval(ohmS.replace(' ohms', '').replace('k', '*1000').replace('M', '*1000000'))))
	return '{} {} {} gold'.format(COLORS[ohmS[0]], COLORS[ohmS[1]], COLORS[str(len(ohmS[2:]))])


def encode_resistor_colors(ohms_string):
	s = str(get_num_ohms(ohms_string))
	ohms = s[:2]
	tolerance = str(len(s[2:]))
	color_bands = [get_color(ohms[0]), get_color(ohms[1]), get_color(tolerance), 'gold']
	return ' '.join(color_bands)

def get_num_ohms(s):
	n = s.split(' ')[0]
	multiplier = 1000 if n[-1] == 'k' else 1000000 if n[-1] == 'M' else 1
	f = float(n[:-1]) * multiplier if multiplier > 1 else float(n)
	return int(f)

def get_color(n):
	color_map = {'0': 'black', '1': 'brown', '2': 'red', '3': 'orange', '4': 'yellow', '5': 'green', '6': 'blue', '7': 'violet', '8': 'gray', '9': 'white'}
	return color_map[n]


c = 'black brown red orange yellow green blue violet gray white'.split()

def encode_resistor_colors(ohms_string):
	(t, u, *p) = str(int(eval(ohms_string.replace('M', '*1000k').replace('k', '*1000').split()[0])))
	return '%s %s %s gold' % (c[int(t)], c[int(u)], c[len(p)])


import math
import re
COLORS = {'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):
	ohms = re.sub('([0-9\\.]+)([kM])?(.*)', lambda x: str(float(x[1]) * (int(x[2].translate(str.maketrans({'k': '1000', 'M': '1000000'}))) if x[2] else 1)), ohms_string)
	return f"{' '.join((COLORS.get(x) for x in ohms[:2]))} {COLORS.get(str(len(ohms[2:]) - 2))} gold"


def encode_resistor_colors(ohms_string):
	color_digit = {'0': 'black', '1': 'brown', '2': 'red', '3': 'orange', '4': 'yellow', '5': 'green', '6': 'blue', '7': 'violet', '8': 'gray', '9': 'white'}
	multiplier = {'1': 'black', '2': 'black', '3': 'brown', '4': 'red', '5': 'orange', '6': 'yellow', '7': 'green', '8': 'blue', '9': 'violet', '10': 'grey', '11': 'white'}
	number = ohms_string.rstrip('ohms')
	snumber = ''
	mult = ''
	answer = ''
	fnum = ''
	check = []
	(a, b, c) = ('', '', '')
	for i in number:
		if i.isdigit() == True:
			snumber += i
	if len(snumber) > 1:
		a = snumber[0]
		b = snumber[1]
		answer = str(color_digit.get(a)) + ' ' + str(color_digit.get(b))
	elif len(snumber) == 1:
		a = snumber[0]
		answer = str(color_digit.get(a)) + ' ' + 'black'
	for s in number:
		if s.isdigit() == True or s == '.':
			fnum += s
	for j in number:
		check.append(j)
		if 'M' in check:
			mult = 1000000
		elif 'k' in check:
			mult = 1000
		else:
			mult = 1
	c = str(int(eval(str(fnum) + '*' + str(mult))))
	return answer + ' ' + multiplier.get(str(len(c))) + ' gold'


import re
colors = ['black', 'brown', 'red', 'orange', 'yellow', 'green', 'blue', 'violet', 'gray', 'white']

def encode_resistor_colors(ohms_string):
	split_string = ohms_string.split()
	number = split_string[0]
	if number[-1] == 'k':
		number = str(int(float(number[0:-1]) * 1000))
	elif number[-1] == 'M':
		number = str(int(float(number[0:-1]) * 1000000))
	color1 = colors[int(number[0])]
	color2 = colors[int(number[1])]
	color3 = colors[len(number) - 2]
	return color1 + ' ' + color2 + ' ' + color3 + ' gold'


import re
import math
REGEX_NUMBERS = '\\d+\\.?\\d*'
RESISTOR_COLORS = {0: 'black', 1: 'brown', 2: 'red', 3: 'orange', 4: 'yellow', 5: 'green', 6: 'blue', 7: 'violet', 8: 'gray', 9: 'white'}
MULTIPLIER = {'k': 1000, 'M': 1000000}

def encode_resistor_colors(ohms_string):
	retrieved_val = re.findall(REGEX_NUMBERS, ohms_string.replace('ohms', ''))[0]
	needs_trailing_zero = len(retrieved_val) == 1
	retrieved_val = retrieved_val + '0' if needs_trailing_zero else retrieved_val
	translation = ' '.join([RESISTOR_COLORS[int(digit)] for digit in retrieved_val if digit.isnumeric()][:2])
	for key in MULTIPLIER:
		retrieved_val = float(retrieved_val) * MULTIPLIER.get(key) if key in ohms_string else float(retrieved_val)
	subtract = 2 if needs_trailing_zero else 1
	return translation + ' ' + RESISTOR_COLORS[math.floor(math.log10(retrieved_val)) - subtract] + ' gold'


import re
from math import log10

def encode_resistor_colors(ohms_string):
	codes = {'0': 'black', '1': 'brown', '2': 'red', '3': 'orange', '4': 'yellow', '5': 'green', '6': 'blue', '7': 'violet', '8': 'gray', '9': 'white'}
	(initial_number, group) = re.findall('([0-9.]+)([k,M])?', ohms_string).pop()
	num = float(initial_number) * int(group == 'k' and 1000 or (group == 'M' and 1000000) or 1)
	processed_number = str(float(initial_number) * 10)[:2]
	final = [codes[x] for x in processed_number] + [codes[str(int(log10(num / float(processed_number))))], 'gold']
	return ' '.join(final)


colors = ['black', 'brown', 'red', 'orange', 'yellow', 'green', 'blue', 'violet', 'gray', 'white', 'gold']

def encode_resistor_colors(ohms_string):
	ohms = int(eval(ohms_string.split()[0].replace('k', '*1E3').replace('M', '*1E6')))
	return ' '.join((colors[i] for i in [int(str(ohms)[0]), int(str(ohms)[1]), len(str(ohms)) - 2, 10]))