UNIVERSITY COLLEGE OF ENGINEERING, ARIYALUR
(A Constituent College of Anna University, Chennai)
Ariyalur-621704
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LABORATORY RECORD
NAME
SUBJECT CODE & NAME :.........................................................................
: 
REGISTER NUMBER : ....
BRANCH : ....
UNIVERSITY COLLEGE OF ENGINEERING, ARIYALUR
(A Constituent College of Anna University, Chennai)
Ariyalur-621704
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
BONAFIDE CERTIFICATE
Certified that this is bonafide record work done by
Mr/Ms …………………………..……With Register No ……………………..
for GE8161 - Problem Solving & Python Programming Laboratory in the
First year - Ist
semester of B.E Degree Course in Computer Science and
Engineering branch during the Academic Year 2019-2020.
Staff-In charge Head of the Department
Submitted for University Practical Examination held on ……………………
INTERNAL EXAMINER EXTERNAL EXAMINER
INDEX
S.No Name of the Experiments Page No Staff Sign
1. Compute the GCD of two numbers
2. Find the square root of a number (Newton‘s
method)
3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word
count)
11. Find the most frequent words in a text read from a
file
12. Simulate elliptical orbits in Pygame
13. Simulate bouncing ball using Pygame
Compute the GCD of two numbers
Program
d1 = int(input("Enter a number:"))
d2 = int(input("Enter another number"))
rem = d1 % d2
while rem != 0 :
d1 = d2
d2 = rem
rem=d1 % d2
print ("gcd of given numbers is :", d2)
Output :
Find the square root of a number (Newton‘s method)
Program
def newtonSqrt(n, howmany):
approx = 0.5 * n
for i in range(howmany):
betterapprox = 0.5 * (approx + n/ approx)
approx = betterapprox
return betterapprox
print(newtonSqrt(10, 3))
print(newtonSqrt(10, 5))
print(newtonSqrt(10, 10))
Find the square root of a number
n = int(input("Enter a number"))
howmany = int(input("Enter another number"))
approx = 0.5 * n
for i in range(howmany):
betterapprox = 0.5 *(approx + n/approx)
approx = betterapprox
print("The square root of a number is:", betterapprox)
Output:
Exponentiation (power of a number)
Program
n = input ("Enter a number : ")
n = int(n)
e = input ("Enter an exponent : ")
e = int(e)
r = n
for i in range (1,e):
r = n * r
print(r)
Output:
Find the maximum of a list of numbers
Program
a = []
n = int(input("Enter number of elements:"))
for i in range(1, n+1):
b = int(input("Enter element:"))
a.append(b)
a.sort()
print("Largest element is:",a[n-1])
Output:
Linear search
Program
list = [4,1,2,5,3] #Set up array
search = int(input("Enter search number")) # Ask for a number
for i in range(0,len(list)): # Repeat for each item in list
if search==list[i]: #if item at position i is search time
print(str(search)+"found at position " + str(i)) #Report found
Output:
Binary search
Program
def binary_search(item_list,item):
first = 0
last = len(item_list)-1
found = False
while( first<=last and not found):
mid = (first + last)//2
if item_list[mid] == item :
found = True
else:
if item < item_list[mid]:
last = mid - 1
else:
first = mid + 1
return found
print(binary_search([1,2,3,5,8], 6))
print(binary_search([1,2,3,5,8], 5))
Output:
Selection sort
Program
def selectionSort(alist):
for fillslot in range(len(alist) - 1, 0, -1):
positionOfMax = 0
for location in range(1, fillslot + 1):
if alist[location] > alist[positionOfMax]:
positionOfMax = location
temp = alist[fillslot]
alist[fillslot] = alist[positionOfMax]
alist[positionOfMax] = temp
alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
selectionSort(alist)
print(alist)
Output:
Insertion sort
Program
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position > 0 and alist[position - 1] > currentvalue:
alist[position] = alist[position - 1]
position = position - 1
alist[position] = currentvalue
alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
insertionSort(alist)
print(alist)
Output:
Merge sort
Program
def mergeSort(alist):
print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=0
j=0
k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)
Output:
First n prime numbers
Program
a = int(input("Enter number: "))
k = 0
for i in range(2,a//2):
if(a%i==0):
k = k + 1
if(k <= 0):
print("Number is prime")
else:
print("Number isn't prime")
Output:
Multiply matrices
Program
matrix1 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
matrix2 = [[10, 11, 12, 13],
[14, 15, 16, 17],
[18, 19, 20, 21]]
rmatrix = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
rmatrix[i][j] += matrix1[i][k] * matrix2[k][j]
for r in rmatrix:
print(r)
Output:
Programs that take command line arguments (word count)
Program
fname = raw_input("Enter file name: ") #test.txt
num_words = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
num_words += len(words)
print("Number of words:")
print(num_words)
Output:
Find the most frequent words in a text read from a file
Program
fname = input("Enter file name: ") #test.txt
l=input("Enter letter to be searched:")
k = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
for i in words:
for letter in i:
if(letter==l):
k=k+1
print("Occurrences of the letter:")
print(k)
Output:
Simulate elliptical orbits in Pygame
Program
import pygame
import math
import sys
pygame.init()
screen = pygame.display.set_mode((600, 300))
pygame.display.set_caption("Elliptical orbit")
clock = pygame.time.Clock()
while(True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
xRadius = 250
yRadius = 100
for degree in range(0,360,10):
x1 = int(math.cos(degree * 2 * math.pi / 360) * xRadius) + 300
y1 = int(math.sin(degree * 2 * math.pi / 360) * yRadius) + 150
screen.fill((0, 0, 0))
pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1)
pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
pygame.display.flip()
clock.tick(5)
Output:
Simulate bouncing ball using Pygame
Program
import sys, pygame
pygame.init()
size = width, height = 700, 300
speed = [1, 1]
background = 255, 255, 255
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Bouncing ball")
ball = pygame.image.load("ball2.jpg")
ballrect = ball.get_rect()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(background)
screen.blit(ball, ballrect)
pygame.display.flip()
Output:

Basic python laboratoty_ PSPP Manual .docx

  • 1.
    UNIVERSITY COLLEGE OFENGINEERING, ARIYALUR (A Constituent College of Anna University, Chennai) Ariyalur-621704 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING LABORATORY RECORD NAME SUBJECT CODE & NAME :......................................................................... :  REGISTER NUMBER : .... BRANCH : ....
  • 2.
    UNIVERSITY COLLEGE OFENGINEERING, ARIYALUR (A Constituent College of Anna University, Chennai) Ariyalur-621704 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BONAFIDE CERTIFICATE Certified that this is bonafide record work done by Mr/Ms …………………………..……With Register No …………………….. for GE8161 - Problem Solving & Python Programming Laboratory in the First year - Ist semester of B.E Degree Course in Computer Science and Engineering branch during the Academic Year 2019-2020. Staff-In charge Head of the Department Submitted for University Practical Examination held on …………………… INTERNAL EXAMINER EXTERNAL EXAMINER
  • 3.
    INDEX S.No Name ofthe Experiments Page No Staff Sign 1. Compute the GCD of two numbers 2. Find the square root of a number (Newton‘s method) 3. Exponentiation (power of a number) 4. Find the maximum of a list of numbers 5. Linear search and Binary search 6. Selection sort, Insertion sort 7. Merge sort 8. First n prime numbers 9. Multiply matrices 10. Programs that take command line arguments (word count) 11. Find the most frequent words in a text read from a file 12. Simulate elliptical orbits in Pygame 13. Simulate bouncing ball using Pygame Compute the GCD of two numbers
  • 4.
    Program d1 = int(input("Entera number:")) d2 = int(input("Enter another number")) rem = d1 % d2 while rem != 0 : d1 = d2 d2 = rem rem=d1 % d2 print ("gcd of given numbers is :", d2) Output :
  • 5.
    Find the squareroot of a number (Newton‘s method)
  • 6.
    Program def newtonSqrt(n, howmany): approx= 0.5 * n for i in range(howmany): betterapprox = 0.5 * (approx + n/ approx) approx = betterapprox return betterapprox print(newtonSqrt(10, 3)) print(newtonSqrt(10, 5)) print(newtonSqrt(10, 10)) Find the square root of a number n = int(input("Enter a number")) howmany = int(input("Enter another number")) approx = 0.5 * n for i in range(howmany): betterapprox = 0.5 *(approx + n/approx) approx = betterapprox print("The square root of a number is:", betterapprox) Output:
  • 8.
    Exponentiation (power ofa number) Program n = input ("Enter a number : ") n = int(n) e = input ("Enter an exponent : ") e = int(e) r = n for i in range (1,e): r = n * r print(r) Output:
  • 10.
    Find the maximumof a list of numbers Program a = [] n = int(input("Enter number of elements:")) for i in range(1, n+1): b = int(input("Enter element:")) a.append(b) a.sort() print("Largest element is:",a[n-1])
  • 11.
  • 12.
    Program list = [4,1,2,5,3]#Set up array search = int(input("Enter search number")) # Ask for a number for i in range(0,len(list)): # Repeat for each item in list if search==list[i]: #if item at position i is search time print(str(search)+"found at position " + str(i)) #Report found
  • 13.
  • 14.
    Program def binary_search(item_list,item): first =0 last = len(item_list)-1 found = False while( first<=last and not found): mid = (first + last)//2 if item_list[mid] == item : found = True else: if item < item_list[mid]: last = mid - 1 else: first = mid + 1 return found print(binary_search([1,2,3,5,8], 6)) print(binary_search([1,2,3,5,8], 5))
  • 15.
  • 16.
    Program def selectionSort(alist): for fillslotin range(len(alist) - 1, 0, -1): positionOfMax = 0 for location in range(1, fillslot + 1): if alist[location] > alist[positionOfMax]: positionOfMax = location temp = alist[fillslot] alist[fillslot] = alist[positionOfMax] alist[positionOfMax] = temp alist = [54, 26, 93, 17, 77, 31, 44, 55, 20] selectionSort(alist) print(alist) Output:
  • 17.
  • 18.
    Program def insertionSort(alist): for indexin range(1,len(alist)): currentvalue = alist[index] position = index while position > 0 and alist[position - 1] > currentvalue: alist[position] = alist[position - 1] position = position - 1 alist[position] = currentvalue alist = [54, 26, 93, 17, 77, 31, 44, 55, 20] insertionSort(alist) print(alist) Output:
  • 19.
  • 20.
    Program def mergeSort(alist): print("Splitting ",alist) iflen(alist)>1: mid = len(alist)//2 lefthalf = alist[:mid] righthalf = alist[mid:] mergeSort(lefthalf) mergeSort(righthalf) i=0 j=0 k=0 while i < len(lefthalf) and j < len(righthalf): if lefthalf[i] < righthalf[j]: alist[k]=lefthalf[i] i=i+1 else: alist[k]=righthalf[j] j=j+1 k=k+1 while i < len(lefthalf): alist[k]=lefthalf[i] i=i+1 k=k+1 while j < len(righthalf): alist[k]=righthalf[j] j=j+1
  • 21.
    k=k+1 print("Merging ",alist) alist =[54,26,93,17,77,31,44,55,20] mergeSort(alist) print(alist) Output:
  • 22.
  • 23.
    Program a = int(input("Enternumber: ")) k = 0 for i in range(2,a//2): if(a%i==0): k = k + 1 if(k <= 0): print("Number is prime") else: print("Number isn't prime") Output:
  • 24.
  • 25.
    Program matrix1 = [[1,2, 3], [4, 5, 6], [7, 8, 9]] matrix2 = [[10, 11, 12, 13], [14, 15, 16, 17], [18, 19, 20, 21]] rmatrix = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] for i in range(len(matrix1)): for j in range(len(matrix2[0])): for k in range(len(matrix2)): rmatrix[i][j] += matrix1[i][k] * matrix2[k][j] for r in rmatrix: print(r) Output:
  • 26.
    Programs that takecommand line arguments (word count)
  • 27.
    Program fname = raw_input("Enterfile name: ") #test.txt num_words = 0 with open(fname, 'r') as f: for line in f: words = line.split() num_words += len(words) print("Number of words:") print(num_words)
  • 28.
  • 29.
    Find the mostfrequent words in a text read from a file Program fname = input("Enter file name: ") #test.txt l=input("Enter letter to be searched:") k = 0 with open(fname, 'r') as f: for line in f: words = line.split() for i in words: for letter in i: if(letter==l): k=k+1 print("Occurrences of the letter:") print(k)
  • 30.
  • 31.
    Simulate elliptical orbitsin Pygame Program import pygame import math import sys pygame.init() screen = pygame.display.set_mode((600, 300)) pygame.display.set_caption("Elliptical orbit") clock = pygame.time.Clock() while(True): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() xRadius = 250 yRadius = 100 for degree in range(0,360,10): x1 = int(math.cos(degree * 2 * math.pi / 360) * xRadius) + 300 y1 = int(math.sin(degree * 2 * math.pi / 360) * yRadius) + 150 screen.fill((0, 0, 0)) pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35) pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1) pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15) pygame.display.flip() clock.tick(5)
  • 32.
  • 33.
    Simulate bouncing ballusing Pygame Program import sys, pygame pygame.init() size = width, height = 700, 300 speed = [1, 1] background = 255, 255, 255 screen = pygame.display.set_mode(size) pygame.display.set_caption("Bouncing ball") ball = pygame.image.load("ball2.jpg") ballrect = ball.get_rect() while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ballrect = ballrect.move(speed) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] screen.fill(background) screen.blit(ball, ballrect) pygame.display.flip()
  • 34.