Python exercise 1
Question: Get the first and last value of a list?
Expected Input:
["Apple","Mango","Banana","Orange"]
Expected Output
Apple
Orange
Solution:
Input:
l1=["Apple","Mango","Banana","Orange"]
for i in l1:
if i==l1[0]:
print(l1[0])
if i==l1[-1]:
print(l1[-1])
Output:
Apple
Orange
Question: Find that which element is present how many times in a list?
Expected Input:
[11,15,11,18,11,14,15,11]
Expected Output
11 4
15 2
11 4
18 1
11 4
14 1
15 2
11 4
Solution
Input:
l1=[11,15,11,18,11,14,15,11]
k=0
for l in range(0,len(l1)):
el=0
for i in l1:
cmpr_elmnt=l1[k]
if cmpr_elmnt==i:
el=el+1
else:
el=el+0
print(cmpr_elmnt,el)
k=k+1
Output:
11 4
15 2
11 4
18 1
11 4
14 1
15 2
11 4
Question: Find the length of a list?
Expected Input:
[11,15,18,11,14,15,11]
Expected Output
4
Solution
Input:
l1=[11,15,18,11,14,15,11]
k=0
for i in l1:
k=k+1
print(k)
Output:
4
Question: Find the length of a string
Expected Input:
Rafsan Ahmad
Expected Output
12
Solution
Input:
l1="Rafsan Ahmad"
k=0
for i in l1:
k=k+1
print(k)
Output:
12
Question: Print True if the length of the list is more than equal eight and the fifth element appear 3 times
in the list otherwise print False
Expected Input:
[11,14,18,21,12,15,14,12,13,12]
Expected Output
True
Solution
Input:
l1=[11,14,18,21,12,15,14,12,13,12]
k=0
for i in l1:
k=k+1
k=0
v=[]
for l in range(0,len(l1)):
el=0
for i in l1:
cmpr_elmnt=l1[k]
if cmpr_elmnt==i:
el=el+1
else:
el=el+0
v.append(el)
k=k+1
q=4
if k>=8 and v[q]==3:
print("True")
else:
print("false")
Output:
True
Question: Suppose you have two lists. Now convert those two lists into one list and list the same index value
of both lists should be in side by side?
Expected Input:
l1=[1,2,3,4]
l2=[5,6,7,8]
Expected Output
[1, 5, 2, 6, 3, 7, 4, 8]
Solution
Input:
l1=[1,2,3,4]
l2=[5,6,7,8]
el=[]
v=0
k=0
for i in l1:
k=k+1
for i in range(0,k):
el.append(l1[v])
el.append(l2[v])
v=v+1
print(el)
Output:
[1, 5, 2, 6, 3, 7, 4, 8]
Question: Sort a list using Selection sort algorithm?
Expected Input:
l1=[2,4,1,8,3]
Expected Output
[1, 2, 3, 4, 8]
Solution
Input:
l1=[2,4,1,8,3]
for i in range(0,len(l1)):
min_val=i
for j in range(i,len(l1)):
if l1[min_val]>l1[j]:
min_val=j
temp=l1[i]
l1[i]=l1[min_val]
l1[min_val]=temp
print(l1)
Output:
Question: Search a given value from a list using Binary search?
Expected Input:
[4,1,7,3,9,11,5,15,13,79,19,25]
Expected Output
Value is found and index is 9
Solution
Input:
l1=[4,1,7,3,9,11,5,15,13,79,19,25]
print("Given list",l1)
for i in range(0,len(l1)):
min_val=i
for j in range(i,len(l1)):
if l1[min_val]>l1[j]:
min_val=j
temp=l1[i]#2
l1[i]=l1[min_val]#1
l1[min_val]=temp
print("Sorted list",l1)
k=0
for i in l1:
k=k+1
print("The length of the list",k)
lowerPts=0
upperPts=k
mid_val=int(k/2)
target_Val=19
for i in range(0,k):
if l1[mid_val]==target_Val:
print("Value is found and index is",mid_val)
break
if target_Val>l1[mid_val]:
lowerPts=mid_val
mid_val=int((lowerPts+upperPts)/2)
if target_Val<l1[mid_val]:
upperPts=mid_val
mid_val=int((lowerPts+upperPts)/2)
Output:
Given list [4, 1, 7, 3, 9, 11, 5, 15, 13, 79, 19, 25]
Sorted list [1, 3, 4, 5, 7, 9, 11, 13, 15, 19, 25, 79]
The length of the list 12
Value is found and index is 9
Question: Take a value from user and append it in the first position of a list?
Expected Input:
4 Expected Output
[4,
2, 2, 2]
Solution
Input:
value=int(input("Enter number:"))
currli=[value]
li=[2,2,2]
NELI=currli+li
print(NELI)
Output:
[4, 2, 2, 2]
Question: Find list length, Sort a list and then exchange the 1 index value with last index value?
Expected Input:
[1,8,14,2,6,15]
Expected Output
Length of the list 6
sorted list [1, 2, 6, 8, 14, 15]
[1, 15, 6, 8, 14, 2]
Solution
Input:
l1=[1,8,14,2,6,15]
k=0
for i in l1:
k=k+1
print("Length of the list",k)
for i in range(0,k):
min_val=i
for j in range(i,len(l1)):
if l1[min_val]>l1[j]:
min_val=j
temp=l1[i]
l1[i]=l1[min_val]
l1[min_val]=temp
print("sorted list",l1)
v=1
v2=k-1
for i in range(1,k-1):
temp=l1[v]
l1[v]=l1[v2]
l1[v2]=temp
v=v+1
v2=v2-1
print(l1)
Output:
Length of the list 6
sorted list [1, 2, 6, 8, 14, 15]
[1, 15, 6, 8, 14, 2]