100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Python - Lists, Tuples, Dictionaries practice tests with solutions $5.49   Add to cart

Exam (elaborations)

Python - Lists, Tuples, Dictionaries practice tests with solutions

 3 views  0 purchase
  • Course
  • Institution

This document contains all the exam questions with solutions related to Functions in Lists, Tuples and Dictionaries in the python coding language **Its recommended to solve the questions first and then compare with the official solutions.**

Preview 2 out of 13  pages

  • November 4, 2021
  • 13
  • 2021/2022
  • Exam (elaborations)
  • Questions & answers
avatar-seller
FUNCTIONS AND LISTS

Problem Definition :
Define functions:
• REPLACE(L,N) – this function takes a list L containing integers and an
integer N as parameters and increases the value of all odd elements by N and
decreases the value of all even elements by N.
• EXCHANGE(L) – this function takes a list L containing integers and
exchanges the first half elements of the list with the second half. ( Note : - if
the list has 6 elements then exchange first 3 elements with the last 3
elements and in case the list has 7 elements then exchange first 3 elements
with the last 3 elements leaving the middle element untouched.)
Both the functions will not display the list but only perform the task

Write a menu driven program in python which displays a menu to the user and
depending upon the user’s choice invokes the above mentioned functions and
displays the modified list.

PROGRAM CODE:

def replace(l,n):
for i in l:
if i%2==1:
i+=n
else:
i-=n
print(i)

def exchange(l):
mid=len(l)//2
for i in range(0,mid):
l[i],l[mid+i]= l[mid+i],l[i]
print(l)

, while True:
print("""MENU:
1.ADD ODD AND SUBTRACT EVEN
2.REPLACES FIRST HALF WITH SECOND HALF
3.EXIT""")

ch=int(input('enter choice'))
if ch==1:
l=eval(input("Enter elements of ure choice"))
n=int(input("Enter even or odd"))
replace(l,n)
elif ch==2:
l=eval(input("Enter elements of ure choice"))
exchange(l)
elif ch==3:
break
print()


O/P:

MENU:
1.ADD ODD AND SUBTRACT EVEN
2.REPLACES FIRST HALF WITH SECOND HALF
3.EXIT

enter choice1

Enter elements of ure choice[5,9,6,7,2,6,4]

Enter even or odd9
-5

MENU:

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller Stevendurby. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $5.49. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

72841 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$5.49
  • (0)
  Add to cart