Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

lOMoARcPSD|11981679

Model QP - GE3151 PSPP March 2023 CSE

Problem Solving and Python Programming (Anna University)

Studocu is not sponsored or endorsed by any college or university


Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)
lOMoARcPSD|11981679

M.A.M. SCHOOL OF ENGINEERING


(Accredited by NAAC)
Approved by AICTE, New Delhi | Affiliated to Anna University, Chennai
Siruganur, Trichy-621105
MODEL EXAM
Academic Year – 2022-2023
Subject Code: GE3151, Subject Name: PROBLEM SOLVING AND PYTHON PROGRAMMING
Time: Three Hours Maximum: 100 Marks
Dept: Common to all bracnches Year/Sem: I/ I
Part A (10 x 2 = 20 Marks)
1. Compare machine language, assembly language and high-level language.

BTL4 Analyze

2. Write an algorithm to find minimum in a list.

BTL5 Evaluate

3. State about Logical operators available in python language with example.

BTL1 Remember

4. Compare Interpreter and Compiler.

BTL4 Analyze

Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)


lOMoARcPSD|11981679

5. Write a Python program to accept two numbers, find the greatest and print the result.
print(“Enter two numbers”)
val1=int(input()) val2=int(input())
else: BTL5 Evaluate
largest=val2
print(“Largest of two numbers is:”,largest)
6. Do loop statements have else clauses? When will it be executed?
Loop statements may have an else clause, it is executed when the loop terminates
through exhaustion of the list (with for) or when the condition becomes false (with BTL1 Remember
while), but not when the loop is terminated by a break statement.

7. Can function return tuples? If yes Give examples.


Function can return tuples as return values. def circle_Info(r):
#Return circumference and area and tuple c = 2 * 3.14 * r a = 3.14 * r * r
return(c,a) BTL2 Understand
print(circle_Info(10)) Output
(62.800000000000004, 314.0)

8. How will you update list items? Give one example.


Using the indexing operator (square brackets) on the left side of an assignment,
we can update one of the list items
fruit = ["banana","apple","cherry"] print(fruit) ['banana', 'apple', 'cherry'] BTL2 Understand
fruit[0] = "pear"
print(fruit) ['pear', 'apple', 'orange']

9. Write a note on modular design.


Modules are files containing Python definitions and statements (ex: name.py)
Modules can contain executable statements along with function definitions.
Each modules has its own private symbol table used as the global symbol table
all functions in the module. BTL5 Evaluate
Modules can import other modules.
Syntax:
import <modulename>

10. Write a Python script to display the current date and time.
import datetime print(“date and time”, datetime.datetime.now()) BTL5 Evaluate

Part B (5 x 16 = 80 Marks)
11. a. (i) Sketch a flow chart to accept three distinct numbers, find the greatest and
print the result. (8)

BTL3
Apply
BTL3
Apply

Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)


lOMoARcPSD|11981679

(ii) Sketch a flow chart to find the sum of the series 1+2+3+……+100. (8)

or
b. Outline the Towers of Hanoi problem. Suggest a solution to the Towers of
Hanoi problem with relevant diagrams. (16)

BTL4
Analyze

Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)


lOMoARcPSD|11981679

Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)


lOMoARcPSD|11981679

12. a.(i)Write a python program to rotate a list by right n times without


Slicing technique. (8)

Right Rotate a List by n with for loop


Here, the for loop is used to right rotate a list −

Example
# The value of n for rotation position

num = 4

BTL5
# Create a List Evaluate
myList =[5, 20, 34, 67, 89, 94, 98, 110] BTL2
Understand
print("List before rotation = ",myList)

newlist = []

for item in range(len(myList) - num, len(myList)): newlist.append(myList[item])

for item in range(0, len(myList) - num): newlist.append(myList[item])

# Display the Update List after rotation

print("Updated List after rotation = ",newlist)

Output
List before rotation = [5, 20, 34, 67, 89, 94, 98, 110]
Updated List after rotation = [89, 94, 98, 110, 5, 20, 34, 67]

Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)


lOMoARcPSD|11981679

(ii)Discuss about keyword arguments and default arguments in python with example. (8)

or
b.(i)Write a python program to print the maximum among „n‟ randomly
generate „d‟ numbers by storing them in a list. (10)
import random

print("Random integers between 0 and 9: ")


for i in range(4, 15):
y = random.randrange(9)
print(y)

Output:
Random integers between 0 and 9:
4
7
2 BTL5
8
4
Evaluate
6 BTL4
2 Analyze
3
1
5
3

Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)


lOMoARcPSD|11981679

(ii) Outline the operator precedence of arithmetic operators in Python. (6)

13. a. (i) Write a Python program to find the factorial of the given number without
recursion with recursion. (10)
The factorial of a positive integer n is the product of all positive integers less than or equal to n. The factorial
of a number is represented by the symbol "!" . For example, the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.

Here is a Python function that calculates the factorial of a given number using a for loop:

def factorial(n):
if n < 0:
return None
if n == 0:
return 1
result = 1
for i in range(1, n+1):
result *= i
return result

This function takes a single argument, n, and returns the factorial of n. It first checks if n is less than 0, if so it
returns None. If n is equal to 0, it returns 1 (since the factorial of 0 is defined to be 1). It then initializes a BTL5
variable named result to 1, then uses a for loop to iterate over the range of integers from 1 to n inclusive and
Evaluate
multiply each number to result variable. Finally, the function returns the value of result, which is the factorial
of n. BTL5
Evaluate
You can call this function and pass in any positive integer to calculate its factorial:

>>> factorial(5)
120
>>> factorial(3)
6
>>> factorial(10)
3628800

Alternatively python has math.factorial function which you can use without writing your own function.

import math
math.factorial(5)

Do note that factorial of number can be very large, even for relatively small numbers and python integers
may not be large enough to store those values.

#1: Python program to find factorial of a given


Downloaded bynumber
NADHIYAwithout using recursion
S (nadhiya.spkd@gmail.com)
lOMoARcPSD|11981679

#Factorial without recursion

n=int(input("Enter the number: "))


fact=1
if n<0:
print("factorial doesn't exist for negative numbers")
else:
for i in range(1,n+1):
fact=fact*i
print("factorial of", n, "is", fact)

Output:

Enter the number: 5


factorial of 5 is 120

(ii)Write a Python program to generate first „N‟ Fibonacci series numbers. (6)
A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....
The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. This
means to say the nth term is the sum of (n-1)th and (n-2)th term.
Source Code
# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Output
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8

or
Downloaded by NADHIYA S (nadhiya.spkd@gmail.com) BTL5
lOMoARcPSD|11981679

b. (i) Write a python code to search a string in the given list. (6) Evaluate
BTL2
When it is required to find the string in a list, a simple „if‟ condition along with „in‟ operator Understand
can be used.

Example
Below is a demonstration of the same
my_list = [4, 3.0, 'python', 'is', 'fun']

print("The list is :")

print(my_list)

key = 'fun'

print("The key is :")

print(key)

print("The result is :")

if key in my_list:

print("The key is present in the list")

else:

print("The key is not present in the list")

Output
The list is :
[4, 3.0, 'python', 'is', 'fun']
The key is :
fun
The result is :
The key is present in the list

Explanation
 A list of integers and strings is defined and is displayed on the console.
 A value for key is defined and is displayed on the console.
 An „if‟ loop is used to check if the key is present in the list.
 If yes, the result is displayed on the console.

(ii) Explain string functions and methods in detail. (10)

Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)


lOMoARcPSD|11981679

14. a. (i) Write a Python program to perform linear search on a list. (7)

BTL2
Understand
BTL3
Apply

Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)


lOMoARcPSD|11981679

(ii) Demonstrate with code the various operations that can be performed on tuples. (8)

or
b.Write a Python program for (i) merge sort (10) (ii) quick sort (6)

BTL5
Evaluate

Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)


lOMoARcPSD|11981679

15. a. (i).Describe how exceptions are handled in python with necessary examples. (10)

BTL2
Understand

(ii) Discuss about the use of format operator in file processing. (6)

Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)


lOMoARcPSD|11981679

or
b. Mention the commands and their syntax for the following: get current directory, changing directory,
list, directories and files, make a new directory, renaming and removing directory. (16)

A directory is a collection of files and subdirectories. A directory inside a directory is known as a


subdirectory.
Python has the os module that provides us with many useful methods to work with directories (and files
as well).

Get Current Directory in Python


We can get the present working directory using the getcwd() method of the os module.
This method returns the current working directory in the form of a string. For example,
import os

print(os.getcwd())

# Output: C:\Program Files\PyScripter


Here, getcwd() returns the current directory in the form of a string.

Changing Directory in Python


In Python, we can change the current working directory by using the chdir() method.
The new path that we want to change into must be supplied as a string to this method. And we can use
both the forward-slash / or the backward-slash \ to separate the path elements.
Let's see an example,
import os

# change directory
os.chdir('C:\\Python33')

print(os.getcwd())
BTL2
Output: C:\Python33 Understand
Here, we have used the chdir() method to change the current working directory and passed a new path
as a string to chdir().

List Directories and Files in Python


All files and sub-directories inside a directory can be retrieved using the listdir() method.
This method takes in a path and returns a list of subdirectories and files in that path.
If no path is specified, it returns the list of subdirectories and files from the current working directory.
import os

print(os.getcwd())
C:\Python33

# list all sub-directories


os.listdir()
['DLLs',
'Doc',
'include',
'Lib',
'libs',
'LICENSE.txt',
'NEWS.txt',
'python.exe',
'pythonw.exe',
'README.txt',
'Scripts',
'tcl',
'Tools']

os.listdir('G:\\') Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)


lOMoARcPSD|11981679

['$RECYCLE.BIN',
'Movies',
'Music',
'Photos',
'Series',
'System Volume Information']

Making a New Directory in Python


In Python, we can make a new directory using the mkdir() method.
This method takes in the path of the new directory. If the full path is not specified, the new directory is
created in the current working directory.
os.mkdir('test')

os.listdir()
['test']

Renaming a Directory or a File


The rename() method can rename a directory or a file.
For renaming any directory or file, rename() takes in two basic arguments:
the old name as the first argument
the new name as the second argument.
Let's see an example,
import os

os.listdir()
['test']

# rename a directory
os.rename('test','new_one')

os.listdir()
['new_one']
Here, 'test' directory is renamed to 'new_one' using the rename() method.

Removing Directory or File in Python


In Python, we can use the remove() method or the rmdir() method to remove a file or directory.
First let's use remove() to delete a file,
import os

# delete "myfile.txt" file


os.remove("myfile.txt")
Here, we have used the remove() method to remove the "myfile.txt" file.
Now let's use rmdir() to delete an empty directory,
import os

# delete the empty directory "mydir"


os.rmdir("mydir")
In order to remove a non-empty directory, we can use the rmtree() method inside the shutil module. For
example,
import shutil

# delete "mydir" directory and all of its contents


shutil.rmtree("mydir")
It's important to note that these functions permanently delete the files or directories, so we need to
careful when using them.

***All the Best***

Downloaded by NADHIYA S (nadhiya.spkd@gmail.com)

You might also like