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

Udf List String Programs

The document describes 13 Python programs (P1-P13) that implement various list and string processing functions using user-defined functions. The functions include calculating sum and average of a list (P1), reversing a list (P2), reversing halves of a list (P3), reversing elements in pairs (P4), finding maximum/minimum of a list (P5/P6), reversing a string (P7), checking palindrome for string/number (P8/P9), counting character types (P10), getting string length (P11), implementing bubble sort (P12), and selection sort (P13).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Udf List String Programs

The document describes 13 Python programs (P1-P13) that implement various list and string processing functions using user-defined functions. The functions include calculating sum and average of a list (P1), reversing a list (P2), reversing halves of a list (P3), reversing elements in pairs (P4), finding maximum/minimum of a list (P5/P6), reversing a string (P7), checking palindrome for string/number (P8/P9), counting character types (P10), getting string length (P11), implementing bubble sort (P12), and selection sort (P13).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Python : UDF(List – 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

print('new reversed list is',a)

A=[10,15,20,30,40,50,60,70,80]

print('original list is',A)

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

print('new reversed FIRST HALF WITH SECOND HALF list is',a)

A=[10,15,20,30,40,50,60,70,80]

print('original list is',A)

reverse_list_FH_SH(A)

P.4 Write a method/user defined function in python reverse_list_first_with_second


(A) which reverse first element with second element of the given list A and third with
fourth element ---so on where list A is passed as parameter.

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

print('new reversed list is',a)


A=[10,15,20,30,40,50,60,70,80]

print('original list is',A)

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]

print('biggest element from the list is',max)

A=[10,15,20,30,100,50,60,70,80]

print('original list is',A)

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]

print('smallest element from the list is',small)

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

print('new reversed string is',sr)

ST=input("string to be reversed")

reverse_string(ST)

P.8 Write a method in python isPalindrome(s),which check the given string s is


palindrome or not ,where s is passed as parameter.

def isPalindrome(s):

for i in range(0,len(s)//2):

if s[i] != s[len(s)-i-1]:

return 0

return 1

s=input("enter any string")

ans=isPalindrome(s)

if (ans):

print("given string is palindrome")

else:

print("given string is not palindrome")


P.9 Write a method in python reverse_number_palindrome(n):

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):

print("The number is a palindrome!")

else:

print("The number isn't a palindrome!")

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

for i in range(0, len(s)):

ch = s[i]

if ( (ch >= 'a' and ch <= 'z') or

(ch >= 'A' and ch <= 'Z') ):

ch = ch.lower()

if (ch == 'a' or ch == 'e' or ch == 'i'

or ch == 'o' or ch == 'u'):

vowels += 1

else:

consonant += 1

elif (ch >= '0' and ch <= '9'):

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)

ST=input(" enter any string ")

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

if a[x]>a[x+1]:#< for descending order

a[x],a[x+1]=a[x+1],a[x]#swap statement

print(a)
a=[2,1,4,5,6]

Bsort(a)

P13.

def Ssort(a):#Selection Sort

for i in range(n-1):#rounds

for j in range(i+1,n): #comparisons

if a[i]>a[j]:#< for descending

a[i],a[j]=a[j],a[i]

print(a)

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

Ssort(a)

By: R K Tiwari Sir

*********

You might also like