Python @ DjangoSpin

PyPro #50 Check for a Geometric Progression

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Check for Geometric Progression in Python

Check for Geometric Progression in Python

Write a Python function which accepts a list of terms and tells the user whether these terms make up a Geometric Progression or not.

Geometric Progression

def gpChecker(listOfTerms):
    if len(listOfTerms) <= 1:
    	return True
    commonRatio = listOfTerms[1] / float(listOfTerms[0])
    for index in range( 1, len(listOfTerms) ):
    	if listOfTerms[index] / float(listOfTerms[index - 1]) != commonRatio:
    		return False
    return True

print(gpChecker([3, 12, 48, 192]))           # True

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply