Udf List String Programs
Udf List String Programs
P.1 Write a method/user defined function in python sum_avg_list(A) which find the
sum of all elements and average of given list A where list A is passed as parameter.
def sum_avg_list(a):
s=0
for i in range(len(a)):
s+=a[i]
print('sum is ',s)
print(‘average is’,s/9)
A=[10,15,20,30,40,50,60,70,80]
sum_avg_list (A)
P.2 Write a method/user defined function in python Reverse_List (A) which reverse the
all elements of given list A where list A is passed as parameter.
def Reverse_List(a):
l=len(a)
for i in range(l//2):
t=a[i]
a[i]=a[l-1-i]
a[l-1-i]=t
A=[10,15,20,30,40,50,60,70,80]
Reverse_List(A)
P.3 Write a method/user defined function in python reverse_list_FH_SH (A) which
reverse the first half elements with second half elements of the given list A where list A
is passed as parameter.
def reverse_list_FH_SH(a):
l=len(a)
for i in range(l//2):
t=a[i]
a[i]=a[l//2+i+1]
a[l//2+i+1]=t
A=[10,15,20,30,40,50,60,70,80]
reverse_list_FH_SH(A)
def reverse_list_first_with_second(a):
l=len(a)
for i in range(0,l-1,2):
t=a[i]
a[i]=a[i+1]
a[i+1]=t
reverse_list_first_with_second(A)
P.5 Write a method/user defined function in python MAX_LIST (A) which finds the
biggest element from the given list A where list A is passed as parameter.
def MAX_LIST(a):
l=len(a)
max=a[0]
for i in range(l):
if (a[i]>max):
max=a[i]
A=[10,15,20,30,100,50,60,70,80]
MAX_LIST(A)
P.6 Write a method/user defined function in python SMALL_LIST (A) which finds the
smallest element from the given list A where list A is passed as parameter.
def SMALL_LIST(a):
l=len(a)
small=a[0]
for i in range(l):
if (a[i]<small):
small=a[i]
A=[10,15,20,30,5,50,60,70,80]
P.7 Write a method in python reverse_string(s) ,which reverse the any string ,where s
is passed as parameter.
def reverse_string(s):
sr=' '
for i in s:
sr=i+sr
ST=input("string to be reversed")
reverse_string(ST)
def isPalindrome(s):
for i in range(0,len(s)//2):
if s[i] != s[len(s)-i-1]:
return 0
return 1
ans=isPalindrome(s)
if (ans):
else:
which check the given number n is palindrome or not ,where n is passed as parameter.
def reverse_number_palindrome(n):
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
else:
N=int(input("Enter number:"))
reverse_number_palindrome(N)
P.10 Write a method in python countCharacterType(s), to count total
vowels,consonants,digits and special characters present in a string s,where s is passed as
parameter.
def countCharacterType(s):
vowels = 0
consonant = 0
specialChar = 0
digit = 0
ch = s[i]
ch = ch.lower()
or ch == 'o' or ch == 'u'):
vowels += 1
else:
consonant += 1
digit += 1
else:
specialChar += 1
print("Vowels:", vowels)
print("Consonant:", consonant)
print("Digit:", digit)
print("Special Character:", specialChar)
s = "made in india121"
countCharacterType(s)
P.11 Write a method in python length_string(s),to count the length of the string s, where s is
passed as parameter.
def length_string(s):
count=0
for i in s:
count+=1
print('length of string',count)
length_string(ST)
P12.
#BUBBLE SORTING
def Bsort(a):
n=len(a)
for y in range(1,n):#rounds
for x in range(n-y):#comparisons
a[x],a[x+1]=a[x+1],a[x]#swap statement
print(a)
a=[2,1,4,5,6]
Bsort(a)
P13.
for i in range(n-1):#rounds
a[i],a[j]=a[j],a[i]
print(a)
a=[5,4,3,2,1]
Ssort(a)
*********