Sie sind auf Seite 1von 19

INDEX

PAGE
S.NO DATE NAME OF THE EXPERIMENT MARK SIGN
NO.

1 GCD OF TWO NUMBERS

2 NEWTON‘S METHOD TO FIND


SQUARE ROOT OF A NUMBER

3 EXPONENTIATION (POWER OF A
NUMBER)
FINDING THE MAXIMUM OF A
4 LIST OF NUMBERS

LINEAR SEARCH AND BINARY


5 SEARCH

SELECTION SORT, INSERTION


6 SORT

7 MERGE SORT

FIRST N PRIME NUMBERS


8

MULTIPLY MATRICES
9

WORD COUNT FROM COMMAND


10 LINE ARGUMENTS

11 MOST FREQUENT WORDS IN A


TEXT READ FROM A FILE

12 SIMULATE ELLIPTICAL ORBITS


IN PYGAME
13 SIMULATE BOUNCING BALL
USING PYGAME
TOTAL
AVERAGE
Ex.No:1GCD OF TWO NUMBERS
Date:

Program:

def GCD(x, y):

if x > y:

smaller = y

else:

smaller = x

for i in range(1, smaller+1):

if((x % i == 0) and (y % i == 0)):

gcd = i

return gcd

num1 = 20

num2 = 10

print("The GCD. of", num1,"and", num2,"is", GCD(num1, num2))

Output:

The GCD.of 20 and 10 is 10


Ex.No:2 FIND THE SQUARE ROOT OF A NUMBER (NEWTON’S METHOD)
Date:

Program:

def newtonSqrt(n, a):

approx = 0.5 * n

for i in range(a):

betterapprox = 0.5 * (approx + n/approx)

approx = betterapprox

return betterapprox

print(newtonSqrt(10, 3))

print(newtonSqrt(10, 5))

print(newtonSqrt(10, 10))

Output:

3.162319422150883
3.162277660168379
3.162277660168379
Ex.No:3EXPONENTIATION
Date:

Program:

Output:

Enter base Value: 5


Enter exponential Value: 3

Result: 125
Ex.No:4FIND THE MAXIMUM OF A LIST OF NUMBERS
Date:

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("Maximum of a List of Number is:",a[n-1])

Output:

Enter number of elements: 5


Enter element: 5
Enter element: 8
Enter element: 2
Enter element: 1
Enter element: 8
Maximum of a List of Number is : 24
Ex.No:5 LINEAR SEARCH AND BINARY SEARCH
Date:

Program:

Size= int (input( “Enter the number of elements:”))


Lst=[]
Flag=False
Print(“Enter the elements: ”)

for i in range (0,size):


lst.append(int(input() ))
print(“Elements of the list are : “, lst)
ele = int (input(“Enter the element to be searched :” ))
for i in range (0,size):
if ele == lst[i]:
flag= True
break
if flag == True:
print (“The element {0} was found at position {1}”. format(ele, i+1))\
else:
print(“The element {} is not found in the list”.format (ele))

Output:

Enter number of elements: 5


Enter element: 2
Enter element: 3
Enter element: 44
Enter element: 1
Enter element: 5
The elements of the list are : [2, 3, 44, 1, 5]
Enter the element to be searched : 1
The element 1 was found at the position : 4
>>>=====================RESTART=========================
Enter number of elements: 10
Enter element: 3
Enter element: 2
Enter element: 4
Enter element: 5
Enter element: 2
Enter element: 7
Enter element: 6
Enter element: 8
Enter element: 9
Enter element: 10
The elements of the list are : [3, 2, 4, 5, 2, 7, 6, 8, 9, 10]
Enter the element to be searched : 14
The element 14 is not found in the list
Program:

def binarySearch(alist, item):


first = 0
last = len(alist)-1
found = False
while first<=last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1

return found
if(found = = FALSE):
print (“The element {} is not found in the list”.format(-1))
el s e :
print (“The element {} is found in the list”.format(13))
blist= [0, 1 , 2, 8 , 13, 17, 19, 32, 42]
print(binarySearch(blist, -1))

print(binarySearch(blist, 13))

Output:

The element -1 is not found in the list


The element 13 is found in the list
Ex.No:6 SELECTION SORT, INSERTION SORT
Date:

Program:

def selsort(sample):
print("intial list:",sample)
for i in range(len(sample)):
print(sample)
minIndex=i

j=i+1

while(j < len(sample)):


if(sample[j] < sample[minIndex]):
minIndex = j
j+=1
sample[i], sample[minIndex] = sample[minIndex], sample[i]
print("sorted list",sample)
sample1 = [12,1,3,2,7,-100]
selsort(sample1)

Output:

[-100,1,2, 3,7, 12]


Program:

def insertsort(sample):

print("intial sample:", sample)

for i in range(1, len(sample)):


print(sample)
j=i

while(j!=0 and sample[j] < sample[j-1]):

sample[j-1], sample[j] = sample[j], sample[j-1]


j-=1

print("sorted list:",sample)
sample1 = [12,300,-90,-100-1000,1,4]
Insertsort(sample1)
Output:

[-1000,-100,-90,1,4,12,300]
Ex.No:7 MERGE SORT
Date:

Program:

def merge(left,right):
result = []
i,j = 0, 0
while i<len(left) and j<len(right):
if left[i] <= right[j]:
result.append(left[i])
i+=1
else:
result.append(right[j])
j+=1

result += left[i:]
result += right[j:]
returnresult
defmergesort(lst):
if(len(lst) <= 1):
returnlst
mid = int(len(lst)/2)
left = mergesort(lst[:mid])
right = mergesort(lst[mid:])
return merge(left,right)
arr = [1,2,-1,0,9,65,7,3,4,1,2]
print(mergesort(arr))

Output:

[-1,0,1,2,2,3,4,,7,9,65]
Ex.No:8FIRST N PRIME NUMBERS
Date:

Program:

def isprime(n): # user function checks


for x in range (2, n//2):
if n%x==0 :
return 0
return 1
n=int(input(“Enter the number of prime numbers:”))
primes=[]
i=2
while len (primes)<n:
if isprime(i)==1:
primes.append(i)
i+=1
print(primes)

Output:

Enter number of prime numbers : 14


[2,3,4,5,7,11,13,17,19,23,29,31,37,41]
Ex.No:9 MULTIPLY MATRICES
Date:

Program:

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

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]
Ex.No:10 PROGRAMS THAT TAKE COMMAND LINE ARGUMENTS(WORD COUNT)
Date:

Program:

fname = input("Enter file name: ")

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:

Enter file name: prem.txt


Number of words: 27
Ex.No:11 FIND THE MOST FREQUENT WORDS IN A TEXT READ FROM A FILE
Date:

Program:

fr = open("prempaul.txt","r")
wordcount = {}
for word in fr.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for k,v in wordcount.items():
print(k, v)
fr.close()

Output:

To - 1
Find - 1
The - 1
Most-1
Frequent-1
Words-1
In-1
A-2
Text-1
Read-1
From-1
File -1
Ex.No:12SIMULATE ELLIPTICAL ORBITS IN PYGAME
Date:

Program:

import math
import random
import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):
self.x = x
self.y = y
self.vx = 0
self.vy = 0
self.colour = colour
def apply_gravity (self, target):
dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2
#g = G*m/dsqd * normalized (self - target)
if dsqd == 0:
return
self.vx += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5
self.vy += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5
def update (self):
self.x += self.vx
self.y += self.vy
pygame.init()
window = pygame.display.set_mode ((600, 400))
main_surface = pygame.Surface ((600, 400))
colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555,
0x666666, 0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] +
[0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF,
0x888888, 0xFFFFFF, 0x808000, 0x008080, 0x800080, 0x800000]
#colours = [0xFF0000, 0x00FF00, 0x0000FF,
0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,
0xFFFFFF, 0x808000, 0x008080, 0x800080,
0x800000] particles = [Particle (200, 100,
colours [i]) for i in range (20)] earth =
Particle (200, 200)
for i, p in enumerate (particles):
p.vx = i / 100
while (True):
# main_surface.fill(0x000000)
pygame.draw.circle (main_surface, 0x00FF00, (earth.x, earth.y), 5, 2)
for p in particles:
p.apply_gravity (earth)
p.update ()
pygame.draw.circle (main_surface, p.colour, (int (p.x), int (p.y)), 5, 2)
window.blit(main_surface, (0, 0))
pygame.display.flip()

Output:

\
Ex.No:13 SIMULATE BOUNCING BALL USING PYGAME
Date:

Program:

import sys
import pygame
pygame.init()

size = width, height = 320, 240


speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load('C:\\Users\\admin\\Desktop//ball.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(black)
screen.blit(ball, ballrect)
pygame.display.flip()
Output:

Das könnte Ihnen auch gefallen