'''
example 5.1 page no :41
'''
import math
def area (radius):
area = math.pi * radius * radius;
return area;
print area(3)
'''
example 5.2 page no :42
'''
def absoluteValue (x):
if (x < 0):
return -x
else:
return x
print absoluteValue(-3)
print absoluteValue(3)
'''
example 5.3 page no :44
'''
def distance (x1, y1,x2,y2):
dx = x2 - x1
dy = y2 - y1;
print "dx is " , dx
print "dy is " , dy
return 0.0
distance(10,10,20,20)
'''
example 5.4 page no :44
'''
def distance (x1, y1,x2,y2):
dx = x2 - x1
dy = y2 - y1;
dsquared = dx*dx + dy*dy;
print "dsquared is " , dsquared
return 0.0;
distance(10,10,20,20)
'''
example 5.5 page no :45
'''
import math
def distance (x1, y1,x2,y2):
dx = x2 - x1
dy = y2 - y1;
dsquared = dx*dx + dy*dy;
return math.sqrt(dsquared)
print distance(10,10,20,20)
'''
example 5.6 page no :45
'''
import math
def area (radius):
area = math.pi * radius * radius;
return area;
def distance (x1, y1,x2,y2):
dx = x2 - x1
dy = y2 - y1;
dsquared = dx*dx + dy*dy;
return math.sqrt(dsquared)
def fred(xc, yc, xp,yp):
radius = distance (xc, yc, xp, yp);
result = area (radius);
return result;
print fred(10,10,20,20)
'''
example 5.7 page no :48
'''
def isSingleDigit(x):
if (x >= 0 and x < 10):
return True
else:
return False;
print isSingleDigit(11)
print isSingleDigit(4)
'''
example 5.8 page no :51/53
'''
def factorial (n):
if (n == 0):
return 1;
else:
recurse = factorial (n-1)
result = n * recurse
return result;
print factorial(5)
'''
example 5.9 page no :53
'''
def fibonacci (n):
if (n == 0 or n == 1):
return 1
else:
return fibonacci (n-1) + fibonacci (n-2)
print fibonacci(5)