0% found this document useful (0 votes)
8 views8 pages

Solution

The document contains several Python programming tasks, each with a specific function to perform. Tasks include counting place names with more than 5 characters, left-shifting elements in a list, summing even numbers in a tuple, calculating the average of tuple elements, managing student marks with stack operations, filtering stationary items based on price, and pairing names with ages. Each task is accompanied by a solution function and example input/output for clarity.

Uploaded by

olivia01369631
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Solution

The document contains several Python programming tasks, each with a specific function to perform. Tasks include counting place names with more than 5 characters, left-shifting elements in a list, summing even numbers in a tuple, calculating the average of tuple elements, managing student marks with stack operations, filtering stationary items based on price, and pairing names with ages. Each task is accompanied by a solution function and example input/output for clarity.

Uploaded by

olivia01369631
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Write a method COUNTNOW(PLACES) to find and display those place


names in which there are more than 5 characters after storing the name of
places in a dictionary. For example, if the dictionary PLACES contains:
{'1': "DELHI", '2': "LONDON", '3': "PARIS", '4': "NEW YORK", '5': "DUBAI"}
The following output should be displayed:
LONDON
NEW YORK
Solution
def countNow(PLACES):
for place in PLACES.values():
if len(place) > 5:
print(place.upper())
PLACES = {1: "Delhi", 2: "London", 3: "Paris", 4: "New York", 5: "Doha"}
countNow(PLACES)

Output
LONDON
NEW YORK
2.Write a function LeftShift(lst, x) in Python which accepts numbers in a list
(lst) and all the elements of the list should be shifted to left according to the
value of x.
Sample Input Data of the list lst = [20, 40, 60, 30, 10, 50, 90, 80, 45, 29]
where x = 3
Output lst = [30, 10, 50, 90, 80, 45, 29, 20, 40, 60]
Solution
def LeftShift(lst, x):
if x >= len(lst):
x %= len(lst)
shifted_lst = lst[x:] + lst[:x]
return shifted_lst

lst = eval(input("Enter the list: "))


x = int(input("Enter the value of x: "))
result = LeftShift(lst, x)
print(result)

Output
Enter the list: [20, 40, 60, 30, 10, 50, 90, 80, 45, 29]
Enter the value of x: 3
[30, 10, 50, 90, 80, 45, 29, 20, 40, 60]

Enter the list: [55, 66, 77, 88, 99, 44, 33, 22, 11]
Enter the value of x: 4
[99, 44, 33, 22, 11, 55, 66, 77, 88]
3. Write a method EvenSum(NUMBERS) to add those values in the tuple of
NUMBERS which are even.
Solution
def EvenSum(NUMBERS):
even_sum = 0
for number in NUMBERS:
if number % 2 == 0:
even_sum += number
return even_sum

n = eval(input("Enter the tuple: "))


result = EvenSum(n)
print("The sum of even numbers in the tuple is:", result)

Output
Enter the tuple: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
The sum of even numbers in the tuple is: 30
4. Write a program using user-defined function to calculate and display
average of all the elements in a user-defined tuple containing numbers.
Solution
def calculate_average(num):
total = 0
for element in num:
total += element
average = total / len(num)
return average

n = eval(input("Enter the tuple: "))


average = calculate_average(n)
print("The average of the elements in the tuple is:", average)

Output
Enter the tuple: (2, 4, 6, 8, 10)
The average of the elements in the tuple is: 6.0
5. A dictionary stu contains rollno and marks of students. Two empty lists
stack_roll and stack_mark will be used as Stack. Two functions Push_stu()
and Pop_stu() are defined and perform the following operation:
Push_stu(): It reads dictionary stu and adds keys into stack_roll and values
into stack_marks for all students who secured more than 60 marks.
Pop_stu(): It removes last rollno and marks from both list and prints
"underflow" if there is nothing to remove.
For example,
stu={ 1 : 56, 2 : 45, 3 : 78, 4 : 65, 5 : 35, 6 : 90 }
Values of stack_roll and stack_mark after push_stu() function:
[3, 4, 6] and {78, 65, 90}
#Solution

def Push_stu(stu, stack_roll, stack_mark):


for rollno, marks in stu.items():
if marks > 60:
stack_roll.append(rollno)
stack_mark.append(marks)

def Pop_stu(stack_roll, stack_mark):


if stack_roll and stack_mark:
print("Removed rollno:", stack_roll.pop())
print("Removed marks:", stack_mark.pop())
else:
print("Underflow - Nothing to remove")

stu = {1: 56, 2: 45, 3: 78, 4: 65, 5: 35, 6: 90}


stack_roll = []
stack_mark = []

Push_stu(stu, stack_roll, stack_mark)


print("Values of stack_roll and stack_mark after Push_stu() function:")
print(stack_roll, stack_mark)

print("\nThe output of first execution of Pop_stu() is:")


Pop_stu(stack_roll, stack_mark)
6. write a program in python ,push(sitem)where sitem is dictinary
containing details of stationary items{sname:price}.s
#the function should push push the names of those items in the stack which
have price > 75,display the count of elements pushed into the stack.
for example:
sitem{"pen":106,"pencil":55,"notebook":80,"eraser",25}
the stack should contain:
notebook
pen
the output should be:
the of elements in the stack is 2

def push(sitem, stackitem):


count=0
for k in sitem:
if (sitem[k]>=75):
stackitem.append(k)
count=count+1
print("the count of elements in the stack is :",count)
stackitem=[]
sitem={"pen":106,"pencil":55,"notebook":80,"eraser",25}
push(sitem, stackitem)
7.Two lists Lname and Lage contain name of person and age of person
respectively. A list named Lnameage is empty. Write functions as per details
given below:
1. Push_na(): It will push the tuple containing pair of name and age
from Lname and Lage whose age is above 50.
2. Pop_na(): It will remove the last pair of name and age and also print
name and age of the removed person. It should also print "underflow"
if there is nothing to remove.
For example, the two lists have the following data:
Lname=['Narender', 'Jaya', 'Raju', 'Ramesh', 'Amit', 'Piyush' ]
Lage= [45, 23, 59, 34, 51, 43]
After Push_na() the Lnameage Stack contains:
[('Raju', 59), ('Amit', 51)]
The output of first execution of Pop_na() is:
The name removed is Amit
The age of person is 51
Solution
def Push_na(Lname, Lage, Lnameage):
for i in range(len(Lname)):
if Lage[i] > 50:
Lnameage.append((Lname[i], Lage[i]))

def Pop_na(Lnameage):
if Lnameage:
removed_name, removed_age = Lnameage.pop()
print("The name removed is", removed_name)
print("The age of person is", removed_age)
else:
print("Underflow - Nothing to remove")

Lname = ['Narender', 'Jaya', 'Raju', 'Ramesh', 'Amit', 'Piyush']


Lage = [45, 23, 59, 34, 51, 43]
Lnameage = []

Push_na(Lname, Lage, Lnameage)


print("After Push_na() the Lnameage Stack contains:")
print(Lnameage)
print("\nThe output of first execution of Pop_na() is:")
Pop_na(Lnameage)
Output
After Push_na() the Lnameage Stack contains:
[('Raju', 59), ('Amit', 51)]

The output of first execution of Pop_na() is:


The name removed is Amit
The age of person is 51

You might also like