有许多不同类型的电阻器可用于电路和电子电路。电阻值,容差和瓦特额定值通常以色带的形式印在电阻器的主体上。任务是找到4频段电阻器和5频段电阻器的电阻。
查找4频段电阻器电阻的程序
给定四个字符串A , B , C和D ,它们表示4波段电阻器的颜色代码。任务是使用给定的颜色代码查找电阻,公差和瓦特数额定值。
例子:
Input: A = “black”, B = “brown”, C = “red”, D = “green”
Output: Resistance = 01 x 100 ohms +/- 0.5 %
Input: A = “red”, B = “orange”, C = “yellow”, D = “green”
Output: Resistance = 23 x 10k ohms +/- 0.5 %
方法:想法是将数字和乘法器存储在哈希图中,然后可以计算电阻器的电阻。
下面是上述方法的实现:
Python3
# Python implementation to find the
# resistance of the resistor with
# the given color codes
# Function to find the resistance
# using color codes
def findResistance(a, b, c, d):
# Hash-map to store the values
# of the color-digits
color_digit = {'black': '0',
'brown': '1',
'red': '2',
'orange': '3',
'yellow': '4',
'green' : '5',
'blue' : '6',
'violet' : '7',
'grey' : '8',
'white': '9'}
multiplier = {'black': '1',
'brown': '10',
'red': '100',
'orange': '1k',
'yellow': '10k',
'green' : '100k',
'blue' : '1M',
'violet' : '10M',
'grey' : '100M',
'white': '1G'}
tolerance = {'brown': '+/- 1 %',
'red' : '+/- 2 %',
'green': "+/- 0.5 %",
'blue': '+/- 0.25 %',
'violet' : '+/- 0.1 %',
'gold': '+/- 5 %',
'silver' : '+/- 10 %',
'none': '+/-20 %'}
if a in color_digit
and b in color_digit\
and c in multiplier
and d in tolerance:
xx = color_digit.get(a)
yy = color_digit.get(b)
zz = multiplier.get(c)
aa = tolerance.get(d)
print("Resistance = "+xx + yy+\
" x "+zz+" ohms "+aa)
else:
print("Invalid Colors")
# Driver Code
if __name__ == "__main__":
a = "black"
b = "brown"
c = "red"
d = "green"
# Function Call
findResistance(a, b, c, d)
Python3
# Python implementation to find the
# resistance of the resistor with
# the given color codes
# Function to find the resistance
# using color codes
def findResistance(a, b, c, d, e):
# Hash-map to store the values
# of the color-digits
color_digit = {'black': '0',
'brown': '1',
'red': '2',
'orange': '3',
'yellow': '4',
'green' : '5',
'blue' : '6',
'violet' : '7',
'grey' : '8',
'white': '9'}
multiplier = {'black': '1',
'brown': '10',
'red': '100',
'orange': '1k',
'yellow': '10k',
'green' : '100k',
'blue' : '1M',
'violet' : '10M',
'grey' : '100M',
'white': '1G'}
tolerance = {'brown': '+/- 1 %',
'red' : '+/- 2 %',
'green': "+/- 0.5 %",
'blue': '+/- 0.25 %',
'violet' : '+/- 0.1 %',
'gold': '+/- 5 %',
'silver' : '+/- 10 %',
'none': '+/-20 %'}
if a in color_digit
and b in color_digit\
and c in color_digit
and d in multiplier\
and e in tolerance:
xx = color_digit.get(a)
yy = color_digit.get(b)
zz = color_digit.get(c)
aa = multiplier.get(d)
bb = tolerance.get(e)
print("Resistance = "+xx + yy\
+ zz+" x "+aa+" ohms "+bb)
else:
print("Invalid Colors")
# Driver Code
if __name__ == "__main__":
a = "red"
b = "orange"
c = "yellow"
d = "green"
e = "gold"
# Function Call
findResistance(a, b, c, d, e)
Resistance = 01 x 100 ohms +/- 0.5 %
查找5频段电阻器电阻的程序
给定五个字符串A , B , C , D和E ,它们表示5波段电阻器的颜色代码。任务是使用给定的颜色代码找到电阻,公差和瓦特数额定值。
例子:
Input: A = “black”, B = “brown”, C = “red”, D = “green”, E = “silver”
Output: Resistance = 012 x 100k ohms +/- 10 %
Input: A = “red”, B = “orange”, C = “yellow”, D = “green”, E = “gold”
Output: Resistance = 234 x 100k ohms +/- 5 %
方法:想法是将数字和乘法器存储在哈希图中,然后可以计算电阻器的电阻。
下面是上述方法的实现:
Python3
# Python implementation to find the
# resistance of the resistor with
# the given color codes
# Function to find the resistance
# using color codes
def findResistance(a, b, c, d, e):
# Hash-map to store the values
# of the color-digits
color_digit = {'black': '0',
'brown': '1',
'red': '2',
'orange': '3',
'yellow': '4',
'green' : '5',
'blue' : '6',
'violet' : '7',
'grey' : '8',
'white': '9'}
multiplier = {'black': '1',
'brown': '10',
'red': '100',
'orange': '1k',
'yellow': '10k',
'green' : '100k',
'blue' : '1M',
'violet' : '10M',
'grey' : '100M',
'white': '1G'}
tolerance = {'brown': '+/- 1 %',
'red' : '+/- 2 %',
'green': "+/- 0.5 %",
'blue': '+/- 0.25 %',
'violet' : '+/- 0.1 %',
'gold': '+/- 5 %',
'silver' : '+/- 10 %',
'none': '+/-20 %'}
if a in color_digit
and b in color_digit\
and c in color_digit
and d in multiplier\
and e in tolerance:
xx = color_digit.get(a)
yy = color_digit.get(b)
zz = color_digit.get(c)
aa = multiplier.get(d)
bb = tolerance.get(e)
print("Resistance = "+xx + yy\
+ zz+" x "+aa+" ohms "+bb)
else:
print("Invalid Colors")
# Driver Code
if __name__ == "__main__":
a = "red"
b = "orange"
c = "yellow"
d = "green"
e = "gold"
# Function Call
findResistance(a, b, c, d, e)
Resistance = 234 x 100k ohms +/- 5 %