0% found this document useful (0 votes)
51 views5 pages

Worksheet - 2 - Functions WAP

C s

Uploaded by

Sana
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)
51 views5 pages

Worksheet - 2 - Functions WAP

C s

Uploaded by

Sana
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/ 5

XII – COMPUTER SCIENCE

WORKSHEET – 2023 -24


FUNCTIONS
WAP Questions :
Q01. Write a function lenFOURword(L), where L is the list of elements
(list of words) passed as argument to the function. The function
returns another list named ‘indexList’ that stores the indices of all
four lettered word of L.
Ans def lenFOURword(L):
indexList=[]
for i in range(len(L)):
if len(L[i])==4:
indexList.append(i)
return indexList
print(lenFOURword(["ME","FINE","PYTHON","MY, "WELL"]))
Q02. Write a function modilst(L) that accepts a list of numbers as argument
and increases the value of the elements by 10 if the elements are
divisible by 5. Also write a proper call statement for the function.
For example: If list L contains [3,5,10,12,15]. Then the modilist()
should make the list L as [3,15,20,12,25]
Ans def modilst(L):
for i in range(len(L)):
if L[i] % 5 == 0:
L[i]+=10
L = [12,10,15,20,25]
modilst(L)
print(L)
Q03. 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. Example: If L contains [12,4,0,11,0,56]
The indexList will have [0,1,3,5]
Ans def Index_List(L):
indexlist=[]
for i in range(len(L)):
if L[i] != 0:
indexlist.append(i)
return indexlist
L = [2,5,0,6,4,0,0,8]
print(Index_List(L))
Q04. Write a function LeftShift(Numlist,N) in python, which accepts a list of
numbers Numlist and N is a numeric values by which all the elements
of the list are shifted to the left.
Sample Input and Output list below:
Input Data: Numlist=[10,20,30,40,50] and N is 2
Output Data: Numlist=[30,40,50,10,20]
Ans def LeftShift(numlist, n):
L = len(numlist)
for x in range(0,n):
y = numlist[0]
for i in range(0,L-1):
numlist[i] = numlist[i+1]
numlist[L-1] = y
nlist = [12,25,34,90,55,70]
LeftShift(nlist,2)
print(nlist)
(or) using slicing
def Lshift(L,N):
L = L[N:]+L[:N]
print(L)
L1= [2,4,6,8,0,10,12]
Lshift(L1,2)
Q05. Write a function SQUARE_LIST(L), where L is the list of elements
passed as argument to the function. The function returns another list
named ‘SList’ that stores the Squares of all Non-Zero Elements of L
Example: If L contains [9,4,0,11,0,6,0]
The SList will have- [81,16,121,36]
Ans def SQUARE_LIST(L):
SList=[]
for i in L:
if i!= 0:
SList.append(i*i)
return SList
L=[4,2,0,5,0,6,0,0,12]
print(SQUARE_LIST(L))
Q06. Write a function in Python Convert() to replaces element shaving even values
with its half and elements having odd values with twice its value in a list.
eg: If the list contains 3,4,5,16,9 the rearranged list as 6,2,10,8,18
Ans def convert(l1):
for i in range(0,len(l1)):
if l1[i]%2==0:
l1[i]=l1[i]//2
else:
l1[i]=l1[i]*2
print(l1)
l1=[3,4,5,16,9]
convert(l1)
Q07. Write a function AdjustList(L), where L is a list of integers. The
function should reverse the contents of the list without slicing the list
and without using any second list.
Example: If the list initially contains [2,15,3,14,7,9,19,6,1,10]
Then after reversal the list should contain [10,1,6,19,9,7,14,3,15,2]
Ans def AdjustList(list1):
print(list(reversed(list1)))
L=[3,4,5,6,2,1]
AdjustList(L)
Q08. Write a function EVEN_LIST(L), where L is the list of elements passed
as argument to the function. The function returns another list named
‘evenList’ that stores the indices of all even numbers of L.
Example: If L contains [12,4,3,11,13,56] The evenList will have - [0,1,5]
Ans def EVEN_LIST(L):
evenList=[]
for i in range(len(L)):
if L[i]%2==0:
evenList.append(i)
return(evenList)
L1=[13,55,12,63,10,95,34]
print(EVEN_LIST(L1))
Q09. Write definition of a method/function DoubletheOdd( ) to add and
display twice the sum of odd values from the list of Nums.
For example : If the list Nums contains [25,24,35,20,32,41]
The function should display Twice of Odd Sum: 202
Ans def DoubletheOdd( ):
Nums=[25,24,35,20,32,41]
s=0
for i in Nums :
if i%2!=0:
s+=i*2
print("Double the sum of odd values:",s)
DoubletheOdd( )
Q10. Write a function in python named SwapHalfList(Array), which accepts
a list of numbers and swaps the elements of 1st Half of the list with the
2nd Half of the list, ONLY if the sum of 1st Half is greater than 2nd
Half of the list.
Sample Input Data of the list Array= [ 100, 200, 300, 40, 50, 60],
Output Array=[40,50,60,100,200,300]
Ans def SwapHalfList(Arr):
s1=s2=0
L=len(Arr)
for i in range(0,L//2):
s1+=Arr[i]
for i in range(L//2, L):
s2+=Arr[i]
if s1>s2:
for i in range(0,L//2):
Arr[i],Arr[i+L//2]=Arr[i+L//2],Arr[i]
L=[6,5,4,1,2,3]
SwapHalfList(L)
print(L)
Q11. 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 Elements of L which
has a even unit place digit.
For example: If L contains [12,4,15,11,9,56]
The indexList will have - [0,1,5]
Ans def INDEX_LIST(L):
indexList = [ ]
for i in range(0,len(L)):
if (L[i]%10)%2 == 0:
indexList.append(i)
return indexList
print(INDEX_LIST([10,34,25,33,65]))

You might also like