Professional Documents
Culture Documents
PREPARE FOR
70/70 IN
COMPUTER
SCIENCE (083)
2019-20
Board Exam
DoyPyEdu PLAY WITH PYTHON
Unsolved Assignment
Find Output:
1) ch=input(“Enter Your Choice: 3) in=int(input(“Enter in: “)) a=a*4
“) col=int(input(“Enter col: “)) else:
m=3 if in==5: a=a+a
if x==‟a‟ : m*=2 col=col+1 print(a)
elif x==‟c‟ : m/=2 elif in==10: Result after execution if:
elif x==‟d‟: m+=3 col=col*2 a 2 7 9 7 13
else: m*=3 else: Ans
print (m) col=col+4 5) a=int(input(“Enter Value of a:
Result after execution if: print(col) “))
x „a‟ „c‟ „g‟ „d‟ Result after execution if: b=int(input(“Enter Value of b: “))
m in 6 5 10 9 if (a<b or a>5)>10:
2) ch=input(“Enter Your Choice: col 16 4 7 2 print(a)
“) col else: print(b)
if ch= = „a‟ : print(“I”) 4) a=int(input(“Enter Value of a: Result after execution if:
elif ch= = „b‟ or ch== „d‟: ”)) a 4 12 10 5
print (“II”) if a==1: b 6 3 7 9
else: print( “III”) a=a*a Ans
Result after execution if: elif a==3 or a==4 or a==5:
6) x=int(input(“Enter Value of x :
x „d‟ „h‟ „g‟ „a‟ a=a*3 “))
Ans elif a==6 or a==7 or a==8: y=int(input(“Enter Value of y : “))
Page 3 of 41 EDUCATION FOR EVERYONE
DoyPyEdu PLAY WITH PYTHON
if x>5: else: print (x) Ans
if y>5: x 4 12 10 5
print(y) y 6 3 7 9
Condition (if else) Output Assignment 1
Que. 1 . x=x+3 y=4
x=2 y=x+4 if x>=y:
y=1 print("x= ",x,", y= ",y) if x>y:
if x>12: ----------------------- x=x*3
x=x*2 Que. 7 . print("x= ",x,", y= ",y)
y=x+10 x=12 -----------------------
print("x= ",x,", y= ",y) y=4 Que. 12 .
----------------------- if x>y: x=4
Que. 2 . x=x*3 y=4
x=12 y=x*4 if x>y:
y=11 else: if x<y:
if x>=12: x=x+3 x=y*3
x=x*2 y=x+4 print("x= ",x,", y= ",y)
y=x+10 print("x= ",x,", y= ",y) -----------------------
print("x= ",x,", y= ",y) ----------------------- Que. 13 .
----------------------- Que. 8 . x=4
Que. 3 . x=2 y=4
x=11 y=4 if x>y:
y=11 if x!=y: if x>y:
if x<12: x=x-3 x=y*3
x=x+2 y=x*4 print("x= ",x,", y= ",y)
y=x*3 else: -----------------------
print("x= ",x,", y= ",y) x=x-3 Que. 14 .
----------------------- y=x+4 x=4
Que. 4 . print("x= ",x,", y= ",y) y=4
x=11 ----------------------- if x<y:
y=11 Que. 9 . if x>y:
if x!=y: x=2 x=y*3
x=x+3 y=4 print("x= ",x,", y= ",y)
y=x+4 if x!=y: -----------------------
print("x= ",x,", y= ",y) if x>y: Que. 15 .
----------------------- x=x-3 x=4
Que. 5 . else: y=4
x=2 y=x+4 if x==y:
y=4 print("x= ",x,", y= ",y) if x>y:
if x==y: ----------------------- x=y*3
x=x*3 Que. 10 . print("x= ",x,", y= ",y)
y=x*4 x=2 -----------------------
print("x= ",x,", y= ",y) y=4 Que. 16 .
----------------------- if x>=y: x=4
Que. 6 . if x>y: y=4
x=2 x=x*3 if x==y:
y=4 else: y=x+9 if x>y:
if x>y: print("x= ",x,", y= ",y) x=y*3
x=x*3 ----------------------- y+=6
y=x*4 Que. 11 . print("x= ",x,", y= ",y)
else: x=2 -----------------------
Output:
ANS 1 : x= 2 , y= 1 ------------------------------
Page 4 of 41 EDUCATION FOR EVERYONE
DoyPyEdu PLAY WITH PYTHON
Unsolved problems
1. Write PYTHON Code for this :
6. WAP to take a string and display longest/ smallest word present in it.
7. WAP to take 10 names and display those name start and end with same character.
8. WAP to take 10 names and display that name are palindrome.
9. WAP to take 10 names and display those name length is 3 only.
10. WAP to take 10 names and display all name in pig Latin format.
11. WAP to take 10 names and arrange all names in length in ascending order.
12. WAP to take 10 names and display number of consonants and vowel of each name.
13. WAP to take 10 names and display all names in reverse.
14. WAP to accept a sentence and print the Palindrome words
New Python Worksheet
String Output Assignment 1
Que. 1 . str="DOTPYEDU" str="DOTPYEDU" print(str[i:])
x="DOT" for i in range(len(str)): for i in range(len(str)): -----------------------
y="PY" print(str) print(str[i*-1::]) Que. 20.
z="EDU" ----------------------- ----------------------- str="DOTPYEDU"
r=x+y+z Que. 8. Que. 14. for i in range(len(str)):
print(r) str="DOTPYEDU" str="DOTPYEDU" print(str[i:i+1])
----------------------- for i in range(len(str)): for i in range(len(str)): -----------------------
Que. 2 . print(str*i) print(str[i*-1::]) Que. 21.
x="DOT" ----------------------- ----------------------- str="DOTPYEDU"
y="PY" Que. 9. Que. 15. for i in range(len(str)):
z="EDU" str="DOTPYEDU" str="DOTPYEDU" print(str[i::i+1])
r=x*3+y*1+z*2 for i in range(len(str)): for i in range(len(str)): -----------------------
print(r) print(str[:]) print(str[i*-1::-2]) Que. 22.
----------------------- ----------------------- ----------------------- str="DOTPYEDU"
Que. 3 . Que. 9. Que. 16. for i in range(len(str)):
str="DOTPYEDU" str="DOTPYEDU" str="DOTPYEDU" print(str[i:len(str)])
r=str*3 for i in range(len(str)): for i in range(len(str)): -----------------------
print(r) print(str[::]) print(str[i*-1::2]) Que. 23.
----------------------- ----------------------- ----------------------- str="DOTPYEDU"
Que. 4 . Que. 10. Que. 17. for i in range(len(str)):
str="DOTPYEDU" str="DOTPYEDU" str="DOTPYEDU" print(str[i:len(str)-
r=str*3 for i in range(len(str)): for i in range(len(str)): i])
print(r) print(str[::2]) if i%2==0: -----------------------
----------------------- ----------------------- print(str[i:]) Que. 24.
Que. 5. Que. 11. ----------------------- str="DOTPYEDU"
str="DOTPYEDU" str="DOTPYEDU" Que. 18. for i in range(len(str)):
for i in str: for i in range(len(str)): str="DOTPYEDU" print(str[i:len(str)-
print(str) print(str[i::]) for i in range(len(str)): i%2])
----------------------- ----------------------- if i%3==0: -----------------------
Que. 6. Que. 12. print(str[i:]) Que. 25.
str="DOTPYEDU" str="DOTPYEDU" ----------------------- str="DOTPYEDU"
for i in str: for i in range(len(str)): Que. 19. for i in range(len(str)):
print(i) print(str[i::i+1]) str="DOTPYEDU" print(str[i:len(str)-
----------------------- ----------------------- for i in range(len(str)): i%2])
Que. 7. Que. 13. if i%3!=0: -----------------------
OUTPUT:
ANS 1 : DOTPYEDUDOTPYEDUDOTPYEDUDOT E
DOTPYEDU PYEDUDOTPYEDUDOTPYEDU D
------------------------------ DOTPYEDUDOTPYEDUDOTPYEDUDOT U
ANS 2 : PYEDUDOTPYEDUDOTPYEDUDOTPYE ------------------------------
DOTDOTDOTPYEDUEDU DU ANS 13:
------------------------------ ------------------------------ DOTPYEDU
ANS 3 : ANS 9: U
DOTPYEDUDOTPYEDUDOTPYEDU DOTPYEDU DU
------------------------------ DOTPYEDU EDU
ANS 4 : DOTPYEDU YEDU
DOTPYEDUDOTPYEDUDOTPYEDU DOTPYEDU PYEDU
------------------------------ DOTPYEDU TPYEDU
ANS 5: DOTPYEDU OTPYEDU
DOTPYEDU DOTPYEDU ------------------------------
DOTPYEDU DOTPYEDU ANS 14:
DOTPYEDU ------------------------------ DOTPYEDU
DOTPYEDU ANS 9: U
DOTPYEDU DOTPYEDU DU
DOTPYEDU DOTPYEDU EDU
DOTPYEDU DOTPYEDU YEDU
DOTPYEDU DOTPYEDU PYEDU
------------------------------ DOTPYEDU TPYEDU
ANS 6: DOTPYEDU OTPYEDU
D DOTPYEDU ------------------------------
O DOTPYEDU ANS 15:
T ------------------------------ D
P ANS 10: UEPO
Y DTYD DYTD
E DTYD EPO
D DTYD YTD
U DTYD PO
------------------------------ DTYD TD
ANS 7: DTYD O
DOTPYEDU DTYD ------------------------------
DOTPYEDU DTYD ANS 16:
DOTPYEDU ------------------------------ DTYD
DOTPYEDU ANS 11: U
DOTPYEDU DOTPYEDU D
DOTPYEDU OTPYEDU EU
DOTPYEDU TPYEDU YD
DOTPYEDU PYEDU PEU
------------------------------ YEDU TYD
ANS 8: EDU OPEU
DU ------------------------------
DOTPYEDU U ANS 17:
DOTPYEDUDOTPYEDU ------------------------------ DOTPYEDU
DOTPYEDUDOTPYEDUDOTPYEDU ANS 12: TPYEDU
DOTPYEDUDOTPYEDUDOTPYEDUDOT DOTPYEDU YEDU
PYEDU OPEU DU
DOTPYEDUDOTPYEDUDOTPYEDUDOT TE ------------------------------
PYEDUDOTPYEDU PU ANS 18:
Y DOTPYEDU
PYEDU OPEU PY
DU TE ------------------------------
------------------------------ PU ANS 24:
ANS 19: Y DOTPYEDU
OTPYEDU E OTPYED
TPYEDU D TPYEDU
YEDU U PYED
EDU ------------------------------ YEDU
U ANS 22: ED
------------------------------ DOTPYEDU DU
ANS 20: OTPYEDU ------------------------------
D TPYEDU ANS 25:
O PYEDU DOTPYEDU
T YEDU OTPYED
P EDU TPYEDU
Y DU PYED
E U YEDU
D ------------------------------ ED
U ANS 23: DU
------------------------------ DOTPYEDU ------------------------------
ANS 21: OTPYED
DOTPYEDU TPYE
String Output Assignment 2
Que. 1 . if i%2!=0: print(r)
s="CBSE Exam 2019-2020" r+=s[i].upper() ***************
print(s) else: Que. 6 .
r="" r+=s[i].lower() s="Cbse Exam 2019-2020"
size=len(s) print(r) print(s)
for i in range(size): *************** r=""
if i%2==0: Que. 4 . for i in s:
r+=s[i].upper() s="Cbse Exam 2019-2020" if i.isupper():
else: print(s) r+=i.lower()
r+=s[i] r="" elif i.islower():
print(r) size=len(s) r+=i.upper()
*************** for i in range(size): else:
Que. 2 . if i%3==0: r=r+i
s="Cbse Exam 2019-2020" r+=s[i].upper() print(r)
print(s) else: ***************
r="" r+=s[i].lower() Que. 7 .
size=len(s) print(r) s="Cbse Exam 2019-2020"
for i in range(size): *************** print(s)
if i%2==0: Que. 5 . r=""
r+=s[i].upper() s="Cbse Exam 2019-2020" for i in s:
else: print(s) if i.isupper()== False:
r+=s[i].lower() r="" r+=i.lower()
print(r) size=len(s) elif i.islower()== False:
*************** for i in range(size): r+=i.upper()
Que. 3 . if i%3==0: else:
s="Cbse Exam 2019-2020" r+=s[i].upper() r=r+i
print(s) elif i%2==0: print(r)
r="" r+=s[i].lower() ***************
size=len(s) else: Que. 8 .
for i in range(size): r=r+s[i] s="Cbse Exam 2019-2020"
ASSIGNMENT OF FUNCTION
print("Q1. ") a+=2 print(a,":",b,"\n")
def switchover(A,N, split): b[0]=b[0]+5 a,b=4,8
K=0 print(a,":",b,"\n") print(a,":",b,"\n")
while K<N: a,b=4,[8] s(a,b)
if K<split : print(a,":",b,"\n") print(a,":",b,"\n")
A[K]+= K s(a,b) s(a)
else: print(a,":",b,"\n") print(a,":",b,"\n")
A[K]*= K print("="*30) s()
K+=1 print("Q4. ") print(a,":",b,"\n")
def display(A,N): def s(a, b): print("="*30)
K=0 a[0]+=2 print("Q7. ")
while K<N: b[0]+=5 def func(x,y):
if K%2== 0: print(a,":",b,"\n") if x[0]%y[0]==0 :
print(A[K],end="%") a,b=[4],[8] return x[0]+2
else: print(a,":",b,"\n") else:
print(A[K],"\n") s(a,b) return y[0]-1
K+=1 print(a,":",b,"\n") p,q=[20],[23];
H= [30,40,50,20,10,5] print("="*30) print(p,",",q)
switchover(H,6,3) print("Q5. ") q=func(p,q)
print(H) def s(a, b=7): print(p,",",q)
display(H,6) a+=2 print("="*30)
print("="*30) b*=5 print("Q8. ")
print("Q2. ") print(a,":",b,"\n") def func(x,y=10):
def s(a, b): a,b=4,8 if x%y==0 :
a+=2 print(a,":",b,"\n") return x+2
b*=5 s(a,b) else:
print(a,":",b,"\n") print(a,":",b,"\n") return y-1
a,b=4,8 s(a) p,q=20,23;
print(a,":",b,"\n") print(a,":",b,"\n") print(p,",",q)
s(a,b) print("="*30) q=func(p,q)
print(a,":",b,"\n") print("Q6. ") print(p,",",q)
print("="*30) def s(a=6, b=7): print("="*30)
print("Q3. ") a+=2 print("Q9. ")
def s(a, b): b*=5 def func(x,y=10):
Page 20 of 41 EDUCATION FOR EVERYONE
DoyPyEdu PLAY WITH PYTHON
if x%y==0 : p=func(q) if(y<=20):
return x+2 print(p,",",q) print(temp,",",x,",",y)
else: q=func(p) a,b =50,20
return y-1 print(p,",",q) execute(b)
p,q=20,23; print("="*30) print(a,",",b)
print(p,",",q) print("Q11.") execute(b)
q=func(p,q) def execute(x,y=200): print(a,",",b)
print(p,",",q) temp=x+y print("="*30)
p=func(q) x+=temp print("Q13.")
print(p,",",q) if(y!=200): def execute(x,y=20):
print("="*30) print(temp,",",x,",",y) temp=x*y
print("Q10. ") a,b =50,20 x+=temp
def func(x,y=10): execute(b) if(y>=20):
if x%y==0 : print(a,",",b) print(temp,",",x,",",y)
return x+2 execute(a,b) a,b =50,20
else: print(a,",",b) execute(b)
return y-1 print("="*30) print(a,",",b)
p,q=20,23; print("Q12.") execute(a)
print(p,",",q) def execute(x,y=15): print(a,",",b)
q=func(p,q) temp=x+y
print(p,",",q) x+=temp
OUTPUT:
Q1. [6] : [13] Q7. ===============
[30, 41, 52, 60, 40, 25] [6] : [13] [20] , [23] Q11.
30%41 ================== [20] , 22 50 , 20
52%60 Q5. ================ 70 , 120 , 20
40%25 4:8 Q8. 50 , 20
================= 6 : 40 20 , 23 =================
Q2. 4:8 20 , 22 Q12.
4:8 6 : 35 ================ 35 , 55 , 15
6 : 40 4:8 Q9. 50 , 20
4:8 ================= 20 , 23 35 , 55 , 15
================= Q6. 20 , 22 50 , 20
Q3. 4:8 9 , 22 ==============
4 : [8] 6 : 40 ============== Q13.
6 : [13] 4:8 Q10. 400 , 420 , 20
4 : [13] 6 : 35 20 , 23 50 , 20
================ 4:8 20 , 22 1000 , 1050 , 20
Q4. 8 : 35 9 , 22 50 , 20
[4] : [8] 4:8 9,9
===============
#Example 1:
import matplotlib.pyplot as p1
p1.plot([1,7,3,8],[2,4,6,8])
p1.ylabel('Y Range')
p1.xlabel('X Range')
p1.title("First Plot")
p1.show()
#Example 2:
import matplotlib.pyplot as p1
p1.plot([1,7,3,8,11],[2,4,6,8,11])
p1.ylabel('Y Range')
p1.xlabel('X Range')
p1.title("First Plot")
p1.show()
#Example 3:
x=[1,2,3,4,5]
y=[6,7,8,9,10]
p1.xlabel("DATA FROM A")
p1.ylabel("DATA FROM B")
p1.title("DATA ALL")
p1.bar(x,y,width=[0.3,0.2,0.4,0.1,0.5],color=["red","black","b","g","y",])#width=0.1 to 0.9
p1.show()
Page 25 of 41 EDUCATION FOR EVERYONE
DoyPyEdu PLAY WITH PYTHON
#Example 4:
import numpy as np
x=np.arange(0,10,2)
y=[6,7,8,9,10]
p1.xlabel("DATA FROM A")
p1.ylabel("DATA FROM B")
p1.title("DATA ALL")
p1.bar(x,y,width=0.2,color="b")
p1.bar(x+0.3,y,width=0.2,color="r")
p1.show()
#Example 5:
import numpy as np
x=np.arange(10,100,20)
print(x)
L=["Python","PHP","CSS","C++","HTML"]
mcolor=['red','black','pink','yellow','silver']
p1.title("DATA ALL")
p1.pie(x,labels=L,colors=mcolor,autopct="%05.3f%%",explode=[0,0.2,0,0.1,0])
p1.show()
#Example 6:
import numpy as np
x=np.arange(10,100,20)
print(x)
L=["Python","PHP","CSS","C++","HTML"]
mcolor=['red','yellow','silver','black','pink']
myex=[0,0.2,0,0.1,0]
p1.title("DATA ALL")
p1.pie(x,labels=L,colors=mcolor,autopct="%05.3f%%",explode=myex)
p1.show()
#Example 7:
x=[ 1,2,3, 4,5,6, 6,7,8, 8, 9,10]
y=[13,7,2,11,0,3,17,1,5,11,22,14]
print(x)
print(y)
p1.xlabel("Overs",color="red")
p1.ylabel("Score",color="blue")
p1.title("IPL 2019")
p1.plot(x,y,'black')
p1.savefig("my pi chart .jpeg")
p1.show()
PLOT UNSOLVE PROBLEMS
1. Bar chart is drawn(using pyplot) to represent data of various countries and growth_rate_countrys.
countrys =['BRAZIL','US','BRITAIN','UAE','BANG','KENYA','AFRICA','AUST']
growth_rate_countrys =[24,57,48,60,15,41,19,66]
3. Line chart is drawn(using pyplot) to represent data of various cities,
Population.
cities=["GHAZIABAD","DELHI","KANPUR","JAIPUR","UDAIPUR","MUMBAI","GOA","P
UNE","AHEMBAD"]
population=[10,12.5, 16.18,15.17,19.89, 16.7,8.9,10.12,13.12]
4. Bar chart is drawn(using pyplot) to represent data of various isps and profit _ rate.
isps=["AIRTEL","VODAFONE","AIRCELL","IDIA","BSNL","JIO","TATA","VSNL","UNICOM"]
profit _ rate =[10,13,25,9,5,7,19,11,17]
5. Pie chart is drawn(using pyplot) to represent data of various cars and growth_rate_comp.
cars=["MARUTI","SUZUKI","FORD","KIA","MG","TOYTA","TATA","BMW","AUDI"]
growth_rate_comp=[10,13,25,9,5,7,19,11,17]
2. WAF to read and count Words start with from text file : Upper /Lower / Digits / Special / Vowel /
Consonant Character
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
for word in words:
if word[0].isupper()==True: #same Q1 part 1st
count+=1
print(count)
3. WAF to read and count Words end with from text file : Upper /Lower / Digits / Special / Vowel /
Consonant/User Define Character
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
for word in words:
if word[-1].isupper()==True: #same Q1 part 1st
count+=1
print(count)
4. WAF to read and count Words start and end with from text file : Upper /Lower / Digits / Special / Vowel /
Consonant/User Define Character
ANS:
f1=open("data.txt","r")
s=f1.read()
count+=1
print("Lines start with 't' is ",count)
Q4. WAF to count the number of words in a text file „DATA.TXT‟ which is starting/ended with an word
„The‟, „the‟, „my‟, „he‟, „they‟.
ANS:
f1=open("data.txt","r")
s=f1.read()
print("All Data of file in string : \n",s)
print("="*30)
count=0
words=s.split()
print("All Words: ",words,", length is ",len(words))
for word in words:
if word.startswith("the")==True: # word.endswith(“the”)
count+=1
print("Words start with 'the' is ",count)
Q5. WAF to count the number of line in a text file „DATA.TXT‟ which is starting/ended with an word „The‟,
„the‟, „my‟, „he‟, „they‟.
ANS:
f1=open("data.txt","r")
s=f1.read()
print("All Data of file in string : \n",s)
print("="*30)
f1=open("data.txt","r")
lines=f1.readlines()
print("All Data of file in lines: \n",lines)
count=0
i=1
print("="*30)
for line in lines:
print("Line %d : %s ,length is : %d"%(i,line,len(line)))
i+=1
if line.startswith("the")==True: # line.endswith(“the”)
count+=1
print("Lines start with 'the' is ",count)
Q6. WAF to read data from a text file DATA.TXT, and display those words, which are less than 4 characters.
ANS:
f1=open("data.txt","r") for word in words:
s=f1.read() if len(word)==4:
print(s) print(word)
count=0 count+=1
words=s.split() print(count)
print(words,”, ”,len(words))
Q7. WAF to read data from a text file DATA.TXT, and display words with number of characters.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
for word in words:
print(word,”, ”,len(word))
Q8. WAF to read data from a text file DATA.TXT, and display each words with number of vowels and
consonants.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
countV=0
countC=0
words=s.split()
print(words,”, ”,len(words))
for word in words:
countV=0
countC=0
for ch in word:
if ch.isalnum()==True:
if ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u':
countV+=1
else:
countC+=1
print("Word : ",word,", V: ",countV,", C= ", countC)
Q9. WAF to read data from a text file DATA.TXT, and display word which have maximum number of vowels
characters.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
countV=0
countC=0
words=s.split()
print(words,", ",len(words))
maxV=0
final=""
for word in words:
countV=0
for ch in word:
if ch.isalnum()==True:
if ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u':
countV+=1
if maxV<countV:
maxV=countV
final=word
print("Final : ",final,", maxV: ",maxV)
Q10. WAF to read data from a text file DATA.TXT, and display word which have maximum/minimum
characters.
Ans:
f1=open("data.txt","r") minfinal=""
s=f1.read() maxfinal=""
print(s) for word in words[1:]:
words=s.split() length=len(word)
print(words,", ",len(words)) if maxC<length:
maxC=len(words[0]) maxC=length
minC=len(words[0]) maxfinal=word
Bubble Sort
def bubblesort(list): list[idx] = list[idx+1]
# Swap the elements to arrange in order list[idx+1] = temp
for iter_num in range(len(list)-1,0,-1): list = [19,2,31,45,6,11,121,27]
for idx in range(iter_num): bubblesort(list)
if list[idx]>list[idx+1]: print(list)
temp = list[idx]
Merge Sort
def merge(left_half,right_half): right_half.remove(right_half[0])
res = [] if len(left_half) == 0:
while len(left_half) != 0 and len(right_half) != 0: res = res + right_half
if left_half[0] < right_half[0]: else:
res.append(left_half[0]) res = res + left_half
left_half.remove(left_half[0]) return res
else: unsorted_list = [64, 34, 25, 12, 22, 11, 90]
res.append(right_half[0]) print(merge_sort(unsorted_list))
Insertion Sort
def insertion_sort(InputList): InputList[j+1] = InputList[j]
for i in range(1, len(InputList)): j=j-1
j = i-1 InputList[j+1] = nxt_element
nxt_element = InputList[i] list = [19,2,31,45,30,11,121,27]
# Compare the current element with next one insertion_sort(list)
while (InputList[j] > nxt_element) and (j >= 0): print(list)
Selection Sort
def selection_sort(input_list): # Swap the minimum value with the compared value
for idx in range(len(input_list)): input_list[idx], input_list[min_idx] =
min_idx = idx input_list[min_idx], input_list[idx]
for j in range( idx +1, len(input_list)): l = [19,2,31,45,30,11,121,27]
if input_list[min_idx] > input_list[j]: selection_sort(l)
min_idx = j print(l)