Saturday, 29 November 2025
Solution
📘 SET – 1 — ANSWER KEY
Context: Library / Books
Q1 — Python Functions (4 × 2 = 8 marks)
a) COUNT_VOWELS()
def COUNT_VOWELS(s: str) -> dict:
vowels = "aeiouAEIOU"
d = {}
for ch in s:
if ch in vowels:
key = ch.lower() # treat uppercase and lowercase as same
d[key] = d.get(key, 0) + 1
return d
# Example:
print(COUNT_VOWELS("Education"))
# Expected:
# {'e':1, 'u':1, 'a':1, 'i':1, 'o':1}
b) SAVE_VOWEL_DATA()
import pickle
def SAVE_VOWEL_DATA(s: str, filename="VOWELS.dat"):
data = COUNT_VOWELS(s)
with open(filename, "wb") as f:
pickle.dump(data, f)
print("Vowel data saved to", filename)
# Example call:
SAVE_VOWEL_DATA("Education")
# Expected output message:
# Vowel data saved to VOWELS.dat
Q2 — SQL (4 marks)
Database: LIBRARY
1. Display title and total fees collected for each book
SELECT B.Title,
COALESCE(SUM(I.Fees), 0) AS TotalFees
FROM BookDetails B
LEFT JOIN BookIssue I
ON B.BookId = I.BookId
GROUP BY B.Title;
Expected result:
Time Machine 20
Invisible Man 20
Brief History 70
Hamlet 25
2. IssueDate of all Science books
SELECT I.IssueDate, B.Title
FROM BookIssue I
JOIN BookDetails B
ON I.BookId = B.BookId
WHERE B.Genre = 'Science';
Expected result:
2025-01-05 Brief History
2025-01-20 Brief History
3. Delete all Drama books not issued
DELETE FROM BookDetails
WHERE Genre = 'Drama'
AND BookId NOT IN (SELECT BookId FROM BookIssue);
(Hamlet is issued, so it will NOT be deleted.)
4. Update author of "Time Machine"
UPDATE BookDetails
SET Author = 'Herbert Wells'
WHERE Title = 'Time Machine';
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment