import math
# tests the sqrt() function:
for i in range(0,6):
print "\t %d \t %f" %(i,math.sqrt(i))
import math
# tests the identity sin 2x = 2 sin x cos x:
x = 0
while x < 2:
print "%f \t\t %f \t %f" %(x,math.sin(2*x),2*math.sin(x)*math.cos(x))
x += 0.2
def cube(x):
# returns cube of x:
return x*x*x
def cube(x):
# returns cube of x:
return x*x*x
# tests the cube() function:
n=1
while (n != 0):
n = int(raw_input())
print "\tcube( %d ) = %d" %(n,cube(n))
def maximum(x,y):
# returns larger of the two given integers:
if (x < y):
return y
else:
return x
# tests the max() function:
m = 1
n = 1
while m != 0:
m = int(raw_input())
n = int(raw_input())
print "\tmax( %d , %d ) = %d" %(m,n,maximum(m,n))
def maximum(x,y):
# returns larger of the two given integers:
if (x < y):
return y
else:
return x
# tests the max() function:
m = 1
n = 1
while m != 0:
m = int(raw_input())
n = int(raw_input())
print "\tmax( %d , %d ) = %d" %(m,n,maximum(m,n))
# returns larger of the two given integers:
m = 1
n = 1
while m!=0:
m = int(raw_input())
n = int(raw_input())
print "\tmax(%d,%d) = %d" %(m,n, max(m,n))
def fact(n):
if (n < 0):
return 0
f = 1
while (n > 1):
f *= n
n -= 1
return f
for i in range(-1,6):
print fact(i),
def fact(n):
if (n < 0):
return 0
f = 1
while (n > 1):
f *= n
n -= 1
return f
def perm(n,k):
# returns P(n,k), the number of permutations of k from n:
if (n < 0 or k < 0 or k > n):
return 0
return fact(n)/fact(n-k)
for i in range(-1,8):
for j in range(-1,i+2):
print perm(i,j),
print ''
def printDate(m,d,y):
# prints the given date in literal form:
if (m < 1 or m > 12 or d < 1 or d > 31 or y < 0):
print "Error: parameter out of range.\n"
return
if m == 1:
print "January ",
elif m ==2:
print "February ",
elif m==3 :
print "March ",
elif m==4:
print "April ",
elif m==5:
print "May ",
elif m==6:
print "June ",
elif m==7:
print "July ",
elif m==8:
print "August ",
elif m==9:
print "September ",
elif m==10:
print "October ",
elif m==1:
print "November ",
else:
print "December ",
print d , ", ", y
# tests the printDate() function:
month = 1
while month > 0:
month = int(raw_input())
day = int(raw_input())
year = int(raw_input())
printDate(month,day,year)
import string
def ispunct(s):
return all(c in string.punctuation for c in s)
def printCharCategory(c):
# prints the category to which the given character belongs:
print "The character [" + c + "] is a ",
if(c.isdigit()):
print "digit.\n"
elif (c.islower()):
print "lower-case letter.\n"
elif (c.isupper()):
print "capital letter.\n"
elif (c.isspace()):
print "white space character.\n"
elif (ord(c) >= 10 and ord(c) <= 15 or ord(c) == 0):
print "control character.\n"
elif (ispunct(c)):
print "punctuation mark.\n"
else:
print "Error.\n"
# prints the category to which the given character belongs;
# tests the printCharCategory() function:
for c in range(128):
printCharCategory(chr(c))
import math
def isPrime(n):
# returns True if n is prime, False otherwise:
sqrtn = math.sqrt(n)
if (n < 2):
return False
# 0 and 1 are not primes
if (n < 4):
return True
# 2 and 3 are the first primes
if (n%2 == 0):
return False
# 2 is the only even prime
for d in range(3,int(sqrtn+1),2):
if (n%d == 0):
return False
# n has a nontrivial divisor
return True;
for n in range(0,80):
if (isPrime(n)):
print n,
def isLeapYear(y):
# returns true iff y is a leap year:
return (y % 4 == 0 and y % 100 != 0 or y % 400 == 0)
# tests the isLeapYear() function:
n = 2
while n > 1:
n = int(raw_input())
if (isLeapYear(n)):
print "%d is a leap year." % n
else:
print "%d is not a leap year." %n
def age():
# prompts the user to input his/her age, and returns that value:
while (True):
print "How old are you: "
n = int(raw_input())
if (n < 0):
print "\a\tYour age could not be negative."
elif (n > 120):
print "\a\tYou could not be over 120."
else:
return n
print "\n\tTry again.\n"
a = age();
print "\nYou are %d years old." %a
def swap(x,y):
# exchanges the values of x and y:
x[0],y[0] = y[0],x[0]
a = [22.2]
b = [44.4]
print "a = %.2f , b = %.2f " %(a[0],b[0])
swap(a,b)
print "a = %.2f , b = %.2f " %(a[0],b[0])
'''
Note : Python doesn't support pass value by reference. but can be done by passing list.
'''
def f(x,y):
x[0]= 88
y[0] = 99
# tests the f() function:
a = [22]
b = [44]
print "a = %.2f , b = %.2f " %(a[0],b[0])
f(a,b)
print "a = %.2f , b = %.2f " %(a[0],b[0])
f(2*a,b)
print "a = %.2f , b = %.2f " %(a[0],b[0])
def computeCircle(r):
# returns the area and circumference of a circle with radius r:
PI = 3.141592653589793
area = PI*r*r
circumference = 2*PI*r
return area,circumference
# tests the computeCircle() function:
print "Enter radius: "
r = int(raw_input())
a,c = computeCircle(r)
print "area = %.2f , circumference = %.2f" %(a,c)
'''
Note : Python passes variable by value and not by reference. So output would be differ.
'''
def f(x,y,z):
x[0] += z[0]
y[0] += z[0]
print "x = %d , y = %d , z = %d" %(x[0],y[0],z[0])
x = [22]
y = [33]
z = [44]
print "x = %d , y = %d , z = %d" %(x[0],y[0],z[0])
f(x,y,z)
print "x = %d , y = %d , z = %d" %(x[0],y[0],z[0])
x[0] = 2*x[0] - 3
f(x,y,z)
print "x = %d , y = %d , z = %d" %(x[0],y[0],z[0])
def cube(x):
# returns cube of x:
return x*x*x
# tests the cube() function:
print cube(4)
x = int(raw_input())
y = cube(2*x-3)
print y
'''
Python has it's own scope so output would be differ.
'''
x = 11
def f():
x = 44
print "In f(): x = %d" % x
def g():
print "In g(): x = %d" % x
x = 22
x = 33
print "In block inside main(): x = %d" % x
print "In main(): x = %d" % x
print "In main(): ::x = %d" % x
f()
g()
def max_(x, y,z=0):
if x > y and x > y:
return x
elif y > x and y > z:
return y
else:
return z
print max(99,77), " " , max(55,66,33)
# prints the quotient of two input integers:
print "Enter two integers: "
n = int(raw_input())
d = int(raw_input())
if (d == 0):
import sys
sys.exit(0)
print n , "/" , d , " = " , n/d
def reciprocal(x):
#returns the reciprocal of x:
if (x == 0):
import sys
sys.exit(1); # terminate the program
return 1.0/x
x = float(raw_input())
print reciprocal(x)
'''
This function evaluates the third degree polynomial a0 + a1x + a2x2 + a3x3.
'''
def p(x,a0,a1=0,a2=0,a3=0):
# returns a0 + a1*x + a2*x^2 + a3*x^3:
return (a0 + (a1 + (a2 + a3*x)*x)*x)
# tests the p() function:
x = 2.0003
print "p(x,7) = %f" % p(x,7)
print "p(x,7,6) = %f" % p(x,7,6)
print "p(x,7,6,5) = %f" % p(x,7,6,5)
print "p(x,7,6,5,4) = %f" % p(x,7,6,5,4)