def factorial(n):
prod=1
if n>1:
for i in xrange(2,n+1):
prod*=i
return prod
n=7
print 'n!=%d' %factorial(n)
def linecount(text):
return len(text)
Sum=0
lines=['Now is the time for all good men','to come to the aid of their country.']
for i in lines:
Sum+=linecount(i)
count=len(lines)
count=float(count)
avg=Sum/count
print 'Average numbers of characters per line: ',avg
import math
a,b,x1,y1,xr,yr,CNST=0,0,0,0,0,0,0.0001
def reduce():
global x1,xr,y1,yr,a,b
x1 = a+0.5*(b-a-CNST)
xr = x1 + CNST
y1 = curve(x1)
yr = curve(xr)
if y1 > yr:
b = xr
elif y1 < yr:
a = x1
return
def curve(x):
return x*math.cos(x)
a = 0
b = 3.141593
while True:
reduce()
if y1 == yr or b-a <= 3*CNST:
break
xmax = 0.5*(x1+xr)
ymax = curve(xmax)
print 'xmax = %8.6f ymax = %8.6f' %(xmax,ymax)
def linecount(text):
global Sum
Sum += len(text)
Sum=0
lines=['Now is the time for all good men','to come to the aid of their country.']
for i in lines:
linecount(i)
count=len(lines)
count=float(count)
avg=Sum/count
print 'Average numbers of characters per line: ',avg
def fibonacci(count):
if count<3:
f=1
else:
f = fibonacci.f1 + fibonacci.f2
fibonacci.f2 = fibonacci.f1
fibonacci.f1 = f
return f
fibonacci.f1,fibonacci.f2=1,1
n=30
for count in range(1,n+1):
print 'i = %2d F = %d' %(count,fibonacci(count))
def play():
print "Throwing the dice...."
score1=throw()
print "%2d" %(score1)
if score1==7 or score1==11:
print "Congratulations!! you WIN on the first throw"
elif score1==2 or score1==3 or score1==12:
print "sorry!! you LOSE on the first throw"
else:
while(True):
print "Throwing the dice again..."
score2=throw()
print "%2d" %(score2)
if score2==score1 or score2==7:
break
if score2==score1:
print "You WIN by matching your first score"
else:
print "You LOSE by failing to match your first score"
return
def throw():
n1=random.randrange(1,7)
n2=random.randrange(1,7)
return n1+n2
import random
print "Welcome to the Game of Craps \n\n"
random.seed(563)
play()
import math
a,b,x1,y1,xr,yr,CNST=0,0,0,0,0,0,0.0001
def reduce():
global x1,xr,y1,yr,a,b
x1 = a+0.5*(b-a-CNST)
xr = x1 + CNST
y1 = curve(x1)
yr = curve(xr)
if y1 > yr:
b = xr
elif y1 < yr:
a = x1
return
def curve(x):
return x*math.cos(x)
a = 0
b = 3.141593
while True:
reduce()
if y1 == yr or b-a <= 3*CNST:
break
xmax = 0.5*(x1+xr)
ymax = curve(xmax)
print 'xmax = %8.6f ymax = %8.6f' %(xmax,ymax)
def fibonacci(count):
if count<3:
f=1
else:
f = fibonacci.f1 + fibonacci.f2
fibonacci.f2 = fibonacci.f1
fibonacci.f1 = f
return f
fibonacci.f1,fibonacci.f2=1,1
n=40
for count in range(1,n+1):
print 'i = %2d F = %d' %(count,fibonacci(count))
import math
p,r,n=10000,10,3
i=r/100.0
f=p*math.pow(1+i,n)
print "The final value (F) is: %.2f" %f