0% found this document useful (0 votes)
9 views

Python Interview Questions

Uploaded by

parthu12347299
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python Interview Questions

Uploaded by

parthu12347299
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Interview questions

Reverse a List from the 4th number


lst = [8,1,3,6,4,2,10,15,12]
x =4

Solution:
def revlst(l,x):
lst1= l[:x]
lst2= l[x:]
z= len(lst2)-1
lst3=[]
for i in range(z):
lst3.append(lst2[z-i])

l=lst1+lst3
return l

revlst(lst,x)

Solution2:
def revlst2(l,x):
lst1= l[:x]
lst2= l[x:]
lst2=lst2[::-1]
l=lst1+lst2
return l

revlst2(lst,x)
Output: [8, 1, 3, 6, 12, 15, 10, 2]

lis = ['1', '1', '10', '6', '99', '2','10'] --> o/p : ['1', '2', '6', '10', '99']

Solution:
def sort1(x):
n = len(x)

for i in range(n):
key = int(x[i])
j=i-1
while j >= 0 and int(key) < int(x[j]):
x[j + 1] = x[j]
j -= 1
x[j + 1] = key
x1=[]

for i in range(len(x)):
if x[i] != x[i - 1]:
x1.append(x[i])

return x1

print(sort1(lis))

Solution2:

def sorted_unique_list(lis):
n=len(lis)
for i in range(n):
key= int(lis[i])
lis[i]=key

lis=set(lis)
lis= list(lis)
lis.sort()
return lis

sorted_unique_list(lis)

Output:[1, 2, 6, 10, 99]

write a function to convert "kkkccdddakk" to "3k2c3d1a2k"

def encode_string(s):
if not s:
return ""

encoded_str = ""
count = 1
for i in range(1, len(s)):
if s[i] == s[i - 1]:
count += 1
else:
encoded_str += str(count) + s[i - 1]
count = 1
encoded_str += str(count) + s[-1]
return encoded_str

str1 = "3k2c3d1a2k"
output = encode_string(str1)
print(output) # Output should be "3k2c3d1a2k"

Output: 3k2c3d1a2k

You might also like