FUNCTIONS PROGRAM BASED QUESTIONS
1. Write a function countNow(PLACES) in Python, that takes the
dictionary, PLACES as an argument and displays the names (in uppercase)
of the places whosenames are longer than 5 characters.
For example, Consider the following dictionary
PLACES={1:"Delhi",2:"London",3:"Paris",4:"New York",5:"Doha"}
The output should be:
LONDON NEW YORK
PLACES={1:"Delhi",2:"London",3:"Paris",4:"NewYork",5:"Doha"}
def countNow(PLACES):
for i in PLACES:
if len(PLACES[i])>5:
print(PLACES[i].upper())
countNow(PLACES)
2. Write a function, lenWords(STRING), that takes a string as an
argument and returns a tuple containing length of each word of a string.
For example, if the string is "Come let us have some fun", the tuple will
have (4, 3, 2, 4, 4, 3)
STRING="Come let us have some fun"
def lenWords(STRING):
words=STRING.split()
t=()
for word in words:
L=len(word)
t=t+(L,)
return t
T=lenWords(STRING)
print(T)
3. Write a function EOReplace() in Python, which accepts a list L of
numbers. Thereafter, it increments all even numbers by 1 and
decrements all odd numbers by 1.
Example
If Sample Input data of the list is : L=[10,20,30,40,35,55]
Output will be L=[11,21,31,41,34,54]
89
L=[10,20,30,40,35,55]
def EOReplace(L):
for i in range(len(L)):
if L[i]%2==0:
L[i]=L[i]+1
else:
L[i]=L[i]-1
print(L)
EOReplace(L)
4. Write a function search_replace() in Python which accepts a list L
of numbers and a number to be searched.
If the number exists, it is replaced by 0 and if the number does not
exist, an appropriate message is displayed.
Example
L = [10,20,30,10,40]
Number to be searched = 10
List after replacement
L = [0,20,30,0,40]
L = [10,20,30,10,40]
N=10
def search_replace(L,N):
found=0
for i in range(len(L)):
if L[i]==N:
L[i]=0
found=1
if found==0:
print(N,"Element not exist...")
print(L)
search_replace(L,N)
5. Write the definition of a function Sum3(L) inPython, which accepts
a list L of integers and displays the sum of all such integers from the
list L which end with the digit 3.
For example, if the list L is passed [ 123, 10, 13,15, 23]
Then the function should display the sum of 123, 13,
23 i.e. 159 as follows: Sum of integers ending withdigit 3 = 159
Method 1
L=[ 123, 10, 13, 15, 23]
def Sum3(L):
S=0
for i in L:
if i%10==3:
S=S+i
print("Sum of integers ending with digit 3 =",S)
Sum3(L)
Method 2
L=[ 123, 10, 13, 15, 23]
def Sum3(L):
S=0
for i in range(len(L)):
if L[i]%10==3:
S=S+L[i]
print("Sum of integers ending with digit 3 =",S)
Sum3(L)
6. Write a function INDEX_LIST(L), where L is the list of elements
passed as argument to the function. The function returns another list
named indexList that stores the indices of all Non-Zero Elements of
L.
For example:
If L contains [12,4,0,11,0,56]
The indexList will have - [0,1,3,5]
L=[12,4,0,11,0,56]
def INDEX_LIST(L):
indexList=[]
for i in range(len(L)):
if L[i]!=0:
indexList.append(i)
return indexList
R=INDEX_LIST(L)
print(R)
7. Kritika was asked to accept a list of even numbers but she did not
put the relevant condition while accepting the list of numbers.
You are required to write a user defined function oddtoeven(L) that
accepts the List L as an argument and convert all the odd numbers into
even by multiplying them by 2.
def oddtoeven(L):
for i in range(len(L)):
if L[i]%2!=0:
L[i]=L[i]*2
print(L)
L=[23,4,3,5,1,2]
oddtoeven(L)
8. Write a method in python to display the elements of list thrice if it
is a number and display the element terminated with '#' if it is not a
number.
For example, if the content of list is as follows:
ThisList=['41','DROND','GIRIRAJ','13','ZARA']
414141
DROND#
GIRlRAJ#
131313
ZARA#
ThisList = ['41', 'DROND', 'GIRIRAJ', '13', 'ZARA']
def Convert(ThisList):
for i in ThisList:
if i.isdigit():
print(i*3)
else:
print(i+'#')
Convert(ThisList)
9. Write definition of a method OddSum(NUMBERS) to add those
values from the list of NUMBERS, which are at odd positions.
#Method 1
def OddSum(NUMBERS):
S=0
for i in range(len(NUMBERS)):
if i%2!=0:
S=S+NUMBERS[i]
print("Sum of Values at odd position=",S)
NUMBERS=[2,4,5,2,4,5]
OddSum(NUMBERS)
#Method 2
def OddSum(NUMBERS):
S=0
for i in range(1,len(NUMBERS),2):
S=S+NUMBERS[i]
print("Sum of Values at odd position=",S)
NUMBERS=[2,4,5,2,4,5]
OddSum(NUMBERS)
10. Write definition of a Method MSEARCH(STATES) to display all
the state names from a list of STATES, which are starting with
alphabet M.
For example:
If the list STATES contains
["MP","UP","WB","TN","MH","MZ","DL","BH","RJ","HR"]
The following should get displayed
MP
MH
MZ
#Method 1
STATES=["MP","UP","WB","TN","MH","MZ","DL","BH","RJ",
"HR"]
def MSEARCH(STATES):
for i in STATES:
if i.startswith('M'):
print(i)
MSEARCH(STATES)
#Method 2
def MSEARCH(STATES):
for i in STATES: if i[0]=='M':
print(i)
MSEARCH(STATES)
11. Write definition of a method Endingwith5(SCORES) to add all
those values in the list of SCORES, which are ending with 5 and
display the sum.
For example,
If the SCORES contain [205,506,365,100,230,335]
The sum should be displayed as 905
SCORES=[205,506,365,100,230,335]
def Endingwith5(SCORES):
S=0
for i in SCORES:
if i%10==5:
S=S+i
print(S)
Endingwith5(SCORES)
12. Write definition of a Method COUNTNOW(REGIONS) to find
and display names of those REGIONS, in which there are less than or
equal to 5 characters.
For example :If the list REGIONS contains ["GOA","NEW
DELHI","DAMAN","CHENNAI","BANGALORE"]
The following should get displayed
GOA
DAMAN
REGIONS=["GOA","NEW DELHI","DAMAN","CHENNAI",
"BANGALORE"]
def COUNTNOW(REGIONS):
for i in REGIONS:
if len(i)<=5:
print(i)
COUNTNOW(REGIONS)
13. Write definition of a method/function FindOut(Names, HisName)
to search for HisName string from a list Names, and display the
position of its presence.
For example
If the Names contain ["Arun","Raj","Tarun","Kanika"] and
HisName contains "Tarun"
The function should display Tarun at 2
Names=["Arun","Raj","Tarun","Kanika"]
HisName="Tarun"
def FindOut(Names, HisName):
found=0
for i in range(len(Names)):
if Names[i]==HisName:
print(HisName,'at',i)
found=1
if found==0:
print("Sorry,",HisName,'not exist.')
FindOut(Names,HisName)
14. Write definition of a method/function TenTimesEven(VALUES)
to add and display the sum of ten times of the even values present in
the list of VALUES .
For example,
If the Nums contain [5,2,3,6,3,4] The method/function should display
Even Sum: 120
(i.e. 2x10 + 6x10 + 4x10 )
def TenTimesEven(VALUES):
S=0
for i in VALUES:
if i%2==0:
S=S+i*10
print("Even Sum:",S)
Nums= [5,2,3,6,3,4]
TenTimesEven(Nums)
15. Write definition of a method/function EndingA(Names) to search
and display those strings from the list of Names, which are ending
with 'A’.
For example,
If the Names contain ["JAYA","KAREEM","TARUNA","LOVISH"]
The method/function should display
JAYA
TARUNA
#Method 1
def EndingA(Names):
for i in Names:
if i.endswith('A'):
print(i)
Names=["JAYA", "KAREEM", "TARUNA", "LOVISH"]
EndingA(Names)
#Method 2
def EndingA(Names):
for i in Names:
if i[-1]=='A':
print(i)
Names=["JAYA", "KAREEM", "TARUNA", "LOVISH"]
EndingA(Names)
16. Write a python method/function Swapper(Numbers) to swap the
first half of the content of a list Numbers with second half of the
content of list Numbers and display the swapped values.
Note: Assuming that the list has even number of values in it For
Example
If the list Numbers contain [35,67,89,23,12,45]
After swapping the list content should be displayed as
[23,12,45,35,67,89]
def Swapper(Numbers):
mid=len(Numbers)//2
for i in range(0,mid):
Numbers[i],Numbers[mid+i]=Numbers[mid+i],Numbers[i]
print(Numbers)
Numbers=[35,67,89,23,12,45]
Swapper(Numbers)
17. Write a function LShift(Arr,n) in Python, which accepts a list Arr
of numbers and n is a numeric value by which all elements of the list
are shifted to left.
Sample Input Data of the list
Arr= [ 10,20,30,40,12,11], n=2
Output Arr = [30,40,12,11,10,20]
def LShift(Arr,n):
L=len(Arr)
for x in range(0,n):
y=Arr[0]
for i in range(0,L-1):
Arr[i]=Arr[i+1]
Arr[L-1]=y
print(Arr)
Arr=[10,20,30,40,12,11]
n=2
LShift(Arr,n)