You are on page 1of 8

AMITY INTERNATIONAL SCHOOL

PRACTICAL LIST 2023-24


CLASS XII- COMPUTER SCIENCE
For all questions, you are required to take print of codes and respective outputs when executed.
File Handling, Functions and Data Structures

1) Write a function to create a text file containing following data:


Neither apple nor pine are in pineapple. Boxing rings are square.
Writers write, but fingers don’t fing. Overlook and oversee are opposites.
A house can burn up as it burns down. An alarm goes off by going on.

a) Read back the entire file content using read( ) or readlines( ) and display on the screen.
b) Append more text of your choice in the file and display the content of file with line
numbers prefixed to line.
c) Display last line of file.
d) Display first line from 10th character onwards.
e) Read and display a line from the file. Ask user to provide the line number to be read.
f) Find the frequency of words beginning with every letter i.e.
(for the above example)
Words beginning with a: 5
Words beginning with n: 2
Words beginning with p: 2
Words beginning with o: 5 and so on

2) Assume that a text file named file1.txt contains some text, write a function named
isvowel( ) that reads the file file1.txt and creates a new file named file2.txt, which shall contain only
those words from the file file1.txt which don’t start with a vowel
For example, if the file1.txt contains:
Carry Umbrella and Overcoat When it Rains
Then the file file2.txt shall contain
Carry When Rains

1
3) A file containing data about a collection of students has the following format.
Each line contains a first name, a second name, a registration number, no of years and a
department separated by tabs.
Rajat Sen 12345 1 CSEE
Jagat Narain 13467 3 CSEE
Anu Sharma 11756 2 Biology
Sumita Trikha 23451 4 Biology
Sumder Kumra 11234 3 MME
Kanti Bhushan 23211 3 CSEE
a) Write a Python program that will copy the contents of the file into a list of tuples
b) Display full details of the student sorted by registration number
· The names of all students with no of year less than 3
· The number of people in each department

4) Write is a program that reads a file“myfile.txt” and builds a histogram (a dictionary having key
value pair as word: occurrence) of the words in the file.
a) Now use histogram to print
i) Total number of words
ii) Number of different words
iii) The most common words
b) Using above text file “myfile.txt”, write a program that maps a list of words read from the file
to an integer representing the length of the corresponding words.( use dictionary having key
value pair as length : list of word )
Now using above dictionary design a function find_longest_word() to display a list of longest
words from file.
Define a function filter_long_words(n) that takes an integer n and returns the list of words that
are longer than n from file.

5) A dictionary Customer contains the following keys {roomno,name,duration}


A binary file “hotel.dat” contains details of customer checked in the hotel.
Write Code in python to perform the following using pickle module
i) Read n dictionary objects and load them into the file
ii) Read all the dictionary objects from the file and print them
iii) Counts the number of customers present in the hotel.

2
(Counts the total number of customers present in the hotel.(Assume that file might have
few record before adding n records in part i)
iv) Display those customers from the file, who have stayed more than 2 days in the hotel.

6) Sun Microsystems when held recruitment test. The file placement.csv containing the below format
of data

The marks are from 5 different tests conducted and each col is out of 5 marks

SNO NAME MARKS1 MARKS2 MARKS3 MARKS4 MARKS5

1 JOHN 4 3 4 2 5

2 PETER 3 4 4 3 5

a) Read the above file and print the data

b) Write User Defined Function to find total no of people who came for the placement test

c) Write the UDF to find the top n Names on basis of total Marks

7) Write a program to input a number and then call the functions


count(n) which returns the number of digits
reverse(n) which returns the reverse of a number
hasdigit(n) which returns True if the number has n as a digit else False
show(n) to show the number in its expanded form (sum of place values of the digits in n)
(eg 124 = 100 + 20 + 4)

8) A Number is a perfect number if the sum of all the factors of the number (including 1) excluding
itself is equal to number.

For example: 6 = 1+2+3 and 28=1+2+4+7+14


Number is a prime number if it 's factors are 1 and itself.
Write functions i) Generatefactors() to populate a list of factors
ii) isPrimeNo() to check whether the number is prime number or not
iii) isPerfectNo() to check whether the number is perfect number or not

Save the above as a module perfect.py and use in the program main.py as a menu driven program.
9) Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

3
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII,
which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is
not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making
four. The same principle applies to the number nine, which is written as IX. There are six instances where
subtraction is used:

• I can be placed before V (5) and X (10) to make 4 and 9.


• X can be placed before L (50) and C (100) to make 40 and 90.
• C can be placed before D (500) and M (1000) to make 400 and 900.

Write a User Defined Function which takes a string (Roman Numeral) as an argument and returns the
integer equivalent.

def romanToInt(s):

return Ans

print(romanToInt(‘LVIII’)) should print 58

print(romanToInt(‘MCMXCIV’)) should print 1994

10) Data can be represented in memory in different ways Binary, Decimal, Octal, and Hexadecimal.
Input number in decimal and desired type (Specify B for Binary, O for Octal, H for Hexadecimal)
for output.

4
Write a user defined function to perform the conversions-(Do not use built in functions)

SAMPLE INPUT 12 SAMPLE INPUT 25


DESIRED TYPE B DESIRED TYPE O
Result: 1100 Result: 41

11) In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a
new one with a different size r x c keeping its original data.

You are given an m x n matrix mat and two integers r and c representing the number of rows and the
number of columns of the wanted reshaped matrix.

The reshaped matrix should be filled with all the elements of the original matrix in the same row-
traversing order as they were.

If the reshape operation with given parameters is possible and legal, output the new reshaped matrix;
Otherwise, output the original matrix.

Example 1:

Input: mat = [[1,2],[3,4]], r = 1, c = 4

Output: [[1,2,3,4]]

Example 2:

Input: mat = [[1,2],[3,4]], r = 2, c = 2

Output: [[1,2],[3,4]]

def reshape(mat,r,c):

return newshape

5
12) Create a stack to take in stack of numbers and then simulate a ring game.

A ring stand is such that only a ring of higher diameter can be placed on lower one. The diameters
are given by the user the program will compare the diameter of ring at stack top with the diameter
of ring to be placed if condition specified is true ring is added to the stack otherwise keep popping
and put them into temporary ring stand to arrange them into specific order.

You are required to simulate a ring game by implementing and maintaining a stack of numbers. A number
represents the diameter of a ring and the stack represents a stand to hold the rings. Keep on reading
numbers(diameters) from user and add in a stack as per the given criteria

13) Create a program to take in two strings and pass them to a stack object
The strings are made in the following manner
They contain alphanumeric characters and # and # here signifies backspace
Example – string1= ab#c string2=ad#c
Perform push() operations to put the strings into stack objects
Perform pop() operations and whenever # is encountered pop out the next character also
(#means backspace)
For example
ab#c when pushed and popped becomes ac
ad#c when pushed and popped becomes ac
Finally check if both the strings are equal or not

Test cases:
AreEqual(“ab#c”,”ad#c”) should return True as both strings become ‘ac’
AreEqual(“ab##”,”c#d#”) should return True as both strings become ‘’ after backspace handling
AreEqual(‘a#c’,’b’) should return False

DATABASE MANAGEMENT (MySQL)-(Write SQL queries and respective outputs)

6
1) The given table “Tutor” is shown below , write commands to do the following :
ID NAME AGE CITY FEE PHONE
P1 SAMEER 34 DELHI 45000 9811076656
P2 ARYAN 35 NAGARKOT 54000 9911343989
P4 RAM 34 CHENNAI 45000 9810593578
P6 PREMLATA 36 BHOPAL 60000 9910139987
P7 SHIKHA 36 RAJKOT 34000 9912139456
P8 RADHA 33 DELHI 23000 8110668888

(i) Display name in descending order of those students whose age does not lie between 35 and 40.
(ii) List cities with their average fee in it.
iii) Decrease the fees of shikha by 5%
iv) Display cities where fees is maximum and minimum respectively
v) Display the name and city of tutor who lives in a city with name having ‘O’ but not ‘P’

2. Consider the following WATCHES and SALE table and Write the SQL commands for (i) to (v):
i) To display watch name and their quantity sold in first quarter.
ii) To display the details of those watches whose name ends with ‘Time’
iii) To display total quantity in store of Unisex type watches.
iv) To display watch’s name and price of those watches which have price range in between 5000-
15000.
v) To display Quantity sold of all watches WatchId wise.
Watchid Watch_Name Price Type Qty_Store
W001 High Time 10000 Unisex 100 Table-WATCHES

W002 LifeTime 15000 Ladies 150


W003 Wave 20000 Gents 200
W004 High Fashion 7000 Unisex 250
W005 Golden Time 2500 Gents 100

WatchId Qty_Sold Quarter


W001 10 1
W003 5 1
7
W002 20 2
W003 10 2
W001 15 3 Table-SALES

W002 20 3
W005 10 3
W003 15 4

MySQL- Python connectivity


1. Consider the table “ITEM” having the following fields
Itemcode varchar, Itemname varchar , Price float

i) Create the table ITEM in the mydb database


ii) Create a menu driven program in python to have
a) function for inserting records in the table
b) function for displaying all the records from the table item
c) Function for searching for a particular record on basis of Itemcode
2. Create a Table “STUDENT” in MySQL with the following attributes. Table: STUDENT

i) Create a menu driven program ColumnName Datatype Size Constraint


in Python for the user to enter the
RollNo Number Primary
details and save the data in MySQL
table Key
ii) Allow the user to update the
details for a particular rollno and Name Varchar 30 Not Null
ensure the changes have been made Class Number
in the table student.
DOB Date
Gender Varchar 2

3.Create a Table “BUS” in MySQL with shown structure: BusNo int Primary Key, Origin Varchar, Dest,
Varchar, Rate Number, Km Number
Now build a connection with Python to add a new record and Display the details in above table. You
may use Tkinter to create the front end.

You might also like