28
RADHAKRISHNAN . P DEPT OF CSE PANIMALAR ENGINEERING COLLEGE PANIMALAR ENGINEERING COLLEGE (A CHRISITIAN MINORITY INSTITUTION) JAISAKTHI EDUCATIONAL TRUST ACCREDITED BY NATIONAL BOARD OF ACCREDITATION (NBA) AN ISO 9001:2000 CERTIFIED INSTITUTION Bangalore Trunk Road,Varadharajapuram,Nasarathpettai, Poonamallee, Chennai 600 123. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY I YEAR I SEMESTER

PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

PANIMALAR ENGINEERING COLLEGE

(A CHRISITIAN MINORITY INSTITUTION)

JAISAKTHI EDUCATIONAL TRUST

ACCREDITED BY NATIONAL BOARD OF ACCREDITATION (NBA)

AN ISO 9001:2000 CERTIFIED INSTITUTION

Bangalore Trunk Road,Varadharajapuram,Nasarathpettai,

Poonamallee, Chennai – 600 123.

DEPARTMENT

OF

COMPUTER SCIENCE AND ENGINEERING

PROBLEM SOLVING AND PYTHON

PROGRAMMING LABORATORY I YEAR – I SEMESTER

Page 2: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO LIST OF PROGRAMS

1 PROGRAM TO COMPUTE THE GCD OF TWO

NUMBERS

2 PROGRAM TO FIND THE SQUARE ROOT OF A NUMBER

( NEWTON ‘S METHOD )

3 PROGRAM FOR FINDING EXPONENTIATION

( POWER OF A NUMBER )

4 PROGRAM TO FIND THE MAXIMUM NUMBER IN THE

LIST

5 (A) PROGRAM FOR LINEAR SEARCH

5 (B) PROGRAM FOR BINARY SEARCH

6 (A) PROGRAM FOR SELECTION SORT

6 (B) PROGRAM FOR INSERTION SORT

7 PROGRAM FOR MERGE SORT

8 PROGRAM TO FIND THE FIRST ‘ N TERMS ‘ PRIME

NUMBERS

9 PROGRAM FOR MATRIX MULTIPLICATION

10 PROGRAM FOR COMMAND LINE ARGUMENTS (WORD

COUNT )

11 FIND THE MOST FREQUENT WORDS IN A TEXT READ

FROM A FILE

12 STIMULATE ELLIPTICAL ORBITS IN PYGAME

13 STIMULATE BOUNCING BALL USING PYGAME

Page 3: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO LIST OF LLUSTRATIVE PROGRAMS

1 PROGRAM TO EXCHANGE THE VALUES OF TWO

VARIABLES

2 PROGRAM TO CIRCULATE THE VALUES OF N

VARIABLES

3 PROGRAM TO FIND THE DISTANCE BETWEEN TWO

POINTS

4 PROGRAM FOR SUM OF AN ARRAY OF NUMBERS

5 PROGRAM TO CREATE A HISTOGRAM FROM A LIST OF

INTEGERS

6 PROGRAM FOR COPY FILE

Page 4: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

NOTE

x = 54 OR x = int ( input ( "enter number " ) )

a=[15,-8,-1,4,36,1] OR a=eval(input("enter the list")) OR a = input ( "Enter list : " ) . split() a = list ( map (int,a) )

Page 5: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 1

PROGRAM TO COMPUTE THE GCD OF TWO NUMBERS

x = 24

y = 54

while y != 0 :

x , y = y , x % y

print ( x )

OUTPUT

6

Page 6: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 2

PROGRAM TO FIND THE SQUARE ROOT OF A NUMBER

( NEWTON ‘S METHOD )

def newton ( n , times ) :

approx = 0.5 * n

for i in range ( times ):

approx = 0.5 * ( approx + n / approx )

return approx

print ( newton (36,4) )

print ( newton (36,7) )

OUTPUT

6.00018310826276

6.0

Page 7: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 3

PROGRAM FOR FINDING EXPONENTIATION

( POWER OF A NUMBER )

def power ( base , exp ) :

if ( exp == 1 ):

return ( base )

else:

return ( base * power ( base , exp - 1 ) )

print ( "result = " , power ( 6 , 3 ) )

OUTPUT

result = 216

Page 8: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 4 (A)

PROGRAM FOR LINEAR SEARCH

a=[-4 ,10 , 25 , 15 , 3]

search = int ( input ( " Enter search number " ) )

for i in a :

if search == i:

print( search ," found ")

break

else:

print ( "Not Found" )

OUTPUT

>>>

Enter search number 25

25 found

>>>

Enter search number 22

Not Found

Page 9: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 4 (B)

PROGRAM FOR BINARY SEARCH

a=[ 15 , 5 , 21 , -5 , 32 , 9 ]

a . sort ()

search = int ( input ( "Enter Search Number " ) )

start = 0

stop = len(a) - 1

while ( start <= stop ) :

mid = ( start + stop ) // 2

if ( search == a [mid] ):

print( search , "element found at the position" , mid)

break

elif ( search < a[mid] ):

stop = mid - 1

else:

start = mid + 1

else:

print ( "element not found" )

Page 10: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

OUTPUT

Enter Search Number 5

5 element found at the position 1

Enter Search Number 22

element not found

Page 11: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 5

PROGRAM TO FIND THE FIRST ‘ N TERMS ‘ PRIME NUMBERS

upto = int ( input ( "enter upto" ) )

count = 0

for n in range ( 2 , 1000 ) :

for i in range ( 2 , n ) :

if n % i == 0 :

break

else:

count = count + 1

if ( count <= upto ) :

print( n )

OUTPUT

enter upto 30

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

101 103 107 109 113

Page 12: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 6

PROGRAM TO FIND THE MAXIMUM OF A LIST OF NUMBERS

a=[25, 9, -3, 12, 5]

max=0

for i in a:

if( max < i):

max = i

print( "maximum number = " , max )

OUTPUT

maximum number = 25

Page 13: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 7 (A)

PROGRAM FOR SELECTION SORT

a = [ 16,10,-1,369,5 ]

for i in range( len (a) ):

small = min ( a[ i: ] )

index = a.index ( small )

a[ i ] , a [ index ] = a [ index ] , a[ i ]

print ( a )

OUTPUT

[-1, 5, 10, 16, 369]

Page 14: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 7 (B)

PROGRAM FOR INSERTION SORT

a = [ 16,10,-1,369,5 ]

for i in a :

j = a.index ( i )

while j > 0 :

if a[ j -1 ] > a[j] :

a [ j - 1 ] , a [ j ] = a [ j ] , a [ j - 1 ]

else:

break

j = j - 1

print ( a )

OUTPUT

[-1, 5, 10, 16, 369]

Page 15: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 8

PROGRAM FOR MERGE SORT

def mergesort ( seq ):

mid = len( seq ) // 2

lft , rgt = seq [ :mid ] , seq [ mid: ]

if len ( lft ) > 1 : lft = mergesort ( lft )

if len ( rgt ) > 1 : rgt = mergesort ( rgt )

res = []

while lft and rgt :

if lft [ -1 ] >= rgt [ -1 ] :

res.append ( lft.pop ( ) )

else:

res.append ( rgt.pop ( ) )

res.reverse()

return ( lft or rgt ) + res

a = [ 54,26,93,-17,1 ]

print ( mergesort ( a ) )

OUTPUT

[-17, 1, 26, 54, 93]

Page 16: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 9

PROGRAM FOR MATRIX MULTIPLICATION

a = [ [ 2 , 2 ] , [ 2 , 2 ] ]

b = [ [ 3 , 3 ] , [ 3 , 3 ] ]

c = [ [ 0 , 0 ] , [ 0 , 0 ] ]

for i in range ( len ( a ) ):

for j in range ( len ( b [ 0 ] ) ):

for k in range ( len ( c ) ):

c [ i ] [ j ] = a [ i ][ k ] * b [ k ][ j ] + c [ i ] [ j ]

print(c)

OUTPUT

[[12, 12], [12, 12]]

Page 17: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 10

PROGRAM FOR COMMAND LINE ARGUMENTS ( WORD COUNT )

from sys import argv

from collections import Counter

f = open( argv[1] ," r ")

c = Counter ( f.read( ) . split( ) )

print( " Number of words in the file : " , c )

INPUT (ex.txt)

panimalar anna panimalar anna panimalar annauniversity

OUTPUT

sam@syse:~$ python cmd.py ex.txt

('Number of words in the file :', Counter({'panimalar': 3, 'anna': 2,

'annauniversity': 1}))

Page 18: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 11

PROGRAM TO FIND MOST FREQUENT WORDS IN A

TEXT READ FROM A FILE

from collections import Counter

f=open( "ex.txt" , "r" )

c=Counter( f.read( ) . split( ) )

print("Number of words in the file :",c)

INPUT (ex.txt )

panimalar anna panimalar anna panimalar annauniversity

OUTPUT

sam@syse:~$ python coun.py

('Number of words in the file :', Counter({'panimalar': 3, 'anna': 2,

'annauniversity': 1}))

Page 19: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 12

SIMULATE ELLIPTICAL ORBITS IN PYGAME

import pygame

import math

from time import *

pygame.init()

screen = pygame.display.set_mode((700, 700))

black = (0, 0, 0)

yellow = (255, 255, 0)

blue = (0, 0, 255)

ex = 50

ey = 350

oe = 0

a=1

while a==1:

screen.fill(black)

ex = math.cos(oe) * 300 + 350

ey = -math.sin(oe) * 300 + 350

oe += .002

pygame.draw.circle(screen, yellow, (350,350), 50)

pygame.draw.circle(screen, blue, (int(ex), int(ey)), 15)

pygame.display.flip()

sleep(.010)

Page 20: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

for event in pygame.event.get():

if event.type == pygame.QUIT:

a = 0

pygame.quit()

OUTPUT

Page 21: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

EX.NO : 13

SIMULATE BOUNCING BALL USING PYGAME

import pygame

from time import *

pygame.init()

screen = pygame.display.set_mode((500, 400))

black = (0, 0, 0)

blue = (0, 0, 255)

x = 1

y = 0

a = 1

while a ==1:

screen.fill(black)

pygame.draw.circle(screen, blue , (250,y), 10)

y += x

if y >= 400:

x = -1

elif y <= 0:

x = 1

pygame.display.flip()

sleep(.005)

for event in pygame.event.get():

if event.type == pygame.QUIT:

a=0

pygame.quit()

OUTPUT

Page 22: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

Page 23: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

LLUSTRATIVE PROGRAMS ( UNIT – 2 )

1 . PROGRAM FOR EXCHANGE THE VALUE OF 2 VARIABLES

x = int ( input("Enter value of x: ") )

y = int ( input("Enter value of y: ") )

print("Before swapping: " , x , " and " , y )

x , y = y , x

print("After swapping: " , x , " and " , y )

OUTPUT :

Enter value of x: 15

Enter value of y: 21

Before swapping : 15 and 21

After swapping : 21 and 15

Page 24: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

LLUSTRATIVE PROGRAMS ( UNIT – 2 )

2 . PROGRAM FOR CIRCULATE THE VALUES OF ‘N’ VARIABLES

a=[14, -1, 59, -100, 21]

for i in range ( len (a) ):

print( a [ i: ] + a [ :i ] )

OUTPUT :

[14, -1, 59, -100, 21]

[-1, 59, -100, 21, 14]

[59, -100, 21, 14, -1]

[-100, 21, 14, -1, 59]

[21, 14, -1, 59, -100]

Page 25: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

LLUSTRATIVE PROGRAMS ( UNIT – 2 )

3 . PROGRAM FOR DISTANCE BETWEEN TWO POINTS

x1=int(input("enter x1 :"))

x2=int(input("enter x2 :"))

y1=int(input("enter y1 :"))

y2=int(input("enter y2 :"))

calx=(x2-x1)**2

caly=(y2-y1)**2

point=calx-caly

print("Distance Between Two Points = " , point)

OUTPUT

enter x1 : 2

enter x2 : 8

enter y1 : 8

enter y2 : 10

Distance Between Two Points = 32

Page 26: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

LLUSTRATIVE PROGRAMS ( UNIT – 3 )

4 . PROGRAM FOR SUM AN ARRAY OF NUMBERS

a=[11,22,33]

print("Given List = " , a)

sum=0

for i in a:

sum=sum+i

print("sum an array of numbers = ", sum)

OUTPUT:

Given List = [11, 22, 33]

sum an array of numbers = 66

Page 27: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

LLUSTRATIVE PROGRAMS ( UNIT – 4 )

5 . TO CREATE A HISTOGRAM FROM A LIST OF INTEGERS

a=[5,15,9,6,20,2]

for i in a:

sum=''

while(i>0):

sum=sum+'*'

i=i-1

print(sum)

OUTPUT

*****

***************

*********

******

********************

**

Page 28: PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY · radhakrishnan . p dept of cse panimalar engineering college ex.no list of programs 1 program to compute the gcd of two numbers

RADHAKRISHNAN . P

DEPT OF CSE PANIMALAR ENGINEERING COLLEGE

LLUSTRATIVE PROGRAMS ( UNIT – 5 )

6 . PROGRAM FOR COPY FILE

f1=open("r1.txt","r")

f2=open("r2.txt","w")

for i in f1:

f2.write(i)

f1.close()

f2.close()

INPUT (r1.txt )

panimalar anna panimalar anna panimalar annauniversity

OUTPUT (r2.txt )

panimalar anna panimalar anna panimalar annauniversity