Python exercise 2
Question: Get the unique letters of a strings and create a list of those unique letters?
Expected Input:
"Banana"
Expected Output
['B', 'a', 'n']
Solution
Input:
st="Banana"
l1=[]
for i in st:
if i not in l1:
l1.append(i)
print(l1)
Output:
Question: Get those string elements of a list which lengths are even number?
Expected Input:
["Mango","Cake","Juice","Banana"]
Expected Output
['Cake', 'Banana']
Solution
Input:
l1=["Mango","Cake","Juice","Banana"]
l2=[]
for j in l1:
cou=0
for k in j:
cou=cou+1
if cou%2==0:
l2.append(j)
print(l2)
Output:
['Cake', 'Banana']
Question: Remove duplicate numbers from a list?
Expected Input:
[1,1,2,5,5,8,9,11,2]
Expected Output
[1, 2, 5, 8, 9, 11]
Solution
Input:
l1=[1,1,2,5,5,8,9,11,2]
l2=[]
for i in l1:
if i not in l2:
l2.append(i)
print(l2)
Output:
[1, 2, 5, 8, 9, 11]
Question: Find the numbers from a list which are greater than 10 and first and last digit of the number is odd
number?
Expected Input:
[11,1,31,3,56,8,20,109,188,222,111]
Expected Output
[11, 31, 109, 111]
Solution
Input:
l1=[11,1,31,3,56,8,20,109,188,222,111]
l2=[]
for i in l1:
if i>10:
v=str(i)
if int(v[0])%2!=0 and int(v[len(v)-1])%2!=0:
l2.append(int(v))
print(l2)
Output:
[11, 31, 109, 111]
Question:Find a number which makes the total sum 0 of the exist list when appended it to the list?
Expected Input:
[1, -2, 4, -15 ,-10, 14, -3]
Expected Output
[1, -2, 4, -15, -10, 14, -3, 11]
Solution
Input:
l1=[1,-2,4,-15,-10,14,-3]
v=0
for i in l1:
v=v+i
z=v-v
n=v+v
if z==0:
l1.append(-v)
else:
l1.append(+v)
print(l1)
Output:
[1, -2, 4, -15, -10, 14, -3, 11]
Question: Shift the given number of digits to the end of a digit?
Expected Input:
12345566
Expected Output
34556612
Solution
Input:
num=str(12345566)
shift=int(2)
num1=num[shift:]
num2=num[:shift]
newNum=int(num1+num2)
print(newNum)
Output:
34556612
Question:Find a those word from a sentence which length is an odd number?
Expected Input:
"The quick brown fox jumps over the lazy dog"
Expected Output
['The', 'quick', 'brown', 'fox', 'jumps', 'the', 'dog']
Solution
Input:
text="The quick brown fox jumps over the lazy dog"
mainTxt=text.split()
newText=[]
v=0
for i in mainTxt:
k=0
for j in i:
k=k+1
if k%2!=0:
newText.append(mainTxt[v])
v=v+1
print(str(newText))
Output:
['The', 'quick', 'brown', 'fox', 'jumps', 'the', 'dog']
Question: Inject a specific value after each element of a list?
Expected Input:
[1,2,3,4,5,6,7,8]
Expected Output
[1, 999, 2, 999, 3, 999, 4, 999, 5, 999, 6, 999, 7, 999,
8, 999]
Solution
Input:
l1=[1,2,3,4,5,6,7,8]
l2=[]
inject_val=999
for i in range(0,len(l1)):
l2.append(l1[i])
l2.append(val)
print(l2)
Output:
[1, 999, 2, 999, 3, 999, 4, 999, 5, 999, 6, 999, 7, 999, 8, 999]
Question:Sort the array but the condition is, if the last and first index value is odd then sort in descending
order and if even then ascending order. If one is odd and other is even then sort in ascending order?
Expected Input:
[5,3,12,5,7,9,2]
Expected Output
sorted list [2, 12, 9, 7, 5, 5, 3]
Solution
Input:
l1=[5,3,12,5,7,9,2]
k=0
for i in l1:
k=k+1
print("Length of the list",k)
for i in range(0,k):
min_val=i
if l1[k-1]%2==0 and l1[0]%2==0:
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
elif l1[k-1]%2!=0 and l1[0]%2!=0:
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
else:
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)
Output:
Length of the list 7
sorted list [2, 12, 9, 7, 5, 5, 3]
Question: Suppose there is an array.Y ou have to compare, pair of element of the array. If the sum of the pair
is 6 then append those in element in another array?
Expected Input:
[4,4,2,1,5,1,9,5,8,6,86]
Expected Output
[4, 2, 1, 5, 5, 1]
Solution
Input:
l1=[4,4,2,1,5,1,9,5,8,6,86]
v=0
j=1
l2=[]
for i in range(0,len(l1)):
if j==len(l1):
break
else:
if l1[v]+l1[j]==6:
l2.append(l1[v])
l2.append(l1[j])
v=v+1
j=j+1
print(l2)
Output:
[4, 2, 1, 5, 5, 1]