SlideShare a Scribd company logo
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
print("Enter first Number:")
a=int(input())
print("Enter second number: ")
b=int(input())
rem=a%b
while rem!=0:
a=b
b=rem
rem=a%b
print("GCD of given number is:", b)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter first Number:
5
Enter second number:
10
('GCDof given number is:', 5)
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx
print('The square root is' ,newtonSqrt(81))
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
('The square root is', 9.0)
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
n = int(input("Enter the number: "))
e = int(input("Enter the expo: "))
r=n
for i in range(1,e):
r=n*r
print("Exponent is :", r)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter the number: 2
Enter the expo: 3
('Exponent is :', 8)
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
alist = [-45,0,3,10,90,5,- 2,4,18,45,100,1,-266,706]
largest = alist[0]
for large in alist:
if large > alist:
largest = large
print(“The Largest Number is:”,largest)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
(‘The Largest of the List is:’, 706)
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
def Linearsearch(alist,item):
pos=0
found=False
stop=False
while pos<len(alist) and not found and not stop:
if alist[pos]==item:
found=True
print("Element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:
pos=pos+1
return found
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"))
Linearsearch(a,x)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter upper limit:5
Enter the elements:1
Enter the elements:8
Enter the elements:9
Enter the elements:6
Enter the elements:7
Enter element to search:9
Element found in position: 2
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
def binarysearch(a,n,k):
low = 0
high = n
while(low<=high):
mid=int((low+high)/2)
if(k==a[mid]):
return mid
elif(k<a[mid]):
high=mid-1
else:
low=mid+1
return -1
n=int(input("Enter the size of list"))
a=[i for i in range(n)]
print(n)
print("Enter the element")
for i in range(0,n):
a[i]=int(input())
print("Enter the element to be searched")
k=int(input())
position=binarysearch(a,n,k)
if(position!=-1):
print("Entered no:%d is present:%d"%(k,position))
else:
print("Entered no:%d is not present in list"%k)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter the size of list
5
Enter the element
1
8
6
3
4
Enter the element to be searched
1
Entered no:1 is present:0
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]
GE 8161 – Problem Solving and Python Programming Laboratory
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)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]
GE 8161 – Problem Solving and Python Programming Laboratory
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 = [23, 6, 4 ,12, 9]
mergeSort(alist)
print(alist)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
('Splitting ', [23, 6, 4, 12, 9])
('Splitting ', [23, 6])
('Splitting ', [23])
('Merging ', [23])
('Splitting ', [6])
('Merging ', [6])
('Merging ', [6, 23])
('Splitting ', [4, 12, 9])
('Splitting ', [4])
('Merging ', [4])
('Splitting ', [12, 9])
('Splitting ', [12])
('Merging ', [12])
('Splitting ', [9])
('Merging ', [9])
('Merging ', [9, 12])
('Merging ', [4, 9, 12])
('Merging ', [4, 6, 9, 12, 23])
[4, 6, 9, 12, 23]
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
i=1
x = int(input("Enter the Upper Limit:"))
for k in range (1, (x+1), 1):
c=0;
for j in range (1, (i+1), 1):
a = i%j
if (a==0):
c = c+1
if (c==2):
print (i)
else:
k = k-1
i=i+1
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter the Upper Limit:100
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
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
X= [[1,2,7],
[4,5,6],
[1,2,3]]
Y= [[5,8,1],
[6,7,3],
[4,5,1]]
result= [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
result[i][j]+=X[i][k]*Y[k][j]
for r in result:
print(r)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
[45, 57, 14]
[74, 97, 25]
[29, 37, 10]
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
import sys
if len(sys.argv)!=2:
print('Error: filename missing')
sys.exit(1)
filename = sys.argv[1]
file = open(filename, "r+")
wordcount={}
for word in file.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word]+=1
for k,v in wordcount.items():
print (k, v)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
filename = input("Enter file name:")
inf = open(filename,'r')
wordcount={}
for word in inf.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for key in wordcount.keys():
print("%sn%sn"%(key, wordcount[key]))
linecount = 0
for i in inf:
paragraphcount = 0
if'n'in i:
linecount += 1
if len(i)<2: paragraphcount *= 0
elif len(i)>2: paragraphcount = paragraphcount + 1
print('%-4dn%4dn%sn'%(paragraphcount,linecount,i))
inf.close()
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter file name:'text.txt'
a
1
used
1
for
1
language
1
Python
1
is
1
programming
2
general-purpose
1
widely
1
high-level
1
GE 8161 – Problem Solving and Python Programming Laboratory
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)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
import pygame,sys,time,random
from pygame.locals import*
from time import*
pygame.init()
windowSurface=pygame.display.set_mode((500,400),0,32)
pygame.display.set_caption("BOUNCE")
BLACK=(0,0,0)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
info=pygame.display.Info()
sw=info.current_w
sh=info.current_h
y=0
direction=1
while True:
windowSurface.fill(BLACK)
pygame.draw.circle(windowSurface,GREEN,(250,y),13,0)
sleep(.006)
y+=direction
if y>=sh:
direction=-1
elif y<=0:
direction=1
pygame.display.update()
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
GE 8161 – Problem Solving and Python Programming Laboratory
Output:

More Related Content

What's hot (20)

PDF
Parse Tree
A. S. M. Shafi
 
PPTX
Advanced topics in artificial neural networks
swapnac12
 
PPSX
Type conversion
Frijo Francis
 
PDF
Daa notes 3
smruti sarangi
 
PPTX
Divide and conquer
ramya marichamy
 
PPTX
6-Practice Problems - LL(1) parser-16-05-2023.pptx
venkatapranaykumarGa
 
PPT
02 order of growth
Hira Gul
 
PDF
Mathematics For Artificial Intelligence
Suraj Kumar Jana
 
PPTX
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
SKUP1
 
PPTX
Operators in java presentation
kunal kishore
 
PPTX
Deciability (automata presentation)
Sagar Kumar
 
PPTX
Programming in C
Rvishnupriya2
 
PDF
Fuzzy Logic in the Real World
BCSLeicester
 
PDF
Cs6402 design and analysis of algorithms may june 2016 answer key
appasami
 
PDF
Lecture 4 asymptotic notations
jayavignesh86
 
PPT
Greedy method by Dr. B. J. Mohite
Zeal Education Society, Pune
 
ODP
Garbage collection
Mudit Gupta
 
PPTX
Presentation of daa on approximation algorithm and vertex cover problem
sumit gyawali
 
PDF
Undecidabality
Anshuman Biswal
 
PDF
Object Oriented Programming Lab Manual
Abdul Hannan
 
Parse Tree
A. S. M. Shafi
 
Advanced topics in artificial neural networks
swapnac12
 
Type conversion
Frijo Francis
 
Daa notes 3
smruti sarangi
 
Divide and conquer
ramya marichamy
 
6-Practice Problems - LL(1) parser-16-05-2023.pptx
venkatapranaykumarGa
 
02 order of growth
Hira Gul
 
Mathematics For Artificial Intelligence
Suraj Kumar Jana
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
SKUP1
 
Operators in java presentation
kunal kishore
 
Deciability (automata presentation)
Sagar Kumar
 
Programming in C
Rvishnupriya2
 
Fuzzy Logic in the Real World
BCSLeicester
 
Cs6402 design and analysis of algorithms may june 2016 answer key
appasami
 
Lecture 4 asymptotic notations
jayavignesh86
 
Greedy method by Dr. B. J. Mohite
Zeal Education Society, Pune
 
Garbage collection
Mudit Gupta
 
Presentation of daa on approximation algorithm and vertex cover problem
sumit gyawali
 
Undecidabality
Anshuman Biswal
 
Object Oriented Programming Lab Manual
Abdul Hannan
 

Similar to Python Lab manual program for BE First semester (all department (20)

DOCX
Basic python laboratoty_ PSPP Manual .docx
Kirubaburi R
 
DOCX
Python lab manual all the experiments are available
Nitesh Dubey
 
PPTX
ANSHUL RANA - PROGRAM FILE.pptx
jeyel85227
 
PDF
Python for High School Programmers
Siva Arunachalam
 
PDF
Looping
MuhammadBakri13
 
PDF
Sample Program file class 11.pdf
YashMirge2
 
PDF
GE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
ANJALAI AMMAL MAHALINGAM ENGINEERING COLLEGE
 
PPTX
Python.pptx
AshaS74
 
PDF
xii cs practicals class 12 computer science.pdf
gmaiihghtg
 
DOCX
ECE-PYTHON.docx
Chaithanya89350
 
PDF
Python Lab Manual
Bobby Murugesan
 
PDF
25 MARCH1 list, generator.pdf list generator
PravinDhanrao3
 
PDF
xii cs practicals
JaswinderKaurSarao
 
PDF
Python Manuel-R2021.pdf
RamprakashSingaravel1
 
PPTX
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Live Exam Helper
 
PPTX
Practice_Exercises_Control_Flow.pptx
Rahul Borate
 
PDF
Algorithm Design and Analysis - Practical File
KushagraChadha1
 
PDF
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
parthp5150s
 
PDF
python_lab_manual_final (1).pdf
keerthu0442
 
PDF
Introduction to python cheat sheet for all
shwetakushwaha45
 
Basic python laboratoty_ PSPP Manual .docx
Kirubaburi R
 
Python lab manual all the experiments are available
Nitesh Dubey
 
ANSHUL RANA - PROGRAM FILE.pptx
jeyel85227
 
Python for High School Programmers
Siva Arunachalam
 
Sample Program file class 11.pdf
YashMirge2
 
GE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
ANJALAI AMMAL MAHALINGAM ENGINEERING COLLEGE
 
Python.pptx
AshaS74
 
xii cs practicals class 12 computer science.pdf
gmaiihghtg
 
ECE-PYTHON.docx
Chaithanya89350
 
Python Lab Manual
Bobby Murugesan
 
25 MARCH1 list, generator.pdf list generator
PravinDhanrao3
 
xii cs practicals
JaswinderKaurSarao
 
Python Manuel-R2021.pdf
RamprakashSingaravel1
 
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Live Exam Helper
 
Practice_Exercises_Control_Flow.pptx
Rahul Borate
 
Algorithm Design and Analysis - Practical File
KushagraChadha1
 
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
parthp5150s
 
python_lab_manual_final (1).pdf
keerthu0442
 
Introduction to python cheat sheet for all
shwetakushwaha45
 
Ad

Recently uploaded (20)

PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Ad

Python Lab manual program for BE First semester (all department

  • 1. GE 8161 – Problem Solving and Python Programming Laboratory Program: print("Enter first Number:") a=int(input()) print("Enter second number: ") b=int(input()) rem=a%b while rem!=0: a=b b=rem rem=a%b print("GCD of given number is:", b)
  • 2. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter first Number: 5 Enter second number: 10 ('GCDof given number is:', 5)
  • 3. GE 8161 – Problem Solving and Python Programming Laboratory Program: def newtonSqrt(n): approx = 0.5 * n better = 0.5 * (approx + n/approx) while better != approx: approx = better better = 0.5 * (approx + n/approx) return approx print('The square root is' ,newtonSqrt(81))
  • 4. GE 8161 – Problem Solving and Python Programming Laboratory Output: ('The square root is', 9.0)
  • 5. GE 8161 – Problem Solving and Python Programming Laboratory Program: n = int(input("Enter the number: ")) e = int(input("Enter the expo: ")) r=n for i in range(1,e): r=n*r print("Exponent is :", r)
  • 6. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter the number: 2 Enter the expo: 3 ('Exponent is :', 8)
  • 7. GE 8161 – Problem Solving and Python Programming Laboratory Program: alist = [-45,0,3,10,90,5,- 2,4,18,45,100,1,-266,706] largest = alist[0] for large in alist: if large > alist: largest = large print(“The Largest Number is:”,largest)
  • 8. GE 8161 – Problem Solving and Python Programming Laboratory Output: (‘The Largest of the List is:’, 706)
  • 9. GE 8161 – Problem Solving and Python Programming Laboratory Program: def Linearsearch(alist,item): pos=0 found=False stop=False while pos<len(alist) and not found and not stop: if alist[pos]==item: found=True print("Element found in position",pos) else: if alist[pos]>item: stop=True else: pos=pos+1 return found 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")) Linearsearch(a,x)
  • 10. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter upper limit:5 Enter the elements:1 Enter the elements:8 Enter the elements:9 Enter the elements:6 Enter the elements:7 Enter element to search:9 Element found in position: 2
  • 11. GE 8161 – Problem Solving and Python Programming Laboratory Program: def binarysearch(a,n,k): low = 0 high = n while(low<=high): mid=int((low+high)/2) if(k==a[mid]): return mid elif(k<a[mid]): high=mid-1 else: low=mid+1 return -1 n=int(input("Enter the size of list")) a=[i for i in range(n)] print(n) print("Enter the element") for i in range(0,n): a[i]=int(input()) print("Enter the element to be searched") k=int(input()) position=binarysearch(a,n,k) if(position!=-1): print("Entered no:%d is present:%d"%(k,position)) else: print("Entered no:%d is not present in list"%k)
  • 12. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter the size of list 5 Enter the element 1 8 6 3 4 Enter the element to be searched 1 Entered no:1 is present:0
  • 13. GE 8161 – Problem Solving and Python Programming Laboratory Program: def selectionSort(alist): for i in range(len(alist)-1,0,-1): pos=0 for location in range(1,i+1): if alist[location]>alist[pos]: pos= location temp = alist[i] alist[i] = alist[pos] alist[pos] = temp alist = [54,26,93,17,77,31,44,55,20] selectionSort(alist) print(alist)
  • 14. GE 8161 – Problem Solving and Python Programming Laboratory Output: [17, 20, 26, 31, 44, 54, 55, 77, 93]
  • 15. GE 8161 – Problem Solving and Python Programming Laboratory 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)
  • 16. GE 8161 – Problem Solving and Python Programming Laboratory Output: [17, 20, 26, 31, 44, 54, 55, 77, 93]
  • 17. GE 8161 – Problem Solving and Python Programming Laboratory 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 = [23, 6, 4 ,12, 9] mergeSort(alist) print(alist)
  • 18. GE 8161 – Problem Solving and Python Programming Laboratory Output: ('Splitting ', [23, 6, 4, 12, 9]) ('Splitting ', [23, 6]) ('Splitting ', [23]) ('Merging ', [23]) ('Splitting ', [6]) ('Merging ', [6]) ('Merging ', [6, 23]) ('Splitting ', [4, 12, 9]) ('Splitting ', [4]) ('Merging ', [4]) ('Splitting ', [12, 9]) ('Splitting ', [12]) ('Merging ', [12]) ('Splitting ', [9]) ('Merging ', [9]) ('Merging ', [9, 12]) ('Merging ', [4, 9, 12]) ('Merging ', [4, 6, 9, 12, 23]) [4, 6, 9, 12, 23]
  • 19. GE 8161 – Problem Solving and Python Programming Laboratory Program: i=1 x = int(input("Enter the Upper Limit:")) for k in range (1, (x+1), 1): c=0; for j in range (1, (i+1), 1): a = i%j if (a==0): c = c+1 if (c==2): print (i) else: k = k-1 i=i+1
  • 20. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter the Upper Limit:100 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
  • 21. GE 8161 – Problem Solving and Python Programming Laboratory Program: X= [[1,2,7], [4,5,6], [1,2,3]] Y= [[5,8,1], [6,7,3], [4,5,1]] result= [[0,0,0], [0,0,0], [0,0,0]] for i in range(len(X)): for j in range(len(Y[0])): for k in range(len(Y)): result[i][j]+=X[i][k]*Y[k][j] for r in result: print(r)
  • 22. GE 8161 – Problem Solving and Python Programming Laboratory Output: [45, 57, 14] [74, 97, 25] [29, 37, 10]
  • 23. GE 8161 – Problem Solving and Python Programming Laboratory Program: import sys if len(sys.argv)!=2: print('Error: filename missing') sys.exit(1) filename = sys.argv[1] file = open(filename, "r+") wordcount={} for word in file.read().split(): if word not in wordcount: wordcount[word] = 1 else: wordcount[word]+=1 for k,v in wordcount.items(): print (k, v)
  • 24. GE 8161 – Problem Solving and Python Programming Laboratory Output:
  • 25. GE 8161 – Problem Solving and Python Programming Laboratory Program: filename = input("Enter file name:") inf = open(filename,'r') wordcount={} for word in inf.read().split(): if word not in wordcount: wordcount[word] = 1 else: wordcount[word] += 1 for key in wordcount.keys(): print("%sn%sn"%(key, wordcount[key])) linecount = 0 for i in inf: paragraphcount = 0 if'n'in i: linecount += 1 if len(i)<2: paragraphcount *= 0 elif len(i)>2: paragraphcount = paragraphcount + 1 print('%-4dn%4dn%sn'%(paragraphcount,linecount,i)) inf.close()
  • 26. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter file name:'text.txt' a 1 used 1 for 1 language 1 Python 1 is 1 programming 2 general-purpose 1 widely 1 high-level 1
  • 27. GE 8161 – Problem Solving and Python Programming Laboratory 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)
  • 28. GE 8161 – Problem Solving and Python Programming Laboratory Output:
  • 29. GE 8161 – Problem Solving and Python Programming Laboratory Program: import pygame,sys,time,random from pygame.locals import* from time import* pygame.init() windowSurface=pygame.display.set_mode((500,400),0,32) pygame.display.set_caption("BOUNCE") BLACK=(0,0,0) WHITE=(255,255,255) RED=(255,0,0) GREEN=(0,255,0) BLUE=(0,0,255) info=pygame.display.Info() sw=info.current_w sh=info.current_h y=0 direction=1 while True: windowSurface.fill(BLACK) pygame.draw.circle(windowSurface,GREEN,(250,y),13,0) sleep(.006) y+=direction if y>=sh: direction=-1 elif y<=0: direction=1 pygame.display.update() for event in pygame.event.get(): if event.type==QUIT: pygame.quit() sys.exit()
  • 30. GE 8161 – Problem Solving and Python Programming Laboratory Output: