ComputerScience 83782
ComputerScience 83782
Write a one line list comprehension which create a list that contains only the
elements that are unique between the lists.
i. b=[[9,6,1],[4,5,6],[7,7,0,2,12,45],[12,344,1]]
X=b[:3]
print(X)
X[0:1]=[2]
print(X[0:2][1:0:-1])
ii. numberGames = {}
numberGames[(1,2,4)] = 8
numberGames[(4,2,1)] = 10
numberGames[(1,2)] = 12
sum = 0
for k in numberGames:
sum += numberGames[k]
print(len(numberGames) + sum )
(d) Write the possible output/s of the following program: 2
import random
AR=[20,30,40,50,60,70]
FROM=random.randint(1,3)
TO=random.randint(2,4)
for K in range(FROM,TO+1):
print (AR[k],end="#")
(e) Consider a file containing dictionaries. Each dictionary has rollno and marks as 2
keys. Based on the roll no the marks has to be changed.
Fill in the blanks.
import pickle
file1 = open("student.dat", "rb+")
search = int(input("Enter the roll no you want to change"))
while True :
try:
y=__________________
stud = _________________
if stud['roll'] == search:
marks = int(input("enter new marks"))
stud["marks"] = marks
___________________
___________________
break
except:
print("error")
break
file1.close()
(f) Write a program to read a file called me.txt and output the size of the file without 2
using the os module.
Evaluate the expression 20,45,+,20,10,-,15,+,* using stacks
(g) 2
(h) Give any two applications of a queue. 2
(i) Write a program to create a csv file Marks.csv and write the data Physics: 90 and 2
Chemistry: 100 into it
(j) Write the names of the functions (along with the module names if required)which 2
can do the following:
a. Function returns a string where lower case becomes upper case and vice
versa
b. Removes the last inserted key value pair from dictionary
c. Make iterables (can be zero or more), makes iterator that aggregates
elements based on the iterables passed, and returns an iterator of tuples.
d. Function used to open and read a URL.
3 (a) Using Stacks , display the conversion of the infix expression to postfix expression in 3
a tabular format:
(R-S)*(T/U)+V^W*N
(b) Write a program which uses the urllib module. The program should open the 3
website www.python.org, read the source code and count the number of times the
word python is mentioned in the source code. If a 404 occurs, the program should
print “Website not accessible”
(c) Write a function which receives a list of numbers and its length as parameter, The 3
function should have the following attributes:
a. Default parameters set to [] and 0
b. Should reverse each number in the list.
c. Should return 1 or 0 depending upon whether the list was changed or not
respectively
Example input : L[123,23,45], length =3
Return value: [321, 32,54]
(d) Write a python program to read a file called STORY.txt , for each line print the 3
number or words, vowels, consonants and special characters
e.g. if the file contains:
This is an example for story.txt
We will count the number of words
And the number of vowels
And the number of special characters
(g) Consider a list of characters. Write a function which accepts the list of characters 3
and moves all the vowels to the beginning of the list.
Do not use any temporary array. No in built functions other than len () can be used.
All swaps must be in-place.
e.g. LIST WHICH HAS O, R, I, G, I, N, A, L becomes O, I, A, A, R , G, N, L
(h) Perform the following: (Write only the required python statement, all modules can 3
be considered inserted, default database is MySql)
a. Format a string to Insert a row into a table “Student” with column heading
“Id, Name, Class” with values taken from the user.
b. Supposing a results set rs contains rows from the above said table. How
would the code using fetchall () be different from fetchmany (). Show
programmatically. (Only the loop)
4. (a) Write a program containing two functions. For an existing list of numbers, write 4
two functions:
Function 1: accepts the list and deletes an element in the list in such a way that the
order of the elements does not change and the size of the list does not decrease
Function 2: accepts a sorted list , searches for the element and deletes it from the
list (List size can change)
*Search mechanism can be a simple call to the right function.
(b) Write a function which accepts a matrix as its parameter. The function should then 4
interchange the primary diagonal with its secondary diagonal.
e.g. Input : [4, 2, 3, 1,
5, 7, 6, 8,
9, 11, 10, 12,
16, 14, 15, 13]
Output : [1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
11, 14, 15, 16]
(c) A Stack STemp represents the temperatures of a city on different days. Write 4
functions which represent pop () and push () for the same. Implement the calls as
well.
(d) Consider the following MySQL table: 4
Table Name: Movies
Movie Id Movie Name Rating Price
M001 Last Chance 2 100
M002 Good Luck 3 200
M003 Experimental 4 500
Write a python program to connect to the database “Theatre” with user id and
password “user”.
a. With the help of the program Add a column to the table called Cast.
b. Set all values Of Cast to “Not Known”.
c. Retrieve data from the table where Movie starts with L.
*****BEST OF LUCK*****