## 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. While you could always get a tattoo like Jimmie Rodgers to help you remember the resistor color codes, in the meantime, you can write a function that will take a string containing a resistor's band colors and return a string identifying the resistor's ohms and tolerance values.

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

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

Each resistor will 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 the three bands "yellow violet black" would be 47 * 10^0 ohms, or 47 ohms.

Most resistors will also have a fourth band that is either gold or silver, with gold indicating plus or minus 5% tolerance, and silver indicating 10% tolerance. Resistors that do not have a fourth band are rated at 20% tolerance. (There are also more specialized resistors which can have more bands and additional meanings for some of the colors, but this kata will not cover them.)

## Your mission
The way the ohms value needs to be formatted in the string you return depends on the magnitude of the value:

* For resistors less than 1000 ohms, return a string containing the number of ohms, a space, the word "ohms" followed by a comma and a space, the tolerance value (5, 10, or 20), and a percent sign. For example,  for the "yellow violet black" resistor mentioned above, you would return `"47 ohms, 20%"`. 

* For resistors greater than or equal to 1000 ohms, but less than 1000000 ohms, you will use the same format as above, except that the ohms value will be divided by 1000 and have a lower-case "k" after it. For example, for a resistor with bands of "yellow violet red gold", you would return `"4.7k ohms, 5%"`

* For resistors of 1000000 ohms or greater, you will divide the ohms value by 1000000 and have an upper-case "M" after it. For example, for a resistor with bands of "brown black green silver", you would return `"1M ohms, 10%"`

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

## More examples, featuring some common resistor values
```
"brown black black"                "10 ohms, 20%"
"brown black brown gold"          "100 ohms, 5%"
"red red brown"                   "220 ohms, 20%"
"orange orange brown gold"        "330 ohms, 5%"
"yellow violet brown silver"      "470 ohms, 10%"
"blue gray brown"                 "680 ohms, 20%"
"brown black red silver"           "1k ohms, 10%"
"brown black orange"              "10k ohms, 20%"
"red red orange silver"           "22k ohms, 10%"
"yellow violet orange gold"       "47k ohms, 5%"
"brown black yellow gold"        "100k ohms, 5%"
"orange orange yellow gold"      "330k ohms, 5%"
"red black green gold"             "2M ohms, 5%"
```
Have fun! And if you enjoy this kata, check out the sequel: Resistor Color Codes, Part 2

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code: def decode_resistor_colors(bands):
	

Solutions:
code = {'black': 0, 'brown': 1, 'red': 2, 'orange': 3, 'yellow': 4, 'green': 5, 'blue': 6, 'violet': 7, 'gray': 8, 'white': 9, 'gold': 5, 'silver': 10, '': 20}

def decode_resistor_colors(bands):
	colors = (bands + ' ').split(' ')
	value = 10 * code[colors[0]] + code[colors[1]]
	value *= 10 ** code[colors[2]]
	tolerance = code[colors[3]]
	prefix = ''
	for p in 'kM':
		if value // 1000:
			prefix = p
			value /= 1000
	return '%g%s ohms, %d%%' % (value, prefix, tolerance)


d = {'black': 0, 'brown': 1, 'red': 2, 'orange': 3, 'yellow': 4, 'green': 5, 'blue': 6, 'violet': 7, 'gray': 8, 'white': 9, 'silver': 10, 'gold': 5}

def decode_resistor_colors(bands):
	bands = [d[b] for b in bands.split()]
	ohms = (bands[0] * 10 + bands[1]) * 10 ** bands[2]
	(ohms, sfx) = (ohms / 1000000.0, 'M') if ohms > 999999 else (ohms / 1000.0, 'k') if ohms > 999 else (ohms, '')
	return '{}{} ohms, {}%'.format(int(ohms) if ohms // 1 == ohms else ohms, sfx, bands[3] if len(bands) > 3 else 20)


BANDS = {'black': 0, 'brown': 1, 'red': 2, 'orange': 3, 'yellow': 4, 'green': 5, 'blue': 6, 'violet': 7, 'gray': 8, 'white': 9}
TOLERANCE_BAND = {'gold': 5, 'silver': 10}
LETTERS = {0: '', 1: 'k', 2: 'M'}
PATTERN = '{}{} ohms, {}%'

def decode_resistor_colors(bands):
	(first, second, third, *fourth) = bands.split(' ')
	ohms = (BANDS[first] * 10 + BANDS[second]) * 10 ** BANDS[third]
	count = 0
	while ohms >= 1000:
		ohms /= 1000
		count += 1
	ohms = int(ohms) if int(ohms) == ohms else ohms
	letter = LETTERS[count]
	if fourth:
		tolerance = TOLERANCE_BAND[fourth[0]]
	else:
		tolerance = 20
	return PATTERN.format(ohms, letter, tolerance)


def decode_resistor_colors(bands):
	color = {'black': 0, 'brown': 1, 'red': 2, 'orange': 3, 'yellow': 4, 'green': 5, 'blue': 6, 'violet': 7, 'gray': 8, 'white': 9}
	tolerance = {'gold': 5, 'silver': 10, 'none': 20}
	(a, b, p, t, *_) = bands.split() + ['none']
	c = (10 * color[a] + color[b]) * 10 ** color[p]
	(r, m) = next(((c / x, y) for (x, y) in [(10 ** 6, 'M'), (10 ** 3, 'k'), (1, '')] if c // x > 0))
	return '{:g}{} ohms, {}%'.format(r, m, tolerance[t])


codes = {'black': 0, 'brown': 1, 'red': 2, 'orange': 3, 'yellow': 4, 'green': 5, 'blue': 6, 'violet': 7, 'gray': 8, 'white': 9, 'gold': 5, 'silver': 10}

def decode_resistor_colors(bands):
	bands = [codes[band] for band in bands.split()] + [20]
	ohms = (bands[0] * 10 + bands[1]) * 10 ** bands[2]
	p = ''
	for c in 'kM':
		if ohms // 1000:
			ohms /= 1000
			p = c
	return '%s%s ohms, %s%%' % (str(ohms).replace('.0', ''), p, bands[3])


def decode_resistor_colors(bands):
	bands_lst = bands.split(' ')
	VALUES = {'black': 0, 'brown': 1, 'red': 2, 'orange': 3, 'yellow': 4, 'green': 5, 'blue': 6, 'violet': 7, 'gray': 8, 'white': 9, 'gold': 5, 'silver': 10}
	first = VALUES[bands_lst[0]]
	second = VALUES[bands_lst[1]]
	num = str(first) + str(second)
	ohms = int(num) * pow(10, VALUES[bands_lst[2]])
	unit = ' ohms'
	tolerance = 20
	if len(bands_lst) > 3:
		tolerance = VALUES[bands_lst[3]]
	if ohms in range(0, 999):
		unit = ' ohms'
	elif ohms in range(1000, 999999):
		ohms = ohms / 1000
		unit = 'k ohms'
	elif ohms >= 1000000:
		ohms = ohms / 1000000
		unit = 'M ohms'
	return '{:g}{}, {}%'.format(ohms, unit, tolerance)


def decode_resistor_colors(bands):
	dic = {'black': 0, 'brown': 1, 'red': 2, 'orange': 3, 'yellow': 4, 'green': 5, 'blue': 6, 'violet': 7, 'gray': 8, 'white': 9}
	lst = bands.split()
	num = dic[lst[0]] * 10 + dic[lst[1]]
	numbe = num * 10 ** dic[lst[2]]
	if len(lst) > 3:
		if lst[3] == 'gold':
			tol = 5
		elif lst[3] == 'silver':
			tol = 10
	else:
		tol = 20
	if numbe >= 1000000:
		if numbe % 1000000 == 0:
			number = numbe // 1000000
		else:
			number = numbe / 1000000
		return str(number) + 'M ohms, ' + str(tol) + '%'
	elif numbe >= 1000:
		if numbe % 1000 == 0:
			number = numbe // 1000
		else:
			number = numbe / 1000
		return str(number) + 'k ohms, ' + str(tol) + '%'
	else:
		number = numbe
		return str(number) + ' ohms, ' + str(tol) + '%'


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

def decode_resistor_colors(bands):
	bands = bands.split()
	tol = ' ohms, ' + ('20%' if len(bands) < 4 else '10%' if bands[-1] == 'silver' else '5%')
	(first, second, power) = [COLOR_CODES[band] for band in bands[:3]]
	resistance = '{:.1f}'.format((first + second / 10) * 10 ** ((power + 1) % 3))
	return (resistance if not resistance.endswith('0') else resistance[:-2]) + ('  kkkMMM'[power] if power > 1 else '') + tol


COLOR_CODE = {'silver': 10, 'gold': 5, 'black': 0, 'brown': 1, 'red': 2, 'orange': 3, 'yellow': 4, 'green': 5, 'blue': 6, 'violet': 7, 'gray': 8, 'white': 9}
DEFAULT_TOLERANCE = 20

def decode_resistor_colors(bands):
	bands = [COLOR_CODE[band] for band in bands.split()] + [DEFAULT_TOLERANCE]
	value = bands[0] * 10 + bands[1]
	multiplier = bands[2]
	tolerance = bands[3]
	exponent = multiplier + 1
	if exponent >= 6:
		letter = 'M'
		exponent -= 6
	elif exponent >= 3:
		letter = 'k'
		exponent -= 3
	else:
		letter = ''
	value *= 10 ** (exponent - 1)
	return '{:g}{} ohms, {}%'.format(value, letter, tolerance)