PSP LAB MANUAL-First Semester-2024
PSP LAB MANUAL-First Semester-2024
CIE-Marks
Max. Marks MarksAwarded
Examiners
1. ………………………………………….
2. ………………………………………….
04 Towers of Hanoi
Algorithm:
Step 1: Start
Step 2: Declare a list with set of number values
Step 3: Use the sort() in-built function to sort the list in ascending order
Step 4: Display the smallest element by indexing the first element in the
sorted list
Step 6: End
Program:
Output:
[1, 2, 3, 4]
Program 3: Guess an integer number in a range.
Aim: To write the python program for Guessing game – guessing a
number with in a range of numbers.
Algorithm:
Step 1: Start
Step 2: Read n.
Step 3: Read a guess number.
Step 4: If guess > n
Step 5: Print “Your guess is too high”
Step 6: If guess < n
Step 7: Print “Your guess is too low”
Step 8: Else
Step 9: If guess = n
Step 10: Print “Good job” else print “no”
Step 11: Stop
Program:
n=10
guess=int(input("Guess the number: "))
if(guess>n):
print("Your is too high")
elif(guess==n):
print("Good Job")
else:
print("Your is too low")
Output:
Guess the number: 10
Good Job
Guess the number: 4
Your is too low
Guess the number: 23
Your is too high
Rules to be followed:
a) Only one disk can be moved among the towers at any given time.
b) Only the “top” disk can be removed.
c) No large disk can sit over a small disk.
The objective is to transfer the disks from the left most pole to the right
most pole by using the center pole as the temporary pole.
The steps are:
a) Move the top n-1 disks from the left pole to the centre pole, n is the
number of disks.
b) Move the nth largest disk to the right pole. Move the n-1 disks on
the centre pole to the right pole.
c) The total number of moves will be 2n – 1, where n is the number of
disks.)
Algorithm:
1. Start
2. Read disk, source, dest, aux
3. Call the function Hanoi(disk, source, dest, aux)
4. Stop
Algorithm for function Hanoi(disk, source, dest,
aux): 1. Start
2. If disk=1
Move disk from source to dest
3. Else
Hanoi(disk-1, source, aux, dest)
Move disk from source to dest Hanoi(disk1,aux, dest,source)
4. Return
Pseudo code:
BEGIN
READ disk, source, dest, aux
FUNCTION Hanoi (disk, source, dest, aux) END
Pseudo code for function Hanoi (disk, source, dest, aux)
BEGIN
IF disk=1 THEN
Move disk from source to dest
ELSE
Hanoi (disk-1, source, aux, dest)
Move disk from source to dest
Hanoi (disk-1, aux, dest, source)
ENDIF
END
Program :
Program:
a = int( input("Please enter value for a: "))
b = int( input("Please enter value for b: "))
# we will user third variable which is a temporary variable
temp = a
a=b
b = temp
Output:
Output:
Enter the n for circulate:
4
[5, 6, 7, 8, 9, 10, 1, 2, 3, 4]
Program 7: Calculate distance between two points
Algorithm:
Step 1: Start
Step 2: Declare variables x1, x2, y1 and y2, then input them using input ()
function
Step 3: Declare the third variable for storing results and
assign result= ((((x2 - x1 )**2) + ((y2-y1)**2)
)**0.5)
Step 5: Display result using print ()
Step 6: End
Program:
x1=int(input("enter x1 : "))
x2=int(input("enter x2 : "))
y1=int(input("enter y1 : "))
y2=int(input("enter y2 : "))
result= ((((x2 - x1 )**2) + ((y2-y1)**2) )**0.5)
Output:
Enter x1 : 6
Enter x2 : 6
Enter y1 : 7
Enter y2 : 8
Distance between (6, 6) and (7, 8) is : 1.0
Program 8: Find the square root of number(Newtons method)
Algorithm:
Step 1: Start
Step 2: The first parameter is the value whose square root will be
approximated.
Step 3: The second is the number of times to iterate the calculation
yielding a better result from the user.
Step 4: Calculate better = ½*(approx.+n/approx.)
Step 5: Print the final result
Step 6: Stop
Program:
n=int(input("Enter a number:"))
newton_squareroot= 0.5*((0.5*n)+n/(0.5*n))
print (newton_squareroot)
Output:
Enter a number: 4 2.0
Result:
Thus the Python program to find the square root of a number
(Newton’s method) was created and executed successfully.
Program 9: Compute GCD of two numbers
Algorithm:
Step 1: Start
Step 2: Take two numbers from the user as input
Step 3: Calculate the remainder a%b
Step 4: While the remainder is not equal to 0.
Step 5: print the GCD
Step 9: Stop
Program:
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
print ("The GCD of the two numbers is",gcd(a,b))
Output:
Enter the first number:15 Enter the second number:5
The GCD of the two numbers is 5
Result:
Thus the Python program to compute the GCD of two numbers was
created and executed successfully.
Program 10: Exponentiation (power of a number)
Aim: To Write a Python program to find the exponentiation (Power of a
number)
Algorithm:
Step 1: Start
Step 2: r->n
Step 3: Read Base Number n
Step 4: Read Exponent Number e
Step 5: FOR i-> 1 to e
Step 5.1: Compute r->n*r
Step 6: Print ‘Exponent’
Step 7: Stop
Program:
n=int(input("Enter a n value:"))
e=int(input("Enter a power value:"))
r=n
for i in range(1,e):
r=n*r
print ("Exponent",r)
Output:
Enter a number:3
Enter a number:2
Exponent :9
Result:
Thus the a Python program to find the exponentiation (Power of a
number) was created and executed successfully.
Program 11: Program to print the sum of all elements in an array Aim:
To write python program to print the sum of all elements in an array
Algorithm:
Step1: Start
Step2: Declare and initialize an array
Step3: The variable sum will be used to calculate the sum of the elements.
Initialize it to 0
Step4: Loop through the array and add each element of the array to the
variable sum as sum = sum + arr[i]
Step5: Print the sum value
Step6: Stop
Program:
arr = [1, 2, 3, 4, 5]
sum = 0
#Loop through the array to calculate sum of elements
for i in range(0, len(arr)):
sum = sum + arr[i]
print("Sum of all the elements of an array: " + str(sum))
Output:
Sum of all the elements of an array: 15
Program 12: Selection sort
Aim: To write a Python program to sort the elements using selection
sort.
Algorithm:
Step 1: Start
Step 2: Declare list []
Step 3: Set MIN to location 0
Step 4: Search the minimum element in the list. Step 5: Swap with value
at location MIN
Step 6: Increment MIN to point to next element Step 7: Repeat until list is
sorted.
Step 8: Stop
Program:
def selectionSort(a):
for i in range(len(a)):
least=i
for k in range(i+1,len(a)):
if a[k]<a[least]:
least=k
if least !=i:
temp=a[i]
a[i]=a[least]
a[least]=temp
return a
a=[50,30,10,20,40,70,60]
print ("Original list",a)
selectionSort(a)
print("Selection Sort:",a)
Output:
Original list: [50, 30, 10, 20, 40, 70, 60]
Selection Sort: [10, 20, 30, 40, 50, 60, 70]
Program 13: Insertion sort
Aim: To write a Python program to sort the elements using insertion
sort. Algorithm:
Step 1: Start
Step 2: Get the elements from the user
Step 3a: The second element in the list is compared with the elements that
appear before it (only first element in this case).
Step 3b: If the second element is smaller than first element, second element
is inserted in the position of first element. After first step, first two elements
of an array will be sorted.
Step 3c: Pick next element
Step 4: Repeat step 3 until all the elements in the list is sorted Step 5: Stop
Program:
def insertionSort(arr):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >= 0 and key < arr[j] :
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
n=int(input("Enter the no. of element"))
i=0
arr=[i for i in range(n)]
for i in range(0,n):
arr[i]=int(input("enter the array element"))
insertionSort(arr)
print("Sorted element are")
for i in range(len(arr)):
print ("% d" % arr[i])
Output:
Enter the no. of element:5
Enter the array element :7
Enter the array element :8
Enter the array element :5
Enter the array element :7
Enter the array element :9
Sorted element are
5
7
7
8
9
Result:
Thus a Python program to sort the elements using selection sort method
was created and executed successfully
Program:
def mergeSort(nlist):
print("Splitting ",nlist)
if len(nlist)>1:
mid = len(nlist)//2
lefthalf = nlist[:mid]
righthalf = nlist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
nlist[k]=lefthalf[i]
i=i+1
else:
nlist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
nlist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
nlist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",nlist)
nlist =
[14,46,43,27,57,41,45,21,70]
mergeSort(nlist)
print(nlist)
Output:
Splitting [14, 46, 43, 27, 57, 41, 45, 21, 70]
Splitting [14, 46, 43, 27]
Splitting [14, 46]
Splitting [14]
Merging [14]
Splitting [46]
Merging [46]
Merging [14, 46]
Splitting [43, 27]
Splitting [43]
Merging [43]
Splitting [27]
Merging [27]
Merging [27, 43]
Merging [14, 27, 43, 46]
Splitting [57, 41, 45, 21, 70]
Splitting [57, 41]
Splitting [57]
Merging [57]
Splitting [41]
Merging [41]
Merging [41, 57]
Splitting [45, 21, 70]
Splitting [45]
Merging [45]
Splitting [21, 70]
Splitting [21]
Merging [21]
Splitting [70]
Merging [70]
Merging [21, 70]
Merging [21, 45, 70]
Merging [21, 41, 45, 57, 70]
Merging [14, 21, 27, 41, 43, 45, 46, 57, 70]
[14, 21, 27, 41, 43, 45, 46, 57, 70]
Result:
Thus a Python programs to sort the element using merge sort method was
created and executed successfully.
Program:
Output:
**
***
******
*****
Program 16 : Write a Python program to accessing data
members. Aim: Write a Python program to accessing data
members.
Program:
class Person:
def __init__(self, name, age, ssn):
self.name = name # Public attribute
self.age = age # Public attribute
self._ssn = ssn # Private attribute
Explanation:
Class Definition:
The Person class is defined with an __init__ method that initializes the
name, age, and a private attribute _ssn.
Public attributes name and age can be accessed directly. Private
attribute _ssn is meant to be accessed through getter and setter methods.
Methods:
Getters: get_name, get_age, and get_ssn return the values of name, age,
and _ssn, respectively.
Setters: set_name, set_age, and set_ssn modify the values of name, age,
and _ssn, respectively.The display_info method prints all attributes of the
instance.
Creating an Instance:
The program demonstrates how to access and modify the name attribute
directly.It shows how to use getter and setter methods to access and
modify the age and ssn attributes.
Program:
class Person:
def __init__(self, name, age, ssn):
self.name = name # Public attribute
self.age = age # Public attribute
self._ssn = ssn # Private attribute
# Example usage
Explanation
Example Usage: