Practice problem (26/07/2023)

1.) Write a python program for given example :
example: a4b3c2
output: aaaabbbcc

2.) Python program to interchange first and last elements in a list
Input : [12, 35, 9, 56, 24]
Output : [24, 35, 9, 56, 12]

3.) Python program to find second largest number in a list
Input : [12, 10,35, 9, 56, 24,56,55]
Output : 55

4.) Check if two lists have at-least one element common or not
Input : a = [1, 2, 3, 4, 5]
b = [5, 6, 7, 8, 9]
Output : True
Input : a=[1, 2, 3, 4, 5]
b=[6, 7, 8, 9]
Output : False

2 Likes

1.) Write a python program for given example :

example: a4b3c2

output: aaaabbbcc

str = “a4b3c2”

for i in str:
a = str.split()
a = str[0]*4 +str[2]*3 +str[4]*2
print(a)

2.) Python program to interchange first and last elements in a list

Input : [12, 35, 9, 56, 24]

Output : [24, 35, 9, 56, 12]

l = [12,35,9,56,24]

for i in l:
l[0],l[-1] = l[-1],l[0]
print(l)

4.) Check if two lists have at-least one element common or not

Input : a = [1, 2, 3, 4, 5]

b = [5, 6, 7, 8, 9]

Output : True

Input : a=[1, 2, 3, 4, 5]

b=[6, 7, 8, 9]

Output : False

a = [1,2,3,4,5]
b = [5,6,7,8,9]

common_element = set(a).intersection(b)

if common_element:
print(“True”)
else:
print(“False”)

1 Like

Question1 solution.

Question2 solution.

Question3 solution.

Quetion4 solution.

1 Like