***PYTHON INTERVIEW QUESTION***
1)
*
***
*****
*******
*********
***********
*************
***************
*****************
8) Write a program in python to execute a bubble sort algorithm.
def bs(a):
... b=len(a)-1
... for x in range(b):
... for y in range(b-x):
... if a[y]>a[y+1]:
... a[y],a[y+1]=a[y+1],a[y]
... return a
bs([32,5,3,6,7,54,87])
[3, 5, 6, 7, 32, 54, 87]
9) Python Program for Fibonacci Number:
def Fibonacci(n):
if n<0:
print("Incorrect Input")
elif n==0:
return 0
elif n==1:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
10) Write a program in Python to check if a number is prime:
def prime(n):
... if n>1:
... for x in range(2,n):
... if(n%x)==0:
... print("nor a prime")
... break
... else:
... print("Prime")
... else:
... print("not Prime")
prime(3)
Prime
prime(4)
nor a prime
11) Write a program in Python to check if a string is a Palindrome.
def isPalindrome(str):
... for i in range(0, int(len(str)/2)):
... if str[i] != str[len(str)-i-1]:
... return False
... return True
s = "malayalam"
ans = isPalindrome(s)
if (ans):
... print ("yes")
... else:
... print ("No")
yes
def uniq(list1):
... uniq_list=[]
... for i in list1:
... if i not in uniq_list:
... uniq_list.append(i)
... return uniq_list
uniq([1,1,2,3,3,2,1,4])
[1, 2, 3, 4]
13) Program to interchange the first and last element in a list:
def swapList(newList):
... size = len(newList)
... temp=newList[0]
... newList[0]=newList[size-1]
... newList[size-1]=temp
... return newList
print(swapList([12, 35, 9, 56, 24]))
[24, 35, 9, 56, 12]
14) Python to find sum of elements in list:
total = 0
list1 = [11, 5, 17, 18, 23]
for x in range(0,len(list1)):
... total=total+list1[x]
print("Sum of all elements in given list: ", total)
('Sum of all elements in given list: ', 74)
15)
No comments:
Post a Comment