def fact(k):
p = 1
for i in range(1,k+1):
p = p * i
return p
n = int(raw_input("\nEnter value to n and r : "))
r = int(raw_input(""))
ncr = fact(n)/ (fact(r)*fact(n-r))
print "\nValue of nCr = ",ncr
def big(a,b,c):
if a > b:
if a > c:
return a
else:
return c
else:
if b > c:
return b
else:
return c
t1 = int(raw_input("\nEnter three test scores : "))
t2 = int(raw_input(""))
t3 = int(raw_input(""))
a1 = int(raw_input("\nEnter three assignment scores : "))
a2 = int(raw_input(""))
a3 = int(raw_input(""))
total = big(t1,t2,t3) + big(a1,a2,a3)
print "\nTotal marks = ",total
import sys
def cosine(x):
x = x * 3.14/180
s = 0
term = 1
i = 0
for k in range(1,15+1):
s = s + term
term = term*x*x*(-1)/((i+1)*(i+2))
i = i + 2
return s
x = 0
print "\n------------------------------------"
print " x in degrees cos(x) "
print "------------------------------------"
while x <= 180:
sys.stdout.write("\n\t%6.0f %6.2f"%(x,cosine(x)))
x = x + 30
print "\n------------------------------------"
def gcd(x,y):
if x >= y:
nr = x
dr = y
else:
nr = y
dr = x
r = nr % dr
while r != 0:
nr = dr
dr = r
r = nr % dr
return dr
a = int(raw_input("\nEnter three integers : "))
b = int(raw_input(""))
c = int(raw_input(""))
d1 = gcd(a,b)
d2 = gcd(a,c)
d3 = gcd(b,c)
if d1 == d2:
if d1 == d3:
print "\nGreatest common divisor is ",d1
else:
print "\nGreatest common divisor is ",gcd(d1,d3)
else:
print "\nGreatest common divisor is ",gcd(d1,d2)
print "\n\nPress any key to continue..."
def reverse(n):
rn = 0
while n > 0:
r = n % 10
rn = rn * 10 + r
n = n / 10
return rn
n = int(raw_input("\nEnter the integer : "))
print "\n",n," is erversed as ",reverse(n)
print "\nPress any key to continue..."
def compare(s1,s2):
if s1 == s2:
return 0
if s1 > s2:
return 1
if s1 < s2:
return -1
s1 = raw_input("\nEnter the first string : ")
s2 = raw_input("\nEnter the second string : ")
print "\nThe result is ",compare(s1,s2)
import sys
def amean(x,n):
s = 0
for i in range(0,n):
s = s + x[i]
return s/n
n = int(raw_input("\nHow many values? "))
print "\nEnter all values\n"
x = [0.0 for i in range(0,10)]
for i in range(0,n):
x[i] = float(raw_input(""))
sys.stdout.write("\nArithmetic mean = %6.2f"%(amean(x,n)))
def elem_sum(a,m,n):
s = 0
for i in xrange(m):
for j in xrange(n):
s = s + a[i][j]
return s
m = int(raw_input("\nHow many rows and columns : "))
n = int(raw_input(""))
a = [[0 for i in range(0,10)] for i in range(0,10)]
print "\nEnter the matrix values\n"
for i in range(0,m):
for j in range(0,n):
a[i][j] = int(raw_input(""))
print "\nSum of all elements in the matrix = ",elem_sum(a,m,n)
def reverse(st):
i = len(st) - 1
rst = ""
while i >= 0:
rst = rst + st[i]
i = i -1
return rst
st = raw_input("\nEnter a string : ")
print "\n",st," is reversed as ",reverse(st)
def bi_search(x,n,s):
flag = 0
start = 0
end = n
while start < end and flag == 0:
mid = (start+end)/2
if x[mid] > s:
end = mid
else:
if x[mid] < s:
start = mid + 1
else:
flag = 1
return flag
n = int(raw_input("\nHow many numbers ? "))
print "\nEnter all numbers in the list\n"
for i in range(0,n):
x[i] = int(raw_input(""))
s = int(raw_input("\nEnter the number to be searched : "))
if bi_search(x,n,s):
print "\nThe number ",s," is present in the list"
else:
print "\nThe number ",s," is not present in the list"
def fact(k):
if k == 1:
return 1
else:
return k*fact(k-1)
n = int(raw_input("\nEnter values to n and r : "))
r = int(raw_input(""))
ncr = fact(n)/(fact(r)*fact(n-r))
print "\nValue of nCr = ",ncr
import sys
def power(x,n):
if n == 1:
return x
else:
return x*power(x,n-1)
x = float(raw_input("\nEnter value to x : "))
n = int(raw_input("\nEnter its power : "))
sys.stdout.write("\n%6.2f raise to %d is %6.2f"%(x,n,power(x,n)))
import sys
def fibo(t1,t2,count):
if count >= n:
return
else:
t3 = t1 + t2
sys.stdout.write("%5d"%(t3))
count = count + 1
t1 = t2
t2 = t3
fibo(t1,t2,count)
n = int(raw_input("\nHow many terms to be printed ? "))
t1 = 0
t2 = 1
print "\nThe first ",n," terms in Fibonacci series are\n"
sys.stdout.write("%5d %5d"%(t1,t2))
count = 2
fibo(t1,t2,count)
import sys
def matadd():
for i in xrange(m):
for j in xrange(n):
c[i][j] = a[i][j] + b[i][j]
m = int(raw_input("\nHow many rows and columns ?"))
n = int(raw_input(""))
a = [[0 for i in range(0,10)]for i in range(0,10)]
b = [[0 for i in range(0,10)]for i in range(0,10)]
c = [[0 for i in range(0,10)]for i in range(0,10)]
#Read matrices
print "\nEnter A matrix values\n"
for i in range(0,m):
for j in range(0,n):
a[i][j] = int(raw_input(""))
print "\nEnter B matrix values\n"
for i in range(0,m):
for j in range(0,n):
b[i][j] = int(raw_input(""))
matadd()
print "\nResultant matrix is \n"
for i in range(0,m):
for j in range(0,n):
sys.stdout.write("%5d"%(c[i][j]))
sys.stdout.write("\n")
import sys
def matmul():
for i in xrange(m):
for j in xrange(l):
c[i][j] = 0
for k in xrange(n):
c[i][j] = c[i][j] + a[i][k] * b[k][j]
m = int(raw_input("\nEnter order of A matrix (m x n) : "))
n = int(raw_input(""))
a = [[0 for i in range(0,10)]for i in range(0,10)]
b = [[0 for i in range(0,10)]for i in range(0,10)]
c = [[0 for i in range(0,10)]for i in range(0,10)]
#Read matrices
print "\nEnter A matrix values\n"
for i in range(0,m):
for j in range(0,n):
a[i][j] = int(raw_input(""))
n = int(raw_input("\nEnter order of B matrix (n x l) : "))
l = int(raw_input(""))
print "\nEnter B matrix values\n"
for i in range(0,n):
for j in range(0,l):
b[i][j] = int(raw_input(""))
matmul()
print "\nResultant matrix is \n"
for i in range(0,m):
for j in range(0,l):
sys.stdout.write("%5d"%(c[i][j]))
sys.stdout.write("\n")
import sys
at = [[0,0],[0,0],[0,0]]
def transpose():
for i in xrange(m):
for j in xrange(n):
at[j][i] = a[i][j]
m = int(raw_input("\nHow many rows and columns ?"))
n = int(raw_input(""))
print "\nEnter the matrix values\n"
for i in range(0,m):
for j in range(0,n):
a[i][j] = int(raw_input(""))
transpose()
print "\nTranspose of the matrix is \n"
for i in xrange(n):
for j in xrange(m):
sys.stdout.write("%5d"%(at[i][j]))
sys.stdout.write("\n")
import sys
def sort():
for i in xrange(n):
for j in range(i+1,n):
if x[i] > x[j]:
temp = x[i]
x[i] = x[j]
x[j] = temp
x = [0 for i in range(0,50)]
n = int(raw_input("\nHow many numbers ? "))
print "\nEnter the list of values\n"
for i in range(0,n):
x[i] = int(raw_input(""))
sort()
print "\nThe sorted list is\n"
for i in xrange(n):
sys.stdout.write("%5d"%(x[i]))