from array import *
# Variable declaration
word = array('c', ['H', 'e', 'l', 'l', 'o'])
# Result
print ('The contents of word [] is %s ' % ''.join(word))
from array import *
# Variable declaration
marks = [0]*10
avg = 0
# Calculation and result
for i in range (0, 10) :
marks[i] = int(raw_input('Enter value in cell [%d] ' % i))
avg = avg + marks[i]
avg = avg/10
print ('The average marks are %f' % avg)
for i in range (0, 10) :
if marks[i] < avg :
print ('Marks in cell [%d] are %d less than average' % (i, marks[i]))
else :
print ('Marks in cell [%d] are %d above/equal to average' % (i, marks[i]))
from array import *
# Variable declaration
data = raw_input('Enter the string ')
# Calculation and result
rev_data = reversed (data)
if list (data) == list (rev_data) :
print ('The string %s is a PALINDROME' % data)
else :
print ('The string %s is not a PALINDROME' % data)
from array import *
# Variable declaration
marks = [0]*10
avg = 0
# Function declaration, calculation and result
def readdata (marks) :
for i in range (0, 10) :
marks[i] = int(raw_input('Enter value in cell [%d] ' % i))
def average (marks) :
mean = 0
for i in range (0, 10) :
mean = mean + marks[i]
return mean/10
def printdata (marks, avg) :
for i in range (0, 10) :
if marks[i] < avg :
print ('Marks in cell [%d] are %d less than average' % (i, marks[i]))
else :
print ('Marks in cell [%d] are %d above/equal to average' % (i, marks[i]))
readdata (marks)
avg = average (marks)
print ('The average marks are %f' % avg)
printdata (marks, avg)
from array import *
# Variable declaration
a = [10, 20, 30, 40, 50]
# Function declaration, calculation and result
def fun (x, y, z) :
x = x + 5
y = y + 10
z[4] = z[4] + 100
for i in range (0, 5) :
print ('The cell [%d] contains %d' % (i, a[i]))
print ('The address of cell [%d] is %x' % (i, id(a[i])))
fun (a[0], a[3], a)
print
for i in range (0, 5) :
print ('The cell [%d] contains %d' % (i, a[i]))
print ('The address of cell [%d] is %x' % (i, id(a[i])))
from array import *
# Variable declaration
data = raw_input('Enter the string ')
# Function declaration, calculation and result
def revstring (data) :
rev_data = reversed (data)
if list (data) == list (rev_data) :
print ('The string %s is a PALINDROME' % data)
else :
print ('The string %s is not a PALINDROME' % data)
revstring (data)
from array import *
# Variable declaration
a = [0]*10
# Function declaration, calculation and result
def readnsort (a) :
pos = 0
for len in range (0, 10) :
num = int(raw_input('Enter data element no. %d ' % len))
for pos in range (0, len+1) :
if num < a[pos] :
break
i = len
while i>=pos :
a[i] = a[i-1]
i = i-1
a[pos] = num
def printdata (a) :
for i in range (0, 10) :
print ('The data in cell [%d] is %d ' % (i, a[i]))
readnsort (a)
print
printdata (a)
from array import *
# Function declaration, calculation and result
def readdata () :
a = []
for i in range (0, 10) :
a.append(int(raw_input('Enter data in cell [%d] ' % i)))
return a
def sortdata (a) :
for p in range (0, 9) :
min = p
for i in range (p, 10) :
if a[min] > a[i] :
min = i
temp = a[p]
a[p] = a[min]
a[min] = temp
return a
def printdata (a) :
for i in range (0, 10) :
print ('The data in cell [%d] is %d ' % (i, a[i]))
a = readdata ()
a = sortdata (a)
print ('\nThe sorted data is ')
printdata (a)
from array import *
# Function declaration, calculation and result
def readdata () :
a = []
for i in range (0, 10) :
a.append(int(raw_input('Enter data in cell [%d] ' % i)))
return a
def bubble (a) :
for p in range (0, 10) :
for i in range (0, 9-p) :
if a[i] > a[i+1] :
temp = a[i]
a[i] = a[i+1]
a[i+1] = temp
return a
def printdata (a) :
for i in range (0, 10) :
print ('The data in cell [%d] is %d ' % (i, a[i]))
a = readdata ()
a = bubble (a)
print ('\nThe sorted data is ')
printdata (a)
from array import *
# Function declaration, calculation and result
def readdata (size) :
a = []
for i in range (0, size) :
a.append(int(raw_input('Enter data in cell [%d] ' % i)))
return a
def bubble (a, size) :
for p in range (0, size) :
for i in range (0, size-p-1) :
if a[i] > a[i+1] :
temp = a[i]
a[i] = a[i+1]
a[i+1] = temp
return a
def printdata (a, size) :
for i in range (0, size) :
print ('The data in cell [%d] is %d ' % (i, a[i]))
def lsearch (a, key, size) :
for i in range (0, size) :
if a[i] == key :
return i
if i == size :
return -1
size = int(raw_input('Enter the total number of elements '))
a = readdata (size)
a = bubble (a, size)
print ('\nThe sorted array is ')
printdata (a, size)
find = int(raw_input('\nEnter the element to be searched '))
pos = lsearch (a, find, size)
if pos<0 :
print ('\nThe element %d NOT FOUND ' % find)
else :
print ('\nThe element %d found at position %d ' % (find, pos))
from array import *
# Function declaration, calculation and result
def readdata (size) :
a = []
for i in range (0, size) :
a.append(int(raw_input('Enter data in cell [%d] ' % i)))
return a
def bubble (a, size) :
for p in range (0, size) :
for i in range (0, size-p-1) :
if a[i] > a[i+1] :
temp = a[i]
a[i] = a[i+1]
a[i+1] = temp
return a
def printdata (a, size) :
for i in range (0, size) :
print ('The data in cell [%d] is %d ' % (i, a[i]))
def bsearch (a, key, size) :
first = 0
last = size - 1
mid = (first + last)/2
while (first <= last and a[mid] != key) :
if a[mid] < key :
first = mid + 1
else :
if a[mid] > key :
last = mid - 1
mid = (first + last)/2
if a[mid] == key :
return mid
else :
return -1
size = int(raw_input('Enter the number of elements in the array '))
a = readdata (size)
a = bubble (a, size)
print ('\nThe sorted array is ')
printdata (a, size)
find = int(raw_input('\nPlease enter the element to be searched for '))
pos = bsearch (a, find, size)
if pos<0 :
print ('\nThe element %d NOT FOUND ' % find)
else :
print ('\nThe element %d found at position %d ' % (find, pos))
from array import *
# Function declaration, calculation and result
def readncompute (no_of_students) :
a = [[0 for y in xrange(5)] for x in xrange(no_of_students)]
per = [0 for x in range(no_of_students)]
avg = 0
for i in range (0, no_of_students) :
print ('\nEnter marks of 5 subjects for Roll no. %d ' % (i+1))
for j in range (0, 5) :
a[i][j] = int(raw_input('Subject %d = ' % (j+1)))
per[i] = per[i] + a[i][j]
per[i] = per[i]/5
avg = avg + per[i]
return a, avg/no_of_students, per
def printdata (a, no_of_students, avg, per) :
for i in range (0, no_of_students) :
print ('\nMarks for Roll no. %d are ' % (i+1))
for j in range (0, 5) :
print a[i][j]
print ('Percentage for Roll no. %d is %f ' % ((i+1), per[i]))
if per[i] < avg :
print ('Below average')
else :
print ('Above average')
no_of_students = int(raw_input('How many students marks do you want to enter '))
a, average, per = readncompute (no_of_students)
print ('\nThe average marks are %f ' % average)
printdata (a, no_of_students, average, per)
# Function declaration, calculation and result
def getchoice () :
print
print ('1 - Input two matrices ')
print ('2 - Add and display sum ')
print ('3 - Exit ')
choice = int(raw_input('Enter choice : '))
return choice
def read (matrix, rows, cols) :
for i in range (0, rows) :
print ('\nEnter elements for row %d ' % (i+1))
for j in range (0, cols) :
matrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))
def add (sum, augend, addend, rows, cols) :
for i in range (0, rows) :
for j in range (0, cols) :
sum[i][j] = augend[i][j] + addend[i][j]
def display2matrices (augend, addend, rows, cols) :
print ('\nMatrix A ')
for i in range (0, rows) :
for j in range (0, cols) :
print ('%3d ' % augend[i][j]),
print
print ('\nMatrix B ')
for i in range (0, rows) :
for j in range (0, cols) :
print ('%3d ' % addend[i][j]),
print
def display3matrices (sum, augend, addend, rows, cols) :
print
for i in range (0, rows) :
for j in range (0, cols) :
print ('%3d ' % augend[i][j]),
print ('%s ' % ('+' if i==rows/2 else ' ')),
for j in range (0, cols) :
print ('%2d ' % addend[i][j]),
print ('%s ' % ('=' if i==rows/2 else ' ')),
for j in range (0, cols) :
print ('%2d ' % sum[i][j]),
print
def display (matrix, rows, cols) :
for i in range (0, rows) :
for j in range (0, cols) :
print ('%3d ' % matrix[i][j]),
print
augend = [[0 for y in xrange(10)] for x in xrange(10)]
addend = [[0 for y in xrange(10)] for x in xrange(10)]
sum = [[0 for y in xrange(10)] for x in xrange(10)]
condition = True
while condition :
choice = getchoice ()
if choice == 1 :
rows = int(raw_input('\nEnter the number of rows '))
cols = int(raw_input('Enter the number of columns '))
print ('\n\nEnter the first matrix ')
read (augend, rows, cols)
print ('\n\nEnter the second matrix ')
read (addend, rows, cols)
print ('\n\nYou entered ')
display2matrices (augend, addend, rows, cols)
elif choice == 2 :
add (sum, augend, addend, rows, cols)
display3matrices (sum, augend, addend, rows, cols)
elif choice == 3 :
condition = False
exit ()
# Function declaration, calculation and result
def readmatrix (matrix, rows, cols) :
for i in range (0, rows) :
print ('\nPlease enter elements for row %d ' % (i+1))
for j in range (0, cols) :
matrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))
def multiply (prod, a, b, Ar, Ac, Bc) :
for i in range (0, Ar) :
for j in range (0, Bc) :
prod[i][j] = 0
for k in range (0, Ac) :
prod[i][j] += a[i][k] * b[k][j]
def display (matrix, rows, cols) :
for i in range (0, rows) :
for j in range (0, cols) :
print ('%3d ' % matrix[i][j]),
print
a = [[0 for y in xrange(10)] for x in xrange(10)]
b = [[0 for y in xrange(10)] for x in xrange(10)]
product = [[0 for y in xrange(10)] for x in xrange(10)]
condition = True
while condition :
Arows, Acols = map(int, raw_input('Enter order of matrix A: ').split())
Brows, Bcols = map(int, raw_input('Enter order of matrix B: ').split())
if Acols == Brows :
condition = False
break
else :
print ('\nMatrices are not compatible for multiplication. Enter the order again. ')
continue
print ('\nEnter matrix A: ')
readmatrix (a, Arows, Acols)
print ('\nEnter matrix B: ')
readmatrix (b, Brows, Bcols)
multiply (product, a, b, Arows, Acols, Bcols)
print ('\nThe resultant product (A.B) is: ')
display (product, Arows, Bcols)
# Function declaration, calculation and result
def getchoice () :
print
print ('1 - Input square matrix ')
print ('2 - Display upper triangular matrix ')
print ('3 - Display lower triangular matrix ')
print ('4 - Exit ')
condition = True
while condition :
choice = int(raw_input('Enter choice : '))
if choice in range (1, 5) :
condition = False
return choice
def displayupper (matrix, order) :
print
for i in range (0, order) :
for j in range (0, order) :
if j >= i :
print ('%3d ' % matrix[i][j]),
else :
print ('%3d ' % 0),
print
def displaylower (matrix, order) :
print
for i in range (0, order) :
for j in range (0, order) :
if j <= i :
print ('%3d ' % matrix[i][j]),
else :
print ('%3d ' % 0),
print
def read (matrix, rows, cols) :
for i in range (0, rows) :
print ('\nEnter %d elements for row %d ' % (rows, (i+1)))
for j in range (0, cols) :
matrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))
matrix = [[0 for y in xrange(10)] for x in xrange(10)]
condition = True
while condition :
choice = getchoice ()
if choice == 1 :
order = int(raw_input('\nEnter order of square matrix '))
read (matrix, order, order)
elif choice == 2 :
displayupper (matrix, order)
elif choice == 3 :
displaylower (matrix, order)
elif choice == 4 :
condition = False
exit ()
# Function declaration, calculation and result
def readmatrix (matrix, n) :
for i in range (0, n) :
print ('\nEnter elements for row no %d ' % (i+1))
for j in range (0, n) :
matrix[i][j] = int(raw_input('matrix[%d][%d] = ' % ((i+1),(j+1))))
def display (matrix, n) :
print
for i in range (0, n) :
for j in range (0, n) :
print ('%5d ' % matrix[i][j]),
print
def isunitmatrix(matrix, n) :
for i in range (0, n) :
if matrix[i][i] != 1 :
return 0
for i in range (0, n) :
for j in range (0, n) :
if i != j :
if matrix[i][j] != 0 :
return 0
return 1
matrix = [[0 for y in xrange(10)] for x in xrange(10)]
n = int(raw_input('Enter order of matrix '))
readmatrix (matrix, n)
display (matrix, n)
if (isunitmatrix (matrix, n)) :
print "UNIT MATRIX"
else :
print "NOT A UNIT MATRIX"
# Variable declaration
string = raw_input('Enter a string: ')
# Calculation and result
swap_string = string.swapcase()
print swap_string
# Variable declaration
string = raw_input('Enter a string ')
# Function declaration, calculation and result
def printstr (string) :
for i in range (0, len(string)) :
for j in range (0, i+1) :
print string[j],
print
i = i - 2
while i >= 0 :
for j in range (0, i+1) :
print string[j],
print
i = i - 1
printstr (string)
# Variable declaration
a = raw_input('Enter any string ')
# Function declaration, calculation and result
def copy (a) :
b = a
return b
b = copy (a)
print
print ('The first string is %s ' % a)
print ('The second string is %s ' % b)
# Variable declaration
a = raw_input('Enter the major string: ')
b = raw_input('Enter the minor string: ')
# Function declaration, calculation and result
def ispart (a, b) :
if b in a :
print ('The string "%s" is A SUBSTRING of "%s" ' % (b, a))
else :
print ('The string "%s" is NOT A SUBSTRING of "%s" ' % (b, a))
ispart (a, b)