sorting)
m
(iv) Write the code to delete 17 from the above sorted list
10 Write the function sumAlternate(MYLIST) as argument and calculate the sum of all alternate
elements of MYLIST and print it
For e.g. if the elements are
5 11 17 19 25 29 30 32 56 90
Output should be :
Total = 133
11 Write the function SumEvenOdd(MYLIST) to find the sum of all Even elements and sum of all Odd
elements present in MYLIST
For e.g if the elements are
8 12 17 19 25 29 33 32 56 90
Output should be:
Even Sum = 198 Odd
Sum = 123
12 Write the function CountEvenOdd(MYLIST) to find the count of all Even elements and sum of all
Odd elements.
For e.g if the elements are
8 12 17 19 25 28 33 32 56 90
Output should be:
Even Count = 6 Odd
Sum = 4
13 Write code using list comprehension for the following:
(i) To create a list(mylist) with all even numbers from 2 to 100
(ii) To create a list(mylist) with all alternate values of another list(templist)
Ans (i) mylist = [i for i in range(2,101) if i%2==0]
(ii) templist=[10,20,30,40,50,60,70,80,90,100]
mylist = [templist[i] for i in range(0,len(templist),2) ]
14 Write a function PrintDiagonal(MATRIX), where MATRIX is a list storing 3 list
inside it with each list contains 3 items and print only diagonal elements and
also sum of it.
For e.g. is the MATRIX element is [[10,20,30],[40,50,60],[70,80,90]]
Output should be
10
50
90
Sum=150
Page | 1
15 Write a function EvenOdd(MYLIST), which doubles each Odd elements of MYLIST and half each
Even element of MYLIST.
For e.g. if MYLIST = [10,11,40,4,17,23,45,100,80]
The output should be
[5,22,20,2,34,46,90,50,40]
16 Write a function Sum7End(MYLIST), which display only those items from the list which ends from
the digit 7, also find total of these elements.
For e.g. if MYLIST = [10,27,15,107,97,5,7,81,47]
The output should be
27
107
97
7
47
Total = 285
18 Write a function in python, Push(Employee) and Pop(Employee) to add a new Employee and delete a
Employee from a List of Employee Names, considering them to act as push and pop operations of the
Stack data structure.
Ans def Push(Employee):
name=input('Enter Employee name ')
[Link](name)
def Pop(Employee):
if len(Employee)==0: # or if Employee==[]:
print('Underflow')
else:
name = [Link]() print('Popped
Name was ',name)
20 Write a function in python, Push(Employee) and Show(Employee) to add a new Employee and display
Employee names from a List of Employee, considering them to
act as push and show operations of the Stack data structure.
21 Write a function Count(Salary,Val) to count and display number of times Val is present in the list
Salary.
For example
If the Salary contains: 50000,40000,50000,60000,70000,50000,1000
and Val contains 50000
The function should display
50000 found 3 Times
22 Consider the randomly ordered numbers stored in a list
55,53,57,51,52,54,56
Show the content of list after the First, Second and third pass of the Bubble sort method used for arranging
in descending order
Note: show the status of all elements after each pass very clearly encircling the changes.
23 Consider the randomly ordered numbers stored in a list
55,53,57,51,52,54,56
Show the content of list after the First, Second and third pass of the Insertion sort method used for
arranging in ascending order
Note: show the status of all elements after each pass very clearly encircling the changes.
24 Consider the randomly ordered numbers stored in a list
55,53,57,51,52,54,56
Show the content of list after the First, Second and third pass of the Bubble sort method used for arranging
in descending order
Note: show the status of all elements after each pass very clearly encircling the changes.
25 Raj is a Python programmer working on sorting module. For a small list of values he has written the
Bubble sorting code but code is not executing. Help Raj and rewrite the code after removing all the errors
and underlining the correction(s) made:
def BubbleSort(num):
for i in range(num-1):
for j in range(num-1-i):
if num[j+1]>num[j]:
num[i],num[j+1]=num[j+1],num[i]
26 Raj is a Python programmer working on sorting module. For this he has written the Insertion sorting code
but code is not executing. Help Raj and rewrite the code after removing all the errors and underlining the
correction(s) made:
def InsertionSort(mylist): i=1
while i<len(mylist):
key=mylist[i]
j=i-1
while j>=0 and mylist[j]<key: mylist[j]=mylist[j+1]
j-=1
key = mylist[j+1] i-
=1
27 Write a Python function/method Count5and7(N), to find and display count of all number between 1
to N which are either divisible by 5 or by 7.
For e.g. if the N is 20 then output should be:
Count=6
As (5,7,10,14,15,20) are the number between 1 to N which are divisible by either 5 or by 7)
28 Write a Python function/method SwapMiddle(Codes) to swap the first half of the content of the list
Codes with second half of the list Codes and display the swapped values.
For e.g. if the list Codes contains : [22,44,55,66,88,11] then function should swap and display as:
[66,88,11,22,44,55]
29 Raj is a Python programmer working of Data structure Stack to store name of visitors, he has
implemented the code for PUSH and POP, but both functions are not producing the correct result. Help
Raj in identifying the error(s) and also write the Correct code for specific line number:
def PUSH(VISITOR,name):
[Link](VISITOR) #Line 1 top =
len(VISITOR)-1 #Line 2
def POP(VISITOR):
if len(VISITOR)==1: #Line 3
return "Sorry! No Visitor to delete " #Line 4
else:
val = [Link](1) #Line 5
if len(VISITOR)==1: #Line 6
top=None #Line 7
else
top=len(VISITOR) #Line 8 return
val
#Line 3
if len(VISITOR)==0:
#Line 5
val = [Link]() #Line 6
if len(VISITOR)==0:
#Line 8
top = len(VISITOR)-1
30 Raj is a Python programmer working of Data structure Queue to store name of REQUESTNO, he has
implemented the code for ENQUEUE and DEQUEUE, for insert and delete operation in Queue resp. But
both functions are not producing the correct result. Help Raj in identifying the Line Number where code
is incorrect and also write the Correct code for same Line Number.
def Enqueue(REQUESTNO,item):
[Link](item) #Line 1 if
len(REQUESTNO)==1: #Line 2
front=rear=1 #Line 3
else:
rear=len(REQUESTNO)-1 #Line4
def Dequeue(REQUESTNO):
if REQUESTNO==0: #Line 5
print("Underflow") #Line 6
else:
val = [Link](len(REQUESTNO)-1) #Line 7
Ans
if len(REQUESTNO)==0: #Line 8
front=None #Line 9
return val
#Line 1 [Link](item) #Line 3
front = rear = 0 #Line 5
if len(REQUESTNO)==0:
#Line 7
val = [Link](0) #Line 9
front = rear = None