Data Structures
Data Structures
🔢 1. Lists
🟩 Question 24 – List Operations
(I)
Q: Write a statement to count the occurrences of 4 in L1 = [1,2,3,2,1,2,4,2,...]
✅ A: L1.count(4)
OR
🌀 2. Tuples
🟩 Question 26 – Tuple Error Correction
Q: The function below is intended to swap the first and last elements of a tuple.
Correct all syntax and logical errors.
Original:
def swap_first_last(tup)
if len(tup) < 2:
return tup
new_tup = (tup[-1],) + tup[1:-1] + (tup[0])
return new_tup
result = swap_first_last((1, 2, 3, 4))
print("Swapped tuple: " result)
✅ Corrected Version:
python
CopyEdit
def swap_first_last(tup):
if len(tup) < 2:
return tup
new_tup = (tup[-1],) + tup[1:-1] + (tup[0],)
return new_tup
📚 3. Dictionaries
🟩 Question 31(A) – Predict Output of Dictionary Iteration
Q:
✅ A:
CopyEdit
15@
7@
9
🧱 4. Stacks
🟩 Question 30(A) – Stack Operations on Book Records
Q: You have a stack named BooksStack containing records [title, author, year].
Write Python functions to:
● Push a book
● Pop a book
✅ A:
def push_book(BooksStack, new_book):
BooksStack.append(new_book)
def pop_book(BooksStack):
if not BooksStack:
print("Underflow")
else:
return BooksStack.pop()
def peep(BooksStack):
if not BooksStack:
print("None")
else:
print(BooksStack[-1])
✅ A:
def push_even(N):
EvenNumbers = []
for num in N:
if num % 2 == 0:
EvenNumbers.append(num)
return EvenNumbers
VALUES = []
for i in range(5):
VALUES.append(int(input("Enter an integer: ")))
EvenNumbers = push_even(VALUES)
def pop_even():
if not EvenNumbers:
print("Underflow")
else:
print(EvenNumbers.pop())
def Disp_even():
if not EvenNumbers:
print("None")
else:
print(EvenNumbers[-1])
📝 Example:
If input list is [10, 5, 8, 3, 12], then stack becomes [10, 8, 12]
1. Lists
Q23(A)
Q: Remove the item with key "NISHA" from the dictionary Students.
✅ A:
python
CopyEdit
Students.pop("NISHA")
# or del Students["NISHA"]
python
CopyEdit
message.count("is")
Q23(B)
python
CopyEdit
subject = list(subject)
subject.pop() # or del subject[-1]
Q: Write PushBig() and PopBig() to handle 5-digit or larger integers from list Nums using a
stack BigNums.
Sample Input:
python
CopyEdit
Nums = [213, 10025, 167, 254923, 14, 1297653, 31498, 386, 92765]
✅ A:
python
CopyEdit
def PushBig(Nums, BigNums):
for N in Nums:
if len(str(N)) >= 5:
BigNums.append(N)
def PopBig(BigNums):
while BigNums:
print(BigNums.pop())
print("Stack Empty")
✅ Expected Output:
mathematica
CopyEdit
92765
31498
1297653
254923
10025
Stack Empty
🔁 2. Tuples
Q23(B)
✅
Q: Convert a tuple to a list and delete the last element.
A (repeated for reference):
python
CopyEdit
subject = list(subject)
subject.pop()
📦 3. Dictionaries
Q21(A)
python
CopyEdit
S = {"AMIT": [92, 86, 64], "NAGMA": [65, 42, 43], "DAVID": [92, 90,
88]}
✅ A:
python
CopyEdit
def showGrades(S):
for K, V in S.items():
avg = sum(V)/3
if avg >= 90:
Grade = "A"
elif avg >= 60:
Grade = "B"
else:
Grade = "C"
print(K, "–", Grade)
✅ Output:
css
CopyEdit
AMIT – B
NAGMA – C
DAVID – A
Q22
python
CopyEdit
LS = ["HIMALAYA", "NILGIRI", "ALASKA", "ALPS"]
D = {}
for S in LS:
if len(S) % 4 == 0:
D[S] = len(S)
for K in D:
print(K, D[K], sep="#")
✅ A:
bash
CopyEdit
HIMALAYA#8
ALPS#4
Q21(B)
✅ A:
python
CopyEdit
def Puzzle(W, N):
result = ""
for i in range(len(W)):
if (i + 1) % N == 0:
result += "_"
else:
result += W[i]
return result
1. Lists
Q2.
Q: Given the list L = [1, 3, 6, 82, 5, 7, 11, 92], what will print(L[2:5]) output?
✅ A: [6, 82, 5]
Q24(a)
Q: Evaluate: 2 * 3 + 4 ** 2 - 5 // 2
✅ A: 20
Q33
python
CopyEdit
def change(A):
S = 0
for i in range(len(A)//2):
S += (A[i]*2)
return S
B = [10,11,12,30,32,34,35,38,40,2]
C = change(B)
print('Output is', C)
✅ A: Output is 190
🌀 2. Tuples
Q5.
● a) print(T[1])
● b) T[2] = -29
● c) print(max(T))
● d) print(len(T))
✅ A:
● Tuples are rows of a table.
● Example:
text
CopyEdit
TNO NAME START END
T1 RAVI KUMAR DELHI MUMBAI
T2 NISHANT JAIN DELHI KOLKATA
📦 3. Dictionaries
Q2 (MCQ):
python
CopyEdit
D = {'Rno': 1, 'Name': 'Suraj'}
print(D('Name'))
📚 4. Stacks
Q37 (a).
Q: Write POP_PUSH(LPop, LPush, N) to pop last N elements from LPop and push to LPush.
✅ A:
python
CopyEdit
def POP_PUSH(LPop, LPush, N):
if len(LPop) < N:
print("Pop not possible")
else:
for i in range(N):
LPush.append(LPop.pop())
Q37 (b).