0% found this document useful (0 votes)
65 views39 pages

Panimalar Institute of Technology: Name of The Student:G Venkat

The document contains details of a student's academic record including their name, register number, academic year, degree, branch and subjects from Panimalar Institute of Technology. It includes a list of experiments conducted in the Problem Solving and Python Programming Laboratory course along with the marks obtained and signatures of staff. The record has been certified by the staff-in-charge and is being submitted for the external examination.

Uploaded by

G venkat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views39 pages

Panimalar Institute of Technology: Name of The Student:G Venkat

The document contains details of a student's academic record including their name, register number, academic year, degree, branch and subjects from Panimalar Institute of Technology. It includes a list of experiments conducted in the Problem Solving and Python Programming Laboratory course along with the marks obtained and signatures of staff. The record has been certified by the staff-in-charge and is being submitted for the external examination.

Uploaded by

G venkat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

PANIMALAR INSTITUTE OF TECHNOLOGY

(A CHRISTIAN MINORITY INSTITUTION)

AN ISO 9001:2008 CERTIFIED INSTITUTION


JAISAKTHI EDUCATIONAL TRUST
BANGALAORE TRUNK ROAD, VARADHARAJAPURAM,
NASARATHPET, POONAMALLEE,
CHENNAI – 600123.

NAME OF THE STUDENT :G Venkat

REGISTER NUMBER :2020PITCSE278

ACEDEMIC YEAR : 2020 - 2021

DEGREE : BE

BRANCH : CSE

YEAR / SEMESTER : I /I

SUBJECT CODE : GE8161

SUBJECT NAME : PROBLEM SOLVING AND PYTHON


PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

REGISTER NO. 2020PITCSE278

Certified that this is the bonafide record of work done by


G VENKAT in the I Year / I Semester B.E Computer Science and
Engineering Degree Course, GE8161 – PROBLEM SOLVING AND
PYTHON PROGRAMMING LABORATORY during the academic year
2020 - 2021.

Sign. of Staff-In-Charge

Date:

Submitted for the Anna University Practical Examination held on


at Panimalar Institute of Technology, Chennai – 600 123.

INTERNAL EXAMINER EXTERNAL EXAMINER

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

INDEX
PAGE STAFF
S.NO DATE NAME OF THE EXPERIMENT MARKS
NO SIGNATURE
BASIC PYTHON PROGRAMS
PYTHON PROGRAMS-MATHEMATICAL PROBLEM SOLVING
1. 5-12-2020 Arithmetical operations
2. 5-12-2020 Area and circumference of circle
3. 5-12-2020 Square root of a number
4. 5-12-2020 Swapping of two numbers
PYTHON PROGRAMS-SELECTION/DECISION STATEMENTS
5. 12-12-2020 To check leap year
6. 12-12-2020 To check a number for odd or even
12-12-2020
7. To check a number for positive or negative
8. 12-12-2020 To find largest among three numbers
PYTHON PROGRAMS-ITERATION/LOOPING STATEMENTS
9. 19-12-2020 To find factorial of a number
10. 19-12-2020 To find sum of digits of a number
19-12-2020 To find reverse of a number and check
11.
palindrome
19-12-2020 To print numbers divisible by 2 and not by 3
12.
and 5
PYTHON PROGRAMS-FUNCTIONS AND RECURSION
2-1-2021 Square and Cube of a number using
13.
functions
2-1-2021
14. To Find Armstrong number using Functions

2-1-2021
15. Fibonacci Series using Recursion

LABORATORY EXPERIMENTS
1. 9-1-2021 Program to compute the gcd/hcf of two
numbers
2. 9-1-2021 Program to find the square root of a number
using newton’s method
3. 9-1-2021 Program to find the exponentiation/power of
a number
4. 23-1-2021 Program to find the maximum of a list of
numbers
5. 23-1-2021 Program to search using linear search
6. 23-1-2021 Program to search using binary search
7. 30-1-2021 Program to sort using selection sort
8. 30-1-2021 Program to sort using insertion sort
9. 30-1-2021 Program to sort using merge sort
1 6-2-2021 Program to generate first n prime numbers
0.
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

PAGE STAFF
S.NO DATE NAME OF THE EXPERIMENT MARKS
NO SIGNATURE
1 6-2-2021 Program to perform matrix multiplication
1.
1 6-2-2021 Program for word count using command line
2. arguments
1 13-2-2021 Program to find the most frequent words in
3. a text read from a file
.
1 13-2-2021 Program to simulate elliptical orbits using
4. pygame
1 13-2-2021 Program to simulate bouncing ball using
5. pygame

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

BASIC PYTHON PROGRAMS

PYTHON PROGRAMS-MATHEMATICAL PROBLEM SOLVING


1.PYTHON PROGRAM TO DO ARITHMETICAL OPERATIONS
# Store input numbers:
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
# Add two numbers
sum = float(num1) + float(num2)
# Subtract two numbers
min = float(num1) - float(num2)
# Multiply two numbers
mul = float(num1) * float(num2)
#Divide two numbers
div = float(num1) / float(num2)
# Display the sum
print("The sum of two numbers is:",sum)
# Display the subtraction
print("The sub of two numbers is:",min)
# Display the multiplication
print("The sum of two numbers is:",mul)
# Display the division
print("The sum of two numbers is:",div)
OUTPUT:
Enter first number: 10
Enter second number: 5
The sum of 10 and 5 is 15.0
The sum of 10 and 5 is 5.0
The sum of 10 and 5 is 50.0
The sum of 10 and 5 is 2.0

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

2.AREA AND CIRCUMFERENCE OF CIRCLE


r=int(input("Enter the radius of the circle:"))
area=3.14*r*r
circum=2*3.14*r
print(area)
print(circum)

OUTPUT:
Enter the radius of the circle:5
78.5
31.400000000000002

3.SQUAREROOT OF A NUMBER
num=float(input("Enter a number:"))
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

num_sqrt =num** 0.5


print('The square root of %0.3f is %0.3f' %(num ,num_sqrt))

OUTPUT:
Enter a number:25
The square root of 25.000 is 5.000

4.SWAPPING OF TWO NUMBERS


x = input('Enter value of x: ')

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

y = input('Enter value of y: ')


# create a temporary variable and swap the values
temp = x
x=y
y = temp
print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))

OUTPUT:
Enter value of x: 10
Enter value of y: 20
The value of x after swapping: 20
The value of y after swapping: 10

PYTHON PROGRAMS-SELECTION/DECISION STATEMENTS

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

5.TO CHECK LEAP YEAR OR NOT


year = int(input("Enter any Year: "))
if year%4==0:
print("Year is Leap")
else:
print ("Year is not Leap")

OUTPUT:
Enter any Year: 2016
Year is Leap

6.TO CHECK ODD OR EVEN


num = int(input("Enter any number: "))

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

rem = num % 2
if rem > 0:
print("Odd number.")
else:
print("Even number.")

OUTPUT:
Enter any number: 6
Even number.

7.POSITIVE OR NEGATIVE
n=int(input("Enter number: "))

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

if(n>0):
print("Number is positive")
else:
print("Number is negative")

OUTPUT:
Enter number: 5
Number is positive

8.LARGEST OF THREE NUMBERS

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number between",num1,",",num2,"and",num3,"is",largest)

OUTPUT:
Enter first number: 12
Enter second number: 15
Enter third number: 10
The largest number between 12.0 , 15.0 and 10.0 is 15.0

PYTHON PROGRAMS-ITERATION/LOOPING STATEMENTS


9.TO FIND FACTORIAL OF A NUMBER

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

num = int(input("Enter a number: "))


factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

OUTPUT:
Enter a number: 5
The factorial of 5 is 120

10.PROGRAM TO FIND THE SUM OF DIGITS OF NUMBER


sum=0

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

num=int(input("Enter a number:"))
while num > 0:
r=num%10
sum=sum+r
num//=10
print("Sum of digits of number:",sum)

OUTPUT:
Enter a number:123
Sum of digits of number: 6

11.TO FIND REVERSE AND TO CHECK A NUMBER IS PALINDROME OR NOT


reverse = 0

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

n=int(input("Enter a number to check :"))


temp = n
while(n >0):
remainder=n%10
reverse=reverse*10+remainder
n = n//10;
print("The reverse of the number is:",reverse)
if(temp==reverse):
print("The given number is a palindrome number")
else:
print("The given number is not a palindrome number")

OUTPUT:
Enter a number to check :121
The reverse of the number is: 121
The given number is a palindrome number
Enter a number to check :123
The reverse of the number is: 321
The given number is not a palindrome number

12.TO PRINT NUMBERS DIVISIBLE BY 2 AND NOT BY 3 AND 5


for i in range(1,101):

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

if((i%2==0)and(i%3!=0)and(i%5!=0)):
print(i)

OUTPUT:
2
4
8
14
16
22
26
28
32
34
38
44
46
52
56
58
62
64
68
74
76
82
86
88
92
94
98

PYTHON PROGRAMS-FUNCTIONS AND RECURSION


13.SQUARE AND CUBE OF A NUMBER USING FUNCTIONS
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

def square(x):
sq=x*x
return sq
def cube(x):
cu=x*x*x
return cu
x=5
print("Square value for the given number is :",square(x))
print("Cube value for the given number is :",cube(x))

OUTPUT:

Square value for the given number is : 25


Cube value for the given number is : 125

14.ARMSTRONG NUMBER USING FUNCTIONS


print('To Find Python Armstrong Number')

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

def Armstrong_Number():
num = int(input('Enter Number to check for Armstrong'))
f = num
sum = 0
while(f!=0):
d = f % 10
f = f / 10
sum = sum+(d*d*d)
if( sum == num):
print('%d is a armstrong number' %num)
else:
print('%d is not a armstrong number' %num)
Armstrong_Number()

OUTPUT :

To Find Python Armstrong Number


Enter Number to check for Armstrong123
123 is not a armstrong number

15.FIBONACCI SERIES USING RECURSION


def recur_fibo(n):

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

"""Recursive function to
print Fibonacci sequence"""
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
OUTPUT:
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

LABORATORY EXPERIMENTS

1. PROGRAM TO COMPUTE THE GCD/HCF OF TWO NUMBERS

PROGRAM:
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

#Program to find the gcd/hcf using recursion


def gcd(a,b):
if(a==0):
return b
if(b==0):
return a
return gcd(b,a%b)
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
GCD=gcd(a,b)
print("GCD is: ")
print(GCD)

OUTPUT:
Enter first number:8
Enter second number:12
GCD is:
4

2. PROGRAM TO FIND THE SQUARE ROOT OF A NUMBER USING NEWTON’S METHOD

PROGRAM:
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

#Python program to find the square root of a number using newton's method using user
defined function
n= float(input("Enter a number to find the squareroot: "))
def nsquare_root(n):
estimate = n/2
newestimate = (estimate+(n/estimate))/2
while newestimate!=estimate:
estimate = newestimate
newestimate = (estimate+(n/estimate))/2
return(newestimate)
newestimate=nsquare_root(n)
print("The square root of the number using newton method is:",newestimate)

OUTPUT:
Enter a number to find the squareroot: 2
The square root of the number using newton method is: 1.414213562373095

3.PROGRAM TO FIND THE EXPONENTIATION(POWER OF A NUMBER)

PROGRAM:
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

#Exponentiation or power of a number using recursion


def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponent: "))
print("The power of a number is",power(base,exp))
OUTPUT:
Enter base: 5
Enter exponent: 2
The power of a number is 25

4.PROGRAM TO FIND THE MAXIMUM OF A LIST OF NUMBERS

PROGRAM:
#Find the maximum of a list of number without inbuilt function and list
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

list=[100,20,60,80,10]
maxno=list[0]
for i in list:
if i>maxno:
maxno=list[i]
print("The maximum number in the list is",maxno)

OUTPUT:
The maximum number in the list is 100

5 PROGRAM TO SEARCH USING LINEAR SEARCH

PROGRAM
#Program for linear search
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

def linearsearch(list,search):
print("The elements in the list is",list)
n=len(list)
for i in range(0,n):
if(list[i]==search):
print("The element is present in the list at position",i)
break
else:
print("The element is not present in the list")
return
l=[20,100,60,120,140]
s=int(input("Enter the element to be searched in the list:"))
linearsearch(l,s)

OUTPUT:
Enter the element to be searched in the list:140
The elements in the list is [20, 100, 60, 120, 140]
The element is present in the list at position 4
On second execution:
Enter the element to be searched in the list:160
The elements in the list is [20, 100, 60, 120, 140]
The element is not present in the list

6 PROGRAM TO SEARCH USING BINARY SEARCH

PROGRAM

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

#Program for Binary Search


def bsearch(list,search):
first=0
last=len(list)-1
while first<=last:
mid=(first+last)//2
if list[mid]==search:
print("element found in position",mid)
break
else:
if search<list[mid]:
last=mid-1
else:
first=mid+1
else:
print("The element is not present in the list")
return
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))
a.append(e)
x=int(input("enter element to search"))
bsearch(a,x)

OUTPUT:
enter upper limit5
enter the elements20
enter the elements40
enter the elements60
enter the elements80
enter the elements100
enter element to search100
element found in position 4

7 PROGRAM TO SORT USING SELECTION SORT


PROGRAM
# Selection Sort

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

def selection_sort(a):
for i in range(0,len(a)):
min=i
for j in range(i+1,len(a)):
if (a[j]<a[min]):
min=j
temp=a[min]
a[min]=a[i]
a[i]=temp
a=[14,46,43,27,57,41,45,21,70]
print("The list of elements before sorting",a)
selection_sort(a)
print("The list of elements after sorting",a)

OUTPUT:
The list of elements before sorting
[14, 46, 43, 27, 57, 41, 45, 21, 70]
The list of elements after sorting
[14, 21, 27, 41, 43, 45, 46, 57, 70]

8 PROGRAM TO SORT USING INSERTION SORT


PROGRAM
#Insertion Sort
def insertion_sort(a):
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

for i in range(1,len(a)):
j=i
while(j!=0 and a[j]<a[j-1]):
a[j],a[j-1]=a[j-1],a[j]
j=j-1
a=[14,46,43,27,57,41,45,21,70]
print("The list of elements before sorting",a)
insertion_sort(a)
print("The list of elements after sorting",a)
OUTPUT:
The list of elements before sorting [14, 46, 43, 27, 57, 41, 45, 21, 70]
The list of elements after sorting [14, 21, 27, 41, 43, 45, 46, 57, 70]

9. PROGRAM TO SORT USING MERGE SORT


PROGRAM

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

#Merge Sort
def merge(left,right):
result=[]
i=j=0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i=i+1
else:
result.append(right[j])
j=j+1
result=result+left[i:]
result=result+right[j:]
return result
def mergesort(a):
if(len(a)<=1):
return a
mid=int(len(a)/2)
left=mergesort(a[:mid])
right=mergesort(a[mid:])
return merge(left,right)
nlist = [14,46,43,27,57,41,45,21,70]
print("Before sorting",nlist)
print("After sorting",mergesort(nlist))
OUTPUT:
Before sorting [14, 46, 43, 27, 57, 41, 45, 21, 70]
After sorting [14, 21, 27, 41, 43, 45, 46, 57, 70]
10. PROGRAM TO GENERATE FIRST N PRIME NUMBERS
PROGRAM:
#First n prime numbers

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

limit =int(input("Enter the limit until which to print the prime numbers:"))
print("Prime numbers till the limit %d: "%(limit))
for num in range(2,limit + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
OUTPUT:
Enter the limit until which to print the prime numbers:10
Prime numbers till the limit 10:
2
3
5
7

11. PROGRAM TO PERFORM MATRIX MULTIPLICATION


PROGRAM:
# Program to multiply two matrices
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

# 3x3 matrix
X = [[12,7,3], [4 ,5,6],[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],[6,7,3,0], [4,5,9,1]]
# result is 3x4
result = [[0,0,0,0], [0,0,0,0],[0,0,0,0]]
# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)

OUTPUT:
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

12. PROGRAM FOR WORD COUNT USING COMMAND LINE ARGUMENTS

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

PROGRAM:
#Program using command line arguments for word count
import sys
if len(sys.argv) != 2:
print('Usage: ./wc.py <filename>')
sys.exit(1)
num_words = num_lines = num_chars = 0
with open(sys.argv[1]) as infile:
for line in infile:
num_lines += 1
num_chars += len(line)
line = line.strip()
words = line.split()
num_words += len(words)
print('Number of Lines is %d' % num_lines)
print('Number of Words is %d' % num_words)
print('Number of Characters is %d' % num_chars)

OUTPUT:

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

C:\ >python wordcount.py sample.txt


Number of Lines is 1
Number of Words is 8
Number of Characters is 37

C:\ >python wordcount.py sample.py

Number of Lines is 2
Number of Words is 7
Number of Characters is 45

13. PROGRAM TO FIND THE MOST FREQUENT WORDS IN A TEXT READ


GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

FROM A FILE
PROGRAM:
#Program for most frequent words in a text read from a file
s = open('sample1.txt','r').read()
num_chars = len(s)
num_lines = s.count('\n')
words = s.split()
d = {}
for w in words:
if w in d:
d[w] += 1
else:
d[w] = 1
num_words = sum(d[w] for w in d)
list1 = [(d[w],w) for w in d]
list1.sort()
list1.reverse()
print("Your input file has characters",num_chars)
print("Your input file has lines",num_lines)
print("Your input file has the following words = ",num_words)
print("\n The most frequent words are")
i=1
for count, word in list1[:50]:
print(i,count,word)
i+= 1

OUTPUT:

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

Your input file has characters 64


Your input file has lines 1
Your input file has the following words = 11
The most frequent words are
1 5 python
2 1 program
3 1 of
4 1 is
5 1 example
6 1 an
7 1 This

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

14. PROGRAM TO SIMULATE THE ELLIPTICAL ORBITS USING PYGAME


PROGRAM:
#Simulation of Elliptical Orbits in Pygame
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((255, 255, 255))
pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
pygame.draw.ellipse(screen, (200, 125, 0), [50, 50, 500, 200], 1)
pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
pygame.display.flip()
clock.tick(5)

OUTPUT:
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

15. PROGRAM TO SIMULATE BOUNCING BALL USING PYGAME


GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

PROGRAM:
#Simulation of bouncing ball using Pygame
import sys, pygame
pygame.init()
size = width, height = 600, 400
speed = [2, 2]
background = 255,255,255
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Bouncing Ball")
ball = pygame.image.load("ball.bmp")
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:

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY


PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE

GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY

You might also like