Skip to content

Instantly share code, notes, and snippets.

@andermoran
Created July 21, 2017 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andermoran/b82c4ab9563e4d8c5d74b7a1572299f9 to your computer and use it in GitHub Desktop.
Save andermoran/b82c4ab9563e4d8c5d74b7a1572299f9 to your computer and use it in GitHub Desktop.
A program that finds the total resistance of n parallel resistors
#!/usr/bin/python
import numbers
def findDivisor(num):
# 2,3 are the most common divisor for many numbers hence going by divisor of 2,3 can be quicker
# if not then by the same number as divisor
if num%2 == 0:
return 2
elif num%3==0:
return 3
return num
def findLCM(lcmArray):
lcm = 1
while len(lcmArray) > 0:
minOfLCMArray = min(lcmArray)
divisor = findDivisor(minOfLCMArray)
for x in xrange(0, len(lcmArray)):
Quotient = lcmArray[x]/divisor
Reminder = lcmArray[x]%divisor
if Reminder == 0:
lcmArray[x] = Quotient
lcm*=divisor
minOfLCMArray = min(lcmArray)
if minOfLCMArray == 1:
lcmArray.remove(minOfLCMArray)
return lcm
def totalParallelResistance(resistorList):
lcm = findLCM(resistorList[:]) # [:] needed so that a copy of resistorList is passed and the resistorList object does not get manipulated
# print resistorList
numerator = float(0) # Needed so the the calculations have decimal values and not only integer values
for resistor in resistorList:
# print lcm/resistor
numerator += lcm/resistor
# print "numerator = %s" % numerator
totalResistance = lcm/numerator
print "Total resistance = %f Ohms" % totalResistance
def printSampleInput():
print "--------------------------------------------------------"
print "|\t\tVIEW THE SAMPLE INPUT BELOW\t\t|\n| Enter the resistors(Ohms) separated by commas: 4,6,12\t|"
print "--------------------------------------------------------"
def getUserInput():
user_input = []
try:
user_input = input('Enter the resistors(Ohms) separated by commas: ')
except (SyntaxError, NameError): # will catch empty input, input as letters without quotes,
print("Error: No valid input detected!")
printSampleInput()
exit(-1); # Exit with error
if isinstance(user_input, tuple): # Check if the input was read as a tuple (meaning more than one input)
resistors = [i for i in user_input] # Parses user_input and converts it from a tuple into a list
for obj in resistors:
if not isinstance(obj, numbers.Number): # check if all elements of list are numbers
print("Error: Invalid input detected!")
printSampleInput()
exit(-1); # Exit with error
return resistors
elif isinstance(user_input, numbers.Number): # Only one input and that input is valid
print "Total resistance = %f Ohms" % user_input
exit(0) # Exit in good condition
else: # At least one input and at least one invalid input
print("Error: Invalid input detected!")
printSampleInput()
exit(-1) # Exit with error
inputs = getUserInput()
totalParallelResistance(inputs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment