1. 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 : ....
2. 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
3. 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
4. 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 :
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 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:
10. 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])
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
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))
18. 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:
23. 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:
27. 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)
29. 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)