FirstRanker Logo

FirstRanker.com - FirstRanker's Choice is a hub of Question Papers & Study Materials for B-Tech, B.E, M-Tech, MCA, M.Sc, MBBS, BDS, MBA, B.Sc, Degree, B.Sc Nursing, B-Pharmacy, D-Pharmacy, MD, Medical, Dental, Engineering students. All services of FirstRanker.com are FREE

📱

Get the MBBS Question Bank Android App

Access previous years' papers, solved question papers, notes, and more on the go!

Install From Play Store

Download Anna University B-Tech 1st Year First Year GE8161 Problem Solving And Python Programming PSPP Lab Manual Question Paper

Download Anna University B.Tech/BE (Bachelor of Technology) 1st Year (First Year) First Year GE8161 Problem Solving And Python Programming PSPP Lab Manual Question Paper.

This post was last modified on 13 December 2019

NTRUHS MBBS 3rd Year Part-II Year 2010-2025 Question Papers || Final Year NTRUHS


FirstRanker.com

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

I SEMESTER - R 2017

GE8161– PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY

LABORATORY MANUAL


Name : ____________________________________

--- Content provided by‍ FirstRanker.com ---

Register No : ____________________________________

Section : ____________________________________


DHANALAKSHMI COLLEGE OF ENGINEERING

VISION

is committed to provide highly disciplined, conscientious and enterprising professionals conforming to global standards through value based quality education and training.

MISSION

  • To provide competent technical manpower capable of meeting requirements of the industry
  • --- Content provided by FirstRanker.com ---

  • To contribute to the promotion of Academic excellence in pursuit of Technical Education at different levels
  • To train the students to sell his brawn and brain to the highest bidder but to never put a price tag on heart and soul

PROGRAM EDUCATIONAL OBJECTIVES

  1. FUNDAMENTALS

    To impart students with fundamental knowledge in Mathematics, Science and fundamentals of engineering that will mould them to be successful professionals

    --- Content provided by​ FirstRanker.com ---

  2. CORE COMPETENCE

    To provide students with sound knowledge in engineering and experimental skills to identify complex software problems in industry and to develop practical solutions for them

  3. BREADTH

    --- Content provided by‍ FirstRanker.com ---

    To provide relevant training and experience to bridge the gap between theory and practice this enables them to find solutions for real time problems in industry and organization, and to design products requiring interdisciplinary skills

  4. PROFESSIONALISM SKILLS

    To bestow students with adequate training and provide opportunities to work as team that will build up their communication skills, individual leadership and supportive qualities, and to enable them to adapt and work in ever changing technologies

  5. --- Content provided by‍ FirstRanker.com ---

  6. LIFELONG LEARNING

    To develop the ability of students to establish themselves as professionals in Computer Science and Engineering and to create awareness about the need for lifelong learning and pursuing advanced degrees


PROGRAM OUTCOMES

  1. To apply the basic knowledge of Mathematics, Science and engineering fundamentals in Computer Science and Engineering field
  2. --- Content provided by‌ FirstRanker.com ---

  3. To design and conduct experiments as well as to analyze and interpret data and apply the same in the career
  4. To design and develop innovative and creative software applications
  5. To understand a complex real world problem and develop an efficient practical solution
  6. To create, select and apply appropriate techniques, resources, modern engineering and IT tools
  7. To understand their roles as a professionals and give the best to the society
  8. --- Content provided by​ FirstRanker.com ---

  9. To develop a system that will meet expected needs within realistic constraints such as economical, environmental, social, political, ethical, safe and sustainable
  10. To communicate effectively and make others understand exactly what they are trying to convey in both verbal and written forms
  11. To work in a team as team member or a leader and make unique contributions and work with coordination
  12. To exhibit confidence in self-education and ability for lifelong learning
  13. To develop and manage projects in multidisciplinary environments
  14. --- Content provided by‌ FirstRanker.com ---


GE 8161- PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY

(Common to all branches of B.E. / B.Tech Programmes)

SYLLABUS

COURSE OBJECTIVES

To write, test, and debug simple Python programs.

To implement Python programs with conditionals and loops.

Use functions for structuring Python programs.

--- Content provided by‌ FirstRanker.com ---

Represent compound data using Python lists, tuples, dictionaries.

Read and write data from/to files in Python.

LIST OF EXPERIMENTS:

  1. Compute the GCD of two numbers.
  2. Find the square root of a number (Newton's method)
  3. Exponentiation (power of a number)
  4. --- Content provided by‌ FirstRanker.com ---

  5. Find the maximum of a list of numbers
  6. Linear search and Binary search
  7. Selection sort, Insertion sort
  8. Merge sort
  9. First n prime numbers
  10. --- Content provided by​ FirstRanker.com ---

  11. Multiply matrices
  12. Programs that take command line arguments (word count)
  13. Find the most frequent words in a text read from a file
  14. Simulate elliptical orbits in Pygame
  15. Simulate bouncing ball in Pygame
  16. --- Content provided by⁠ FirstRanker.com ---

COURSE OUTCOMES

Upon completion of the course, students will be able to:

Write, test, and debug simple Python programs.

Implement Python programs with conditionals and loops.

Develop Python programs step-wise by defining functions and calling them.

Use Python lists, tuples, dictionaries for representing compound data.

--- Content provided by‌ FirstRanker.com ---

Read and write data from/to files in Python.


GE 8161 - PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY

CONTENTS

SI.No. Name of the Experiment Page No.
1 Compute the GCD of two numbers. 07
2 Find the square root of a number (Newton's method) 09
3 Exponentiation (power of a number) 11
4 Find the maximum of a list of numbers 13
5(a) Linear search 15
5(b) Binary search 17
6(a) Selection sort 19
6(b) Insertion sort 21
7 Merge sort 23
8 First n prime numbers 26
9 Multiply matrices 28
10 Programs that take command line arguments (word count) 30
11 Find the most frequent words in a text read from a file 32
12 Simulate elliptical orbits in Pygame 34
13 Simulate bouncing ball in Pygame 37

Ex. No. :01

Date:

GCD OF TWO NUMBERS

Aim:

To write a Python program to find GCD of two numbers.

Algorithm:

  1. Define a function named computeGCD()
  2. --- Content provided by​ FirstRanker.com ---

  3. Find the smallest among the two inputs x and y
  4. Perform the following step till smaller+1

    Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i

  5. Print the value of gcd

Program:


--- Content provided by‌ FirstRanker.com ---

def computeGCD(x, y): if x > y: smaller = y else: smaller = x for i in range(1, smaller+1): if((x % i == 0) and (y % i == 0)): gcd = i return gcd num1 = 54
num2 = 24 # take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: ")) print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))

Sample output :


--- Content provided by​ FirstRanker.com ---

The GCD of 54 and 24 is 6

Result:

Thus the Python program to compute GCD of two numbers is executed successfully and the output is verified.


Ex. No. :02

Date:

SQUARE ROOT OF A NUMBER

Aim:

To write a Python Program to find the square root of a number by Newton's Method.

Algorithm:

  1. Define a function named newtonSqrt().
  2. --- Content provided by‍ FirstRanker.com ---

  3. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
  4. Use a while loop with a condition better!=approx to perform the following,
    1. Set approx.=better
    2. Better=0.5*(approx.+n/approx.)
  5. Print the value of approx
  6. --- Content provided by‌ FirstRanker.com ---

Program:


def newtonSqrt(n): approx = 0.5 * n better = 0.5 * (approx + n/approx) while better != approx: approx = better better = 0.5 * (approx + n/approx) return approx print('The square root is', newtonSqrt(81))

Output:


The square root is 9

Result:

Thus the Python program for finding the square root of a given number by Newton's Method is executed successfully and the output is verified.

--- Content provided by‌ FirstRanker.com ---


Ex. No. : 03

Date:

EXPONENTIATION OF A NUMBER

Aim:

To write a Python program to find the exponentiation of a number.

Algorithm:

  1. Define a function named power()
  2. Read the values of base and exp
  3. Use 'if' to check if exp is equal to 1 or not
    1. if exp is equal to 1, then return base
    2. --- Content provided by‌ FirstRanker.com ---

    3. if exp is not equal to 1, then return (base*power(base,exp-1))
  4. Print the result

Program:


def power(base,exp): if(exp==1): return(base) if(exp!=1): return(base*power(base,exp-1)) base=int(input("Enter base: "))

--- Content provided by‌ FirstRanker.com ---

exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))

Output:


Enter base: 7
Enter exponential value: 2

--- Content provided by FirstRanker.com ---

Result:49

Result:

Thus the Python program to find the exponentiation of a number is executed successfully and the output is verified.


Ex. No. : 04

Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

Aim:

To write a Python Program to find the maximum from a list of numbers.

Algorithm:

  1. Create an empty list named I
  2. --- Content provided by‍ FirstRanker.com ---

  3. Read the value of n
  4. Read the elements of the list until n
  5. Assign I[0] as maxno
  6. If I[i]>maxno then set maxno=l[i]
  7. Increment i by 1
  8. --- Content provided by⁠ FirstRanker.com ---

  9. Repeat steps 5-6 until i<n
  10. Print the value of maximum number

Program:


I=[]
n=int(input("enter the upper limit"))

--- Content provided by⁠ FirstRanker.com ---

for i in range(0,n): a=int(input("enter the numbers")) l.append(a)
maxno=I[0]
for i in range(0,len(I)): if l[i]>maxno: maxno=l[i]
print("The maximum number is %d"%maxno)

Output:


--- Content provided by‌ FirstRanker.com ---

Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9
Enter the numbers 90
The maximum number is 90

--- Content provided by‍ FirstRanker.com ---

Result:

Thus a Python Program to find the maximum from the given list of numbers is successfully executed and the output is verified.


Ex. No. : 5(a)

Date:

LINEAR SEARCH

Aim:

To write a Python Program to perform Linear Search

Algorithm:

  1. Read n elements into the list
  2. Read the element to be searched
  3. --- Content provided by​ FirstRanker.com ---

  4. If alist[pos]==item, then print the position of the item
  5. Else increment the position and repeat step 3 until pos reaches the length of the list

Program:


def search(alist,item): pos=0 found=False stop=False while pos<len(alist) and not found and not stop: if alist[pos]==item: found=True print("element found in position",pos) else: if alist[pos]>item: stop=True else: pos=pos+1 return found a=[]
n=int(input("enter upper limit"))

--- Content provided by FirstRanker.com ---

for i in range(0,n): e=int(input("enter the elements")) a.append(e)
x=int(input("enter element to search"))
search(a,x)

Output:


Enter upper limit 5

--- Content provided by⁠ FirstRanker.com ---

Enter the elements 6
Enter the elements 45
Enter the elements 2
Enter the elements 61
Enter the elements 26

--- Content provided by⁠ FirstRanker.com ---

Enter element to search 6
Element found in position 1

Result:

Thus the Python Program to perform linear search is executed successfully and the output is verified.


EX. No. : 5(b)

Date:

BINARY SEARCH

Aim:

To write a Python Program to perform binary search.

--- Content provided by⁠ FirstRanker.com ---

Algorithm:

  1. Read the search element
  2. Find the middle element in the sorted list
  3. Compare the search element with the middle element
    1. if both are matching, print element found
    2. else then check if the search element is smaller or larger than the middle element
  4. --- Content provided by‌ FirstRanker.com ---

  5. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the left sublist of the middle element
  6. If the search element is larger than the middle element, then repeat steps 2 and 3 for the right sublist of the middle element
  7. Repeat the process until the search element if found in the list
  8. If element is not found, loop terminates

Program:


--- Content provided by FirstRanker.com ---

def bsearch(alist,item): first=0 last=len(alist)-1 found=False while first<=last and not found: mid=(first+last)//2 if alist[mid]==item: found=True print("element found in position",mid) else: if item<alist[mid]: last=mid-1 else: first=mid+1 return found a=[]
n=int(input("enter upper limit"))
for i in range(0,n): e=int(input("enter the elements")) a.append(e)
x=int(input("enter element to search"))
bsearch(a,x)

--- Content provided by‌ FirstRanker.com ---

Output:


enter upper limit 6
enter the elements 2
enter the elements 3
enter the elements 5

--- Content provided by FirstRanker.com ---

enter the elements 7
enter the elements 14
enter the elements 25
enter element to search 5
element found in position 2

--- Content provided by FirstRanker.com ---

Result:

Thus the Python Program to perform binary search is executed successfully and the output is verified


Ex. No. : 6(a)

Date:

SELECTION SORT

Aim:

To write a Python Program to perform selection sort.

Algorithm:

  1. Create a function named selectionsort
  2. Initialise pos=0
  3. --- Content provided by⁠ FirstRanker.com ---

  4. If alist[location]>alist[pos] then perform the following till i+1,
  5. Set pos=location
  6. Swap alist[i] and alist[pos]
  7. Print the sorted list

Program:


--- Content provided by‌ FirstRanker.com ---

def selectionSort(alist): for i in range(len(alist)-1,0,-1): pos=0 for location in range(1,i+1): if alist[location]>alist[pos]: pos= location temp = alist[i] alist[i] = alist[pos] alist[pos] = temp alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)

Output:


[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by‌ FirstRanker.com ---

Result:

Thus the Python Program to perform selection sort is successfully executed and the output is verified.


Ex. No. : 6(b)

Date:

INSERTION SORT

Aim:

To write a Python Program to perform insertion sort.

Algorithm:

  1. Create a function named insertionsort
  2. Initialise currentvalue=alist[index] and position=index
  3. --- Content provided by‍ FirstRanker.com ---

  4. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
  5. alist[position]=alist[position-1]
  6. position = position-1
  7. alist[position]=currentvalue
  8. Print the sorted list
  9. --- Content provided by‌ FirstRanker.com ---

Program:


def insertionSort(alist): for index in range(1,len(alist)): currentvalue = alist[index] position = index while position>0 and alist[position-1]>currentvalue: alist[position]=alist[position-1] position = position-1 alist[position]=currentvalue alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)

Output:


--- Content provided by‍ FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]

Result:

Thus the Python program to perform insertion sort is successfully executed and the output is verified.


Ex. No. : 7

Date:

MERGE SORT

Aim:

To write a Python Program to perform Merge sort.

Algorithm:

  1. Create a function named mergesort
  2. --- Content provided by‌ FirstRanker.com ---

  3. Find the mid of the list
  4. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
  5. Initialise i=j=k=0
  6. while i < len(lefthalf) and j < len(righthalf), perform the following

    if lefthalf[i] < righthalf[j]:

    alist[k]=lefthalf[i]

    --- Content provided by⁠ FirstRanker.com ---

    Increment i

    else

    alist[k]=righthalf[j]

    Increment j

    Increment k

    --- Content provided by FirstRanker.com ---

  7. while i < len(lefthalf), perform the following

    alist[k]=lefthalf[i]

    Increment i

    Increment k

  8. --- Content provided by‍ FirstRanker.com ---

  9. while j < len(righthalf), perform the following

    alist[k]=righthalf[j]

    Increment j

    Increment k

  10. Print the sorted list
  11. --- Content provided by‌ FirstRanker.com ---

Program:


def mergeSort(alist): # print("Splitting ",alist) if len(alist)>1: mid = len(alist)//2 lefthalf = alist[:mid] righthalf = alist[mid:] mergeSort(lefthalf) mergeSort(righthalf) i=j=k=0 while i < len(lefthalf) and j < len(righthalf): if lefthalf[i] < righthalf[j]: alist[k]=lefthalf[i] i=i+1 else: alist[k]=righthalf[j] j=j+1 k=k+1 while i < len(lefthalf): alist[k]=lefthalf[i] i=i+1 k=k+1 while j < len(righthalf): alist[k]=righthalf[j] j=j+1 k=k+1 #print("Merging ",alist) alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

Output:


--- Content provided by​ FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]

Result:

Thus the Python program to perform merge sort is successfully executed and the output is verifie


Ex. No.: 8

Date:

FIRST N PRIME NUMBERS

Aim:

To write a Python program to find first n prime numbers.

Algorithm:

  1. Read the value of n
  2. --- Content provided by FirstRanker.com ---

  3. for num in range(0,n + 1), perform the following
  4. if num%i is 0 then break

    else print the value of num

  5. Repeat step 3 for i in range(2,num)

Program:


--- Content provided by FirstRanker.com ---

n = int(input("Enter the upper limit: "))
print("Prime numbers are") for num in range(0,n + 1): # prime numbers are greater than 1 if num > 1: for i in range(2,num): if (num % i) == 0: break else: print(num)

Output:


Enter the upper limit:100
Prime numbers are

--- Content provided by​ FirstRanker.com ---

2
3
5
7
11

--- Content provided by FirstRanker.com ---

13
17
19
23
29

--- Content provided by​ FirstRanker.com ---

31
37
41
43
47

--- Content provided by‌ FirstRanker.com ---

53
59
61
67
71

--- Content provided by‌ FirstRanker.com ---

73
79
83
89
97

--- Content provided by‍ FirstRanker.com ---

Result:

Thus the Python Program to find the first n prime numbers is executed successfully and the output is verified.


Ex. No. : 9

Date:

MATRIX MULTIPLICATION

Aim:

To write a Python program to multiply matrices.

Algorithm:

  1. Define two matrices X and Y
  2. Create a resultant matrix named 'result'
  3. --- Content provided by​ FirstRanker.com ---

  4. for i in range(len(X)):
    1. for j in range(len(Y[0])):
      1. for k in range(len(Y))
      2. result[i][j] += X[i][k]* Y[k][j]
  5. for r in result, print the value of r
  6. --- Content provided by FirstRanker.com ---

Program:


X = [[12,7,3], [4,5,6], [7,8,9]] Y = [[5,8,1,2], [6,7,3,0], [4,5,9,1]] result = [[0,0,0,0], [0,0,0,0], [0,0,0,0]] for i in range(len(X)): for j in range(len(Y[0])): for k in range(len(Y)): result[i][j] += X[i][k]* Y[k][j] for r in result: print(r)

Output:


[114, 160, 60, 27]
[74, 97, 73, 14]

--- Content provided by FirstRanker.com ---

[119, 157, 112, 23]

Result:

Thus the Python program to multiply matrices is executed successfully and the output is verified.


Ex. No. :10

Date:

COMMAND LINE ARGUMENTS(WORD COUNT)

Aim:

To write a Python program for command line arguments.

Algorithm:

  1. Add arguments to find the words and lines
  2. --- Content provided by‌ FirstRanker.com ---

  3. Add the filename as argument
  4. Parse the arguments to get the values
  5. Format and print the words and lines

Program:


parser=argparse.ArgumentParser()

--- Content provided by‌ FirstRanker.com ---

parser.add_argument('--words','-w',action='store_true')
parser.add_argument('--lines','-l',action='store_true')
parser.add_argument('filename')
args=parser.parse_args() if args.words: print("words {}".format(print_words(args.filename))
elif args.lines: print("lines {}".format(print_lines(args.filename))

--- Content provided by‍ FirstRanker.com ---

else: print ("words {}".format(print_words(args.filename)) print("lines {}".format(print_lines(args.filename))

Output:


python main.py -i input.txt
words 23
lines 1

--- Content provided by‍ FirstRanker.com ---

Result:

Thus the Python program for command line arguments is executed successfully and the output is verified.


This download link is referred from the post: NTRUHS MBBS 3rd Year Part-II Year 2010-2025 Question Papers || Final Year NTRUHS

--- Content provided by FirstRanker.com ---