Python exercise 3
Question: Find single digit numbers from a list and return the digit string form as a list?
Expected Input:
[1,8,14,2,6,15]
Expected Output
Length of the list 6
sorted list [15, 14, 8, 6, 2, 1]
['Eight', 'Six', 'Two', 'One']
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)
L2=[]
for q in l1:
if q==0 or q==1 or q==2 or q==3 or q==4 or q==5 or q==6 or q==7 or q==8 or q==9:
if q==0:
L2.append("Zero")
if q==1:
L2.append("One")
if q==2:
L2.append("Two")
if q==3:
L2.append("Three")
if q==4:
L2.append("Four")
if q==5:
L2.append("Five")
if q==6:
L2.append("Six")
if q==7:
L2.append("Seven")
if q==8:
L2.append("Eight")
if q==9:
L2.append("Nine")
print(L2)
Output:
Length of the list 6
sorted list [15, 14, 8, 6, 2, 1]
['Eight', 'Six', 'Two', 'One']
Question: Find the strings, starting with a given prefix in a given list.
Expected Input:
["cat","can","bat","rat"]
Expected Output
['cat', 'can']
Solution
Input:
import re
v=["cat","can","bat","rat"]
pattern=re.compile("^ca")
g=[]
for i in v:
if re.search(pattern,i):
g.append(i)
print(g)
Output:
['cat', 'can']
Question: Find the length of each string element of list?
Expected Input:
["cartoon","can","bath","rat"]
Expected Output
Length of the list [7, 3, 4, 3]
Solution
Input:
v=["cartoon","can","bath","rat"]
k=0
for i in v:
k=k+1
z=[]
for y in v:
n=0
for j in y:
n=n+1
z.append(n)
print("Length of the list",z)
Output:
Length of the list [7, 3, 4, 3]
Question: Suppose you have a list. The list contain string elements. Now find that which string has how many
characters and print the string and number of characters of the string side by side in a new list?
Expected Input:
["Rafsun","Ahmad","Muhammad"]
Expected Output
number of characters present in the list [6, 5, 8]
['Rafsun-->6', 'Ahmad-->5', 'Muhammad-->8']
Solution
Input:
l1=["Rafsun","Ahmad","Muhammad"]
nl=[]
for i in range(0,1):
for j in l1:
l=0
for k in j:
l=l+1
nl.append(l)
print("number of characters present in the list",nl)
final_list=[]
j=0
for q in range(0,len(l1)):
w=str(nl[j])
e=l1[j]
r=e+"-->"+w
final_list.append(r)
j=j+1
print(final_list)
Output:
number of characters present in the list [6, 5, 8]
['Rafsun-->6', 'Ahmad-->5', 'Muhammad-->8']
Question: Find Index of vowel characters and their index number from a string and display in the form of list?
Expected Input:
"OrangEYesRvA"
Expected Output
The vowels and their index number: ['O->0', 'E->5',
'A->11']
Solution
Input:
import re
s1="OrangEYesRvA"
l1=[]
j=0
k=""
for l in range(0,len(s1)):
for i in s1[j]:
k=i
break
l1.append(k+"->"+str(j))
j=j+1
print("Value With index",l1)
finalList=[]
for i in l1:
if re.search("^A",i) or re.search("^E",i) or re.search("^I",i) or re.search("^O",i) or
re.search("^U",i):
finalList.append(i)
print("The vowels and their index number:",finalList)
Output:
Value With index ['O->0', 'r->1', 'a->2', 'n->3', 'g->4', 'E->5', 'Y->6', 'e->7', 's->8', 'R->9', 'v->10',
'A->11']
The vowels and their index number: ['O->0', 'E->5', 'A->11']
Question: Multiply odd numbers present in a number
Expected Input:
12345213
Expected Output
45
Solution
Input:
num=12345213
num1=str(num)
v=1
for i in num1:
if int(i)%2!=0:
v=v*int(i)
print(v)
Output:
45
Question: Find the N largest and the smallest number form a list?
Expected Input:
[33,11,64,23,90]
Enter The Number of largest number:2
Expected Output
The n largest number: 64
The n Smallest number: 23
Solution
Input:
l1=[33,11,64,23,90]
n=int(input("Enter The Number of largest number:"))
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)
print("The n largest number:",l1[k-n])
print("The n Smallest number:",l1[n-1])
Output: