Unique Paper Code : 42341102
Name of the Course : B.Sc. Prog./Mathematical Sc
Name of the Paper : BSCS01 Problem Solving Using Computers
Semester : I
Year of Admission : 2019 onwards
Duration: 3 hours Maximum Marks: 75
Instructions for Candidates:
● All questions carry equal marks. Attempt any Four out of Six questions.
● All the coding/error/output statements are based on Python programming language.
Q1. Write a function display(n)to display the following pattern (here, n = 4):
1
21
321
4321
Marking Scheme:6 marks
Sample Solution:
def display(n):
for i in range(1, n + 1):
for j in range(i, 0, -1):
print(j, end ='')
print()
Identify the errors in the following statements and rewrite them after removing the errors:
Marking Scheme: Total marks = 6 (1.5 x 4)
a) "Hello" * "World!"
"Hello" + "World"
b) If s <= "lowercase":
if s <= "lowercase": #If -> if
c) del = "delete"
d = "delete" # del is reserved word
d) a = [16, 82, 39]
print(A)
either A = [16, 82, 39] OR a = [16, 82, 39]
print(A) print(a)
Write a function that accepts three integers as arguments and returns their greatest
common divisor (GCD).
Marking Scheme: 6.75
def gcd(n1, n2, n3):
m = min(n1, n2, n3)
for i in range(m, 0, -1):
if n1 % i == 0 and n2 % i == 0 and n3 % i == 0:
return i
Q2. What will be the output on the execution of the following code segments? Justify your
answers.
Marking Scheme: 4 + 2 + 4.75 + 4 + 4
a) T1 = (1, 2, 3, 4, 5, 6, 7, 8)
print(T1[1 : : 2])
print(T1[-1 : -5 : -2])
print(T1[ : : -1])
print(T1[ : 7 : 2])
(2, 4, 6, 8)
(8, 6)
(8, 7, 6, 5, 4, 3, 2, 1)
(1, 3, 5, 7)
b) a = True
b = False
c = False
if a or b and c:
print("It’s a true value")
else:
print("It’s a false value")
It’s a true value
c) def fn(s):
try:
print("Hello", S)
print("How are you?")
except NameError:
print("Something not defined")
finally:
print("Out of try block")
try:
fn("Amit")
fn("Kunal")
except Exception as e:
print(e)
Something not defined
Out of try block
Something not defined
Out of try block
d) def recurse(n, s):
if n == 0:
print(s)
else:
recurse(n-1, n+s)
recurse(3, 0)
Solution: 6
e) colors = set(['Blue', 'Grey', 'Pink', 'Black'])
mains = set(['Orange', 'Green'])
all=colors.union(mains)
print(all)
print(colors.intersection(mains))
print(colors.difference(mains))
{'Blue', 'Black', 'Pink', 'Green', 'Orange', 'Grey'}
set()
{'Grey', 'Blue', 'Black', 'Pink'}
Q3. Define a class Cuboid that has three data members, namely, length, breadth and
height. The class should support following methods:
__init__ for initializing the data members
surfaceArea() that returns the surface area of the cuboid
display() to display the data members and the surface area of the cuboid
Marking Scheme: 10.75 marks (3 for __init__, 3 for
surfaceArea and 4.75 for display)
class Cuboid:
def __init__(self, l, b, h):
self.length = l
self.breadth = b
self.height = h
def surfaceArea(self):
return 2 * (self.length * self.breadth + self.length * self.height +
self.breadth * self.height)
def display(self):
print("Length:", self.length)
print("Breadth:", self.breadth)
print("Height:", self.height)
print("Surface Area:", self.surfaceArea())
Write statements/mathematical expressions for the following:
Marking Scheme: 2 + 2 + 4
a) 7(3 + 4)
7 * ( 3 + 4)
b) 𝐴 = 𝜋𝑟 !
import math
A = math.pi * r * r
OR
A = 3.14 * r * r
c) to find sum of all the odd integers of a list using lambda, filter and/or reduce
functions
import functools as f
lst = [2, 3, 5, 6, 7] # sample list
print(f.reduce(lambda x, y: x + y, list(filter(lambda x: x % 2 == 1, lst))))
Q4. Write a function called cummulativeSum() that takes a tuple of numbers and returns
the cumulative sum of the elements of the tuple as a list. An element at index i in the
list has the sum of the first i elements of the tuple. The first two elements are same in
the list and tuple. For example, if tuple is (1, 2, 3), then the list created should be [1, 3,
6].
Marking Scheme: 8 marks
def cummulativeSum(t):
lst = [t[0]]
for i in range(1,len(t)):
lst.append(lst[i - 1] + t[i])
return lst
Write a function that accepts one list Lst and returns two lists: first having the elements
occurring only once in Lst and the second having the elements occurring only twice in
Lst. For example, if Lst = [1, 5, 2, 1, 6, 2, 3, 5, 6, 6, 7, 6] then the function should return
[3, 7] and [1, 5, 2].
Marking Scheme: 10.75 marks
def f(Lst):
l1 = []
l2 = []
for i in Lst:
if i not in l1 and i not in l2:
c = Lst.count(i)
if c == 1:
l1.append(i)
elif c == 2:
l2.append(i)
return l1, l2
Q5. Write a program to create 'Dept.csv' file having the following data:
Department Number Department Name Location
10 'Accounting' 'New York'
20 'Research' 'Dallas'
30 'Sales' 'Chicago'
40 'Operations' 'Boston'
Marking Scheme: 8.75 marks
import csv
lst = [['Department Number', 'Department Name', 'Location'],
[10, 'Accounting', 'New York'], [20, 'Research', 'Dallas'],
[30, 'Sales', 'Chicago'], [40, 'Operations', 'Boston']]
with open("Dept.csv", "w") as inf:
p = csv.writer(inf)
p.writerows(lst)
Given the following variables:
Str = "Hi there!"
Tuple = (23, '52', 67, 12)
Set = {'a', 'b', 'c'}
What are the errors in the following code segments? Justify your answers.
Marking Scheme: 10 marks in total (2 marks * 5 answers)
a) Str[1:] = 'ello!'
Strings are immutable, so part of it cannot be changed.
b) min(Tuple)
Minimum value of mixed data types cannot be determined.
c) Set.add(['d', 'e'])
A list cannot be a part of a set.
d) Set[1] = 'x'
Indexing is not possible with sets.
e) print(Str[20])
20 is beyond valid index in Str.
Q6. Apply selection sort algorithm to sort the following list of words in ascending order:
lst = ['apple', 'banana', 'cherry', 'avocado', 'coconut', 'peach', 'pear']
Show all the intermediate steps of each pass. Determine the number of passes to sort the
entire list.
Marking Scheme: 10 marks in total (9 marks for steps and 1
mark for number of passes)
Solution:
['apple', 'banana', 'cherry', 'avocado', 'coconut', 'peach', 'pear']
['apple', 'avocado', 'cherry', 'banana', 'coconut', 'peach', 'pear']
['apple', 'avocado', 'banana', 'cherry', 'coconut', 'peach', 'pear']
['apple', 'avocado', 'banana', 'cherry', 'coconut', 'peach', 'pear']
['apple', 'avocado', 'banana', 'cherry', 'coconut', 'peach', 'pear']
['apple', 'avocado', 'banana', 'cherry', 'coconut', 'peach', 'pear']
Apply binary search to search key value 'avocado'. Show all the comparisons performed
during the search.
Marking Scheme: 8.75 marks
Solution:
Low High Mid List[Mid] Remarks
0 6 3 'cherry' 'cherry' > 'avocado',
update high
0 4 2 'banana' 'banana' > 'avocado',
update high
0 3 3 'avocado' Found