Class 12 Preatical File
Class 12 Preatical File
Solution
return amount_in_rupees
#__main__
if y==True:
print("String is Palindrome")
else:
print("String is Not Palindrome")
STACKS
Write the following user defined functions in Python and perform the
specified
Learning Enhancement Programme (LEP) and Remedial teaching material
Computer Science Class 12
operations on a stack named BigNums.
(i) PushBig(): It checks every number from the list Nums and pushes all
such
numbers which have 5 or more digits into the stack, BigNums.
(ii) PopBig(): It pops the numbers from the stack, BigNums and
displays
them. The function should also display "Stack Empty" when there
are
no more numbers left in the stack.
Ans def
PushBig(Nums,BigNums):
for N in Nums:
if len(str(N)) >=
5:
BigNums.append(N)
def
PopBig(BigNums):
while BigNums:
print(BigNums.pop())
else:
print("Stack Empty")
3 (a) A list contains following record of customer : 3
[Customer_name, Room Type]
Write the following user defined functions to perform given
operations on the stack named 'Hotel' :
Push_Cust() – To Push customers’ names of those customers who
are
staying in ‘Delux’ Room Type.
Pop_Cust() – To Pop the names of customers from the stack and
display them. Also, display “Underflow” when there are no
customers in the stack.
For example :
If the lists with customer details are as follows :
["Siddarth", "Delux"]
["Rahul", "Standard"]
["Jerry", "Delux"]
The stack should
contain Jerry
Siddharth
The output should be:
Jerry
Learning Enhancement Programme (LEP) and Remedial teaching material
Computer Science Class 12
Siddharth
Underflow
Ans. Hotel=[] Customer=[["Siddarth","Delux"],
["Rahul","Standard"],["Jer ry","Delux"]]
def Push_Cust():
for rec in Customer:
if rec[1]=="Delux":
Hotel.append(rec[0])
def Pop_Cust():
while len(Hotel)>0:
print(Hotel.pop())
else:
print("Underflow")
OR
top=0
def Push_Cust(Hotel,Customer):
global top
for cust_rec in Customer:
if cust_rec[1]=="Delux":
Hotel.insert(top, cust_rec[0])
top=top+1
def Pop_Cust(Hotel):
global top
while len(Hotel)>0:
print(Hotel.pop())
top=top-1
else:
print("Underflow")
(b) Write a function in Python, Push (Vehicle) where, Vehicle 3
is a dictionary containing details of vehicles – {Car_Name:
Maker}. The function should push the name of car
manufactured by ‘TATA’ (including all the possible cases
like Tata, TaTa, etc.) to the stack. For example:
If the dictionary contains the following data :
Vehicle={"Santro":"Hyundai","Nexon":"TATA","Safari":"Tata"}
The stack should contain
Safari
Nexon
Learning Enhancement Programme (LEP) and Remedial teaching material
Computer Science Class 12
(c) Write a function in Python, Push (Vehicle) where, Vehicle 3
is a dictionary containing details of vehicles – {Car_Name:
Maker}. The function should push the name of car
manufactured by ‘TATA’ (including all the possible cases
like Tata, TaTa, etc.) to the stack. For example:
If the dictionary contains the following data :
Vehicle={"Santro":"Hyundai","Nexon":"TATA","Safari":"Tata"}
The stack should contain
Safari
Nexon
Ans stack=[]
def Push(Vehicle) :
for v_name in Vehicle :
if Vehicle[v_name].upper()=="TATA" :
stack.append(v_name)
OR
stack=[]
def Push(Vehicle) :
for v_name in Vehicle :
if Vehicle[v_name] in ("TATA", "TaTa","tata","Tata"):
stack.append(v_name)
OR
Any other valid Python code to serve the purpose.
An def COUNTWORDS():
s NW=0
with open("DECODE.TXT",'r') as
F: S=F.read().split()
for W in S:
if len(W)>=5:
NW+=1
return NW
6 Write a method/function COUNTLINES() in Python to read lines
from a text file
3
Example:
If the file content is as
follows: On seat2 VIP1 will
sit and
On seat1 VVIP2 will be
sitting Output will be:
Number of words ending with a digit are 4
Ans def count_Dwords():
. with open ("Details.txt", 'r') as F: # ignore
'r' S=F.read()
Wlist =
S.split() count
= 0
for W in Wlist:
if W[-1].isdigit():
count+=1
print("Number of words ending with a digit are",count)
9 (a) (i) What is the main purpose of seek() and tell() method ? 5
Ans write function - writes the content of a string onto a text file object .
writelines function - writes the content of a list of strings onto a text
file object .
Example:
file.write("Hello World")
file.writelines(["Hello","World"])
Note:
As there is no function writeline, give full 2 Marks for correctly
explaining the write function only.
Where
Icode – Item code
Description – Detail of
item Price – Price of item
2+
1 (A) (i) Differentiate between 'w' and 'a' file modes in Python.
3
=5
1
Learning Enhancement Programme (LEP) and Remedial teaching material
Computer Science Class 12
Ans 'w':
Open the file in write mode.
If the file doesn’t exist, then a new file will be
created. The file pointer is in the beginning of the
file.
If the file exists, the contents of the file, if any, are lost/truncated and the
new data is added as fresh data into the file.
'a':
Open the file in append mode.
If the file doesn’t exist, then a new file will be
created. The file pointer is at the end of the file.
If the file exists, the new data is added at the end of the file without deleting
the previous contents of the file.
12 (ii)
Consider a binary file, items.dat, containing records stored in the given
format :
{item_id: [item_name,amount]}
The advantage of using with clause is that any file that is opened using this
clause is closed automatically, once the control comes outside the with
clause.
Example:
with open("myfile.txt","r+") as file_object:
content = file_object.read()
where
Emp_Id : Employee id
Name : Employee
Name Salary :
Employee Salary
Write a user defined function, disp_Detail(), that would read the contents
of the file EMP.DAT and display the details of those employees whose salary
is below 25000.
def disp_Detail():
try:
with open("EMP.DAT","rb") as F:
Data=pickle.load(F)
for D in Data:
if D[2]<25000:
print(D)
except:
print("File Not Found!!!")
def Count_Medal():
F=open("sports.csv","r")
L=list(csv.reader(F))
for D in L:
if D[2]=="Gold":
print("Competition:",D[1])
F.close()
String functions and methods:
Sno Method name Description
1. isalnum() Returns true if string has at least 1 character and all characters are
alphanumeric and false otherwise.
2. isalpha() Returns true if string has at least 1 character and all characters
are alphabetic and false otherwise.
3. isdigit() Returns true if string contains only digits and false otherwise.
4. islower() Returns true if string has at least 1 cased character and all cased
characters are in lowercase and false otherwise.
5. isnumeric() Returns true if a string contains only numeric characters and false
otherwise.
6. isspace() Returns true if string contains only whitespace characters and
false otherwise.
7. istitle() Returns true if string is properly “titlecased” and false otherwise.
8. isupper() Returns true if string has at least one cased character and all
cased characters are in uppercase and false otherwise.
9. replace(old,new[ Replaces all occurrences of old in string with new or at most max
, max]) occurrences if max given.
10. split() Splits string according to delimiter str (space if not provided) and
returns list of substrings;
11. count() Occurrence of a string in another string
12. find() Finding the index of the first occurrence of a string in another string
13. swapcase() Converts lowercase letters in a string to uppercase and viceversa
14. startswith(str, Determines if string or a substring of string (if starting index beg
beg=0, end= and ending index end are given) starts with substring str;
len(string)) returns true if so and false otherwise.
List:Lists are ordered mutable data types enclosed in square brackets. E.g., [ 'A', 87, 23.5 ]
Python has a set of built-in methods that you can use on lists
Method Description
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
Method Descripti
on
clear() Remove all items form the dictionary.
Return a new dictionary with keys from seq and value
fromkeys(seq[, equal to v (defaults to None).
v])
Return the value of key. If key does not exit, return
get(key[,d]) d (defaults to None).
items() Return a new view of the dictionary's items (key,
value).
keys() Return a new view of the dictionary's keys.
Remove the item with key and return its value or d if
pop(key[,d])
key is not found. If d is not provided and key is not
found, raises
KeyError.
Remove and return an arbitary item (key, value).
popitem() Raises KeyError if the dictionary is empty.
Table: Transport
(i) Display the student and their stop name the tables Admin
name from and
Transport.
An SELECT S_name, Stop_name FROM Admin, Transport
s WHERE Admin.S_id = Transport.S_id;
(iii) Display all details of the students whose name starts with 'V' .
OR
Any other correct query using/without using join
(iv) Display student id and address in alphabetical order of student name, from
the table
Admin.
Learning Enhancement Programme (LEP) and Remedial teaching material
Computer Science Class 12
Ans SELECT S_id, Address FROM
Admin ORDER BY S_name;
7 Table : GAMES
GCode GameName Type Number PrizeMoney
101 Carrom Board Indoor 2 5000
102 Badminton Outdoor 2 12000
103 Table Tennis Indoor 4 NULL
104 Chess Indoor 2 9000
105 Lawn Tennis Outdoor 4 25000
Table : PLAYERS
PCode Name GCode
1 Nabi Ahmad 101
2 Ravi Sahai 108
3 Jatin 101
4 Nazneen 103
Write SQL queries for the following :
(i) Display the game type and average number of games played in each type.
1+
18 (A) (i) Define Cartesian Product with respect to RDBMS.
4
=5
Ans Cartesian Product operation combines rows/tuples from two
tables/relations. It results in all the pairs of rows from both the
tables. It is denoted by 'X'.
Sunil wants to write a program in Python to update the quantity to 20 of the
records whose item code is 111 in the table named shop in MySQL
database named Keeper. The table shop in MySQL contains the following
attributes :
● Item_code: Item code (Integer)
● Item_narne: Name of item (String)
● Qty: Quantity of item (Integer)
● Price: Price of item (Integer)
Consider the following to establish connectivity between Python and
MySQL: Username: admin
Password : Shopping
Host: localhost
DB=pm.connect(host="localhost",user="root",\
password="airplane",database="Travel")
MyCursor=DB.cursor()
MyCursor.execute("SELECT * FROM Flight ")
Rec=MyCursor.fetchall()
for R in
Rec: print
(R)
OR
Any other correct variation of the code
2 Logistic Technologies Ltd. is a Delhi based organization which is
expanding its office set-up to Ambala. At Ambala office campus, they are
5
0 planning to have 3 different blocks for HR, Accounts and Logistics related
work. Each block has a number of computers, which are required to be
connected to a network for communication, data and resource sharing.
as follows :
HR Block 70
Accounts Block 40
Logistics Block 30
(i) Suggest the most appropriate block/location to house the SERVER in
(ii) Suggest the best wired medium to efficiently connect various blocks within
the Ambala office compound.
(iii) Draw an ideal cable layout (Block to Block) for connecting these blocks
for wired connectivity.
Ans
Ans VoIP
OR
Any valid protocol used for voice communication
(v) Which kind of network will it be between Delhi office and Ambala office ?
Ans WAN
OR
Any other cable layout with valid justification
Ans VoIP
OR
Any valid protocol used for voice communication
(v) Which kind of network will it be between Delhi office and Ambala office ?
Ans WAN
Learning Enhancement Programme (LEP) and Remedial teaching material
Computer Science Class 12
Q Quickdev, an IT based firm, located in Delhi is planning to set up a
21 network for its four branches within a city with its Marketing
department in Kanpur. As a network professional, give solutions to
the questions (i) to (v), after going through the branches locations
and other details which are given below:
(i) Suggest the most suitable place to install the server for the Delhi 1
branch with a suitable reason.
Branch D, as it has maximum number of
Ans computers OR any other location with valid
justification
(ii) Suggest an ideal layout for connecting all these branches within 1
Delhi.
Ans