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

Week7 Lab Q

Uploaded by

satikeshva
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)
14 views5 pages

Week7 Lab Q

Uploaded by

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

Department of CSE, PES University

UE24CS151A-Python for Computational Problem Solving


Laboratory Week 7

Problem Statements

1. Event Attendance:

A school has two events, and the attendees are stored in two sets. Create 2 sets
with names of students. Write a program to:

● Find the students who attended both events.


● Find the students who attended only one of the events.
● Find all students who attended at least one event.

2. Student Marks Calculation:

You are given a dictionary containing student names as keys and their marks as
values.

students = {

"Rohan": 85,

"Spoorthi": 90,

"Aditi": 78,

"Tanya": 92

Write a program to:

● Find the student with the highest marks.


● Calculate the average marks for the class.
● Add a new student and their marks to the dictionary.
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 7

3. Sentence Analysis:

Given a sentence, write a program to:

● Count the number of vowels and consonants.


● Find the longest word in the sentence.
● Reverse the sentence.

4. Given a string, count the number of occurrences of each character and store the result in
a dictionary.

Example Input:
"hello world"

Expected Output:
{'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}

5. Merge two dictionaries into one. If a key appears in both dictionaries, the value in the
second dictionary should overwrite the one in the first.

Example Input:
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}

Expected Output:
{'a': 1, 'b': 3, 'c': 4}

6. You work at a movie theater that keeps track of daily bookings. Each booking
record contains the customer's name, selected movie, and seat number in a specific
format. The theater needs to analyze these booking records to manage seating and
prevent duplicate bookings. Take input from the user.

Input format - "CustomerName-MovieName-SeatNumber"


Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 7

Hints:

● Consider using string methods like split(), replace()


● Dictionary and set data structures may be used for tracking duplicates.

7. Password Validator

Define a simple password validator. The password must:

● Be 8 characters long
● Must contain 1 Upper case character, 1 lower case character and 1 number
● If valid return valid password if not print invalid

8. Given a dictionary where values are unique, swap the keys and values.
Example Input:
{'one': 1, 'two': 2, 'three': 3}
Expected Output:
{1: 'one', 2: 'two', 3: 'three'}

9. You are given a list of student names and a corresponding list of their scores. Create a
dictionary that categorizes the students into three groups: "Excellent" (score 80 and
above), "Good" (score between 60 and 79), and "Needs Improvement" (score below 60).
Output the dictionary with the categories as keys and lists of student names as values.

Steps to Solve:

1. Create an empty dictionary with the keys: "Excellent", "Good", and "Needs
Improvement".
2. Iterate through the list of students and their scores.
3. Based on the score, categorize each student and append their name to the corresponding
list in the dictionary.

Example Input:

students = ["Alice", "Bob", "Charlie", "David", "Eva", "Frank"]


scores = [85, 67, 45, 92, 73, 55]
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 7

Expected Output:

"Excellent": ["Alice", "David"],

"Good": ["Bob", "Eva"],

"Needs Improvement": ["Charlie", "Frank"]


}

Solution Explanation:

1. Initialize an empty dictionary with the three categories.


2. Iterate over the students and scores using for loop.
3. Depending on the score, add the student's name to the appropriate category in the
dictionary.

10. Take a dictionary with product names as keys and prices as values. Print each product
and its price. If the price is more than $20, add a message: "Expensive".
11. Create a program that generates a list of common elements between two sets, but only if
those elements are divisible by both 2 and 3. Print the final list of common elements.
Example Input:
set1 = {2, 3, 6, 12, 15, 18, 20, 24}
set2 = {1, 6, 12, 18, 24, 30, 36}
Example Output:
[6, 12, 18, 24]

12. Given a list of numbers, create a new list where each element is the sum of all elements
from the original list except itself. For example, given [1, 2, 3, 4], the output should be [9,
8, 7, 6].
13. Write a program that takes a string as input and extracts all the unique consonants from
the string. Store the consonants in a set to ensure each consonant is unique. If no
consonants are found, print "No consonants found". Consonants are defined as
alphabetic characters that are not vowels (a, e, i, o, u, case-insensitive).
Example Input 1:
string = "OpenAI"
Example Output 1:
{'p', 'n'}

14. Given a dictionary of product prices, find and print the product with the highest price. If
there is a tie, print all products with the highest price.
Department of CSE, PES University
UE24CS151A-Python for Computational Problem Solving
Laboratory Week 7

products = {
"Laptop": 1200,
"Smartphone": 800,
"Tablet": 1200,
"Headphones": 150
}

"Smartphone": 800,
"Tablet": 1200,
"Headphones": 150
}

You might also like