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


FirstRanker Page 1 of 42

?

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



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


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

I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

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






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


Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

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


LABORATORY MANUAL
FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

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


?



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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

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

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY



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





Name : _______________________________________

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

Register No : _______________________________________
Section : _______________________________________


LABORATORY MANUAL

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


FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING


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


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

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

? To provide competent technical manpower capable of meeting requirements of the industry
? 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

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






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






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






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




VISION
MISSION

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

FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

?

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




DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

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


I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

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






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



Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

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



LABORATORY MANUAL

FirstRanker Page 2 of 42

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

DHANALAKSHMI COLLEGE OF ENGINEERING



is committed to provide highly disciplined, c onscientious and

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

enterprising professionals conforming to global standards through value based quality education
and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at

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

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


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






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






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






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


VISION
MISSION

FirstRanker Page 3 of 42

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

PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and

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

fundamentals of engineering that will mould them to be successful professionals

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

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


3. BREADTH
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

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


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

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


5. 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

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

pursuing advanced degrees




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


FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

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

?



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

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



I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

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

LABORATORY




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




Name : _______________________________________
Register No : _______________________________________

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

Section : _______________________________________


LABORATORY MANUAL

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

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING



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

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

? 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

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






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






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






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



VISION
MISSION

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

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS

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

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

2. CORE COMPETENCE
To provide students with sound knowledge in engineering and experimental skills to

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

identify complex software problems in industry and to develop practical solutions for them

3. BREADTH
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

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

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

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

them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

To develop the ability of students to establish themselves as professionals in Computer

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

Science and Engineering and to create awareness about the need for lifelong learning and
pursuing advanced degrees



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




FirstRanker Page 4 of 42
PROGRAM OUTCOMES

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




a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field

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

b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT

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

tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to

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

convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

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






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






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





FirstRanker.com - FirstRanker's Choice

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


FirstRanker Page 1 of 42

?

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



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


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

I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

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






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


Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

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


LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

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




is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education

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

and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at
different levels

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

? 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



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






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






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






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

VISION
MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

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



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

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

3. BREADTH
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

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

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

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

5. 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

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






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


FirstRanker Page 4 of 42
PROGRAM OUTCOMES


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


a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career

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

c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society

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

g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with

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

coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments


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






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






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




FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

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

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


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


To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.

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

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)

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

3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort

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

8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame

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

13.Simulate bouncing ball in Pygame




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

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.




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

COURSE OUTCOMES
COURSE OBJECTIVES
FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

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


?



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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

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

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY



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





Name : _______________________________________

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

Register No : _______________________________________
Section : _______________________________________


LABORATORY MANUAL

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


FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING


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


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

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

? To provide competent technical manpower capable of meeting requirements of the industry
? 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

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






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






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






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




VISION
MISSION

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


FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


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

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

2. CORE COMPETENCE

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

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
To provide relevant training and experience to bridge the gap between theory and practice

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

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

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

will build up their communication skills, individual leadership and supportive qualities, and to enable
them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

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

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


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





FirstRanker Page 4 of 42

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

PROGRAM OUTCOMES



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in

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

Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution

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

e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable

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

h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning

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

k) To develop and manage projects in multidisciplinary environments




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






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






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


FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)

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

SYLLABUS



To write, test, and debug simple Python programs.

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

To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

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

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. Find the maximum of a list of numbers

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

5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices

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

10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

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




Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.

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

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.
Read and write data from/to files in Python.

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




COURSE OUTCOMES
COURSE OBJECTIVES

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


FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

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


Sl.No. Name of the Experiment
Page
No.
1

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

Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)
09

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

3
Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers

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

13
5(a)
Linear search
15
5(b)

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

Binary search
17
6(a)
Selection sort
19

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

6(b)
Insertion sort
21
7
Merge sort

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

23
8
First n prime numbers
26
9

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

Multiply matrices
28
10
Programs that take command line arguments (word count)
30

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

11
Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame

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

34
13
Simulate bouncing ball in Pygame
37

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






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



FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

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


?



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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

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

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY



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





Name : _______________________________________

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

Register No : _______________________________________
Section : _______________________________________


LABORATORY MANUAL

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


FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING


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


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

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

? To provide competent technical manpower capable of meeting requirements of the industry
? 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

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






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






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






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




VISION
MISSION

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


FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


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

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

2. CORE COMPETENCE

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

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
To provide relevant training and experience to bridge the gap between theory and practice

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

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

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

will build up their communication skills, individual leadership and supportive qualities, and to enable
them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

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

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


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





FirstRanker Page 4 of 42

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

PROGRAM OUTCOMES



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in

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

Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution

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

e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable

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

h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning

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

k) To develop and manage projects in multidisciplinary environments




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






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






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


FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)

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

SYLLABUS



To write, test, and debug simple Python programs.

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

To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

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

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. Find the maximum of a list of numbers

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

5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices

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

10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

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




Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.

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

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.
Read and write data from/to files in Python.

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




COURSE OUTCOMES
COURSE OBJECTIVES

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


FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

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


Sl.No. Name of the Experiment
Page
No.
1

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

Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)
09

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

3
Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers

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

13
5(a)
Linear search
15
5(b)

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

Binary search
17
6(a)
Selection sort
19

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

6(b)
Insertion sort
21
7
Merge sort

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

23
8
First n prime numbers
26
9

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

Multiply matrices
28
10
Programs that take command line arguments (word count)
30

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

11
Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame

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

34
13
Simulate bouncing ball in Pygame
37

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






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




FirstRanker Page 7 of 42

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


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

Algorithm:

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


1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i

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

4. Print the value of gcd
Program:

def computeGCD(x, y):
if x > y:

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

smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):

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

gcd = i

return gcd

num1 = 54

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

num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

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


print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))



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





Ex. No. :01

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

Date:

GCD OF TWO NUMBERS
FirstRanker.com - FirstRanker's Choice

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

FirstRanker Page 1 of 42

?


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


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

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


GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY


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






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

Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________


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

LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

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



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

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


? To provide competent technical manpower capable of meeting requirements of the industry
? 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

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

tag on heart and soul




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






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






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





VISION

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

MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

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


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

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

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. LIFELONG LEARNING

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


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

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






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

FirstRanker Page 4 of 42
PROGRAM OUTCOMES



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

a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications

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

d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as

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

economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination

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

j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments



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






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






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



FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

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

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



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

To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

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


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)

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

4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers

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

9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

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





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

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

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.
Read and write data from/to files in Python.

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





COURSE OUTCOMES

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

COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

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

CONTENTS

Sl.No. Name of the Experiment
Page
No.

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

1
Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)

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

09
3
Exponentiation (power of a number)
11
4

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

Find the maximum of a list of numbers
13
5(a)
Linear search
15

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

5(b)
Binary search
17
6(a)
Selection sort

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

19
6(b)
Insertion sort
21
7

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

Merge sort
23
8
First n prime numbers
26

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

9
Multiply matrices
28
10
Programs that take command line arguments (word count)

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

30
11
Find the most frequent words in a text read from a file
32
12

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

Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame
37

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






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





FirstRanker Page 7 of 42

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



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

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

Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1

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

Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

def computeGCD(x, y):

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

if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):

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

if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

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

num1 = 54
num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))

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

# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))


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






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

Ex. No. :01
Date:

GCD OF TWO NUMBERS

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

FirstRanker Page 8 of 42
Sample output :


The GCD of 54 and 24 is 6

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






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






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






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






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






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

Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.
FirstRanker.com - FirstRanker's Choice

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

FirstRanker Page 1 of 42

?


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


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

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


GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY


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






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

Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________


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

LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

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



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

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


? To provide competent technical manpower capable of meeting requirements of the industry
? 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

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

tag on heart and soul




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






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






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





VISION

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

MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

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


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

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

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. LIFELONG LEARNING

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


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

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






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

FirstRanker Page 4 of 42
PROGRAM OUTCOMES



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

a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications

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

d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as

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

economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination

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

j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments



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






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






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



FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

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

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



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

To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

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


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)

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

4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers

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

9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

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





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

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

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.
Read and write data from/to files in Python.

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





COURSE OUTCOMES

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

COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

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

CONTENTS

Sl.No. Name of the Experiment
Page
No.

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

1
Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)

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

09
3
Exponentiation (power of a number)
11
4

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

Find the maximum of a list of numbers
13
5(a)
Linear search
15

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

5(b)
Binary search
17
6(a)
Selection sort

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

19
6(b)
Insertion sort
21
7

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

Merge sort
23
8
First n prime numbers
26

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

9
Multiply matrices
28
10
Programs that take command line arguments (word count)

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

30
11
Find the most frequent words in a text read from a file
32
12

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

Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame
37

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






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





FirstRanker Page 7 of 42

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



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

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

Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1

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

Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

def computeGCD(x, y):

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

if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):

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

if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

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

num1 = 54
num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))

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

# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))


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






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

Ex. No. :01
Date:

GCD OF TWO NUMBERS

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

FirstRanker Page 8 of 42
Sample output :


The GCD of 54 and 24 is 6

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






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






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






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






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






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

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

FirstRanker Page 9 of 42

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


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().

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

2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

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


Program:

def newtonSqrt(n):
approx = 0.5 * n

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

better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

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


print('The square root is' ,newtonSqrt(81))



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






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


Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

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

FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

?

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




DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

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


I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

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






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



Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

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



LABORATORY MANUAL

FirstRanker Page 2 of 42

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

DHANALAKSHMI COLLEGE OF ENGINEERING



is committed to provide highly disciplined, c onscientious and

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

enterprising professionals conforming to global standards through value based quality education
and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at

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

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


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






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






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






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


VISION
MISSION

FirstRanker Page 3 of 42

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

PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and

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

fundamentals of engineering that will mould them to be successful professionals

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

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


3. BREADTH
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

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


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

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


5. 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

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

pursuing advanced degrees




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



FirstRanker Page 4 of 42
PROGRAM OUTCOMES

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



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the

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

same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools

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

f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms

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

i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

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






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






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





FirstRanker Page 5 of 42

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

GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS

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



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.

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

2. Find the square root of a number (Newton ?s method)
3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort

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

7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file

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

12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame



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


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.

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

Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python.



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


COURSE OUTCOMES
COURSE OBJECTIVES

FirstRanker Page 6 of 42

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

GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

Sl.No. Name of the Experiment

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

Page
No.
1
Compute the GCD of two numbers.
07

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

2
Find the square root of a number (Newton ?s method)
09
3
Exponentiation (power of a number)

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

11
4
Find the maximum of a list of numbers
13
5(a)

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

Linear search
15
5(b)
Binary search
17

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

6(a)
Selection sort
19
6(b)
Insertion sort

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

21
7
Merge sort
23
8

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

First n prime numbers
26
9
Multiply matrices
28

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

10
Programs that take command line arguments (word count)
30
11
Find the most frequent words in a text read from a file

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

32
12
Simulate elliptical orbits in Pygame
34
13

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

Simulate bouncing ball in Pygame
37



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






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


FirstRanker Page 7 of 42


Aim:

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

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

Algorithm:

1. Define a function named computeGCD()

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

2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

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


def computeGCD(x, y):
if x > y:
smaller = y
else:

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

smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

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

return gcd

num1 = 54
num2 = 24

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

# 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))

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






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



Ex. No. :01
Date:

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

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42
Sample output :

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


The GCD of 54 and 24 is 6



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






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






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






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






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



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

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


FirstRanker Page 9 of 42

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

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

Algorithm:
1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better

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

ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

Program:

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

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better

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

better = 0.5 * (approx + n/approx)
return approx

print('The square root is' ,newtonSqrt(81))

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






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




Ex. No. :02
Date:

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


SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42
Output:

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


The square root is 9



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






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






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






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






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


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 ---

FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

?

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




DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

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


I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

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






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



Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

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



LABORATORY MANUAL

FirstRanker Page 2 of 42

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

DHANALAKSHMI COLLEGE OF ENGINEERING



is committed to provide highly disciplined, c onscientious and

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

enterprising professionals conforming to global standards through value based quality education
and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at

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

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


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






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






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






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


VISION
MISSION

FirstRanker Page 3 of 42

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

PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and

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

fundamentals of engineering that will mould them to be successful professionals

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

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


3. BREADTH
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

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


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

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


5. 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

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

pursuing advanced degrees




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



FirstRanker Page 4 of 42
PROGRAM OUTCOMES

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



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the

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

same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools

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

f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms

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

i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

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






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






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





FirstRanker Page 5 of 42

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

GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS

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



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.

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

2. Find the square root of a number (Newton ?s method)
3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort

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

7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file

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

12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame



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


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.

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

Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python.



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


COURSE OUTCOMES
COURSE OBJECTIVES

FirstRanker Page 6 of 42

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

GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

Sl.No. Name of the Experiment

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

Page
No.
1
Compute the GCD of two numbers.
07

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

2
Find the square root of a number (Newton ?s method)
09
3
Exponentiation (power of a number)

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

11
4
Find the maximum of a list of numbers
13
5(a)

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

Linear search
15
5(b)
Binary search
17

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

6(a)
Selection sort
19
6(b)
Insertion sort

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

21
7
Merge sort
23
8

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

First n prime numbers
26
9
Multiply matrices
28

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

10
Programs that take command line arguments (word count)
30
11
Find the most frequent words in a text read from a file

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

32
12
Simulate elliptical orbits in Pygame
34
13

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

Simulate bouncing ball in Pygame
37



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






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


FirstRanker Page 7 of 42


Aim:

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

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

Algorithm:

1. Define a function named computeGCD()

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

2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

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


def computeGCD(x, y):
if x > y:
smaller = y
else:

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

smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

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

return gcd

num1 = 54
num2 = 24

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

# 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))

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






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



Ex. No. :01
Date:

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

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42
Sample output :

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


The GCD of 54 and 24 is 6



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






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






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






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






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



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

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


FirstRanker Page 9 of 42

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

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

Algorithm:
1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better

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

ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

Program:

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

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better

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

better = 0.5 * (approx + n/approx)
return approx

print('The square root is' ,newtonSqrt(81))

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






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




Ex. No. :02
Date:

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


SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42
Output:

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


The square root is 9



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






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






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






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






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


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 ---


FirstRanker Page 11 of 42

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

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


Algorithm:

1. Define a function named power()
2. Read the values of base and exp

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

3. Use ?if ? to check if exp is equal to 1 or not
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

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

Program:
def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):

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

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

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






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





Ex. No. : 03

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

Date:

EXPONENTIATION OF A NUMBER
FirstRanker.com - FirstRanker's Choice

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

FirstRanker Page 1 of 42

?


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


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

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


GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY


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






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

Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________


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

LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

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



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

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


? To provide competent technical manpower capable of meeting requirements of the industry
? 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

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

tag on heart and soul




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






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






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





VISION

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

MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

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


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

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

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. LIFELONG LEARNING

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


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

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






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

FirstRanker Page 4 of 42
PROGRAM OUTCOMES



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

a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications

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

d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as

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

economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination

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

j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments



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






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






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



FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

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

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



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

To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

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


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)

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

4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers

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

9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

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





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

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

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.
Read and write data from/to files in Python.

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





COURSE OUTCOMES

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

COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

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

CONTENTS

Sl.No. Name of the Experiment
Page
No.

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

1
Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)

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

09
3
Exponentiation (power of a number)
11
4

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

Find the maximum of a list of numbers
13
5(a)
Linear search
15

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

5(b)
Binary search
17
6(a)
Selection sort

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

19
6(b)
Insertion sort
21
7

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

Merge sort
23
8
First n prime numbers
26

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

9
Multiply matrices
28
10
Programs that take command line arguments (word count)

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

30
11
Find the most frequent words in a text read from a file
32
12

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

Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame
37

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






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





FirstRanker Page 7 of 42

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



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

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

Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1

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

Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

def computeGCD(x, y):

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

if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):

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

if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

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

num1 = 54
num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))

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

# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))


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






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

Ex. No. :01
Date:

GCD OF TWO NUMBERS

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

FirstRanker Page 8 of 42
Sample output :


The GCD of 54 and 24 is 6

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






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






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






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






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






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

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

FirstRanker Page 9 of 42

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


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().

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

2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

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


Program:

def newtonSqrt(n):
approx = 0.5 * n

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

better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

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


print('The square root is' ,newtonSqrt(81))



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






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


Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

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


FirstRanker Page 10 of 42
Output:

The square root is 9

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






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






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






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






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





Result:

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

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.


FirstRanker Page 11 of 42

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


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

Algorithm:

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


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
i. if exp is equal to 1, then return base

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

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

Program:
def power(base,exp):

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

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))



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






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



Ex. No. : 03
Date:

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

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:
Enter base: 7

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

Enter exponential value: 2
Result:49



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






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






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






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






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




Result:
Thus the Python program to find the exponentiation of a number is executed successfully

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

and the output is verified.
FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

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

?



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

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



I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

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

LABORATORY




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




Name : _______________________________________
Register No : _______________________________________

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

Section : _______________________________________


LABORATORY MANUAL

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

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING



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

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

? 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

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






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






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






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



VISION
MISSION

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

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS

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

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

2. CORE COMPETENCE
To provide students with sound knowledge in engineering and experimental skills to

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

identify complex software problems in industry and to develop practical solutions for them

3. BREADTH
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

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

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

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

them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

To develop the ability of students to establish themselves as professionals in Computer

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

Science and Engineering and to create awareness about the need for lifelong learning and
pursuing advanced degrees



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




FirstRanker Page 4 of 42
PROGRAM OUTCOMES

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




a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field

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

b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT

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

tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to

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

convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

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






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






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






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

FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS

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




To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.

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

Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

LIST OF EXPERIMENTS:

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

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. Find the maximum of a list of numbers
5. Linear search and Binary search

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

6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)

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

11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame


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



Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.
Implement Python programs with conditionals and loops.

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

Develop Python programs step-wise by defining functions and calling them.
Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python.


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



COURSE OUTCOMES
COURSE OBJECTIVES

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

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

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

Sl.No. Name of the Experiment
Page
No.
1
Compute the GCD of two numbers.

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

07
2
Find the square root of a number (Newton ?s method)
09
3

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

Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers
13

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

5(a)
Linear search
15
5(b)
Binary search

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

17
6(a)
Selection sort
19
6(b)

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

Insertion sort
21
7
Merge sort
23

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

8
First n prime numbers
26
9
Multiply matrices

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

28
10
Programs that take command line arguments (word count)
30
11

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

Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame
34

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

13
Simulate bouncing ball in Pygame
37


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






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



FirstRanker Page 7 of 42


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

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

Algorithm:

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

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd

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

Program:

def computeGCD(x, y):
if x > y:
smaller = y

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

else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

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


return gcd

num1 = 54
num2 = 24

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


# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

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

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))




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




Ex. No. :01
Date:

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


GCD OF TWO NUMBERS

FirstRanker Page 8 of 42
Sample output :

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



The GCD of 54 and 24 is 6


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






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






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






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






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




Result:
Thus the Python program to compute GCD of two numbers is executed successfully and

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

the output is verified.

FirstRanker Page 9 of 42

Aim:

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

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,

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

i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

Program:

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


def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:

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

approx = better
better = 0.5 * (approx + n/approx)
return approx

print('The square root is' ,newtonSqrt(81))

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






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





Ex. No. :02

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

Date:

SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42

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

Output:

The square root is 9


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






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






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






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






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



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 ---



FirstRanker Page 11 of 42

Aim:

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

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

Algorithm:

1. Define a function named power()

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

2. Read the values of base and exp
3. Use ?if ? to check if exp is equal to 1 or not
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

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


Program:
def power(base,exp):
if(exp==1):
return(base)

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

if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))

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






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






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

Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

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

FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2
Result:49

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






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






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






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






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






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


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

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

FirstRanker Page 13 of 42


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

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

Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno

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

5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:

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

l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))
l.append(a)

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

maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)

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






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



Ex. No. :
04
Date:

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


FINDING MAXIMUM FROM A LIST OF NUMBERS
FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

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


?



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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

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

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY



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





Name : _______________________________________

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

Register No : _______________________________________
Section : _______________________________________


LABORATORY MANUAL

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


FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING


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


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

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

? To provide competent technical manpower capable of meeting requirements of the industry
? 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

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






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






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






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




VISION
MISSION

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


FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


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

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

2. CORE COMPETENCE

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

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
To provide relevant training and experience to bridge the gap between theory and practice

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

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

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

will build up their communication skills, individual leadership and supportive qualities, and to enable
them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

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

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


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





FirstRanker Page 4 of 42

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

PROGRAM OUTCOMES



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in

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

Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution

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

e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable

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

h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning

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

k) To develop and manage projects in multidisciplinary environments




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






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






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


FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)

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

SYLLABUS



To write, test, and debug simple Python programs.

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

To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

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

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. Find the maximum of a list of numbers

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

5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices

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

10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

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




Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.

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

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.
Read and write data from/to files in Python.

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




COURSE OUTCOMES
COURSE OBJECTIVES

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


FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

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


Sl.No. Name of the Experiment
Page
No.
1

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

Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)
09

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

3
Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers

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

13
5(a)
Linear search
15
5(b)

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

Binary search
17
6(a)
Selection sort
19

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

6(b)
Insertion sort
21
7
Merge sort

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

23
8
First n prime numbers
26
9

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

Multiply matrices
28
10
Programs that take command line arguments (word count)
30

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

11
Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame

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

34
13
Simulate bouncing ball in Pygame
37

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






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




FirstRanker Page 7 of 42

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


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

Algorithm:

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


1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i

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

4. Print the value of gcd
Program:

def computeGCD(x, y):
if x > y:

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

smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):

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

gcd = i

return gcd

num1 = 54

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

num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

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


print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))



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





Ex. No. :01

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

Date:

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42

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

Sample output :


The GCD of 54 and 24 is 6

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






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






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






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






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





Result:

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

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

FirstRanker Page 9 of 42

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

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)

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

3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

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

Program:

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)

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

while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

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

print('The square root is' ,newtonSqrt(81))




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






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

Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

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

FirstRanker Page 10 of 42
Output:

The square root is 9

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






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






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






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






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




Result:
Thus the Python program for finding the square root of a given number by Newton ?s

--- Content provided by FirstRanker.com ---

Method is executed successfully and the output is verified.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---

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
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

4. Print the result

Program:
def power(base,exp):
if(exp==1):

--- Content provided by FirstRanker.com ---

return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))

--- Content provided by FirstRanker.com ---

print("Result:",power(base,exp))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2

--- Content provided by FirstRanker.com ---

Result:49




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---


FirstRanker Page 13 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n

--- Content provided by FirstRanker.com ---

4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---

Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))

--- Content provided by FirstRanker.com ---

l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]

--- Content provided by FirstRanker.com ---

print("The maximum number is %d"%maxno)




--- Content provided by FirstRanker.com ---




Ex. No. :
04

--- Content provided by FirstRanker.com ---

Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9
Enter the numbers 90

--- Content provided by FirstRanker.com ---

The maximum number is 90




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.
FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---

FirstRanker Page 1 of 42

?


--- Content provided by FirstRanker.com ---


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---


GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________


--- Content provided by FirstRanker.com ---

LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---



is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---


? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---

tag on heart and soul




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





VISION

--- Content provided by FirstRanker.com ---

MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---

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. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 4 of 42
PROGRAM OUTCOMES



--- Content provided by FirstRanker.com ---

a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications

--- Content provided by FirstRanker.com ---

d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as

--- Content provided by FirstRanker.com ---

economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination

--- Content provided by FirstRanker.com ---

j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS



--- Content provided by FirstRanker.com ---

To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---


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)

--- Content provided by FirstRanker.com ---

4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers

--- Content provided by FirstRanker.com ---

9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---





Upon completion of the course, students will be able to:

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---





COURSE OUTCOMES

--- Content provided by FirstRanker.com ---

COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

CONTENTS

Sl.No. Name of the Experiment
Page
No.

--- Content provided by FirstRanker.com ---

1
Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)

--- Content provided by FirstRanker.com ---

09
3
Exponentiation (power of a number)
11
4

--- Content provided by FirstRanker.com ---

Find the maximum of a list of numbers
13
5(a)
Linear search
15

--- Content provided by FirstRanker.com ---

5(b)
Binary search
17
6(a)
Selection sort

--- Content provided by FirstRanker.com ---

19
6(b)
Insertion sort
21
7

--- Content provided by FirstRanker.com ---

Merge sort
23
8
First n prime numbers
26

--- Content provided by FirstRanker.com ---

9
Multiply matrices
28
10
Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

30
11
Find the most frequent words in a text read from a file
32
12

--- Content provided by FirstRanker.com ---

Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---

Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1

--- Content provided by FirstRanker.com ---

Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

def computeGCD(x, y):

--- Content provided by FirstRanker.com ---

if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):

--- Content provided by FirstRanker.com ---

if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---

num1 = 54
num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))

--- Content provided by FirstRanker.com ---

# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 8 of 42
Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---


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().

--- Content provided by FirstRanker.com ---

2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---


Program:

def newtonSqrt(n):
approx = 0.5 * n

--- Content provided by FirstRanker.com ---

better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---


print('The square root is' ,newtonSqrt(81))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

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.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---


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
i. if exp is equal to 1, then return base

--- Content provided by FirstRanker.com ---

ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:
def power(base,exp):

--- Content provided by FirstRanker.com ---

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))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:
Enter base: 7

--- Content provided by FirstRanker.com ---

Enter exponential value: 2
Result:49



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to find the exponentiation of a number is executed successfully

--- Content provided by FirstRanker.com ---

and the output is verified.

FirstRanker Page 13 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n

--- Content provided by FirstRanker.com ---

3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---


Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:

--- Content provided by FirstRanker.com ---

maxno=l[i]
print("The maximum number is %d"%maxno)



--- Content provided by FirstRanker.com ---





Ex. No. :

--- Content provided by FirstRanker.com ---

04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9

--- Content provided by FirstRanker.com ---

Enter the numbers 90
The maximum number is 90



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:

--- Content provided by FirstRanker.com ---

def search(alist,item):
pos=0
found=False
stop=False
while pos if alist[pos]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",pos)
else:
if alist[pos]>item:
stop=True

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

Ex. No. : 5(a)
Date:

LINEAR SEARCH
FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---


FirstRanker Page 1 of 42

?

--- Content provided by FirstRanker.com ---



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


--- Content provided by FirstRanker.com ---

I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

--- Content provided by FirstRanker.com ---


LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---




is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education

--- Content provided by FirstRanker.com ---

and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at
different levels

--- Content provided by FirstRanker.com ---

? 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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

VISION
MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---



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

--- Content provided by FirstRanker.com ---

3. BREADTH
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

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

5. 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 4 of 42
PROGRAM OUTCOMES


--- Content provided by FirstRanker.com ---


a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career

--- Content provided by FirstRanker.com ---

c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society

--- Content provided by FirstRanker.com ---

g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with

--- Content provided by FirstRanker.com ---

coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS


--- Content provided by FirstRanker.com ---


To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort

--- Content provided by FirstRanker.com ---

8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

13.Simulate bouncing ball in Pygame




--- Content provided by FirstRanker.com ---

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.




--- Content provided by FirstRanker.com ---

COURSE OUTCOMES
COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
CONTENTS

Sl.No. Name of the Experiment
Page

--- Content provided by FirstRanker.com ---

No.
1
Compute the GCD of two numbers.
07
2

--- Content provided by FirstRanker.com ---

Find the square root of a number (Newton ?s method)
09
3
Exponentiation (power of a number)
11

--- Content provided by FirstRanker.com ---

4
Find the maximum of a list of numbers
13
5(a)
Linear search

--- Content provided by FirstRanker.com ---

15
5(b)
Binary search
17
6(a)

--- Content provided by FirstRanker.com ---

Selection sort
19
6(b)
Insertion sort
21

--- Content provided by FirstRanker.com ---

7
Merge sort
23
8
First n prime numbers

--- Content provided by FirstRanker.com ---

26
9
Multiply matrices
28
10

--- Content provided by FirstRanker.com ---

Programs that take command line arguments (word count)
30
11
Find the most frequent words in a text read from a file
32

--- Content provided by FirstRanker.com ---

12
Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---

37




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 7 of 42


Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---


Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y

--- Content provided by FirstRanker.com ---

3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

--- Content provided by FirstRanker.com ---

def computeGCD(x, y):
if x > y:
smaller = y
else:
smaller = x

--- Content provided by FirstRanker.com ---

for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---


num1 = 54
num2 = 24

# take input from the user

--- Content provided by FirstRanker.com ---

# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 8 of 42
Sample output :


--- Content provided by FirstRanker.com ---

The GCD of 54 and 24 is 6




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

--- Content provided by FirstRanker.com ---

FirstRanker Page 9 of 42

Aim:
To write a Python Program to find the square root of a number by Newton ?s Method.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

4. Print the value of approx

Program:

def newtonSqrt(n):

--- Content provided by FirstRanker.com ---

approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

return approx

print('The square root is' ,newtonSqrt(81))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :02
Date:

--- Content provided by FirstRanker.com ---

SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42
Output:

--- Content provided by FirstRanker.com ---

The square root is 9




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

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 ---

FirstRanker Page 11 of 42

Aim:
To write a Python program to find the exponentiation of a number.

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:

--- Content provided by FirstRanker.com ---

def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---


EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:

--- Content provided by FirstRanker.com ---

Enter base: 7
Enter exponential value: 2
Result:49


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

FirstRanker Page 13 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l

--- Content provided by FirstRanker.com ---

2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1

--- Content provided by FirstRanker.com ---

7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:
l=[]
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=l[0]
for i in range(0,len(l)):

--- Content provided by FirstRanker.com ---

if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :
04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6

--- Content provided by FirstRanker.com ---

Enter the numbers 9
Enter the numbers 90
The maximum number is 90


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---


FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list

--- Content provided by FirstRanker.com ---

Program:
def search(alist,item):
pos=0
found=False
stop=False

--- Content provided by FirstRanker.com ---

while pos if alist[pos]==item:
found=True
print("element found in position",pos)
else:
if alist[pos]>item:

--- Content provided by FirstRanker.com ---

stop=True
else:
pos=pos+1
return found
a=[]

--- Content provided by FirstRanker.com ---

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"))

--- Content provided by FirstRanker.com ---

search(a,x)
Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---


FirstRanker Page 16 of 42

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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to perform linear search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified.


FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---

FirstRanker Page 1 of 42

?


--- Content provided by FirstRanker.com ---


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---


GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________


--- Content provided by FirstRanker.com ---

LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---



is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---


? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---

tag on heart and soul




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





VISION

--- Content provided by FirstRanker.com ---

MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---

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. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 4 of 42
PROGRAM OUTCOMES



--- Content provided by FirstRanker.com ---

a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications

--- Content provided by FirstRanker.com ---

d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as

--- Content provided by FirstRanker.com ---

economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination

--- Content provided by FirstRanker.com ---

j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS



--- Content provided by FirstRanker.com ---

To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---


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)

--- Content provided by FirstRanker.com ---

4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers

--- Content provided by FirstRanker.com ---

9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---





Upon completion of the course, students will be able to:

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---





COURSE OUTCOMES

--- Content provided by FirstRanker.com ---

COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

CONTENTS

Sl.No. Name of the Experiment
Page
No.

--- Content provided by FirstRanker.com ---

1
Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)

--- Content provided by FirstRanker.com ---

09
3
Exponentiation (power of a number)
11
4

--- Content provided by FirstRanker.com ---

Find the maximum of a list of numbers
13
5(a)
Linear search
15

--- Content provided by FirstRanker.com ---

5(b)
Binary search
17
6(a)
Selection sort

--- Content provided by FirstRanker.com ---

19
6(b)
Insertion sort
21
7

--- Content provided by FirstRanker.com ---

Merge sort
23
8
First n prime numbers
26

--- Content provided by FirstRanker.com ---

9
Multiply matrices
28
10
Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

30
11
Find the most frequent words in a text read from a file
32
12

--- Content provided by FirstRanker.com ---

Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---

Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1

--- Content provided by FirstRanker.com ---

Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

def computeGCD(x, y):

--- Content provided by FirstRanker.com ---

if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):

--- Content provided by FirstRanker.com ---

if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---

num1 = 54
num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))

--- Content provided by FirstRanker.com ---

# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 8 of 42
Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---


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().

--- Content provided by FirstRanker.com ---

2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---


Program:

def newtonSqrt(n):
approx = 0.5 * n

--- Content provided by FirstRanker.com ---

better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---


print('The square root is' ,newtonSqrt(81))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

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.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---


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
i. if exp is equal to 1, then return base

--- Content provided by FirstRanker.com ---

ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:
def power(base,exp):

--- Content provided by FirstRanker.com ---

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))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:
Enter base: 7

--- Content provided by FirstRanker.com ---

Enter exponential value: 2
Result:49



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to find the exponentiation of a number is executed successfully

--- Content provided by FirstRanker.com ---

and the output is verified.

FirstRanker Page 13 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n

--- Content provided by FirstRanker.com ---

3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---


Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:

--- Content provided by FirstRanker.com ---

maxno=l[i]
print("The maximum number is %d"%maxno)



--- Content provided by FirstRanker.com ---





Ex. No. :

--- Content provided by FirstRanker.com ---

04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9

--- Content provided by FirstRanker.com ---

Enter the numbers 90
The maximum number is 90



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:

--- Content provided by FirstRanker.com ---

def search(alist,item):
pos=0
found=False
stop=False
while pos if alist[pos]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",pos)
else:
if alist[pos]>item:
stop=True

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---

FirstRanker Page 16 of 42

Output:
Enter upper limit 5
Enter the elements 6

--- Content provided by FirstRanker.com ---

Enter the elements 45
Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6

--- Content provided by FirstRanker.com ---

Element found in position 1




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---




FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element

--- Content provided by FirstRanker.com ---

2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the

--- Content provided by FirstRanker.com ---

left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---


Program:
def bsearch(alist,item):
first=0
last=len(alist)-1

--- Content provided by FirstRanker.com ---

found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---


BINARY SEARCH
FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

--- Content provided by FirstRanker.com ---


?



--- Content provided by FirstRanker.com ---

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY



--- Content provided by FirstRanker.com ---





Name : _______________________________________

--- Content provided by FirstRanker.com ---

Register No : _______________________________________
Section : _______________________________________


LABORATORY MANUAL

--- Content provided by FirstRanker.com ---


FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING


--- Content provided by FirstRanker.com ---


is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---

? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




VISION
MISSION

--- Content provided by FirstRanker.com ---


FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


--- Content provided by FirstRanker.com ---

1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and
fundamentals of engineering that will mould them to be successful professionals

2. CORE COMPETENCE

--- Content provided by FirstRanker.com ---

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
To provide relevant training and experience to bridge the gap between theory and practice

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

will build up their communication skills, individual leadership and supportive qualities, and to enable
them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---

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


--- Content provided by FirstRanker.com ---





FirstRanker Page 4 of 42

--- Content provided by FirstRanker.com ---

PROGRAM OUTCOMES



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in

--- Content provided by FirstRanker.com ---

Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution

--- Content provided by FirstRanker.com ---

e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable

--- Content provided by FirstRanker.com ---

h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning

--- Content provided by FirstRanker.com ---

k) To develop and manage projects in multidisciplinary environments




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)

--- Content provided by FirstRanker.com ---

SYLLABUS



To write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---

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. Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices

--- Content provided by FirstRanker.com ---

10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---




Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---




COURSE OUTCOMES
COURSE OBJECTIVES

--- Content provided by FirstRanker.com ---


FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

--- Content provided by FirstRanker.com ---


Sl.No. Name of the Experiment
Page
No.
1

--- Content provided by FirstRanker.com ---

Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)
09

--- Content provided by FirstRanker.com ---

3
Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

13
5(a)
Linear search
15
5(b)

--- Content provided by FirstRanker.com ---

Binary search
17
6(a)
Selection sort
19

--- Content provided by FirstRanker.com ---

6(b)
Insertion sort
21
7
Merge sort

--- Content provided by FirstRanker.com ---

23
8
First n prime numbers
26
9

--- Content provided by FirstRanker.com ---

Multiply matrices
28
10
Programs that take command line arguments (word count)
30

--- Content provided by FirstRanker.com ---

11
Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find GCD of two numbers.

Algorithm:

--- Content provided by FirstRanker.com ---


1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i

--- Content provided by FirstRanker.com ---

4. Print the value of gcd
Program:

def computeGCD(x, y):
if x > y:

--- Content provided by FirstRanker.com ---

smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):

--- Content provided by FirstRanker.com ---

gcd = i

return gcd

num1 = 54

--- Content provided by FirstRanker.com ---

num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

--- Content provided by FirstRanker.com ---


print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))



--- Content provided by FirstRanker.com ---





Ex. No. :01

--- Content provided by FirstRanker.com ---

Date:

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42

--- Content provided by FirstRanker.com ---

Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---

Program:

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---

print('The square root is' ,newtonSqrt(81))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---

FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program for finding the square root of a given number by Newton ?s

--- Content provided by FirstRanker.com ---

Method is executed successfully and the output is verified.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---

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
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

4. Print the result

Program:
def power(base,exp):
if(exp==1):

--- Content provided by FirstRanker.com ---

return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))

--- Content provided by FirstRanker.com ---

print("Result:",power(base,exp))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2

--- Content provided by FirstRanker.com ---

Result:49




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---


FirstRanker Page 13 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n

--- Content provided by FirstRanker.com ---

4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---

Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))

--- Content provided by FirstRanker.com ---

l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]

--- Content provided by FirstRanker.com ---

print("The maximum number is %d"%maxno)




--- Content provided by FirstRanker.com ---




Ex. No. :
04

--- Content provided by FirstRanker.com ---

Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9
Enter the numbers 90

--- Content provided by FirstRanker.com ---

The maximum number is 90




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

FirstRanker Page 15 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform Linear Search
Algorithm:
1. Read n elements into the list

--- Content provided by FirstRanker.com ---

2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):

--- Content provided by FirstRanker.com ---

pos=0
found=False
stop=False
while pos if alist[pos]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:

--- Content provided by FirstRanker.com ---

pos=pos+1
return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

e=int(input("enter the elements"))
a.append(e)
x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)

--- Content provided by FirstRanker.com ---

Date:

LINEAR SEARCH

FirstRanker Page 16 of 42

--- Content provided by FirstRanker.com ---


Output:
Enter upper limit 5
Enter the elements 6
Enter the elements 45

--- Content provided by FirstRanker.com ---

Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6
Element found in position 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---



FirstRanker Page 17 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list

--- Content provided by FirstRanker.com ---

3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element

--- Content provided by FirstRanker.com ---

5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---

Program:
def bsearch(alist,item):
first=0
last=len(alist)-1
found=False

--- Content provided by FirstRanker.com ---

while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)

--- Content provided by FirstRanker.com ---

else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---

BINARY SEARCH

FirstRanker Page 18 of 42
else:
first=mid+mid-1

--- Content provided by FirstRanker.com ---

return found

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

?

--- Content provided by FirstRanker.com ---




DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

--- Content provided by FirstRanker.com ---


I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

--- Content provided by FirstRanker.com ---



LABORATORY MANUAL

FirstRanker Page 2 of 42

--- Content provided by FirstRanker.com ---

DHANALAKSHMI COLLEGE OF ENGINEERING



is committed to provide highly disciplined, c onscientious and

--- Content provided by FirstRanker.com ---

enterprising professionals conforming to global standards through value based quality education
and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at

--- Content provided by FirstRanker.com ---

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


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


VISION
MISSION

FirstRanker Page 3 of 42

--- Content provided by FirstRanker.com ---

PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and

--- Content provided by FirstRanker.com ---

fundamentals of engineering that will mould them to be successful professionals

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

--- Content provided by FirstRanker.com ---


3. BREADTH
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

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---


5. 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

--- Content provided by FirstRanker.com ---

pursuing advanced degrees




--- Content provided by FirstRanker.com ---



FirstRanker Page 4 of 42
PROGRAM OUTCOMES

--- Content provided by FirstRanker.com ---



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the

--- Content provided by FirstRanker.com ---

same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools

--- Content provided by FirstRanker.com ---

f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms

--- Content provided by FirstRanker.com ---

i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





FirstRanker Page 5 of 42

--- Content provided by FirstRanker.com ---

GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS

--- Content provided by FirstRanker.com ---



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.

--- Content provided by FirstRanker.com ---

2. Find the square root of a number (Newton ?s method)
3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort

--- Content provided by FirstRanker.com ---

7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file

--- Content provided by FirstRanker.com ---

12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame



--- Content provided by FirstRanker.com ---


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.

--- Content provided by FirstRanker.com ---

Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python.



--- Content provided by FirstRanker.com ---


COURSE OUTCOMES
COURSE OBJECTIVES

FirstRanker Page 6 of 42

--- Content provided by FirstRanker.com ---

GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

Sl.No. Name of the Experiment

--- Content provided by FirstRanker.com ---

Page
No.
1
Compute the GCD of two numbers.
07

--- Content provided by FirstRanker.com ---

2
Find the square root of a number (Newton ?s method)
09
3
Exponentiation (power of a number)

--- Content provided by FirstRanker.com ---

11
4
Find the maximum of a list of numbers
13
5(a)

--- Content provided by FirstRanker.com ---

Linear search
15
5(b)
Binary search
17

--- Content provided by FirstRanker.com ---

6(a)
Selection sort
19
6(b)
Insertion sort

--- Content provided by FirstRanker.com ---

21
7
Merge sort
23
8

--- Content provided by FirstRanker.com ---

First n prime numbers
26
9
Multiply matrices
28

--- Content provided by FirstRanker.com ---

10
Programs that take command line arguments (word count)
30
11
Find the most frequent words in a text read from a file

--- Content provided by FirstRanker.com ---

32
12
Simulate elliptical orbits in Pygame
34
13

--- Content provided by FirstRanker.com ---

Simulate bouncing ball in Pygame
37



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 7 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find GCD of two numbers.

Algorithm:

1. Define a function named computeGCD()

--- Content provided by FirstRanker.com ---

2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

--- Content provided by FirstRanker.com ---


def computeGCD(x, y):
if x > y:
smaller = y
else:

--- Content provided by FirstRanker.com ---

smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

--- Content provided by FirstRanker.com ---

return gcd

num1 = 54
num2 = 24

--- Content provided by FirstRanker.com ---

# 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))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :01
Date:

--- Content provided by FirstRanker.com ---

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42
Sample output :

--- Content provided by FirstRanker.com ---


The GCD of 54 and 24 is 6



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

--- Content provided by FirstRanker.com ---


FirstRanker Page 9 of 42

Aim:
To write a Python Program to find the square root of a number by Newton ?s Method.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better

--- Content provided by FirstRanker.com ---

ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

Program:

--- Content provided by FirstRanker.com ---

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better

--- Content provided by FirstRanker.com ---

better = 0.5 * (approx + n/approx)
return approx

print('The square root is' ,newtonSqrt(81))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. :02
Date:

--- Content provided by FirstRanker.com ---


SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42
Output:

--- Content provided by FirstRanker.com ---


The square root is 9



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


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 ---


FirstRanker Page 11 of 42

Aim:
To write a Python program to find the exponentiation of a number.

--- Content provided by FirstRanker.com ---


Algorithm:

1. Define a function named power()
2. Read the values of base and exp

--- Content provided by FirstRanker.com ---

3. Use ?if ? to check if exp is equal to 1 or not
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

--- Content provided by FirstRanker.com ---

Program:
def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):

--- Content provided by FirstRanker.com ---

return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Ex. No. : 03

--- Content provided by FirstRanker.com ---

Date:

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter base: 7
Enter exponential value: 2
Result:49

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

FirstRanker Page 13 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]

--- Content provided by FirstRanker.com ---

6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:
l=[]

--- Content provided by FirstRanker.com ---

n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]

--- Content provided by FirstRanker.com ---

for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :
04
Date:

--- Content provided by FirstRanker.com ---

FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42
Output:
Enter the upper limt 3

--- Content provided by FirstRanker.com ---

Enter the numbers 6
Enter the numbers 9
Enter the numbers 90
The maximum number is 90

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus a Python Program to find the maximum from the given list of numbers is successfully

--- Content provided by FirstRanker.com ---

executed and the output is verified.

FirstRanker Page 15 of 42

Aim:

--- Content provided by FirstRanker.com ---

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. If alist[pos]==item, then print the position of the item

--- Content provided by FirstRanker.com ---

4. 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

--- Content provided by FirstRanker.com ---

stop=False
while pos if alist[pos]==item:
found=True
print("element found in position",pos)
else:

--- Content provided by FirstRanker.com ---

if alist[pos]>item:
stop=True
else:
pos=pos+1
return found

--- Content provided by FirstRanker.com ---

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))
a.append(e)

--- Content provided by FirstRanker.com ---

x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)
Date:

--- Content provided by FirstRanker.com ---

LINEAR SEARCH

FirstRanker Page 16 of 42

Output:

--- Content provided by FirstRanker.com ---

Enter upper limit 5
Enter the elements 6
Enter the elements 45
Enter the elements 2
Enter the elements 61

--- Content provided by FirstRanker.com ---

Enter the elements 26
Enter element to search 6
Element found in position 1


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to perform linear search is executed successfully and the output
is verified.



--- Content provided by FirstRanker.com ---

FirstRanker Page 17 of 42


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
i. if both are matching, print element found

--- Content provided by FirstRanker.com ---

ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element

--- Content provided by FirstRanker.com ---

6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

Program:
def bsearch(alist,item):

--- Content provided by FirstRanker.com ---

first=0
last=len(alist)-1
found=False
while first<=last and not found:
mid=(first+last)//2

--- Content provided by FirstRanker.com ---

if alist[mid]==item:
found=True
print("element found in position",mid)
else:
if item last=mid-1

--- Content provided by FirstRanker.com ---

EX. No. : 5(b)
Date:

BINARY SEARCH

--- Content provided by FirstRanker.com ---

FirstRanker Page 18 of 42
else:
first=mid+mid-1
return found

--- Content provided by FirstRanker.com ---

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))
a.append(e)

--- Content provided by FirstRanker.com ---

x=int(input("enter element to search"))
bsearch(a,x)

Output:
enter upper limit 6

--- Content provided by FirstRanker.com ---

enter the elements 2
enter the elements 3
enter the elements 5
enter the elements 7
enter the elements 14

--- Content provided by FirstRanker.com ---

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

FirstRanker Page 19 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort

--- Content provided by FirstRanker.com ---

2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list

--- Content provided by FirstRanker.com ---

Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0
for location in range(1,i+1):

--- Content provided by FirstRanker.com ---

if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

--- Content provided by FirstRanker.com ---


alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 6(a)
Date:

SELECTION SORT
FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---


FirstRanker Page 1 of 42

?

--- Content provided by FirstRanker.com ---



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


--- Content provided by FirstRanker.com ---

I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

--- Content provided by FirstRanker.com ---


LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---




is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education

--- Content provided by FirstRanker.com ---

and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at
different levels

--- Content provided by FirstRanker.com ---

? 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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

VISION
MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---



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

--- Content provided by FirstRanker.com ---

3. BREADTH
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

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

5. 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 4 of 42
PROGRAM OUTCOMES


--- Content provided by FirstRanker.com ---


a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career

--- Content provided by FirstRanker.com ---

c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society

--- Content provided by FirstRanker.com ---

g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with

--- Content provided by FirstRanker.com ---

coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS


--- Content provided by FirstRanker.com ---


To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort

--- Content provided by FirstRanker.com ---

8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

13.Simulate bouncing ball in Pygame




--- Content provided by FirstRanker.com ---

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.




--- Content provided by FirstRanker.com ---

COURSE OUTCOMES
COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
CONTENTS

Sl.No. Name of the Experiment
Page

--- Content provided by FirstRanker.com ---

No.
1
Compute the GCD of two numbers.
07
2

--- Content provided by FirstRanker.com ---

Find the square root of a number (Newton ?s method)
09
3
Exponentiation (power of a number)
11

--- Content provided by FirstRanker.com ---

4
Find the maximum of a list of numbers
13
5(a)
Linear search

--- Content provided by FirstRanker.com ---

15
5(b)
Binary search
17
6(a)

--- Content provided by FirstRanker.com ---

Selection sort
19
6(b)
Insertion sort
21

--- Content provided by FirstRanker.com ---

7
Merge sort
23
8
First n prime numbers

--- Content provided by FirstRanker.com ---

26
9
Multiply matrices
28
10

--- Content provided by FirstRanker.com ---

Programs that take command line arguments (word count)
30
11
Find the most frequent words in a text read from a file
32

--- Content provided by FirstRanker.com ---

12
Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---

37




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 7 of 42


Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---


Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y

--- Content provided by FirstRanker.com ---

3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

--- Content provided by FirstRanker.com ---

def computeGCD(x, y):
if x > y:
smaller = y
else:
smaller = x

--- Content provided by FirstRanker.com ---

for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---


num1 = 54
num2 = 24

# take input from the user

--- Content provided by FirstRanker.com ---

# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 8 of 42
Sample output :


--- Content provided by FirstRanker.com ---

The GCD of 54 and 24 is 6




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

--- Content provided by FirstRanker.com ---

FirstRanker Page 9 of 42

Aim:
To write a Python Program to find the square root of a number by Newton ?s Method.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

4. Print the value of approx

Program:

def newtonSqrt(n):

--- Content provided by FirstRanker.com ---

approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

return approx

print('The square root is' ,newtonSqrt(81))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :02
Date:

--- Content provided by FirstRanker.com ---

SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42
Output:

--- Content provided by FirstRanker.com ---

The square root is 9




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

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 ---

FirstRanker Page 11 of 42

Aim:
To write a Python program to find the exponentiation of a number.

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:

--- Content provided by FirstRanker.com ---

def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---


EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:

--- Content provided by FirstRanker.com ---

Enter base: 7
Enter exponential value: 2
Result:49


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

FirstRanker Page 13 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l

--- Content provided by FirstRanker.com ---

2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1

--- Content provided by FirstRanker.com ---

7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:
l=[]
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=l[0]
for i in range(0,len(l)):

--- Content provided by FirstRanker.com ---

if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :
04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6

--- Content provided by FirstRanker.com ---

Enter the numbers 9
Enter the numbers 90
The maximum number is 90


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---


FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list

--- Content provided by FirstRanker.com ---

Program:
def search(alist,item):
pos=0
found=False
stop=False

--- Content provided by FirstRanker.com ---

while pos if alist[pos]==item:
found=True
print("element found in position",pos)
else:
if alist[pos]>item:

--- Content provided by FirstRanker.com ---

stop=True
else:
pos=pos+1
return found
a=[]

--- Content provided by FirstRanker.com ---

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"))

--- Content provided by FirstRanker.com ---

search(a,x)
Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---


FirstRanker Page 16 of 42

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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to perform linear search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified.



FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python Program to perform binary search.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read the search element
2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element

--- Content provided by FirstRanker.com ---

4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list

--- Content provided by FirstRanker.com ---

7. If element is not found, loop terminates

Program:
def bsearch(alist,item):
first=0

--- Content provided by FirstRanker.com ---

last=len(alist)-1
found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)

--- Content provided by FirstRanker.com ---

Date:

BINARY SEARCH

FirstRanker Page 18 of 42

--- Content provided by FirstRanker.com ---

else:
first=mid+mid-1
return found

a=[]

--- Content provided by FirstRanker.com ---

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"))

--- Content provided by FirstRanker.com ---

bsearch(a,x)

Output:
enter upper limit 6
enter the elements 2

--- Content provided by FirstRanker.com ---

enter the elements 3
enter the elements 5
enter the elements 7
enter the elements 14
enter the elements 25

--- Content provided by FirstRanker.com ---

enter element to search 5
element found in position 2


Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to perform binary search is executed successfully and the output
is verified

FirstRanker Page 19 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0

--- Content provided by FirstRanker.com ---

3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]
6. 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]:

--- Content provided by FirstRanker.com ---

pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

--- Content provided by FirstRanker.com ---

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)


--- Content provided by FirstRanker.com ---





Ex. No. : 6(a)

--- Content provided by FirstRanker.com ---

Date:

SELECTION SORT

FirstRanker Page 20 of 42

--- Content provided by FirstRanker.com ---

Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to perform selection sort is successfully executed and the

--- Content provided by FirstRanker.com ---

output is verified.
FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

--- Content provided by FirstRanker.com ---

?



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

--- Content provided by FirstRanker.com ---



I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY




--- Content provided by FirstRanker.com ---




Name : _______________________________________
Register No : _______________________________________

--- Content provided by FirstRanker.com ---

Section : _______________________________________


LABORATORY MANUAL

--- Content provided by FirstRanker.com ---

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING



--- Content provided by FirstRanker.com ---

is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

? 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



VISION
MISSION

--- Content provided by FirstRanker.com ---

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS

--- Content provided by FirstRanker.com ---

To impart students with fundamental knowledge in Mathematics, Science and
fundamentals of engineering that will mould them to be successful professionals

2. CORE COMPETENCE
To provide students with sound knowledge in engineering and experimental skills to

--- Content provided by FirstRanker.com ---

identify complex software problems in industry and to develop practical solutions for them

3. BREADTH
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

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

To develop the ability of students to establish themselves as professionals in Computer

--- Content provided by FirstRanker.com ---

Science and Engineering and to create awareness about the need for lifelong learning and
pursuing advanced degrees



--- Content provided by FirstRanker.com ---




FirstRanker Page 4 of 42
PROGRAM OUTCOMES

--- Content provided by FirstRanker.com ---




a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field

--- Content provided by FirstRanker.com ---

b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT

--- Content provided by FirstRanker.com ---

tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to

--- Content provided by FirstRanker.com ---

convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS

--- Content provided by FirstRanker.com ---




To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.

--- Content provided by FirstRanker.com ---

Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

LIST OF EXPERIMENTS:

--- Content provided by FirstRanker.com ---

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. Find the maximum of a list of numbers
5. Linear search and Binary search

--- Content provided by FirstRanker.com ---

6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame


--- Content provided by FirstRanker.com ---



Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.
Implement Python programs with conditionals and loops.

--- Content provided by FirstRanker.com ---

Develop Python programs step-wise by defining functions and calling them.
Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python.


--- Content provided by FirstRanker.com ---



COURSE OUTCOMES
COURSE OBJECTIVES

--- Content provided by FirstRanker.com ---

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

--- Content provided by FirstRanker.com ---

Sl.No. Name of the Experiment
Page
No.
1
Compute the GCD of two numbers.

--- Content provided by FirstRanker.com ---

07
2
Find the square root of a number (Newton ?s method)
09
3

--- Content provided by FirstRanker.com ---

Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers
13

--- Content provided by FirstRanker.com ---

5(a)
Linear search
15
5(b)
Binary search

--- Content provided by FirstRanker.com ---

17
6(a)
Selection sort
19
6(b)

--- Content provided by FirstRanker.com ---

Insertion sort
21
7
Merge sort
23

--- Content provided by FirstRanker.com ---

8
First n prime numbers
26
9
Multiply matrices

--- Content provided by FirstRanker.com ---

28
10
Programs that take command line arguments (word count)
30
11

--- Content provided by FirstRanker.com ---

Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame
34

--- Content provided by FirstRanker.com ---

13
Simulate bouncing ball in Pygame
37


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 7 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find GCD of two numbers.

Algorithm:

--- Content provided by FirstRanker.com ---

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd

--- Content provided by FirstRanker.com ---

Program:

def computeGCD(x, y):
if x > y:
smaller = y

--- Content provided by FirstRanker.com ---

else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

--- Content provided by FirstRanker.com ---


return gcd

num1 = 54
num2 = 24

--- Content provided by FirstRanker.com ---


# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

--- Content provided by FirstRanker.com ---

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))




--- Content provided by FirstRanker.com ---




Ex. No. :01
Date:

--- Content provided by FirstRanker.com ---


GCD OF TWO NUMBERS

FirstRanker Page 8 of 42
Sample output :

--- Content provided by FirstRanker.com ---



The GCD of 54 and 24 is 6


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to compute GCD of two numbers is executed successfully and

--- Content provided by FirstRanker.com ---

the output is verified.

FirstRanker Page 9 of 42

Aim:

--- Content provided by FirstRanker.com ---

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,

--- Content provided by FirstRanker.com ---

i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

Program:

--- Content provided by FirstRanker.com ---


def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:

--- Content provided by FirstRanker.com ---

approx = better
better = 0.5 * (approx + n/approx)
return approx

print('The square root is' ,newtonSqrt(81))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Ex. No. :02

--- Content provided by FirstRanker.com ---

Date:

SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42

--- Content provided by FirstRanker.com ---

Output:

The square root is 9


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



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 ---



FirstRanker Page 11 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find the exponentiation of a number.

Algorithm:

1. Define a function named power()

--- Content provided by FirstRanker.com ---

2. Read the values of base and exp
3. Use ?if ? to check if exp is equal to 1 or not
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

--- Content provided by FirstRanker.com ---


Program:
def power(base,exp):
if(exp==1):
return(base)

--- Content provided by FirstRanker.com ---

if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---

FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2
Result:49

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---

FirstRanker Page 13 of 42


Aim:
To write a Python Program to find the maximum from a list of numbers.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno

--- Content provided by FirstRanker.com ---

5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:

--- Content provided by FirstRanker.com ---

l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))
l.append(a)

--- Content provided by FirstRanker.com ---

maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :
04
Date:

--- Content provided by FirstRanker.com ---


FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42
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 ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus a Python Program to find the maximum from the given list of numbers is successfully
executed and the output is verified.

FirstRanker Page 15 of 42

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):
pos=0

--- Content provided by FirstRanker.com ---

found=False
stop=False
while pos if alist[pos]==item:
found=True
print("element found in position",pos)

--- Content provided by FirstRanker.com ---

else:
if alist[pos]>item:
stop=True
else:
pos=pos+1

--- Content provided by FirstRanker.com ---

return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))

--- Content provided by FirstRanker.com ---

a.append(e)
x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)
Date:

--- Content provided by FirstRanker.com ---


LINEAR SEARCH

FirstRanker Page 16 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter upper limit 5
Enter the elements 6
Enter the elements 45
Enter the elements 2

--- Content provided by FirstRanker.com ---

Enter the elements 61
Enter the elements 26
Enter element to search 6
Element found in position 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.


--- Content provided by FirstRanker.com ---


FirstRanker Page 17 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list
3. Compare the search element with the middle element

--- Content provided by FirstRanker.com ---

i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the

--- Content provided by FirstRanker.com ---

right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. 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:

--- Content provided by FirstRanker.com ---

mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)
else:

--- Content provided by FirstRanker.com ---

if item last=mid-1
EX. No. : 5(b)
Date:

BINARY SEARCH

--- Content provided by FirstRanker.com ---


FirstRanker Page 18 of 42
else:
first=mid+mid-1
return found

--- Content provided by FirstRanker.com ---


a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))

--- Content provided by FirstRanker.com ---

a.append(e)
x=int(input("enter element to search"))
bsearch(a,x)

Output:

--- Content provided by FirstRanker.com ---

enter upper limit 6
enter the elements 2
enter the elements 3
enter the elements 5
enter the elements 7

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

FirstRanker Page 19 of 42

Aim:
To write a Python Program to perform selection sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]

--- Content provided by FirstRanker.com ---

6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0

--- Content provided by FirstRanker.com ---

for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]

--- Content provided by FirstRanker.com ---

alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 6(a)
Date:

SELECTION SORT

--- Content provided by FirstRanker.com ---


FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

GE8161 FirstRanker Page 21 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform insertion sort.
Algorithm:
1. Create a function named insertionsort

--- Content provided by FirstRanker.com ---

2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue

--- Content provided by FirstRanker.com ---

7. Print the sorted list

Program:
def insertionSort(alist):
for index in range(1,len(alist)):

--- Content provided by FirstRanker.com ---

currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1

--- Content provided by FirstRanker.com ---

alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 6(b)
Date:

INSERTION SORT

--- Content provided by FirstRanker.com ---

FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

?

--- Content provided by FirstRanker.com ---




DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

--- Content provided by FirstRanker.com ---


I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

--- Content provided by FirstRanker.com ---



LABORATORY MANUAL

FirstRanker Page 2 of 42

--- Content provided by FirstRanker.com ---

DHANALAKSHMI COLLEGE OF ENGINEERING



is committed to provide highly disciplined, c onscientious and

--- Content provided by FirstRanker.com ---

enterprising professionals conforming to global standards through value based quality education
and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at

--- Content provided by FirstRanker.com ---

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


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


VISION
MISSION

FirstRanker Page 3 of 42

--- Content provided by FirstRanker.com ---

PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and

--- Content provided by FirstRanker.com ---

fundamentals of engineering that will mould them to be successful professionals

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

--- Content provided by FirstRanker.com ---


3. BREADTH
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

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---


5. 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

--- Content provided by FirstRanker.com ---

pursuing advanced degrees




--- Content provided by FirstRanker.com ---



FirstRanker Page 4 of 42
PROGRAM OUTCOMES

--- Content provided by FirstRanker.com ---



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the

--- Content provided by FirstRanker.com ---

same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools

--- Content provided by FirstRanker.com ---

f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms

--- Content provided by FirstRanker.com ---

i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





FirstRanker Page 5 of 42

--- Content provided by FirstRanker.com ---

GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS

--- Content provided by FirstRanker.com ---



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.

--- Content provided by FirstRanker.com ---

2. Find the square root of a number (Newton ?s method)
3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort

--- Content provided by FirstRanker.com ---

7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file

--- Content provided by FirstRanker.com ---

12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame



--- Content provided by FirstRanker.com ---


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.

--- Content provided by FirstRanker.com ---

Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python.



--- Content provided by FirstRanker.com ---


COURSE OUTCOMES
COURSE OBJECTIVES

FirstRanker Page 6 of 42

--- Content provided by FirstRanker.com ---

GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

Sl.No. Name of the Experiment

--- Content provided by FirstRanker.com ---

Page
No.
1
Compute the GCD of two numbers.
07

--- Content provided by FirstRanker.com ---

2
Find the square root of a number (Newton ?s method)
09
3
Exponentiation (power of a number)

--- Content provided by FirstRanker.com ---

11
4
Find the maximum of a list of numbers
13
5(a)

--- Content provided by FirstRanker.com ---

Linear search
15
5(b)
Binary search
17

--- Content provided by FirstRanker.com ---

6(a)
Selection sort
19
6(b)
Insertion sort

--- Content provided by FirstRanker.com ---

21
7
Merge sort
23
8

--- Content provided by FirstRanker.com ---

First n prime numbers
26
9
Multiply matrices
28

--- Content provided by FirstRanker.com ---

10
Programs that take command line arguments (word count)
30
11
Find the most frequent words in a text read from a file

--- Content provided by FirstRanker.com ---

32
12
Simulate elliptical orbits in Pygame
34
13

--- Content provided by FirstRanker.com ---

Simulate bouncing ball in Pygame
37



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 7 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find GCD of two numbers.

Algorithm:

1. Define a function named computeGCD()

--- Content provided by FirstRanker.com ---

2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

--- Content provided by FirstRanker.com ---


def computeGCD(x, y):
if x > y:
smaller = y
else:

--- Content provided by FirstRanker.com ---

smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

--- Content provided by FirstRanker.com ---

return gcd

num1 = 54
num2 = 24

--- Content provided by FirstRanker.com ---

# 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))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :01
Date:

--- Content provided by FirstRanker.com ---

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42
Sample output :

--- Content provided by FirstRanker.com ---


The GCD of 54 and 24 is 6



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

--- Content provided by FirstRanker.com ---


FirstRanker Page 9 of 42

Aim:
To write a Python Program to find the square root of a number by Newton ?s Method.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better

--- Content provided by FirstRanker.com ---

ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

Program:

--- Content provided by FirstRanker.com ---

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better

--- Content provided by FirstRanker.com ---

better = 0.5 * (approx + n/approx)
return approx

print('The square root is' ,newtonSqrt(81))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. :02
Date:

--- Content provided by FirstRanker.com ---


SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42
Output:

--- Content provided by FirstRanker.com ---


The square root is 9



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


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 ---


FirstRanker Page 11 of 42

Aim:
To write a Python program to find the exponentiation of a number.

--- Content provided by FirstRanker.com ---


Algorithm:

1. Define a function named power()
2. Read the values of base and exp

--- Content provided by FirstRanker.com ---

3. Use ?if ? to check if exp is equal to 1 or not
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

--- Content provided by FirstRanker.com ---

Program:
def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):

--- Content provided by FirstRanker.com ---

return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Ex. No. : 03

--- Content provided by FirstRanker.com ---

Date:

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter base: 7
Enter exponential value: 2
Result:49

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

FirstRanker Page 13 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]

--- Content provided by FirstRanker.com ---

6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:
l=[]

--- Content provided by FirstRanker.com ---

n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]

--- Content provided by FirstRanker.com ---

for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :
04
Date:

--- Content provided by FirstRanker.com ---

FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42
Output:
Enter the upper limt 3

--- Content provided by FirstRanker.com ---

Enter the numbers 6
Enter the numbers 9
Enter the numbers 90
The maximum number is 90

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus a Python Program to find the maximum from the given list of numbers is successfully

--- Content provided by FirstRanker.com ---

executed and the output is verified.

FirstRanker Page 15 of 42

Aim:

--- Content provided by FirstRanker.com ---

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. If alist[pos]==item, then print the position of the item

--- Content provided by FirstRanker.com ---

4. 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

--- Content provided by FirstRanker.com ---

stop=False
while pos if alist[pos]==item:
found=True
print("element found in position",pos)
else:

--- Content provided by FirstRanker.com ---

if alist[pos]>item:
stop=True
else:
pos=pos+1
return found

--- Content provided by FirstRanker.com ---

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))
a.append(e)

--- Content provided by FirstRanker.com ---

x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)
Date:

--- Content provided by FirstRanker.com ---

LINEAR SEARCH

FirstRanker Page 16 of 42

Output:

--- Content provided by FirstRanker.com ---

Enter upper limit 5
Enter the elements 6
Enter the elements 45
Enter the elements 2
Enter the elements 61

--- Content provided by FirstRanker.com ---

Enter the elements 26
Enter element to search 6
Element found in position 1


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to perform linear search is executed successfully and the output
is verified.



--- Content provided by FirstRanker.com ---

FirstRanker Page 17 of 42


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
i. if both are matching, print element found

--- Content provided by FirstRanker.com ---

ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element

--- Content provided by FirstRanker.com ---

6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

Program:
def bsearch(alist,item):

--- Content provided by FirstRanker.com ---

first=0
last=len(alist)-1
found=False
while first<=last and not found:
mid=(first+last)//2

--- Content provided by FirstRanker.com ---

if alist[mid]==item:
found=True
print("element found in position",mid)
else:
if item last=mid-1

--- Content provided by FirstRanker.com ---

EX. No. : 5(b)
Date:

BINARY SEARCH

--- Content provided by FirstRanker.com ---

FirstRanker Page 18 of 42
else:
first=mid+mid-1
return found

--- Content provided by FirstRanker.com ---

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))
a.append(e)

--- Content provided by FirstRanker.com ---

x=int(input("enter element to search"))
bsearch(a,x)

Output:
enter upper limit 6

--- Content provided by FirstRanker.com ---

enter the elements 2
enter the elements 3
enter the elements 5
enter the elements 7
enter the elements 14

--- Content provided by FirstRanker.com ---

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

FirstRanker Page 19 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort

--- Content provided by FirstRanker.com ---

2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list

--- Content provided by FirstRanker.com ---

Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0
for location in range(1,i+1):

--- Content provided by FirstRanker.com ---

if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

--- Content provided by FirstRanker.com ---


alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 6(a)
Date:

SELECTION SORT

--- Content provided by FirstRanker.com ---

FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

GE8161 FirstRanker Page 21 of 42

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

--- Content provided by FirstRanker.com ---


Program:
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]

--- Content provided by FirstRanker.com ---

position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue

--- Content provided by FirstRanker.com ---

alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)


--- Content provided by FirstRanker.com ---





Ex. No. : 6(b)

--- Content provided by FirstRanker.com ---

Date:

INSERTION SORT


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 22 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.
FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---

FirstRanker Page 1 of 42

?


--- Content provided by FirstRanker.com ---


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---


GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________


--- Content provided by FirstRanker.com ---

LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---



is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---


? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---

tag on heart and soul




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





VISION

--- Content provided by FirstRanker.com ---

MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---

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. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 4 of 42
PROGRAM OUTCOMES



--- Content provided by FirstRanker.com ---

a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications

--- Content provided by FirstRanker.com ---

d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as

--- Content provided by FirstRanker.com ---

economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination

--- Content provided by FirstRanker.com ---

j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS



--- Content provided by FirstRanker.com ---

To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---


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)

--- Content provided by FirstRanker.com ---

4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers

--- Content provided by FirstRanker.com ---

9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---





Upon completion of the course, students will be able to:

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---





COURSE OUTCOMES

--- Content provided by FirstRanker.com ---

COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

CONTENTS

Sl.No. Name of the Experiment
Page
No.

--- Content provided by FirstRanker.com ---

1
Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)

--- Content provided by FirstRanker.com ---

09
3
Exponentiation (power of a number)
11
4

--- Content provided by FirstRanker.com ---

Find the maximum of a list of numbers
13
5(a)
Linear search
15

--- Content provided by FirstRanker.com ---

5(b)
Binary search
17
6(a)
Selection sort

--- Content provided by FirstRanker.com ---

19
6(b)
Insertion sort
21
7

--- Content provided by FirstRanker.com ---

Merge sort
23
8
First n prime numbers
26

--- Content provided by FirstRanker.com ---

9
Multiply matrices
28
10
Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

30
11
Find the most frequent words in a text read from a file
32
12

--- Content provided by FirstRanker.com ---

Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---

Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1

--- Content provided by FirstRanker.com ---

Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

def computeGCD(x, y):

--- Content provided by FirstRanker.com ---

if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):

--- Content provided by FirstRanker.com ---

if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---

num1 = 54
num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))

--- Content provided by FirstRanker.com ---

# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 8 of 42
Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---


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().

--- Content provided by FirstRanker.com ---

2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---


Program:

def newtonSqrt(n):
approx = 0.5 * n

--- Content provided by FirstRanker.com ---

better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---


print('The square root is' ,newtonSqrt(81))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

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.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---


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
i. if exp is equal to 1, then return base

--- Content provided by FirstRanker.com ---

ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:
def power(base,exp):

--- Content provided by FirstRanker.com ---

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))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:
Enter base: 7

--- Content provided by FirstRanker.com ---

Enter exponential value: 2
Result:49



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to find the exponentiation of a number is executed successfully

--- Content provided by FirstRanker.com ---

and the output is verified.

FirstRanker Page 13 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n

--- Content provided by FirstRanker.com ---

3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---


Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:

--- Content provided by FirstRanker.com ---

maxno=l[i]
print("The maximum number is %d"%maxno)



--- Content provided by FirstRanker.com ---





Ex. No. :

--- Content provided by FirstRanker.com ---

04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9

--- Content provided by FirstRanker.com ---

Enter the numbers 90
The maximum number is 90



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:

--- Content provided by FirstRanker.com ---

def search(alist,item):
pos=0
found=False
stop=False
while pos if alist[pos]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",pos)
else:
if alist[pos]>item:
stop=True

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---

FirstRanker Page 16 of 42

Output:
Enter upper limit 5
Enter the elements 6

--- Content provided by FirstRanker.com ---

Enter the elements 45
Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6

--- Content provided by FirstRanker.com ---

Element found in position 1




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---




FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element

--- Content provided by FirstRanker.com ---

2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the

--- Content provided by FirstRanker.com ---

left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---


Program:
def bsearch(alist,item):
first=0
last=len(alist)-1

--- Content provided by FirstRanker.com ---

found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---


BINARY SEARCH

FirstRanker Page 18 of 42
else:

--- Content provided by FirstRanker.com ---

first=mid+mid-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"))
bsearch(a,x)

--- Content provided by FirstRanker.com ---


Output:
enter upper limit 6
enter the elements 2
enter the elements 3

--- Content provided by FirstRanker.com ---

enter the elements 5
enter the elements 7
enter the elements 14
enter the elements 25
enter element to search 5

--- Content provided by FirstRanker.com ---

element found in position 2


Result:
Thus the Python Program to perform binary search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified

FirstRanker Page 19 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,

--- Content provided by FirstRanker.com ---

4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]

--- Content provided by FirstRanker.com ---

selectionSort(alist)
print(alist)



--- Content provided by FirstRanker.com ---




Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---


SELECTION SORT

FirstRanker Page 20 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]

--- Content provided by FirstRanker.com ---

5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

Program:

--- Content provided by FirstRanker.com ---

def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:

--- Content provided by FirstRanker.com ---

alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(b)
Date:

--- Content provided by FirstRanker.com ---

INSERTION SORT


GE8161 FirstRanker Page 22 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to perform insertion sort is successfully executed and the output

--- Content provided by FirstRanker.com ---

is verified.

GE8161 FirstRanker Page 23 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]

--- Content provided by FirstRanker.com ---

4. Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i

--- Content provided by FirstRanker.com ---

else
alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

Increment j
Increment k
8. Print the sorted list


--- Content provided by FirstRanker.com ---




Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---


MERGE SORT

FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---

FirstRanker Page 1 of 42

?


--- Content provided by FirstRanker.com ---


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---


GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________


--- Content provided by FirstRanker.com ---

LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---



is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---


? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---

tag on heart and soul




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





VISION

--- Content provided by FirstRanker.com ---

MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---

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. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 4 of 42
PROGRAM OUTCOMES



--- Content provided by FirstRanker.com ---

a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications

--- Content provided by FirstRanker.com ---

d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as

--- Content provided by FirstRanker.com ---

economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination

--- Content provided by FirstRanker.com ---

j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS



--- Content provided by FirstRanker.com ---

To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---


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)

--- Content provided by FirstRanker.com ---

4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers

--- Content provided by FirstRanker.com ---

9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---





Upon completion of the course, students will be able to:

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---





COURSE OUTCOMES

--- Content provided by FirstRanker.com ---

COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

CONTENTS

Sl.No. Name of the Experiment
Page
No.

--- Content provided by FirstRanker.com ---

1
Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)

--- Content provided by FirstRanker.com ---

09
3
Exponentiation (power of a number)
11
4

--- Content provided by FirstRanker.com ---

Find the maximum of a list of numbers
13
5(a)
Linear search
15

--- Content provided by FirstRanker.com ---

5(b)
Binary search
17
6(a)
Selection sort

--- Content provided by FirstRanker.com ---

19
6(b)
Insertion sort
21
7

--- Content provided by FirstRanker.com ---

Merge sort
23
8
First n prime numbers
26

--- Content provided by FirstRanker.com ---

9
Multiply matrices
28
10
Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

30
11
Find the most frequent words in a text read from a file
32
12

--- Content provided by FirstRanker.com ---

Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---

Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1

--- Content provided by FirstRanker.com ---

Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

def computeGCD(x, y):

--- Content provided by FirstRanker.com ---

if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):

--- Content provided by FirstRanker.com ---

if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---

num1 = 54
num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))

--- Content provided by FirstRanker.com ---

# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 8 of 42
Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---


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().

--- Content provided by FirstRanker.com ---

2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---


Program:

def newtonSqrt(n):
approx = 0.5 * n

--- Content provided by FirstRanker.com ---

better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---


print('The square root is' ,newtonSqrt(81))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

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.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---


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
i. if exp is equal to 1, then return base

--- Content provided by FirstRanker.com ---

ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:
def power(base,exp):

--- Content provided by FirstRanker.com ---

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))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:
Enter base: 7

--- Content provided by FirstRanker.com ---

Enter exponential value: 2
Result:49



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to find the exponentiation of a number is executed successfully

--- Content provided by FirstRanker.com ---

and the output is verified.

FirstRanker Page 13 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n

--- Content provided by FirstRanker.com ---

3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---


Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:

--- Content provided by FirstRanker.com ---

maxno=l[i]
print("The maximum number is %d"%maxno)



--- Content provided by FirstRanker.com ---





Ex. No. :

--- Content provided by FirstRanker.com ---

04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9

--- Content provided by FirstRanker.com ---

Enter the numbers 90
The maximum number is 90



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:

--- Content provided by FirstRanker.com ---

def search(alist,item):
pos=0
found=False
stop=False
while pos if alist[pos]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",pos)
else:
if alist[pos]>item:
stop=True

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---

FirstRanker Page 16 of 42

Output:
Enter upper limit 5
Enter the elements 6

--- Content provided by FirstRanker.com ---

Enter the elements 45
Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6

--- Content provided by FirstRanker.com ---

Element found in position 1




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---




FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element

--- Content provided by FirstRanker.com ---

2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the

--- Content provided by FirstRanker.com ---

left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---


Program:
def bsearch(alist,item):
first=0
last=len(alist)-1

--- Content provided by FirstRanker.com ---

found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---


BINARY SEARCH

FirstRanker Page 18 of 42
else:

--- Content provided by FirstRanker.com ---

first=mid+mid-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"))
bsearch(a,x)

--- Content provided by FirstRanker.com ---


Output:
enter upper limit 6
enter the elements 2
enter the elements 3

--- Content provided by FirstRanker.com ---

enter the elements 5
enter the elements 7
enter the elements 14
enter the elements 25
enter element to search 5

--- Content provided by FirstRanker.com ---

element found in position 2


Result:
Thus the Python Program to perform binary search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified

FirstRanker Page 19 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,

--- Content provided by FirstRanker.com ---

4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]

--- Content provided by FirstRanker.com ---

selectionSort(alist)
print(alist)



--- Content provided by FirstRanker.com ---




Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---


SELECTION SORT

FirstRanker Page 20 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]

--- Content provided by FirstRanker.com ---

5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

Program:

--- Content provided by FirstRanker.com ---

def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:

--- Content provided by FirstRanker.com ---

alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(b)
Date:

--- Content provided by FirstRanker.com ---

INSERTION SORT


GE8161 FirstRanker Page 22 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to perform insertion sort is successfully executed and the output

--- Content provided by FirstRanker.com ---

is verified.

GE8161 FirstRanker Page 23 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]

--- Content provided by FirstRanker.com ---

4. Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i

--- Content provided by FirstRanker.com ---

else
alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

Increment j
Increment k
8. Print the sorted list


--- Content provided by FirstRanker.com ---




Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---


MERGE SORT


GE8161 FirstRanker Page 24 of 42

--- Content provided by FirstRanker.com ---

Program:
def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2

--- Content provided by FirstRanker.com ---

lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0

--- Content provided by FirstRanker.com ---

while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1

--- Content provided by FirstRanker.com ---

k=k+1
#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---


FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

--- Content provided by FirstRanker.com ---

?



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

--- Content provided by FirstRanker.com ---



I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY




--- Content provided by FirstRanker.com ---




Name : _______________________________________
Register No : _______________________________________

--- Content provided by FirstRanker.com ---

Section : _______________________________________


LABORATORY MANUAL

--- Content provided by FirstRanker.com ---

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING



--- Content provided by FirstRanker.com ---

is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

? 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



VISION
MISSION

--- Content provided by FirstRanker.com ---

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS

--- Content provided by FirstRanker.com ---

To impart students with fundamental knowledge in Mathematics, Science and
fundamentals of engineering that will mould them to be successful professionals

2. CORE COMPETENCE
To provide students with sound knowledge in engineering and experimental skills to

--- Content provided by FirstRanker.com ---

identify complex software problems in industry and to develop practical solutions for them

3. BREADTH
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

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

To develop the ability of students to establish themselves as professionals in Computer

--- Content provided by FirstRanker.com ---

Science and Engineering and to create awareness about the need for lifelong learning and
pursuing advanced degrees



--- Content provided by FirstRanker.com ---




FirstRanker Page 4 of 42
PROGRAM OUTCOMES

--- Content provided by FirstRanker.com ---




a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field

--- Content provided by FirstRanker.com ---

b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT

--- Content provided by FirstRanker.com ---

tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to

--- Content provided by FirstRanker.com ---

convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS

--- Content provided by FirstRanker.com ---




To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.

--- Content provided by FirstRanker.com ---

Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

LIST OF EXPERIMENTS:

--- Content provided by FirstRanker.com ---

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. Find the maximum of a list of numbers
5. Linear search and Binary search

--- Content provided by FirstRanker.com ---

6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame


--- Content provided by FirstRanker.com ---



Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.
Implement Python programs with conditionals and loops.

--- Content provided by FirstRanker.com ---

Develop Python programs step-wise by defining functions and calling them.
Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python.


--- Content provided by FirstRanker.com ---



COURSE OUTCOMES
COURSE OBJECTIVES

--- Content provided by FirstRanker.com ---

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

--- Content provided by FirstRanker.com ---

Sl.No. Name of the Experiment
Page
No.
1
Compute the GCD of two numbers.

--- Content provided by FirstRanker.com ---

07
2
Find the square root of a number (Newton ?s method)
09
3

--- Content provided by FirstRanker.com ---

Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers
13

--- Content provided by FirstRanker.com ---

5(a)
Linear search
15
5(b)
Binary search

--- Content provided by FirstRanker.com ---

17
6(a)
Selection sort
19
6(b)

--- Content provided by FirstRanker.com ---

Insertion sort
21
7
Merge sort
23

--- Content provided by FirstRanker.com ---

8
First n prime numbers
26
9
Multiply matrices

--- Content provided by FirstRanker.com ---

28
10
Programs that take command line arguments (word count)
30
11

--- Content provided by FirstRanker.com ---

Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame
34

--- Content provided by FirstRanker.com ---

13
Simulate bouncing ball in Pygame
37


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 7 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find GCD of two numbers.

Algorithm:

--- Content provided by FirstRanker.com ---

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd

--- Content provided by FirstRanker.com ---

Program:

def computeGCD(x, y):
if x > y:
smaller = y

--- Content provided by FirstRanker.com ---

else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

--- Content provided by FirstRanker.com ---


return gcd

num1 = 54
num2 = 24

--- Content provided by FirstRanker.com ---


# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

--- Content provided by FirstRanker.com ---

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))




--- Content provided by FirstRanker.com ---




Ex. No. :01
Date:

--- Content provided by FirstRanker.com ---


GCD OF TWO NUMBERS

FirstRanker Page 8 of 42
Sample output :

--- Content provided by FirstRanker.com ---



The GCD of 54 and 24 is 6


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to compute GCD of two numbers is executed successfully and

--- Content provided by FirstRanker.com ---

the output is verified.

FirstRanker Page 9 of 42

Aim:

--- Content provided by FirstRanker.com ---

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,

--- Content provided by FirstRanker.com ---

i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

Program:

--- Content provided by FirstRanker.com ---


def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:

--- Content provided by FirstRanker.com ---

approx = better
better = 0.5 * (approx + n/approx)
return approx

print('The square root is' ,newtonSqrt(81))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Ex. No. :02

--- Content provided by FirstRanker.com ---

Date:

SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42

--- Content provided by FirstRanker.com ---

Output:

The square root is 9


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



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 ---



FirstRanker Page 11 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find the exponentiation of a number.

Algorithm:

1. Define a function named power()

--- Content provided by FirstRanker.com ---

2. Read the values of base and exp
3. Use ?if ? to check if exp is equal to 1 or not
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

--- Content provided by FirstRanker.com ---


Program:
def power(base,exp):
if(exp==1):
return(base)

--- Content provided by FirstRanker.com ---

if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---

FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2
Result:49

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---

FirstRanker Page 13 of 42


Aim:
To write a Python Program to find the maximum from a list of numbers.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno

--- Content provided by FirstRanker.com ---

5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:

--- Content provided by FirstRanker.com ---

l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))
l.append(a)

--- Content provided by FirstRanker.com ---

maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :
04
Date:

--- Content provided by FirstRanker.com ---


FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42
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 ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus a Python Program to find the maximum from the given list of numbers is successfully
executed and the output is verified.

FirstRanker Page 15 of 42

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):
pos=0

--- Content provided by FirstRanker.com ---

found=False
stop=False
while pos if alist[pos]==item:
found=True
print("element found in position",pos)

--- Content provided by FirstRanker.com ---

else:
if alist[pos]>item:
stop=True
else:
pos=pos+1

--- Content provided by FirstRanker.com ---

return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))

--- Content provided by FirstRanker.com ---

a.append(e)
x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)
Date:

--- Content provided by FirstRanker.com ---


LINEAR SEARCH

FirstRanker Page 16 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter upper limit 5
Enter the elements 6
Enter the elements 45
Enter the elements 2

--- Content provided by FirstRanker.com ---

Enter the elements 61
Enter the elements 26
Enter element to search 6
Element found in position 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.


--- Content provided by FirstRanker.com ---


FirstRanker Page 17 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list
3. Compare the search element with the middle element

--- Content provided by FirstRanker.com ---

i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the

--- Content provided by FirstRanker.com ---

right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. 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:

--- Content provided by FirstRanker.com ---

mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)
else:

--- Content provided by FirstRanker.com ---

if item last=mid-1
EX. No. : 5(b)
Date:

BINARY SEARCH

--- Content provided by FirstRanker.com ---


FirstRanker Page 18 of 42
else:
first=mid+mid-1
return found

--- Content provided by FirstRanker.com ---


a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))

--- Content provided by FirstRanker.com ---

a.append(e)
x=int(input("enter element to search"))
bsearch(a,x)

Output:

--- Content provided by FirstRanker.com ---

enter upper limit 6
enter the elements 2
enter the elements 3
enter the elements 5
enter the elements 7

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

FirstRanker Page 19 of 42

Aim:
To write a Python Program to perform selection sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]

--- Content provided by FirstRanker.com ---

6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0

--- Content provided by FirstRanker.com ---

for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]

--- Content provided by FirstRanker.com ---

alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 6(a)
Date:

SELECTION SORT

--- Content provided by FirstRanker.com ---


FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

GE8161 FirstRanker Page 21 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform insertion sort.
Algorithm:
1. Create a function named insertionsort

--- Content provided by FirstRanker.com ---

2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue

--- Content provided by FirstRanker.com ---

7. Print the sorted list

Program:
def insertionSort(alist):
for index in range(1,len(alist)):

--- Content provided by FirstRanker.com ---

currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1

--- Content provided by FirstRanker.com ---

alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 6(b)
Date:

INSERTION SORT

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 22 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 23 of 42

Aim:
To write a Python Program to perform Merge sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following

--- Content provided by FirstRanker.com ---

if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

Increment j
Increment k
6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]
Increment i

--- Content provided by FirstRanker.com ---

Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j
Increment k

--- Content provided by FirstRanker.com ---

8. Print the sorted list




--- Content provided by FirstRanker.com ---


Ex. No. : 7
Date:

MERGE SORT

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 24 of 42
Program:
def mergeSort(alist):

--- Content provided by FirstRanker.com ---

# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]

--- Content provided by FirstRanker.com ---

mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1

--- Content provided by FirstRanker.com ---

k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1

--- Content provided by FirstRanker.com ---

while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
#print("Merging ",alist)

--- Content provided by FirstRanker.com ---

alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie
FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---

FirstRanker Page 1 of 42

?


--- Content provided by FirstRanker.com ---


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---


GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________


--- Content provided by FirstRanker.com ---

LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---



is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---


? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---

tag on heart and soul




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





VISION

--- Content provided by FirstRanker.com ---

MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---

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. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 4 of 42
PROGRAM OUTCOMES



--- Content provided by FirstRanker.com ---

a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications

--- Content provided by FirstRanker.com ---

d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as

--- Content provided by FirstRanker.com ---

economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination

--- Content provided by FirstRanker.com ---

j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS



--- Content provided by FirstRanker.com ---

To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---


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)

--- Content provided by FirstRanker.com ---

4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers

--- Content provided by FirstRanker.com ---

9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---





Upon completion of the course, students will be able to:

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---





COURSE OUTCOMES

--- Content provided by FirstRanker.com ---

COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

CONTENTS

Sl.No. Name of the Experiment
Page
No.

--- Content provided by FirstRanker.com ---

1
Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)

--- Content provided by FirstRanker.com ---

09
3
Exponentiation (power of a number)
11
4

--- Content provided by FirstRanker.com ---

Find the maximum of a list of numbers
13
5(a)
Linear search
15

--- Content provided by FirstRanker.com ---

5(b)
Binary search
17
6(a)
Selection sort

--- Content provided by FirstRanker.com ---

19
6(b)
Insertion sort
21
7

--- Content provided by FirstRanker.com ---

Merge sort
23
8
First n prime numbers
26

--- Content provided by FirstRanker.com ---

9
Multiply matrices
28
10
Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

30
11
Find the most frequent words in a text read from a file
32
12

--- Content provided by FirstRanker.com ---

Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---

Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1

--- Content provided by FirstRanker.com ---

Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

def computeGCD(x, y):

--- Content provided by FirstRanker.com ---

if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):

--- Content provided by FirstRanker.com ---

if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---

num1 = 54
num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))

--- Content provided by FirstRanker.com ---

# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 8 of 42
Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---


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().

--- Content provided by FirstRanker.com ---

2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---


Program:

def newtonSqrt(n):
approx = 0.5 * n

--- Content provided by FirstRanker.com ---

better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---


print('The square root is' ,newtonSqrt(81))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

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.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---


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
i. if exp is equal to 1, then return base

--- Content provided by FirstRanker.com ---

ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:
def power(base,exp):

--- Content provided by FirstRanker.com ---

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))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:
Enter base: 7

--- Content provided by FirstRanker.com ---

Enter exponential value: 2
Result:49



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to find the exponentiation of a number is executed successfully

--- Content provided by FirstRanker.com ---

and the output is verified.

FirstRanker Page 13 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n

--- Content provided by FirstRanker.com ---

3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---


Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:

--- Content provided by FirstRanker.com ---

maxno=l[i]
print("The maximum number is %d"%maxno)



--- Content provided by FirstRanker.com ---





Ex. No. :

--- Content provided by FirstRanker.com ---

04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9

--- Content provided by FirstRanker.com ---

Enter the numbers 90
The maximum number is 90



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:

--- Content provided by FirstRanker.com ---

def search(alist,item):
pos=0
found=False
stop=False
while pos if alist[pos]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",pos)
else:
if alist[pos]>item:
stop=True

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---

FirstRanker Page 16 of 42

Output:
Enter upper limit 5
Enter the elements 6

--- Content provided by FirstRanker.com ---

Enter the elements 45
Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6

--- Content provided by FirstRanker.com ---

Element found in position 1




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---




FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element

--- Content provided by FirstRanker.com ---

2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the

--- Content provided by FirstRanker.com ---

left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---


Program:
def bsearch(alist,item):
first=0
last=len(alist)-1

--- Content provided by FirstRanker.com ---

found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---


BINARY SEARCH

FirstRanker Page 18 of 42
else:

--- Content provided by FirstRanker.com ---

first=mid+mid-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"))
bsearch(a,x)

--- Content provided by FirstRanker.com ---


Output:
enter upper limit 6
enter the elements 2
enter the elements 3

--- Content provided by FirstRanker.com ---

enter the elements 5
enter the elements 7
enter the elements 14
enter the elements 25
enter element to search 5

--- Content provided by FirstRanker.com ---

element found in position 2


Result:
Thus the Python Program to perform binary search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified

FirstRanker Page 19 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,

--- Content provided by FirstRanker.com ---

4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]

--- Content provided by FirstRanker.com ---

selectionSort(alist)
print(alist)



--- Content provided by FirstRanker.com ---




Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---


SELECTION SORT

FirstRanker Page 20 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]

--- Content provided by FirstRanker.com ---

5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

Program:

--- Content provided by FirstRanker.com ---

def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:

--- Content provided by FirstRanker.com ---

alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(b)
Date:

--- Content provided by FirstRanker.com ---

INSERTION SORT


GE8161 FirstRanker Page 22 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to perform insertion sort is successfully executed and the output

--- Content provided by FirstRanker.com ---

is verified.

GE8161 FirstRanker Page 23 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]

--- Content provided by FirstRanker.com ---

4. Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i

--- Content provided by FirstRanker.com ---

else
alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

Increment j
Increment k
8. Print the sorted list


--- Content provided by FirstRanker.com ---




Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---


MERGE SORT


GE8161 FirstRanker Page 24 of 42

--- Content provided by FirstRanker.com ---

Program:
def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2

--- Content provided by FirstRanker.com ---

lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0

--- Content provided by FirstRanker.com ---

while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1

--- Content provided by FirstRanker.com ---

k=k+1
#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 26 of 42

Aim:
To write a Python program to find first n prime numbers.

--- Content provided by FirstRanker.com ---


Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break

--- Content provided by FirstRanker.com ---

else print the value of num
4. 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):

--- Content provided by FirstRanker.com ---

# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break

--- Content provided by FirstRanker.com ---

else:
print(num)



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 8
Date:

--- Content provided by FirstRanker.com ---

FIRST N PRIME NUMBERS

FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

--- Content provided by FirstRanker.com ---


?



--- Content provided by FirstRanker.com ---

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY



--- Content provided by FirstRanker.com ---





Name : _______________________________________

--- Content provided by FirstRanker.com ---

Register No : _______________________________________
Section : _______________________________________


LABORATORY MANUAL

--- Content provided by FirstRanker.com ---


FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING


--- Content provided by FirstRanker.com ---


is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---

? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




VISION
MISSION

--- Content provided by FirstRanker.com ---


FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


--- Content provided by FirstRanker.com ---

1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and
fundamentals of engineering that will mould them to be successful professionals

2. CORE COMPETENCE

--- Content provided by FirstRanker.com ---

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
To provide relevant training and experience to bridge the gap between theory and practice

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

will build up their communication skills, individual leadership and supportive qualities, and to enable
them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---

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


--- Content provided by FirstRanker.com ---





FirstRanker Page 4 of 42

--- Content provided by FirstRanker.com ---

PROGRAM OUTCOMES



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in

--- Content provided by FirstRanker.com ---

Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution

--- Content provided by FirstRanker.com ---

e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable

--- Content provided by FirstRanker.com ---

h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning

--- Content provided by FirstRanker.com ---

k) To develop and manage projects in multidisciplinary environments




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)

--- Content provided by FirstRanker.com ---

SYLLABUS



To write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---

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. Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices

--- Content provided by FirstRanker.com ---

10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---




Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---




COURSE OUTCOMES
COURSE OBJECTIVES

--- Content provided by FirstRanker.com ---


FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

--- Content provided by FirstRanker.com ---


Sl.No. Name of the Experiment
Page
No.
1

--- Content provided by FirstRanker.com ---

Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)
09

--- Content provided by FirstRanker.com ---

3
Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

13
5(a)
Linear search
15
5(b)

--- Content provided by FirstRanker.com ---

Binary search
17
6(a)
Selection sort
19

--- Content provided by FirstRanker.com ---

6(b)
Insertion sort
21
7
Merge sort

--- Content provided by FirstRanker.com ---

23
8
First n prime numbers
26
9

--- Content provided by FirstRanker.com ---

Multiply matrices
28
10
Programs that take command line arguments (word count)
30

--- Content provided by FirstRanker.com ---

11
Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find GCD of two numbers.

Algorithm:

--- Content provided by FirstRanker.com ---


1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i

--- Content provided by FirstRanker.com ---

4. Print the value of gcd
Program:

def computeGCD(x, y):
if x > y:

--- Content provided by FirstRanker.com ---

smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):

--- Content provided by FirstRanker.com ---

gcd = i

return gcd

num1 = 54

--- Content provided by FirstRanker.com ---

num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

--- Content provided by FirstRanker.com ---


print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))



--- Content provided by FirstRanker.com ---





Ex. No. :01

--- Content provided by FirstRanker.com ---

Date:

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42

--- Content provided by FirstRanker.com ---

Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---

Program:

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---

print('The square root is' ,newtonSqrt(81))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---

FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program for finding the square root of a given number by Newton ?s

--- Content provided by FirstRanker.com ---

Method is executed successfully and the output is verified.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---

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
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

4. Print the result

Program:
def power(base,exp):
if(exp==1):

--- Content provided by FirstRanker.com ---

return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))

--- Content provided by FirstRanker.com ---

print("Result:",power(base,exp))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2

--- Content provided by FirstRanker.com ---

Result:49




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---


FirstRanker Page 13 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n

--- Content provided by FirstRanker.com ---

4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---

Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))

--- Content provided by FirstRanker.com ---

l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]

--- Content provided by FirstRanker.com ---

print("The maximum number is %d"%maxno)




--- Content provided by FirstRanker.com ---




Ex. No. :
04

--- Content provided by FirstRanker.com ---

Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9
Enter the numbers 90

--- Content provided by FirstRanker.com ---

The maximum number is 90




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

FirstRanker Page 15 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform Linear Search
Algorithm:
1. Read n elements into the list

--- Content provided by FirstRanker.com ---

2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):

--- Content provided by FirstRanker.com ---

pos=0
found=False
stop=False
while pos if alist[pos]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:

--- Content provided by FirstRanker.com ---

pos=pos+1
return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

e=int(input("enter the elements"))
a.append(e)
x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)

--- Content provided by FirstRanker.com ---

Date:

LINEAR SEARCH

FirstRanker Page 16 of 42

--- Content provided by FirstRanker.com ---


Output:
Enter upper limit 5
Enter the elements 6
Enter the elements 45

--- Content provided by FirstRanker.com ---

Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6
Element found in position 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---



FirstRanker Page 17 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list

--- Content provided by FirstRanker.com ---

3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element

--- Content provided by FirstRanker.com ---

5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---

Program:
def bsearch(alist,item):
first=0
last=len(alist)-1
found=False

--- Content provided by FirstRanker.com ---

while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)

--- Content provided by FirstRanker.com ---

else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---

BINARY SEARCH

FirstRanker Page 18 of 42
else:
first=mid+mid-1

--- Content provided by FirstRanker.com ---

return found

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---


FirstRanker Page 19 of 42

Aim:
To write a Python Program to perform selection sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location

--- Content provided by FirstRanker.com ---

5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):

--- Content provided by FirstRanker.com ---

pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]

--- Content provided by FirstRanker.com ---

alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---

SELECTION SORT

FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1

--- Content provided by FirstRanker.com ---

6. alist[position]=currentvalue
7. Print the sorted list

Program:
def insertionSort(alist):

--- Content provided by FirstRanker.com ---

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]

--- Content provided by FirstRanker.com ---

position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 6(b)
Date:

INSERTION SORT

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 22 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 23 of 42

Aim:
To write a Python Program to perform Merge sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0

--- Content provided by FirstRanker.com ---

5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j

--- Content provided by FirstRanker.com ---

Increment k
8. Print the sorted list



--- Content provided by FirstRanker.com ---



Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---

MERGE SORT


GE8161 FirstRanker Page 24 of 42
Program:

--- Content provided by FirstRanker.com ---

def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]

--- Content provided by FirstRanker.com ---

righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):

--- Content provided by FirstRanker.com ---

if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1

--- Content provided by FirstRanker.com ---

k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1

--- Content provided by FirstRanker.com ---

#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 26 of 42

Aim:
To write a Python program to find first n prime numbers.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break
else print the value of num

--- Content provided by FirstRanker.com ---

4. Repeat step 3 for i in range(2,num)

Program:

n = int(input("Enter the upper limit: "))

--- Content provided by FirstRanker.com ---


print("Prime numbers are")

for num in range(0,n + 1):
# prime numbers are greater than 1

--- Content provided by FirstRanker.com ---

if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:

--- Content provided by FirstRanker.com ---

print(num)




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 8
Date:

FIRST N PRIME NUMBERS

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 27 of 42
Output:
Enter the upper limit:100

--- Content provided by FirstRanker.com ---

Prime numbers are
2
3
5
7

--- Content provided by FirstRanker.com ---

11
13
17
19
23

--- Content provided by FirstRanker.com ---

29
31
37
41
43

--- Content provided by FirstRanker.com ---

47
53
59
61
67

--- Content provided by FirstRanker.com ---

71
73
79
83
89

--- Content provided by FirstRanker.com ---

97




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.
FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---


FirstRanker Page 1 of 42

?

--- Content provided by FirstRanker.com ---



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


--- Content provided by FirstRanker.com ---

I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

--- Content provided by FirstRanker.com ---


LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---




is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education

--- Content provided by FirstRanker.com ---

and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at
different levels

--- Content provided by FirstRanker.com ---

? 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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

VISION
MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---



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

--- Content provided by FirstRanker.com ---

3. BREADTH
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

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

5. 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 4 of 42
PROGRAM OUTCOMES


--- Content provided by FirstRanker.com ---


a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career

--- Content provided by FirstRanker.com ---

c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society

--- Content provided by FirstRanker.com ---

g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with

--- Content provided by FirstRanker.com ---

coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS


--- Content provided by FirstRanker.com ---


To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort

--- Content provided by FirstRanker.com ---

8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

13.Simulate bouncing ball in Pygame




--- Content provided by FirstRanker.com ---

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.




--- Content provided by FirstRanker.com ---

COURSE OUTCOMES
COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
CONTENTS

Sl.No. Name of the Experiment
Page

--- Content provided by FirstRanker.com ---

No.
1
Compute the GCD of two numbers.
07
2

--- Content provided by FirstRanker.com ---

Find the square root of a number (Newton ?s method)
09
3
Exponentiation (power of a number)
11

--- Content provided by FirstRanker.com ---

4
Find the maximum of a list of numbers
13
5(a)
Linear search

--- Content provided by FirstRanker.com ---

15
5(b)
Binary search
17
6(a)

--- Content provided by FirstRanker.com ---

Selection sort
19
6(b)
Insertion sort
21

--- Content provided by FirstRanker.com ---

7
Merge sort
23
8
First n prime numbers

--- Content provided by FirstRanker.com ---

26
9
Multiply matrices
28
10

--- Content provided by FirstRanker.com ---

Programs that take command line arguments (word count)
30
11
Find the most frequent words in a text read from a file
32

--- Content provided by FirstRanker.com ---

12
Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---

37




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 7 of 42


Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---


Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y

--- Content provided by FirstRanker.com ---

3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

--- Content provided by FirstRanker.com ---

def computeGCD(x, y):
if x > y:
smaller = y
else:
smaller = x

--- Content provided by FirstRanker.com ---

for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---


num1 = 54
num2 = 24

# take input from the user

--- Content provided by FirstRanker.com ---

# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 8 of 42
Sample output :


--- Content provided by FirstRanker.com ---

The GCD of 54 and 24 is 6




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

--- Content provided by FirstRanker.com ---

FirstRanker Page 9 of 42

Aim:
To write a Python Program to find the square root of a number by Newton ?s Method.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

4. Print the value of approx

Program:

def newtonSqrt(n):

--- Content provided by FirstRanker.com ---

approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

return approx

print('The square root is' ,newtonSqrt(81))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :02
Date:

--- Content provided by FirstRanker.com ---

SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42
Output:

--- Content provided by FirstRanker.com ---

The square root is 9




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

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 ---

FirstRanker Page 11 of 42

Aim:
To write a Python program to find the exponentiation of a number.

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:

--- Content provided by FirstRanker.com ---

def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---


EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:

--- Content provided by FirstRanker.com ---

Enter base: 7
Enter exponential value: 2
Result:49


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

FirstRanker Page 13 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l

--- Content provided by FirstRanker.com ---

2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1

--- Content provided by FirstRanker.com ---

7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:
l=[]
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=l[0]
for i in range(0,len(l)):

--- Content provided by FirstRanker.com ---

if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :
04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6

--- Content provided by FirstRanker.com ---

Enter the numbers 9
Enter the numbers 90
The maximum number is 90


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---


FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list

--- Content provided by FirstRanker.com ---

Program:
def search(alist,item):
pos=0
found=False
stop=False

--- Content provided by FirstRanker.com ---

while pos if alist[pos]==item:
found=True
print("element found in position",pos)
else:
if alist[pos]>item:

--- Content provided by FirstRanker.com ---

stop=True
else:
pos=pos+1
return found
a=[]

--- Content provided by FirstRanker.com ---

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"))

--- Content provided by FirstRanker.com ---

search(a,x)
Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---


FirstRanker Page 16 of 42

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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to perform linear search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified.



FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python Program to perform binary search.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read the search element
2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element

--- Content provided by FirstRanker.com ---

4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list

--- Content provided by FirstRanker.com ---

7. If element is not found, loop terminates

Program:
def bsearch(alist,item):
first=0

--- Content provided by FirstRanker.com ---

last=len(alist)-1
found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)

--- Content provided by FirstRanker.com ---

Date:

BINARY SEARCH

FirstRanker Page 18 of 42

--- Content provided by FirstRanker.com ---

else:
first=mid+mid-1
return found

a=[]

--- Content provided by FirstRanker.com ---

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"))

--- Content provided by FirstRanker.com ---

bsearch(a,x)

Output:
enter upper limit 6
enter the elements 2

--- Content provided by FirstRanker.com ---

enter the elements 3
enter the elements 5
enter the elements 7
enter the elements 14
enter the elements 25

--- Content provided by FirstRanker.com ---

enter element to search 5
element found in position 2


Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to perform binary search is executed successfully and the output
is verified

FirstRanker Page 19 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0

--- Content provided by FirstRanker.com ---

3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]
6. 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]:

--- Content provided by FirstRanker.com ---

pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

--- Content provided by FirstRanker.com ---

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)


--- Content provided by FirstRanker.com ---





Ex. No. : 6(a)

--- Content provided by FirstRanker.com ---

Date:

SELECTION SORT

FirstRanker Page 20 of 42

--- Content provided by FirstRanker.com ---

Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to perform selection sort is successfully executed and the

--- Content provided by FirstRanker.com ---

output is verified.

GE8161 FirstRanker Page 21 of 42

Aim:

--- Content provided by FirstRanker.com ---

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. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)

--- Content provided by FirstRanker.com ---

4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

--- Content provided by FirstRanker.com ---

Program:
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index

--- Content provided by FirstRanker.com ---

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]

--- Content provided by FirstRanker.com ---

insertionSort(alist)
print(alist)



--- Content provided by FirstRanker.com ---




Ex. No. : 6(b)
Date:

--- Content provided by FirstRanker.com ---


INSERTION SORT


GE8161 FirstRanker Page 22 of 42

--- Content provided by FirstRanker.com ---

Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

GE8161 FirstRanker Page 23 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list

--- Content provided by FirstRanker.com ---

3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0
5. 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 ---

6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]
Increment i
Increment k
7. while j < len(righthalf), perform the following

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
Increment j
Increment k
8. Print the sorted list

--- Content provided by FirstRanker.com ---





Ex. No. : 7

--- Content provided by FirstRanker.com ---

Date:

MERGE SORT


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 24 of 42
Program:
def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:

--- Content provided by FirstRanker.com ---

mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)

--- Content provided by FirstRanker.com ---

i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1

--- Content provided by FirstRanker.com ---

else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

j=j+1
k=k+1
#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)


GE8161 FirstRanker Page 25 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to perform merge sort is successfully executed and the output is

--- Content provided by FirstRanker.com ---

verifie

GE8161 FirstRanker Page 26 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find first n prime numbers.

Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following

--- Content provided by FirstRanker.com ---

3. if num%i is 0 then break
else print the value of num
4. 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")

--- Content provided by FirstRanker.com ---

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:

--- Content provided by FirstRanker.com ---

break
else:
print(num)


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. : 8
Date:

--- Content provided by FirstRanker.com ---


FIRST N PRIME NUMBERS


GE8161 FirstRanker Page 27 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limit:100
Prime numbers are
2
3

--- Content provided by FirstRanker.com ---

5
7
11
13
17

--- Content provided by FirstRanker.com ---

19
23
29
31
37

--- Content provided by FirstRanker.com ---

41
43
47
53
59

--- Content provided by FirstRanker.com ---

61
67
71
73
79

--- Content provided by FirstRanker.com ---

83
89
97


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to find the first n prime numbers is executed successfully and

--- Content provided by FirstRanker.com ---

the output is verified.

GE8161 FirstRanker Page 28 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to multiply matrices.
Algorithm:
1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):

--- Content provided by FirstRanker.com ---

i. for j in range(len(Y[0])):
a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

--- Content provided by FirstRanker.com ---

Program:

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]

--- Content provided by FirstRanker.com ---

Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],

--- Content provided by FirstRanker.com ---

[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]

--- Content provided by FirstRanker.com ---

for r in result:
print(r)

Ex. No. : 9
Date:

--- Content provided by FirstRanker.com ---


MATRIX MULTIPLICATION

FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---

FirstRanker Page 1 of 42

?


--- Content provided by FirstRanker.com ---


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---


GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________


--- Content provided by FirstRanker.com ---

LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---



is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---


? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---

tag on heart and soul




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





VISION

--- Content provided by FirstRanker.com ---

MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---

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. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 4 of 42
PROGRAM OUTCOMES



--- Content provided by FirstRanker.com ---

a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications

--- Content provided by FirstRanker.com ---

d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as

--- Content provided by FirstRanker.com ---

economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination

--- Content provided by FirstRanker.com ---

j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS



--- Content provided by FirstRanker.com ---

To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---


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)

--- Content provided by FirstRanker.com ---

4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers

--- Content provided by FirstRanker.com ---

9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---





Upon completion of the course, students will be able to:

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---





COURSE OUTCOMES

--- Content provided by FirstRanker.com ---

COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

CONTENTS

Sl.No. Name of the Experiment
Page
No.

--- Content provided by FirstRanker.com ---

1
Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)

--- Content provided by FirstRanker.com ---

09
3
Exponentiation (power of a number)
11
4

--- Content provided by FirstRanker.com ---

Find the maximum of a list of numbers
13
5(a)
Linear search
15

--- Content provided by FirstRanker.com ---

5(b)
Binary search
17
6(a)
Selection sort

--- Content provided by FirstRanker.com ---

19
6(b)
Insertion sort
21
7

--- Content provided by FirstRanker.com ---

Merge sort
23
8
First n prime numbers
26

--- Content provided by FirstRanker.com ---

9
Multiply matrices
28
10
Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

30
11
Find the most frequent words in a text read from a file
32
12

--- Content provided by FirstRanker.com ---

Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---

Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1

--- Content provided by FirstRanker.com ---

Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

def computeGCD(x, y):

--- Content provided by FirstRanker.com ---

if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):

--- Content provided by FirstRanker.com ---

if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---

num1 = 54
num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))

--- Content provided by FirstRanker.com ---

# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 8 of 42
Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---


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().

--- Content provided by FirstRanker.com ---

2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---


Program:

def newtonSqrt(n):
approx = 0.5 * n

--- Content provided by FirstRanker.com ---

better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---


print('The square root is' ,newtonSqrt(81))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

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.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---


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
i. if exp is equal to 1, then return base

--- Content provided by FirstRanker.com ---

ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:
def power(base,exp):

--- Content provided by FirstRanker.com ---

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))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:
Enter base: 7

--- Content provided by FirstRanker.com ---

Enter exponential value: 2
Result:49



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to find the exponentiation of a number is executed successfully

--- Content provided by FirstRanker.com ---

and the output is verified.

FirstRanker Page 13 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n

--- Content provided by FirstRanker.com ---

3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---


Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:

--- Content provided by FirstRanker.com ---

maxno=l[i]
print("The maximum number is %d"%maxno)



--- Content provided by FirstRanker.com ---





Ex. No. :

--- Content provided by FirstRanker.com ---

04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9

--- Content provided by FirstRanker.com ---

Enter the numbers 90
The maximum number is 90



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:

--- Content provided by FirstRanker.com ---

def search(alist,item):
pos=0
found=False
stop=False
while pos if alist[pos]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",pos)
else:
if alist[pos]>item:
stop=True

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---

FirstRanker Page 16 of 42

Output:
Enter upper limit 5
Enter the elements 6

--- Content provided by FirstRanker.com ---

Enter the elements 45
Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6

--- Content provided by FirstRanker.com ---

Element found in position 1




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---




FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element

--- Content provided by FirstRanker.com ---

2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the

--- Content provided by FirstRanker.com ---

left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---


Program:
def bsearch(alist,item):
first=0
last=len(alist)-1

--- Content provided by FirstRanker.com ---

found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---


BINARY SEARCH

FirstRanker Page 18 of 42
else:

--- Content provided by FirstRanker.com ---

first=mid+mid-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"))
bsearch(a,x)

--- Content provided by FirstRanker.com ---


Output:
enter upper limit 6
enter the elements 2
enter the elements 3

--- Content provided by FirstRanker.com ---

enter the elements 5
enter the elements 7
enter the elements 14
enter the elements 25
enter element to search 5

--- Content provided by FirstRanker.com ---

element found in position 2


Result:
Thus the Python Program to perform binary search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified

FirstRanker Page 19 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,

--- Content provided by FirstRanker.com ---

4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]

--- Content provided by FirstRanker.com ---

selectionSort(alist)
print(alist)



--- Content provided by FirstRanker.com ---




Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---


SELECTION SORT

FirstRanker Page 20 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]

--- Content provided by FirstRanker.com ---

5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

Program:

--- Content provided by FirstRanker.com ---

def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:

--- Content provided by FirstRanker.com ---

alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(b)
Date:

--- Content provided by FirstRanker.com ---

INSERTION SORT


GE8161 FirstRanker Page 22 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to perform insertion sort is successfully executed and the output

--- Content provided by FirstRanker.com ---

is verified.

GE8161 FirstRanker Page 23 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]

--- Content provided by FirstRanker.com ---

4. Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i

--- Content provided by FirstRanker.com ---

else
alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

Increment j
Increment k
8. Print the sorted list


--- Content provided by FirstRanker.com ---




Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---


MERGE SORT


GE8161 FirstRanker Page 24 of 42

--- Content provided by FirstRanker.com ---

Program:
def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2

--- Content provided by FirstRanker.com ---

lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0

--- Content provided by FirstRanker.com ---

while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1

--- Content provided by FirstRanker.com ---

k=k+1
#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 26 of 42

Aim:
To write a Python program to find first n prime numbers.

--- Content provided by FirstRanker.com ---


Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break

--- Content provided by FirstRanker.com ---

else print the value of num
4. 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):

--- Content provided by FirstRanker.com ---

# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break

--- Content provided by FirstRanker.com ---

else:
print(num)



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 8
Date:

--- Content provided by FirstRanker.com ---

FIRST N PRIME NUMBERS


GE8161 FirstRanker Page 27 of 42
Output:

--- Content provided by FirstRanker.com ---

Enter the upper limit:100
Prime numbers are
2
3
5

--- Content provided by FirstRanker.com ---

7
11
13
17
19

--- Content provided by FirstRanker.com ---

23
29
31
37
41

--- Content provided by FirstRanker.com ---

43
47
53
59
61

--- Content provided by FirstRanker.com ---

67
71
73
79
83

--- Content provided by FirstRanker.com ---

89
97



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 28 of 42

Aim:
To write a Python program to multiply matrices.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):
i. for j in range(len(Y[0])):

--- Content provided by FirstRanker.com ---

a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

Program:

--- Content provided by FirstRanker.com ---


X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],

--- Content provided by FirstRanker.com ---

[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]

--- Content provided by FirstRanker.com ---

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:

--- Content provided by FirstRanker.com ---

print(r)

Ex. No. : 9
Date:

--- Content provided by FirstRanker.com ---

MATRIX MULTIPLICATION


GE8161 FirstRanker Page 29 of 42
Output:

--- Content provided by FirstRanker.com ---


[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to multiply matrices is executed successfully and the output is
verified.
FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

--- Content provided by FirstRanker.com ---


?



--- Content provided by FirstRanker.com ---

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY



--- Content provided by FirstRanker.com ---





Name : _______________________________________

--- Content provided by FirstRanker.com ---

Register No : _______________________________________
Section : _______________________________________


LABORATORY MANUAL

--- Content provided by FirstRanker.com ---


FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING


--- Content provided by FirstRanker.com ---


is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---

? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




VISION
MISSION

--- Content provided by FirstRanker.com ---


FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


--- Content provided by FirstRanker.com ---

1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and
fundamentals of engineering that will mould them to be successful professionals

2. CORE COMPETENCE

--- Content provided by FirstRanker.com ---

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
To provide relevant training and experience to bridge the gap between theory and practice

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

will build up their communication skills, individual leadership and supportive qualities, and to enable
them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---

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


--- Content provided by FirstRanker.com ---





FirstRanker Page 4 of 42

--- Content provided by FirstRanker.com ---

PROGRAM OUTCOMES



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in

--- Content provided by FirstRanker.com ---

Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution

--- Content provided by FirstRanker.com ---

e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable

--- Content provided by FirstRanker.com ---

h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning

--- Content provided by FirstRanker.com ---

k) To develop and manage projects in multidisciplinary environments




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)

--- Content provided by FirstRanker.com ---

SYLLABUS



To write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---

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. Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices

--- Content provided by FirstRanker.com ---

10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---




Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---




COURSE OUTCOMES
COURSE OBJECTIVES

--- Content provided by FirstRanker.com ---


FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

--- Content provided by FirstRanker.com ---


Sl.No. Name of the Experiment
Page
No.
1

--- Content provided by FirstRanker.com ---

Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)
09

--- Content provided by FirstRanker.com ---

3
Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

13
5(a)
Linear search
15
5(b)

--- Content provided by FirstRanker.com ---

Binary search
17
6(a)
Selection sort
19

--- Content provided by FirstRanker.com ---

6(b)
Insertion sort
21
7
Merge sort

--- Content provided by FirstRanker.com ---

23
8
First n prime numbers
26
9

--- Content provided by FirstRanker.com ---

Multiply matrices
28
10
Programs that take command line arguments (word count)
30

--- Content provided by FirstRanker.com ---

11
Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find GCD of two numbers.

Algorithm:

--- Content provided by FirstRanker.com ---


1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i

--- Content provided by FirstRanker.com ---

4. Print the value of gcd
Program:

def computeGCD(x, y):
if x > y:

--- Content provided by FirstRanker.com ---

smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):

--- Content provided by FirstRanker.com ---

gcd = i

return gcd

num1 = 54

--- Content provided by FirstRanker.com ---

num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

--- Content provided by FirstRanker.com ---


print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))



--- Content provided by FirstRanker.com ---





Ex. No. :01

--- Content provided by FirstRanker.com ---

Date:

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42

--- Content provided by FirstRanker.com ---

Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---

Program:

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---

print('The square root is' ,newtonSqrt(81))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---

FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program for finding the square root of a given number by Newton ?s

--- Content provided by FirstRanker.com ---

Method is executed successfully and the output is verified.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---

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
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

4. Print the result

Program:
def power(base,exp):
if(exp==1):

--- Content provided by FirstRanker.com ---

return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))

--- Content provided by FirstRanker.com ---

print("Result:",power(base,exp))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2

--- Content provided by FirstRanker.com ---

Result:49




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---


FirstRanker Page 13 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n

--- Content provided by FirstRanker.com ---

4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---

Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))

--- Content provided by FirstRanker.com ---

l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]

--- Content provided by FirstRanker.com ---

print("The maximum number is %d"%maxno)




--- Content provided by FirstRanker.com ---




Ex. No. :
04

--- Content provided by FirstRanker.com ---

Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9
Enter the numbers 90

--- Content provided by FirstRanker.com ---

The maximum number is 90




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

FirstRanker Page 15 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform Linear Search
Algorithm:
1. Read n elements into the list

--- Content provided by FirstRanker.com ---

2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):

--- Content provided by FirstRanker.com ---

pos=0
found=False
stop=False
while pos if alist[pos]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:

--- Content provided by FirstRanker.com ---

pos=pos+1
return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

e=int(input("enter the elements"))
a.append(e)
x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)

--- Content provided by FirstRanker.com ---

Date:

LINEAR SEARCH

FirstRanker Page 16 of 42

--- Content provided by FirstRanker.com ---


Output:
Enter upper limit 5
Enter the elements 6
Enter the elements 45

--- Content provided by FirstRanker.com ---

Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6
Element found in position 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---



FirstRanker Page 17 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list

--- Content provided by FirstRanker.com ---

3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element

--- Content provided by FirstRanker.com ---

5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---

Program:
def bsearch(alist,item):
first=0
last=len(alist)-1
found=False

--- Content provided by FirstRanker.com ---

while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)

--- Content provided by FirstRanker.com ---

else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---

BINARY SEARCH

FirstRanker Page 18 of 42
else:
first=mid+mid-1

--- Content provided by FirstRanker.com ---

return found

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---


FirstRanker Page 19 of 42

Aim:
To write a Python Program to perform selection sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location

--- Content provided by FirstRanker.com ---

5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):

--- Content provided by FirstRanker.com ---

pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]

--- Content provided by FirstRanker.com ---

alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---

SELECTION SORT

FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1

--- Content provided by FirstRanker.com ---

6. alist[position]=currentvalue
7. Print the sorted list

Program:
def insertionSort(alist):

--- Content provided by FirstRanker.com ---

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]

--- Content provided by FirstRanker.com ---

position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 6(b)
Date:

INSERTION SORT

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 22 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 23 of 42

Aim:
To write a Python Program to perform Merge sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0

--- Content provided by FirstRanker.com ---

5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j

--- Content provided by FirstRanker.com ---

Increment k
8. Print the sorted list



--- Content provided by FirstRanker.com ---



Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---

MERGE SORT


GE8161 FirstRanker Page 24 of 42
Program:

--- Content provided by FirstRanker.com ---

def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]

--- Content provided by FirstRanker.com ---

righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):

--- Content provided by FirstRanker.com ---

if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1

--- Content provided by FirstRanker.com ---

k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1

--- Content provided by FirstRanker.com ---

#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 26 of 42

Aim:
To write a Python program to find first n prime numbers.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break
else print the value of num

--- Content provided by FirstRanker.com ---

4. Repeat step 3 for i in range(2,num)

Program:

n = int(input("Enter the upper limit: "))

--- Content provided by FirstRanker.com ---


print("Prime numbers are")

for num in range(0,n + 1):
# prime numbers are greater than 1

--- Content provided by FirstRanker.com ---

if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:

--- Content provided by FirstRanker.com ---

print(num)




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 8
Date:

FIRST N PRIME NUMBERS

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 27 of 42
Output:
Enter the upper limit:100

--- Content provided by FirstRanker.com ---

Prime numbers are
2
3
5
7

--- Content provided by FirstRanker.com ---

11
13
17
19
23

--- Content provided by FirstRanker.com ---

29
31
37
41
43

--- Content provided by FirstRanker.com ---

47
53
59
61
67

--- Content provided by FirstRanker.com ---

71
73
79
83
89

--- Content provided by FirstRanker.com ---

97




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 28 of 42

Aim:
To write a Python program to multiply matrices.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):
i. for j in range(len(Y[0])):
a) for k in range(len(Y))

--- Content provided by FirstRanker.com ---

b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

Program:

--- Content provided by FirstRanker.com ---

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],

--- Content provided by FirstRanker.com ---

[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for i in range(len(X)):

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---


Ex. No. : 9
Date:

MATRIX MULTIPLICATION

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 29 of 42
Output:

--- Content provided by FirstRanker.com ---

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to multiply matrices is executed successfully and the output is

--- Content provided by FirstRanker.com ---

verified.

GE8161 FirstRanker Page 30 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program for command line arguments.
Algorithm:
1. Add arguments to find the words and lines
2. Add the filename as argument
3. Parse the arguments to get the values

--- Content provided by FirstRanker.com ---

4. Format and print the words and lines

Program:
parser=argparse.ArgumentParser()
parser.add_argument('--words','-w',action='store_true')

--- Content provided by FirstRanker.com ---

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))

--- Content provided by FirstRanker.com ---

elif args.lines:
print("lines {}".format(print_lines(args.filename))
else:
print ("words {}".format(print_words(args.filename))
print("lines {}".format(print_lines(args.filename))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. :10
Date:

--- Content provided by FirstRanker.com ---


COMMAND LINE ARGUMENTS(WORD COUNT)
FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

--- Content provided by FirstRanker.com ---


?



--- Content provided by FirstRanker.com ---

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY



--- Content provided by FirstRanker.com ---





Name : _______________________________________

--- Content provided by FirstRanker.com ---

Register No : _______________________________________
Section : _______________________________________


LABORATORY MANUAL

--- Content provided by FirstRanker.com ---


FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING


--- Content provided by FirstRanker.com ---


is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---

? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




VISION
MISSION

--- Content provided by FirstRanker.com ---


FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


--- Content provided by FirstRanker.com ---

1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and
fundamentals of engineering that will mould them to be successful professionals

2. CORE COMPETENCE

--- Content provided by FirstRanker.com ---

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
To provide relevant training and experience to bridge the gap between theory and practice

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

will build up their communication skills, individual leadership and supportive qualities, and to enable
them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---

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


--- Content provided by FirstRanker.com ---





FirstRanker Page 4 of 42

--- Content provided by FirstRanker.com ---

PROGRAM OUTCOMES



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in

--- Content provided by FirstRanker.com ---

Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution

--- Content provided by FirstRanker.com ---

e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable

--- Content provided by FirstRanker.com ---

h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning

--- Content provided by FirstRanker.com ---

k) To develop and manage projects in multidisciplinary environments




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)

--- Content provided by FirstRanker.com ---

SYLLABUS



To write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---

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. Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices

--- Content provided by FirstRanker.com ---

10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---




Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---




COURSE OUTCOMES
COURSE OBJECTIVES

--- Content provided by FirstRanker.com ---


FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

--- Content provided by FirstRanker.com ---


Sl.No. Name of the Experiment
Page
No.
1

--- Content provided by FirstRanker.com ---

Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)
09

--- Content provided by FirstRanker.com ---

3
Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

13
5(a)
Linear search
15
5(b)

--- Content provided by FirstRanker.com ---

Binary search
17
6(a)
Selection sort
19

--- Content provided by FirstRanker.com ---

6(b)
Insertion sort
21
7
Merge sort

--- Content provided by FirstRanker.com ---

23
8
First n prime numbers
26
9

--- Content provided by FirstRanker.com ---

Multiply matrices
28
10
Programs that take command line arguments (word count)
30

--- Content provided by FirstRanker.com ---

11
Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find GCD of two numbers.

Algorithm:

--- Content provided by FirstRanker.com ---


1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i

--- Content provided by FirstRanker.com ---

4. Print the value of gcd
Program:

def computeGCD(x, y):
if x > y:

--- Content provided by FirstRanker.com ---

smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):

--- Content provided by FirstRanker.com ---

gcd = i

return gcd

num1 = 54

--- Content provided by FirstRanker.com ---

num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

--- Content provided by FirstRanker.com ---


print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))



--- Content provided by FirstRanker.com ---





Ex. No. :01

--- Content provided by FirstRanker.com ---

Date:

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42

--- Content provided by FirstRanker.com ---

Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---

Program:

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---

print('The square root is' ,newtonSqrt(81))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---

FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program for finding the square root of a given number by Newton ?s

--- Content provided by FirstRanker.com ---

Method is executed successfully and the output is verified.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---

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
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

4. Print the result

Program:
def power(base,exp):
if(exp==1):

--- Content provided by FirstRanker.com ---

return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))

--- Content provided by FirstRanker.com ---

print("Result:",power(base,exp))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2

--- Content provided by FirstRanker.com ---

Result:49




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---


FirstRanker Page 13 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n

--- Content provided by FirstRanker.com ---

4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---

Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))

--- Content provided by FirstRanker.com ---

l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]

--- Content provided by FirstRanker.com ---

print("The maximum number is %d"%maxno)




--- Content provided by FirstRanker.com ---




Ex. No. :
04

--- Content provided by FirstRanker.com ---

Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9
Enter the numbers 90

--- Content provided by FirstRanker.com ---

The maximum number is 90




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

FirstRanker Page 15 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform Linear Search
Algorithm:
1. Read n elements into the list

--- Content provided by FirstRanker.com ---

2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):

--- Content provided by FirstRanker.com ---

pos=0
found=False
stop=False
while pos if alist[pos]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:

--- Content provided by FirstRanker.com ---

pos=pos+1
return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

e=int(input("enter the elements"))
a.append(e)
x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)

--- Content provided by FirstRanker.com ---

Date:

LINEAR SEARCH

FirstRanker Page 16 of 42

--- Content provided by FirstRanker.com ---


Output:
Enter upper limit 5
Enter the elements 6
Enter the elements 45

--- Content provided by FirstRanker.com ---

Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6
Element found in position 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---



FirstRanker Page 17 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list

--- Content provided by FirstRanker.com ---

3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element

--- Content provided by FirstRanker.com ---

5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---

Program:
def bsearch(alist,item):
first=0
last=len(alist)-1
found=False

--- Content provided by FirstRanker.com ---

while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)

--- Content provided by FirstRanker.com ---

else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---

BINARY SEARCH

FirstRanker Page 18 of 42
else:
first=mid+mid-1

--- Content provided by FirstRanker.com ---

return found

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---


FirstRanker Page 19 of 42

Aim:
To write a Python Program to perform selection sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location

--- Content provided by FirstRanker.com ---

5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):

--- Content provided by FirstRanker.com ---

pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]

--- Content provided by FirstRanker.com ---

alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---

SELECTION SORT

FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1

--- Content provided by FirstRanker.com ---

6. alist[position]=currentvalue
7. Print the sorted list

Program:
def insertionSort(alist):

--- Content provided by FirstRanker.com ---

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]

--- Content provided by FirstRanker.com ---

position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 6(b)
Date:

INSERTION SORT

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 22 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 23 of 42

Aim:
To write a Python Program to perform Merge sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0

--- Content provided by FirstRanker.com ---

5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j

--- Content provided by FirstRanker.com ---

Increment k
8. Print the sorted list



--- Content provided by FirstRanker.com ---



Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---

MERGE SORT


GE8161 FirstRanker Page 24 of 42
Program:

--- Content provided by FirstRanker.com ---

def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]

--- Content provided by FirstRanker.com ---

righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):

--- Content provided by FirstRanker.com ---

if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1

--- Content provided by FirstRanker.com ---

k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1

--- Content provided by FirstRanker.com ---

#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 26 of 42

Aim:
To write a Python program to find first n prime numbers.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break
else print the value of num

--- Content provided by FirstRanker.com ---

4. Repeat step 3 for i in range(2,num)

Program:

n = int(input("Enter the upper limit: "))

--- Content provided by FirstRanker.com ---


print("Prime numbers are")

for num in range(0,n + 1):
# prime numbers are greater than 1

--- Content provided by FirstRanker.com ---

if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:

--- Content provided by FirstRanker.com ---

print(num)




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 8
Date:

FIRST N PRIME NUMBERS

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 27 of 42
Output:
Enter the upper limit:100

--- Content provided by FirstRanker.com ---

Prime numbers are
2
3
5
7

--- Content provided by FirstRanker.com ---

11
13
17
19
23

--- Content provided by FirstRanker.com ---

29
31
37
41
43

--- Content provided by FirstRanker.com ---

47
53
59
61
67

--- Content provided by FirstRanker.com ---

71
73
79
83
89

--- Content provided by FirstRanker.com ---

97




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 28 of 42

Aim:
To write a Python program to multiply matrices.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):
i. for j in range(len(Y[0])):
a) for k in range(len(Y))

--- Content provided by FirstRanker.com ---

b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

Program:

--- Content provided by FirstRanker.com ---

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],

--- Content provided by FirstRanker.com ---

[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for i in range(len(X)):

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---


Ex. No. : 9
Date:

MATRIX MULTIPLICATION

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 29 of 42
Output:

--- Content provided by FirstRanker.com ---

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to multiply matrices is executed successfully and the output is

--- Content provided by FirstRanker.com ---

verified.

GE8161 FirstRanker Page 30 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program for command line arguments.
Algorithm:
1. Add arguments to find the words and lines
2. Add the filename as argument
3. Parse the arguments to get the values

--- Content provided by FirstRanker.com ---

4. Format and print the words and lines

Program:
parser=argparse.ArgumentParser()
parser.add_argument('--words','-w',action='store_true')

--- Content provided by FirstRanker.com ---

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))

--- Content provided by FirstRanker.com ---

elif args.lines:
print("lines {}".format(print_lines(args.filename))
else:
print ("words {}".format(print_words(args.filename))
print("lines {}".format(print_lines(args.filename))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. :10
Date:

--- Content provided by FirstRanker.com ---


COMMAND LINE ARGUMENTS(WORD COUNT)

GE8161 FirstRanker Page 31 of 42
Output:

--- Content provided by FirstRanker.com ---



python main.py ?i input.txt
words 23
lines 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program for command line arguments is executed successfully and the output is

--- Content provided by FirstRanker.com ---

verified.
FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

--- Content provided by FirstRanker.com ---

?



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

--- Content provided by FirstRanker.com ---



I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY




--- Content provided by FirstRanker.com ---




Name : _______________________________________
Register No : _______________________________________

--- Content provided by FirstRanker.com ---

Section : _______________________________________


LABORATORY MANUAL

--- Content provided by FirstRanker.com ---

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING



--- Content provided by FirstRanker.com ---

is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

? 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



VISION
MISSION

--- Content provided by FirstRanker.com ---

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS

--- Content provided by FirstRanker.com ---

To impart students with fundamental knowledge in Mathematics, Science and
fundamentals of engineering that will mould them to be successful professionals

2. CORE COMPETENCE
To provide students with sound knowledge in engineering and experimental skills to

--- Content provided by FirstRanker.com ---

identify complex software problems in industry and to develop practical solutions for them

3. BREADTH
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

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

To develop the ability of students to establish themselves as professionals in Computer

--- Content provided by FirstRanker.com ---

Science and Engineering and to create awareness about the need for lifelong learning and
pursuing advanced degrees



--- Content provided by FirstRanker.com ---




FirstRanker Page 4 of 42
PROGRAM OUTCOMES

--- Content provided by FirstRanker.com ---




a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field

--- Content provided by FirstRanker.com ---

b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT

--- Content provided by FirstRanker.com ---

tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to

--- Content provided by FirstRanker.com ---

convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS

--- Content provided by FirstRanker.com ---




To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.

--- Content provided by FirstRanker.com ---

Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

LIST OF EXPERIMENTS:

--- Content provided by FirstRanker.com ---

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. Find the maximum of a list of numbers
5. Linear search and Binary search

--- Content provided by FirstRanker.com ---

6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame


--- Content provided by FirstRanker.com ---



Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.
Implement Python programs with conditionals and loops.

--- Content provided by FirstRanker.com ---

Develop Python programs step-wise by defining functions and calling them.
Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python.


--- Content provided by FirstRanker.com ---



COURSE OUTCOMES
COURSE OBJECTIVES

--- Content provided by FirstRanker.com ---

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

--- Content provided by FirstRanker.com ---

Sl.No. Name of the Experiment
Page
No.
1
Compute the GCD of two numbers.

--- Content provided by FirstRanker.com ---

07
2
Find the square root of a number (Newton ?s method)
09
3

--- Content provided by FirstRanker.com ---

Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers
13

--- Content provided by FirstRanker.com ---

5(a)
Linear search
15
5(b)
Binary search

--- Content provided by FirstRanker.com ---

17
6(a)
Selection sort
19
6(b)

--- Content provided by FirstRanker.com ---

Insertion sort
21
7
Merge sort
23

--- Content provided by FirstRanker.com ---

8
First n prime numbers
26
9
Multiply matrices

--- Content provided by FirstRanker.com ---

28
10
Programs that take command line arguments (word count)
30
11

--- Content provided by FirstRanker.com ---

Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame
34

--- Content provided by FirstRanker.com ---

13
Simulate bouncing ball in Pygame
37


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 7 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find GCD of two numbers.

Algorithm:

--- Content provided by FirstRanker.com ---

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd

--- Content provided by FirstRanker.com ---

Program:

def computeGCD(x, y):
if x > y:
smaller = y

--- Content provided by FirstRanker.com ---

else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

--- Content provided by FirstRanker.com ---


return gcd

num1 = 54
num2 = 24

--- Content provided by FirstRanker.com ---


# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

--- Content provided by FirstRanker.com ---

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))




--- Content provided by FirstRanker.com ---




Ex. No. :01
Date:

--- Content provided by FirstRanker.com ---


GCD OF TWO NUMBERS

FirstRanker Page 8 of 42
Sample output :

--- Content provided by FirstRanker.com ---



The GCD of 54 and 24 is 6


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to compute GCD of two numbers is executed successfully and

--- Content provided by FirstRanker.com ---

the output is verified.

FirstRanker Page 9 of 42

Aim:

--- Content provided by FirstRanker.com ---

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,

--- Content provided by FirstRanker.com ---

i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

Program:

--- Content provided by FirstRanker.com ---


def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:

--- Content provided by FirstRanker.com ---

approx = better
better = 0.5 * (approx + n/approx)
return approx

print('The square root is' ,newtonSqrt(81))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Ex. No. :02

--- Content provided by FirstRanker.com ---

Date:

SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42

--- Content provided by FirstRanker.com ---

Output:

The square root is 9


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



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 ---



FirstRanker Page 11 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find the exponentiation of a number.

Algorithm:

1. Define a function named power()

--- Content provided by FirstRanker.com ---

2. Read the values of base and exp
3. Use ?if ? to check if exp is equal to 1 or not
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

--- Content provided by FirstRanker.com ---


Program:
def power(base,exp):
if(exp==1):
return(base)

--- Content provided by FirstRanker.com ---

if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---

FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2
Result:49

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---

FirstRanker Page 13 of 42


Aim:
To write a Python Program to find the maximum from a list of numbers.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno

--- Content provided by FirstRanker.com ---

5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:

--- Content provided by FirstRanker.com ---

l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))
l.append(a)

--- Content provided by FirstRanker.com ---

maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :
04
Date:

--- Content provided by FirstRanker.com ---


FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42
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 ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus a Python Program to find the maximum from the given list of numbers is successfully
executed and the output is verified.

FirstRanker Page 15 of 42

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):
pos=0

--- Content provided by FirstRanker.com ---

found=False
stop=False
while pos if alist[pos]==item:
found=True
print("element found in position",pos)

--- Content provided by FirstRanker.com ---

else:
if alist[pos]>item:
stop=True
else:
pos=pos+1

--- Content provided by FirstRanker.com ---

return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))

--- Content provided by FirstRanker.com ---

a.append(e)
x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)
Date:

--- Content provided by FirstRanker.com ---


LINEAR SEARCH

FirstRanker Page 16 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter upper limit 5
Enter the elements 6
Enter the elements 45
Enter the elements 2

--- Content provided by FirstRanker.com ---

Enter the elements 61
Enter the elements 26
Enter element to search 6
Element found in position 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.


--- Content provided by FirstRanker.com ---


FirstRanker Page 17 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list
3. Compare the search element with the middle element

--- Content provided by FirstRanker.com ---

i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the

--- Content provided by FirstRanker.com ---

right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. 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:

--- Content provided by FirstRanker.com ---

mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)
else:

--- Content provided by FirstRanker.com ---

if item last=mid-1
EX. No. : 5(b)
Date:

BINARY SEARCH

--- Content provided by FirstRanker.com ---


FirstRanker Page 18 of 42
else:
first=mid+mid-1
return found

--- Content provided by FirstRanker.com ---


a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))

--- Content provided by FirstRanker.com ---

a.append(e)
x=int(input("enter element to search"))
bsearch(a,x)

Output:

--- Content provided by FirstRanker.com ---

enter upper limit 6
enter the elements 2
enter the elements 3
enter the elements 5
enter the elements 7

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

FirstRanker Page 19 of 42

Aim:
To write a Python Program to perform selection sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]

--- Content provided by FirstRanker.com ---

6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0

--- Content provided by FirstRanker.com ---

for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]

--- Content provided by FirstRanker.com ---

alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 6(a)
Date:

SELECTION SORT

--- Content provided by FirstRanker.com ---


FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

GE8161 FirstRanker Page 21 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform insertion sort.
Algorithm:
1. Create a function named insertionsort

--- Content provided by FirstRanker.com ---

2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue

--- Content provided by FirstRanker.com ---

7. Print the sorted list

Program:
def insertionSort(alist):
for index in range(1,len(alist)):

--- Content provided by FirstRanker.com ---

currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1

--- Content provided by FirstRanker.com ---

alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 6(b)
Date:

INSERTION SORT

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 22 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 23 of 42

Aim:
To write a Python Program to perform Merge sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following

--- Content provided by FirstRanker.com ---

if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

Increment j
Increment k
6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]
Increment i

--- Content provided by FirstRanker.com ---

Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j
Increment k

--- Content provided by FirstRanker.com ---

8. Print the sorted list




--- Content provided by FirstRanker.com ---


Ex. No. : 7
Date:

MERGE SORT

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 24 of 42
Program:
def mergeSort(alist):

--- Content provided by FirstRanker.com ---

# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]

--- Content provided by FirstRanker.com ---

mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1

--- Content provided by FirstRanker.com ---

k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1

--- Content provided by FirstRanker.com ---

while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
#print("Merging ",alist)

--- Content provided by FirstRanker.com ---

alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

GE8161 FirstRanker Page 26 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find first n prime numbers.

Algorithm:

--- Content provided by FirstRanker.com ---

1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break
else print the value of num
4. Repeat step 3 for i in range(2,num)

--- Content provided by FirstRanker.com ---


Program:

n = int(input("Enter the upper limit: "))

--- Content provided by FirstRanker.com ---

print("Prime numbers are")

for num in range(0,n + 1):
# prime numbers are greater than 1
if num > 1:

--- Content provided by FirstRanker.com ---

for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 8
Date:

FIRST N PRIME NUMBERS

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 27 of 42
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 ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

GE8161 FirstRanker Page 28 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to multiply matrices.
Algorithm:
1. Define two matrices X and Y

--- Content provided by FirstRanker.com ---

2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):
i. for j in range(len(Y[0])):
a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]

--- Content provided by FirstRanker.com ---

4. for r in result, print the value of r

Program:

X = [[12,7,3],

--- Content provided by FirstRanker.com ---

[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]

--- Content provided by FirstRanker.com ---

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])):

--- Content provided by FirstRanker.com ---

for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)

--- Content provided by FirstRanker.com ---

Ex. No. : 9
Date:

MATRIX MULTIPLICATION

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 29 of 42
Output:

[114, 160, 60, 27]

--- Content provided by FirstRanker.com ---

[74, 97, 73, 14]
[119, 157, 112, 23]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to multiply matrices is executed successfully and the output is
verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 30 of 42

Aim:
To write a Python program for command line arguments.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Add arguments to find the words and lines
2. Add the filename as argument
3. Parse the arguments to get the values
4. Format and print the words and lines

--- Content provided by FirstRanker.com ---


Program:
parser=argparse.ArgumentParser()
parser.add_argument('--words','-w',action='store_true')
parser.add_argument('--lines','-l',action='store_true')

--- Content provided by FirstRanker.com ---

parser.add_argument('filename')
args=parser.parse_args()
if args.words:
print("words {}".format(print_words(args.filename))
elif args.lines:

--- Content provided by FirstRanker.com ---

print("lines {}".format(print_lines(args.filename))
else:
print ("words {}".format(print_words(args.filename))
print("lines {}".format(print_lines(args.filename))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :10
Date:

--- Content provided by FirstRanker.com ---

COMMAND LINE ARGUMENTS(WORD COUNT)

GE8161 FirstRanker Page 31 of 42
Output:

--- Content provided by FirstRanker.com ---


python main.py ?i input.txt
words 23
lines 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program for command line arguments is executed successfully and the output is
verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 32 of 42

Aim:
To write a Python program to find the most frequent words in a text read from a file.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read the filename
2. Open the file in read mode
3. Read each line from the file to count the lowers and words
4. Read each line from the file to replace the punctuations

--- Content provided by FirstRanker.com ---

5. Split each line into words and count them
6. Print the words and counts

Program:
def main():

--- Content provided by FirstRanker.com ---

filename=raw_input("enter the file").strip()
infile=open(filename,"r")
wordcounts={}
for line in infile:
processLine(line.lower(),wordcounts)

--- Content provided by FirstRanker.com ---

pairs=list(wordcounts.items())
items=[[x,y] for (y,x) in pairs]
items.sort()
for i in range(len(items)-1,len(items)-11,-1):
print(items[i][1]+"\t"+str(items[i][0]))

--- Content provided by FirstRanker.com ---

def processLine(line,wordcounts):
line=replacePunctuations(line)
words=line.split()
for word in words:
if word in wordcounts:

--- Content provided by FirstRanker.com ---

wordcounts[word]+=1
else:
wordcounts[word]=1
def replacePunctuations(line):
for ch in line:

--- Content provided by FirstRanker.com ---

if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":
line=line.replace(ch," ")
return line
main()

--- Content provided by FirstRanker.com ---


Ex. No. : 11
Date:

FINDING MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

--- Content provided by FirstRanker.com ---

FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

?

--- Content provided by FirstRanker.com ---




DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

--- Content provided by FirstRanker.com ---


I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

--- Content provided by FirstRanker.com ---



LABORATORY MANUAL

FirstRanker Page 2 of 42

--- Content provided by FirstRanker.com ---

DHANALAKSHMI COLLEGE OF ENGINEERING



is committed to provide highly disciplined, c onscientious and

--- Content provided by FirstRanker.com ---

enterprising professionals conforming to global standards through value based quality education
and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at

--- Content provided by FirstRanker.com ---

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


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


VISION
MISSION

FirstRanker Page 3 of 42

--- Content provided by FirstRanker.com ---

PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and

--- Content provided by FirstRanker.com ---

fundamentals of engineering that will mould them to be successful professionals

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

--- Content provided by FirstRanker.com ---


3. BREADTH
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

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---


5. 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

--- Content provided by FirstRanker.com ---

pursuing advanced degrees




--- Content provided by FirstRanker.com ---



FirstRanker Page 4 of 42
PROGRAM OUTCOMES

--- Content provided by FirstRanker.com ---



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the

--- Content provided by FirstRanker.com ---

same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools

--- Content provided by FirstRanker.com ---

f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms

--- Content provided by FirstRanker.com ---

i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





FirstRanker Page 5 of 42

--- Content provided by FirstRanker.com ---

GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS

--- Content provided by FirstRanker.com ---



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.

--- Content provided by FirstRanker.com ---

2. Find the square root of a number (Newton ?s method)
3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort

--- Content provided by FirstRanker.com ---

7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file

--- Content provided by FirstRanker.com ---

12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame



--- Content provided by FirstRanker.com ---


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.

--- Content provided by FirstRanker.com ---

Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python.



--- Content provided by FirstRanker.com ---


COURSE OUTCOMES
COURSE OBJECTIVES

FirstRanker Page 6 of 42

--- Content provided by FirstRanker.com ---

GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

Sl.No. Name of the Experiment

--- Content provided by FirstRanker.com ---

Page
No.
1
Compute the GCD of two numbers.
07

--- Content provided by FirstRanker.com ---

2
Find the square root of a number (Newton ?s method)
09
3
Exponentiation (power of a number)

--- Content provided by FirstRanker.com ---

11
4
Find the maximum of a list of numbers
13
5(a)

--- Content provided by FirstRanker.com ---

Linear search
15
5(b)
Binary search
17

--- Content provided by FirstRanker.com ---

6(a)
Selection sort
19
6(b)
Insertion sort

--- Content provided by FirstRanker.com ---

21
7
Merge sort
23
8

--- Content provided by FirstRanker.com ---

First n prime numbers
26
9
Multiply matrices
28

--- Content provided by FirstRanker.com ---

10
Programs that take command line arguments (word count)
30
11
Find the most frequent words in a text read from a file

--- Content provided by FirstRanker.com ---

32
12
Simulate elliptical orbits in Pygame
34
13

--- Content provided by FirstRanker.com ---

Simulate bouncing ball in Pygame
37



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 7 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find GCD of two numbers.

Algorithm:

1. Define a function named computeGCD()

--- Content provided by FirstRanker.com ---

2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

--- Content provided by FirstRanker.com ---


def computeGCD(x, y):
if x > y:
smaller = y
else:

--- Content provided by FirstRanker.com ---

smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

--- Content provided by FirstRanker.com ---

return gcd

num1 = 54
num2 = 24

--- Content provided by FirstRanker.com ---

# 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))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :01
Date:

--- Content provided by FirstRanker.com ---

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42
Sample output :

--- Content provided by FirstRanker.com ---


The GCD of 54 and 24 is 6



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

--- Content provided by FirstRanker.com ---


FirstRanker Page 9 of 42

Aim:
To write a Python Program to find the square root of a number by Newton ?s Method.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better

--- Content provided by FirstRanker.com ---

ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

Program:

--- Content provided by FirstRanker.com ---

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better

--- Content provided by FirstRanker.com ---

better = 0.5 * (approx + n/approx)
return approx

print('The square root is' ,newtonSqrt(81))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. :02
Date:

--- Content provided by FirstRanker.com ---


SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42
Output:

--- Content provided by FirstRanker.com ---


The square root is 9



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


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 ---


FirstRanker Page 11 of 42

Aim:
To write a Python program to find the exponentiation of a number.

--- Content provided by FirstRanker.com ---


Algorithm:

1. Define a function named power()
2. Read the values of base and exp

--- Content provided by FirstRanker.com ---

3. Use ?if ? to check if exp is equal to 1 or not
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

--- Content provided by FirstRanker.com ---

Program:
def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):

--- Content provided by FirstRanker.com ---

return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Ex. No. : 03

--- Content provided by FirstRanker.com ---

Date:

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter base: 7
Enter exponential value: 2
Result:49

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

FirstRanker Page 13 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]

--- Content provided by FirstRanker.com ---

6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:
l=[]

--- Content provided by FirstRanker.com ---

n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]

--- Content provided by FirstRanker.com ---

for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :
04
Date:

--- Content provided by FirstRanker.com ---

FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42
Output:
Enter the upper limt 3

--- Content provided by FirstRanker.com ---

Enter the numbers 6
Enter the numbers 9
Enter the numbers 90
The maximum number is 90

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus a Python Program to find the maximum from the given list of numbers is successfully

--- Content provided by FirstRanker.com ---

executed and the output is verified.

FirstRanker Page 15 of 42

Aim:

--- Content provided by FirstRanker.com ---

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. If alist[pos]==item, then print the position of the item

--- Content provided by FirstRanker.com ---

4. 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

--- Content provided by FirstRanker.com ---

stop=False
while pos if alist[pos]==item:
found=True
print("element found in position",pos)
else:

--- Content provided by FirstRanker.com ---

if alist[pos]>item:
stop=True
else:
pos=pos+1
return found

--- Content provided by FirstRanker.com ---

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))
a.append(e)

--- Content provided by FirstRanker.com ---

x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)
Date:

--- Content provided by FirstRanker.com ---

LINEAR SEARCH

FirstRanker Page 16 of 42

Output:

--- Content provided by FirstRanker.com ---

Enter upper limit 5
Enter the elements 6
Enter the elements 45
Enter the elements 2
Enter the elements 61

--- Content provided by FirstRanker.com ---

Enter the elements 26
Enter element to search 6
Element found in position 1


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to perform linear search is executed successfully and the output
is verified.



--- Content provided by FirstRanker.com ---

FirstRanker Page 17 of 42


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
i. if both are matching, print element found

--- Content provided by FirstRanker.com ---

ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element

--- Content provided by FirstRanker.com ---

6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

Program:
def bsearch(alist,item):

--- Content provided by FirstRanker.com ---

first=0
last=len(alist)-1
found=False
while first<=last and not found:
mid=(first+last)//2

--- Content provided by FirstRanker.com ---

if alist[mid]==item:
found=True
print("element found in position",mid)
else:
if item last=mid-1

--- Content provided by FirstRanker.com ---

EX. No. : 5(b)
Date:

BINARY SEARCH

--- Content provided by FirstRanker.com ---

FirstRanker Page 18 of 42
else:
first=mid+mid-1
return found

--- Content provided by FirstRanker.com ---

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))
a.append(e)

--- Content provided by FirstRanker.com ---

x=int(input("enter element to search"))
bsearch(a,x)

Output:
enter upper limit 6

--- Content provided by FirstRanker.com ---

enter the elements 2
enter the elements 3
enter the elements 5
enter the elements 7
enter the elements 14

--- Content provided by FirstRanker.com ---

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

FirstRanker Page 19 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort

--- Content provided by FirstRanker.com ---

2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list

--- Content provided by FirstRanker.com ---

Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0
for location in range(1,i+1):

--- Content provided by FirstRanker.com ---

if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

--- Content provided by FirstRanker.com ---


alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 6(a)
Date:

SELECTION SORT

--- Content provided by FirstRanker.com ---

FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

GE8161 FirstRanker Page 21 of 42

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

--- Content provided by FirstRanker.com ---


Program:
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]

--- Content provided by FirstRanker.com ---

position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue

--- Content provided by FirstRanker.com ---

alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)


--- Content provided by FirstRanker.com ---





Ex. No. : 6(b)

--- Content provided by FirstRanker.com ---

Date:

INSERTION SORT


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 22 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

GE8161 FirstRanker Page 23 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort

--- Content provided by FirstRanker.com ---

2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
Increment i
else
alist[k]=righthalf[j]
Increment j

--- Content provided by FirstRanker.com ---

Increment k
6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]
Increment i
Increment k

--- Content provided by FirstRanker.com ---

7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j
Increment k
8. Print the sorted list

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 7
Date:

MERGE SORT

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 24 of 42
Program:
def mergeSort(alist):
# print("Splitting ",alist)

--- Content provided by FirstRanker.com ---

if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)

--- Content provided by FirstRanker.com ---

mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1

--- Content provided by FirstRanker.com ---

while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
j=j+1
k=k+1
#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]

--- Content provided by FirstRanker.com ---

mergeSort(alist)
print(alist)


GE8161 FirstRanker Page 25 of 42

--- Content provided by FirstRanker.com ---

Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to perform merge sort is successfully executed and the output is
verifie

GE8161 FirstRanker Page 26 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find first n prime numbers.

Algorithm:
1. Read the value of n

--- Content provided by FirstRanker.com ---

2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break
else print the value of num
4. Repeat step 3 for i in range(2,num)

--- Content provided by FirstRanker.com ---

Program:

n = int(input("Enter the upper limit: "))

print("Prime numbers are")

--- Content provided by FirstRanker.com ---


for num in range(0,n + 1):
# prime numbers are greater than 1
if num > 1:
for i in range(2,num):

--- Content provided by FirstRanker.com ---

if (num % i) == 0:
break
else:
print(num)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Ex. No. : 8

--- Content provided by FirstRanker.com ---

Date:

FIRST N PRIME NUMBERS


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 27 of 42
Output:
Enter the upper limit:100
Prime numbers are
2

--- Content provided by FirstRanker.com ---

3
5
7
11
13

--- Content provided by FirstRanker.com ---

17
19
23
29
31

--- Content provided by FirstRanker.com ---

37
41
43
47
53

--- Content provided by FirstRanker.com ---

59
61
67
71
73

--- Content provided by FirstRanker.com ---

79
83
89
97

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to find the first n prime numbers is executed successfully and
the output is verified.

GE8161 FirstRanker Page 28 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to multiply matrices.
Algorithm:
1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?

--- Content provided by FirstRanker.com ---

3. for i in range(len(X)):
i. for j in range(len(Y[0])):
a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

--- Content provided by FirstRanker.com ---


Program:

X = [[12,7,3],
[4 ,5,6],

--- Content provided by FirstRanker.com ---

[7 ,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],

--- Content provided by FirstRanker.com ---

[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)):

--- Content provided by FirstRanker.com ---

result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)

Ex. No. : 9

--- Content provided by FirstRanker.com ---

Date:

MATRIX MULTIPLICATION


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 29 of 42
Output:

[114, 160, 60, 27]
[74, 97, 73, 14]

--- Content provided by FirstRanker.com ---

[119, 157, 112, 23]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to multiply matrices is executed successfully and the output is
verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 30 of 42

Aim:
To write a Python program for command line arguments.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Add arguments to find the words and lines
2. Add the filename as argument
3. Parse the arguments to get the values
4. Format and print the words and lines

--- Content provided by FirstRanker.com ---

Program:
parser=argparse.ArgumentParser()
parser.add_argument('--words','-w',action='store_true')
parser.add_argument('--lines','-l',action='store_true')
parser.add_argument('filename')

--- Content provided by FirstRanker.com ---

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))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :10
Date:

COMMAND LINE ARGUMENTS(WORD COUNT)

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 31 of 42
Output:


--- Content provided by FirstRanker.com ---

python main.py ?i input.txt
words 23
lines 1


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program for command line arguments is executed successfully and the output is
verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 32 of 42

Aim:
To write a Python program to find the most frequent words in a text read from a file.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read the filename
2. Open the file in read mode
3. Read each line from the file to count the lowers and words
4. Read each line from the file to replace the punctuations
5. Split each line into words and count them

--- Content provided by FirstRanker.com ---

6. Print the words and counts

Program:
def main():
filename=raw_input("enter the file").strip()

--- Content provided by FirstRanker.com ---

infile=open(filename,"r")
wordcounts={}
for line in infile:
processLine(line.lower(),wordcounts)
pairs=list(wordcounts.items())

--- Content provided by FirstRanker.com ---

items=[[x,y] for (y,x) in pairs]
items.sort()
for i in range(len(items)-1,len(items)-11,-1):
print(items[i][1]+"\t"+str(items[i][0]))
def processLine(line,wordcounts):

--- Content provided by FirstRanker.com ---

line=replacePunctuations(line)
words=line.split()
for word in words:
if word in wordcounts:
wordcounts[word]+=1

--- Content provided by FirstRanker.com ---

else:
wordcounts[word]=1
def replacePunctuations(line):
for ch in line:
if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":

--- Content provided by FirstRanker.com ---

line=line.replace(ch," ")
return line
main()


--- Content provided by FirstRanker.com ---

Ex. No. : 11
Date:

FINDING MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 33 of 42
Output:


Enter a filename:a.txt

--- Content provided by FirstRanker.com ---



Hi 1
How 1
Are 1

--- Content provided by FirstRanker.com ---

You 1




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to find the most frequent words in a text read from a file is
executed successfully and the output is verified.
FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---


FirstRanker Page 1 of 42

?

--- Content provided by FirstRanker.com ---



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


--- Content provided by FirstRanker.com ---

I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

--- Content provided by FirstRanker.com ---


LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---




is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education

--- Content provided by FirstRanker.com ---

and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at
different levels

--- Content provided by FirstRanker.com ---

? 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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

VISION
MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---



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

--- Content provided by FirstRanker.com ---

3. BREADTH
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

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

5. 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 4 of 42
PROGRAM OUTCOMES


--- Content provided by FirstRanker.com ---


a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career

--- Content provided by FirstRanker.com ---

c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society

--- Content provided by FirstRanker.com ---

g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with

--- Content provided by FirstRanker.com ---

coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS


--- Content provided by FirstRanker.com ---


To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort

--- Content provided by FirstRanker.com ---

8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

13.Simulate bouncing ball in Pygame




--- Content provided by FirstRanker.com ---

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.




--- Content provided by FirstRanker.com ---

COURSE OUTCOMES
COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
CONTENTS

Sl.No. Name of the Experiment
Page

--- Content provided by FirstRanker.com ---

No.
1
Compute the GCD of two numbers.
07
2

--- Content provided by FirstRanker.com ---

Find the square root of a number (Newton ?s method)
09
3
Exponentiation (power of a number)
11

--- Content provided by FirstRanker.com ---

4
Find the maximum of a list of numbers
13
5(a)
Linear search

--- Content provided by FirstRanker.com ---

15
5(b)
Binary search
17
6(a)

--- Content provided by FirstRanker.com ---

Selection sort
19
6(b)
Insertion sort
21

--- Content provided by FirstRanker.com ---

7
Merge sort
23
8
First n prime numbers

--- Content provided by FirstRanker.com ---

26
9
Multiply matrices
28
10

--- Content provided by FirstRanker.com ---

Programs that take command line arguments (word count)
30
11
Find the most frequent words in a text read from a file
32

--- Content provided by FirstRanker.com ---

12
Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---

37




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 7 of 42


Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---


Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y

--- Content provided by FirstRanker.com ---

3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

--- Content provided by FirstRanker.com ---

def computeGCD(x, y):
if x > y:
smaller = y
else:
smaller = x

--- Content provided by FirstRanker.com ---

for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---


num1 = 54
num2 = 24

# take input from the user

--- Content provided by FirstRanker.com ---

# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 8 of 42
Sample output :


--- Content provided by FirstRanker.com ---

The GCD of 54 and 24 is 6




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

--- Content provided by FirstRanker.com ---

FirstRanker Page 9 of 42

Aim:
To write a Python Program to find the square root of a number by Newton ?s Method.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

4. Print the value of approx

Program:

def newtonSqrt(n):

--- Content provided by FirstRanker.com ---

approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

return approx

print('The square root is' ,newtonSqrt(81))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :02
Date:

--- Content provided by FirstRanker.com ---

SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42
Output:

--- Content provided by FirstRanker.com ---

The square root is 9




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

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 ---

FirstRanker Page 11 of 42

Aim:
To write a Python program to find the exponentiation of a number.

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:

--- Content provided by FirstRanker.com ---

def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---


EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:

--- Content provided by FirstRanker.com ---

Enter base: 7
Enter exponential value: 2
Result:49


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

FirstRanker Page 13 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l

--- Content provided by FirstRanker.com ---

2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1

--- Content provided by FirstRanker.com ---

7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:
l=[]
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=l[0]
for i in range(0,len(l)):

--- Content provided by FirstRanker.com ---

if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :
04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6

--- Content provided by FirstRanker.com ---

Enter the numbers 9
Enter the numbers 90
The maximum number is 90


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---


FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list

--- Content provided by FirstRanker.com ---

Program:
def search(alist,item):
pos=0
found=False
stop=False

--- Content provided by FirstRanker.com ---

while pos if alist[pos]==item:
found=True
print("element found in position",pos)
else:
if alist[pos]>item:

--- Content provided by FirstRanker.com ---

stop=True
else:
pos=pos+1
return found
a=[]

--- Content provided by FirstRanker.com ---

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"))

--- Content provided by FirstRanker.com ---

search(a,x)
Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---


FirstRanker Page 16 of 42

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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to perform linear search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified.



FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python Program to perform binary search.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read the search element
2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element

--- Content provided by FirstRanker.com ---

4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list

--- Content provided by FirstRanker.com ---

7. If element is not found, loop terminates

Program:
def bsearch(alist,item):
first=0

--- Content provided by FirstRanker.com ---

last=len(alist)-1
found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)

--- Content provided by FirstRanker.com ---

Date:

BINARY SEARCH

FirstRanker Page 18 of 42

--- Content provided by FirstRanker.com ---

else:
first=mid+mid-1
return found

a=[]

--- Content provided by FirstRanker.com ---

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"))

--- Content provided by FirstRanker.com ---

bsearch(a,x)

Output:
enter upper limit 6
enter the elements 2

--- Content provided by FirstRanker.com ---

enter the elements 3
enter the elements 5
enter the elements 7
enter the elements 14
enter the elements 25

--- Content provided by FirstRanker.com ---

enter element to search 5
element found in position 2


Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to perform binary search is executed successfully and the output
is verified

FirstRanker Page 19 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0

--- Content provided by FirstRanker.com ---

3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]
6. 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]:

--- Content provided by FirstRanker.com ---

pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

--- Content provided by FirstRanker.com ---

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)


--- Content provided by FirstRanker.com ---





Ex. No. : 6(a)

--- Content provided by FirstRanker.com ---

Date:

SELECTION SORT

FirstRanker Page 20 of 42

--- Content provided by FirstRanker.com ---

Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to perform selection sort is successfully executed and the

--- Content provided by FirstRanker.com ---

output is verified.

GE8161 FirstRanker Page 21 of 42

Aim:

--- Content provided by FirstRanker.com ---

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. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)

--- Content provided by FirstRanker.com ---

4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

--- Content provided by FirstRanker.com ---

Program:
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index

--- Content provided by FirstRanker.com ---

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]

--- Content provided by FirstRanker.com ---

insertionSort(alist)
print(alist)



--- Content provided by FirstRanker.com ---




Ex. No. : 6(b)
Date:

--- Content provided by FirstRanker.com ---


INSERTION SORT


GE8161 FirstRanker Page 22 of 42

--- Content provided by FirstRanker.com ---

Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

GE8161 FirstRanker Page 23 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list

--- Content provided by FirstRanker.com ---

3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0
5. 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 ---

6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]
Increment i
Increment k
7. while j < len(righthalf), perform the following

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
Increment j
Increment k
8. Print the sorted list

--- Content provided by FirstRanker.com ---





Ex. No. : 7

--- Content provided by FirstRanker.com ---

Date:

MERGE SORT


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 24 of 42
Program:
def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:

--- Content provided by FirstRanker.com ---

mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)

--- Content provided by FirstRanker.com ---

i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1

--- Content provided by FirstRanker.com ---

else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

j=j+1
k=k+1
#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)


GE8161 FirstRanker Page 25 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to perform merge sort is successfully executed and the output is

--- Content provided by FirstRanker.com ---

verifie

GE8161 FirstRanker Page 26 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find first n prime numbers.

Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following

--- Content provided by FirstRanker.com ---

3. if num%i is 0 then break
else print the value of num
4. 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")

--- Content provided by FirstRanker.com ---

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:

--- Content provided by FirstRanker.com ---

break
else:
print(num)


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. : 8
Date:

--- Content provided by FirstRanker.com ---


FIRST N PRIME NUMBERS


GE8161 FirstRanker Page 27 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limit:100
Prime numbers are
2
3

--- Content provided by FirstRanker.com ---

5
7
11
13
17

--- Content provided by FirstRanker.com ---

19
23
29
31
37

--- Content provided by FirstRanker.com ---

41
43
47
53
59

--- Content provided by FirstRanker.com ---

61
67
71
73
79

--- Content provided by FirstRanker.com ---

83
89
97


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to find the first n prime numbers is executed successfully and

--- Content provided by FirstRanker.com ---

the output is verified.

GE8161 FirstRanker Page 28 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to multiply matrices.
Algorithm:
1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):

--- Content provided by FirstRanker.com ---

i. for j in range(len(Y[0])):
a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

--- Content provided by FirstRanker.com ---

Program:

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]

--- Content provided by FirstRanker.com ---

Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],

--- Content provided by FirstRanker.com ---

[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]

--- Content provided by FirstRanker.com ---

for r in result:
print(r)

Ex. No. : 9
Date:

--- Content provided by FirstRanker.com ---


MATRIX MULTIPLICATION


GE8161 FirstRanker Page 29 of 42

--- Content provided by FirstRanker.com ---

Output:

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to multiply matrices is executed successfully and the output is
verified.

GE8161 FirstRanker Page 30 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program for command line arguments.
Algorithm:
1. Add arguments to find the words and lines

--- Content provided by FirstRanker.com ---

2. Add the filename as argument
3. Parse the arguments to get the values
4. Format and print the words and lines

Program:

--- Content provided by FirstRanker.com ---

parser=argparse.ArgumentParser()
parser.add_argument('--words','-w',action='store_true')
parser.add_argument('--lines','-l',action='store_true')
parser.add_argument('filename')
args=parser.parse_args()

--- Content provided by FirstRanker.com ---

if args.words:
print("words {}".format(print_words(args.filename))
elif args.lines:
print("lines {}".format(print_lines(args.filename))
else:

--- Content provided by FirstRanker.com ---

print ("words {}".format(print_words(args.filename))
print("lines {}".format(print_lines(args.filename))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :10
Date:

COMMAND LINE ARGUMENTS(WORD COUNT)

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 31 of 42
Output:


python main.py ?i input.txt

--- Content provided by FirstRanker.com ---

words 23
lines 1



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program for command line arguments is executed successfully and the output is
verified.

GE8161 FirstRanker Page 32 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find the most frequent words in a text read from a file.
Algorithm:
1. Read the filename

--- Content provided by FirstRanker.com ---

2. Open the file in read mode
3. Read each line from the file to count the lowers and words
4. Read each line from the file to replace the punctuations
5. Split each line into words and count them
6. Print the words and counts

--- Content provided by FirstRanker.com ---


Program:
def main():
filename=raw_input("enter the file").strip()
infile=open(filename,"r")

--- Content provided by FirstRanker.com ---

wordcounts={}
for line in infile:
processLine(line.lower(),wordcounts)
pairs=list(wordcounts.items())
items=[[x,y] for (y,x) in pairs]

--- Content provided by FirstRanker.com ---

items.sort()
for i in range(len(items)-1,len(items)-11,-1):
print(items[i][1]+"\t"+str(items[i][0]))
def processLine(line,wordcounts):
line=replacePunctuations(line)

--- Content provided by FirstRanker.com ---

words=line.split()
for word in words:
if word in wordcounts:
wordcounts[word]+=1
else:

--- Content provided by FirstRanker.com ---

wordcounts[word]=1
def replacePunctuations(line):
for ch in line:
if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":
line=line.replace(ch," ")

--- Content provided by FirstRanker.com ---

return line
main()


Ex. No. : 11

--- Content provided by FirstRanker.com ---

Date:

FINDING MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

GE8161 FirstRanker Page 33 of 42

--- Content provided by FirstRanker.com ---

Output:


Enter a filename:a.txt

--- Content provided by FirstRanker.com ---


Hi 1
How 1
Are 1
You 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to find the most frequent words in a text read from a file is
executed successfully and the output is verified.

GE8161 FirstRanker Page 34 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to simulate elliptical orbits in Pygame.
Algorithm:
1. Import the required packages

--- Content provided by FirstRanker.com ---

2. Set up the colours for the elliptical orbits
3. Define the parameters to simulate elliptical orbits
4. Display the created orbits

Program:

--- Content provided by FirstRanker.com ---

import math
import random
import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):

--- Content provided by FirstRanker.com ---

self.x = x
self.y = y
self.vx = 0
self.vy = 0
self.colour = colour

--- Content provided by FirstRanker.com ---

def apply_gravity (self, target):
dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2
#g = G*m/dsqd * normalized (self - target)
if dsqd == 0:
return

--- Content provided by FirstRanker.com ---

self.vx += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5
self.vy += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5
def update (self):
self.x += self.vx
self.y += self.vy

--- Content provided by FirstRanker.com ---

pygame.init()
window = pygame.display.set_mode ((600, 400))
main_surface = pygame.Surface ((600, 400))
colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666,
0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] + [0xFF0000, 0x00FF00, 0x0000FF,

--- Content provided by FirstRanker.com ---

0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888, 0xFFFFFF, 0x808000, 0x008080, 0x800080,
0x800000]
#colours = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,
Ex. No. : 12
Date:

--- Content provided by FirstRanker.com ---


SIMULATE ELLIPTICAL ORBITS IN PYGAME
FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

--- Content provided by FirstRanker.com ---


?



--- Content provided by FirstRanker.com ---

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY



--- Content provided by FirstRanker.com ---





Name : _______________________________________

--- Content provided by FirstRanker.com ---

Register No : _______________________________________
Section : _______________________________________


LABORATORY MANUAL

--- Content provided by FirstRanker.com ---


FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING


--- Content provided by FirstRanker.com ---


is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---

? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




VISION
MISSION

--- Content provided by FirstRanker.com ---


FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


--- Content provided by FirstRanker.com ---

1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and
fundamentals of engineering that will mould them to be successful professionals

2. CORE COMPETENCE

--- Content provided by FirstRanker.com ---

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
To provide relevant training and experience to bridge the gap between theory and practice

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

will build up their communication skills, individual leadership and supportive qualities, and to enable
them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---

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


--- Content provided by FirstRanker.com ---





FirstRanker Page 4 of 42

--- Content provided by FirstRanker.com ---

PROGRAM OUTCOMES



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in

--- Content provided by FirstRanker.com ---

Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution

--- Content provided by FirstRanker.com ---

e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable

--- Content provided by FirstRanker.com ---

h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning

--- Content provided by FirstRanker.com ---

k) To develop and manage projects in multidisciplinary environments




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)

--- Content provided by FirstRanker.com ---

SYLLABUS



To write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---

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. Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices

--- Content provided by FirstRanker.com ---

10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---




Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---




COURSE OUTCOMES
COURSE OBJECTIVES

--- Content provided by FirstRanker.com ---


FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

--- Content provided by FirstRanker.com ---


Sl.No. Name of the Experiment
Page
No.
1

--- Content provided by FirstRanker.com ---

Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)
09

--- Content provided by FirstRanker.com ---

3
Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

13
5(a)
Linear search
15
5(b)

--- Content provided by FirstRanker.com ---

Binary search
17
6(a)
Selection sort
19

--- Content provided by FirstRanker.com ---

6(b)
Insertion sort
21
7
Merge sort

--- Content provided by FirstRanker.com ---

23
8
First n prime numbers
26
9

--- Content provided by FirstRanker.com ---

Multiply matrices
28
10
Programs that take command line arguments (word count)
30

--- Content provided by FirstRanker.com ---

11
Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find GCD of two numbers.

Algorithm:

--- Content provided by FirstRanker.com ---


1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i

--- Content provided by FirstRanker.com ---

4. Print the value of gcd
Program:

def computeGCD(x, y):
if x > y:

--- Content provided by FirstRanker.com ---

smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):

--- Content provided by FirstRanker.com ---

gcd = i

return gcd

num1 = 54

--- Content provided by FirstRanker.com ---

num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

--- Content provided by FirstRanker.com ---


print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))



--- Content provided by FirstRanker.com ---





Ex. No. :01

--- Content provided by FirstRanker.com ---

Date:

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42

--- Content provided by FirstRanker.com ---

Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---

Program:

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---

print('The square root is' ,newtonSqrt(81))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---

FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program for finding the square root of a given number by Newton ?s

--- Content provided by FirstRanker.com ---

Method is executed successfully and the output is verified.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---

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
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

4. Print the result

Program:
def power(base,exp):
if(exp==1):

--- Content provided by FirstRanker.com ---

return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))

--- Content provided by FirstRanker.com ---

print("Result:",power(base,exp))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2

--- Content provided by FirstRanker.com ---

Result:49




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---


FirstRanker Page 13 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n

--- Content provided by FirstRanker.com ---

4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---

Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))

--- Content provided by FirstRanker.com ---

l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]

--- Content provided by FirstRanker.com ---

print("The maximum number is %d"%maxno)




--- Content provided by FirstRanker.com ---




Ex. No. :
04

--- Content provided by FirstRanker.com ---

Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9
Enter the numbers 90

--- Content provided by FirstRanker.com ---

The maximum number is 90




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

FirstRanker Page 15 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform Linear Search
Algorithm:
1. Read n elements into the list

--- Content provided by FirstRanker.com ---

2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):

--- Content provided by FirstRanker.com ---

pos=0
found=False
stop=False
while pos if alist[pos]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:

--- Content provided by FirstRanker.com ---

pos=pos+1
return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

e=int(input("enter the elements"))
a.append(e)
x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)

--- Content provided by FirstRanker.com ---

Date:

LINEAR SEARCH

FirstRanker Page 16 of 42

--- Content provided by FirstRanker.com ---


Output:
Enter upper limit 5
Enter the elements 6
Enter the elements 45

--- Content provided by FirstRanker.com ---

Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6
Element found in position 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---



FirstRanker Page 17 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list

--- Content provided by FirstRanker.com ---

3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element

--- Content provided by FirstRanker.com ---

5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---

Program:
def bsearch(alist,item):
first=0
last=len(alist)-1
found=False

--- Content provided by FirstRanker.com ---

while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)

--- Content provided by FirstRanker.com ---

else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---

BINARY SEARCH

FirstRanker Page 18 of 42
else:
first=mid+mid-1

--- Content provided by FirstRanker.com ---

return found

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---


FirstRanker Page 19 of 42

Aim:
To write a Python Program to perform selection sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location

--- Content provided by FirstRanker.com ---

5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):

--- Content provided by FirstRanker.com ---

pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]

--- Content provided by FirstRanker.com ---

alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---

SELECTION SORT

FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1

--- Content provided by FirstRanker.com ---

6. alist[position]=currentvalue
7. Print the sorted list

Program:
def insertionSort(alist):

--- Content provided by FirstRanker.com ---

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]

--- Content provided by FirstRanker.com ---

position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 6(b)
Date:

INSERTION SORT

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 22 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 23 of 42

Aim:
To write a Python Program to perform Merge sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0

--- Content provided by FirstRanker.com ---

5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j

--- Content provided by FirstRanker.com ---

Increment k
8. Print the sorted list



--- Content provided by FirstRanker.com ---



Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---

MERGE SORT


GE8161 FirstRanker Page 24 of 42
Program:

--- Content provided by FirstRanker.com ---

def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]

--- Content provided by FirstRanker.com ---

righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):

--- Content provided by FirstRanker.com ---

if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1

--- Content provided by FirstRanker.com ---

k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1

--- Content provided by FirstRanker.com ---

#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 26 of 42

Aim:
To write a Python program to find first n prime numbers.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break
else print the value of num

--- Content provided by FirstRanker.com ---

4. Repeat step 3 for i in range(2,num)

Program:

n = int(input("Enter the upper limit: "))

--- Content provided by FirstRanker.com ---


print("Prime numbers are")

for num in range(0,n + 1):
# prime numbers are greater than 1

--- Content provided by FirstRanker.com ---

if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:

--- Content provided by FirstRanker.com ---

print(num)




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 8
Date:

FIRST N PRIME NUMBERS

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 27 of 42
Output:
Enter the upper limit:100

--- Content provided by FirstRanker.com ---

Prime numbers are
2
3
5
7

--- Content provided by FirstRanker.com ---

11
13
17
19
23

--- Content provided by FirstRanker.com ---

29
31
37
41
43

--- Content provided by FirstRanker.com ---

47
53
59
61
67

--- Content provided by FirstRanker.com ---

71
73
79
83
89

--- Content provided by FirstRanker.com ---

97




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 28 of 42

Aim:
To write a Python program to multiply matrices.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):
i. for j in range(len(Y[0])):
a) for k in range(len(Y))

--- Content provided by FirstRanker.com ---

b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

Program:

--- Content provided by FirstRanker.com ---

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],

--- Content provided by FirstRanker.com ---

[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for i in range(len(X)):

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---


Ex. No. : 9
Date:

MATRIX MULTIPLICATION

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 29 of 42
Output:

--- Content provided by FirstRanker.com ---

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to multiply matrices is executed successfully and the output is

--- Content provided by FirstRanker.com ---

verified.

GE8161 FirstRanker Page 30 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program for command line arguments.
Algorithm:
1. Add arguments to find the words and lines
2. Add the filename as argument
3. Parse the arguments to get the values

--- Content provided by FirstRanker.com ---

4. Format and print the words and lines

Program:
parser=argparse.ArgumentParser()
parser.add_argument('--words','-w',action='store_true')

--- Content provided by FirstRanker.com ---

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))

--- Content provided by FirstRanker.com ---

elif args.lines:
print("lines {}".format(print_lines(args.filename))
else:
print ("words {}".format(print_words(args.filename))
print("lines {}".format(print_lines(args.filename))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. :10
Date:

--- Content provided by FirstRanker.com ---


COMMAND LINE ARGUMENTS(WORD COUNT)

GE8161 FirstRanker Page 31 of 42
Output:

--- Content provided by FirstRanker.com ---



python main.py ?i input.txt
words 23
lines 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program for command line arguments is executed successfully and the output is

--- Content provided by FirstRanker.com ---

verified.

GE8161 FirstRanker Page 32 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find the most frequent words in a text read from a file.
Algorithm:
1. Read the filename
2. Open the file in read mode
3. Read each line from the file to count the lowers and words

--- Content provided by FirstRanker.com ---

4. Read each line from the file to replace the punctuations
5. Split each line into words and count them
6. Print the words and counts

Program:

--- Content provided by FirstRanker.com ---

def main():
filename=raw_input("enter the file").strip()
infile=open(filename,"r")
wordcounts={}
for line in infile:

--- Content provided by FirstRanker.com ---

processLine(line.lower(),wordcounts)
pairs=list(wordcounts.items())
items=[[x,y] for (y,x) in pairs]
items.sort()
for i in range(len(items)-1,len(items)-11,-1):

--- Content provided by FirstRanker.com ---

print(items[i][1]+"\t"+str(items[i][0]))
def processLine(line,wordcounts):
line=replacePunctuations(line)
words=line.split()
for word in words:

--- Content provided by FirstRanker.com ---

if word in wordcounts:
wordcounts[word]+=1
else:
wordcounts[word]=1
def replacePunctuations(line):

--- Content provided by FirstRanker.com ---

for ch in line:
if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":
line=line.replace(ch," ")
return line
main()

--- Content provided by FirstRanker.com ---



Ex. No. : 11
Date:

--- Content provided by FirstRanker.com ---

FINDING MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

GE8161 FirstRanker Page 33 of 42
Output:

--- Content provided by FirstRanker.com ---


Enter a filename:a.txt


Hi 1

--- Content provided by FirstRanker.com ---

How 1
Are 1
You 1


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to find the most frequent words in a text read from a file is

--- Content provided by FirstRanker.com ---

executed successfully and the output is verified.

GE8161 FirstRanker Page 34 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to simulate elliptical orbits in Pygame.
Algorithm:
1. Import the required packages
2. Set up the colours for the elliptical orbits
3. Define the parameters to simulate elliptical orbits

--- Content provided by FirstRanker.com ---

4. Display the created orbits

Program:
import math
import random

--- Content provided by FirstRanker.com ---

import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):
self.x = x
self.y = y

--- Content provided by FirstRanker.com ---

self.vx = 0
self.vy = 0
self.colour = colour
def apply_gravity (self, target):
dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2

--- Content provided by FirstRanker.com ---

#g = G*m/dsqd * normalized (self - target)
if dsqd == 0:
return
self.vx += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5
self.vy += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5

--- Content provided by FirstRanker.com ---

def update (self):
self.x += self.vx
self.y += self.vy
pygame.init()
window = pygame.display.set_mode ((600, 400))

--- Content provided by FirstRanker.com ---

main_surface = pygame.Surface ((600, 400))
colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666,
0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] + [0xFF0000, 0x00FF00, 0x0000FF,
0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888, 0xFFFFFF, 0x808000, 0x008080, 0x800080,
0x800000]

--- Content provided by FirstRanker.com ---

#colours = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,
Ex. No. : 12
Date:

SIMULATE ELLIPTICAL ORBITS IN PYGAME

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 35 of 42
0xFFFFFF, 0x808000, 0x008080, 0x800080, 0x800000]
particles = [Particle (200, 100, colours [i]) for i in range (20)]
earth = Particle (200, 200)

--- Content provided by FirstRanker.com ---

for i, p in enumerate (particles):
p.vx = i / 100
while (True):
# main_surface.fill(0x000000)
pygame.draw.circle (main_surface, 0x00FF00, (earth.x, earth.y), 5, 2)

--- Content provided by FirstRanker.com ---


for p in particles:
p.apply_gravity (earth)
p.update ()
pygame.draw.circle (main_surface, p.colour, (int (p.x), int (p.y)), 5, 2)

--- Content provided by FirstRanker.com ---

window.blit(main_surface, (0, 0))
pygame.display.flip()



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

--- Content provided by FirstRanker.com ---


?



--- Content provided by FirstRanker.com ---

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY



--- Content provided by FirstRanker.com ---





Name : _______________________________________

--- Content provided by FirstRanker.com ---

Register No : _______________________________________
Section : _______________________________________


LABORATORY MANUAL

--- Content provided by FirstRanker.com ---


FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING


--- Content provided by FirstRanker.com ---


is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---

? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




VISION
MISSION

--- Content provided by FirstRanker.com ---


FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


--- Content provided by FirstRanker.com ---

1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and
fundamentals of engineering that will mould them to be successful professionals

2. CORE COMPETENCE

--- Content provided by FirstRanker.com ---

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
To provide relevant training and experience to bridge the gap between theory and practice

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

will build up their communication skills, individual leadership and supportive qualities, and to enable
them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---

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


--- Content provided by FirstRanker.com ---





FirstRanker Page 4 of 42

--- Content provided by FirstRanker.com ---

PROGRAM OUTCOMES



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in

--- Content provided by FirstRanker.com ---

Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution

--- Content provided by FirstRanker.com ---

e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable

--- Content provided by FirstRanker.com ---

h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning

--- Content provided by FirstRanker.com ---

k) To develop and manage projects in multidisciplinary environments




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)

--- Content provided by FirstRanker.com ---

SYLLABUS



To write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---

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. Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices

--- Content provided by FirstRanker.com ---

10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---




Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---




COURSE OUTCOMES
COURSE OBJECTIVES

--- Content provided by FirstRanker.com ---


FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

--- Content provided by FirstRanker.com ---


Sl.No. Name of the Experiment
Page
No.
1

--- Content provided by FirstRanker.com ---

Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)
09

--- Content provided by FirstRanker.com ---

3
Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

13
5(a)
Linear search
15
5(b)

--- Content provided by FirstRanker.com ---

Binary search
17
6(a)
Selection sort
19

--- Content provided by FirstRanker.com ---

6(b)
Insertion sort
21
7
Merge sort

--- Content provided by FirstRanker.com ---

23
8
First n prime numbers
26
9

--- Content provided by FirstRanker.com ---

Multiply matrices
28
10
Programs that take command line arguments (word count)
30

--- Content provided by FirstRanker.com ---

11
Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find GCD of two numbers.

Algorithm:

--- Content provided by FirstRanker.com ---


1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i

--- Content provided by FirstRanker.com ---

4. Print the value of gcd
Program:

def computeGCD(x, y):
if x > y:

--- Content provided by FirstRanker.com ---

smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):

--- Content provided by FirstRanker.com ---

gcd = i

return gcd

num1 = 54

--- Content provided by FirstRanker.com ---

num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

--- Content provided by FirstRanker.com ---


print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))



--- Content provided by FirstRanker.com ---





Ex. No. :01

--- Content provided by FirstRanker.com ---

Date:

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42

--- Content provided by FirstRanker.com ---

Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---

Program:

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---

print('The square root is' ,newtonSqrt(81))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---

FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program for finding the square root of a given number by Newton ?s

--- Content provided by FirstRanker.com ---

Method is executed successfully and the output is verified.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---

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
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

4. Print the result

Program:
def power(base,exp):
if(exp==1):

--- Content provided by FirstRanker.com ---

return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))

--- Content provided by FirstRanker.com ---

print("Result:",power(base,exp))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2

--- Content provided by FirstRanker.com ---

Result:49




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---


FirstRanker Page 13 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n

--- Content provided by FirstRanker.com ---

4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---

Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))

--- Content provided by FirstRanker.com ---

l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]

--- Content provided by FirstRanker.com ---

print("The maximum number is %d"%maxno)




--- Content provided by FirstRanker.com ---




Ex. No. :
04

--- Content provided by FirstRanker.com ---

Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9
Enter the numbers 90

--- Content provided by FirstRanker.com ---

The maximum number is 90




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

FirstRanker Page 15 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform Linear Search
Algorithm:
1. Read n elements into the list

--- Content provided by FirstRanker.com ---

2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):

--- Content provided by FirstRanker.com ---

pos=0
found=False
stop=False
while pos if alist[pos]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:

--- Content provided by FirstRanker.com ---

pos=pos+1
return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

e=int(input("enter the elements"))
a.append(e)
x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)

--- Content provided by FirstRanker.com ---

Date:

LINEAR SEARCH

FirstRanker Page 16 of 42

--- Content provided by FirstRanker.com ---


Output:
Enter upper limit 5
Enter the elements 6
Enter the elements 45

--- Content provided by FirstRanker.com ---

Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6
Element found in position 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---



FirstRanker Page 17 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list

--- Content provided by FirstRanker.com ---

3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element

--- Content provided by FirstRanker.com ---

5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---

Program:
def bsearch(alist,item):
first=0
last=len(alist)-1
found=False

--- Content provided by FirstRanker.com ---

while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)

--- Content provided by FirstRanker.com ---

else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---

BINARY SEARCH

FirstRanker Page 18 of 42
else:
first=mid+mid-1

--- Content provided by FirstRanker.com ---

return found

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---


FirstRanker Page 19 of 42

Aim:
To write a Python Program to perform selection sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location

--- Content provided by FirstRanker.com ---

5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):

--- Content provided by FirstRanker.com ---

pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]

--- Content provided by FirstRanker.com ---

alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---

SELECTION SORT

FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1

--- Content provided by FirstRanker.com ---

6. alist[position]=currentvalue
7. Print the sorted list

Program:
def insertionSort(alist):

--- Content provided by FirstRanker.com ---

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]

--- Content provided by FirstRanker.com ---

position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 6(b)
Date:

INSERTION SORT

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 22 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 23 of 42

Aim:
To write a Python Program to perform Merge sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0

--- Content provided by FirstRanker.com ---

5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j

--- Content provided by FirstRanker.com ---

Increment k
8. Print the sorted list



--- Content provided by FirstRanker.com ---



Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---

MERGE SORT


GE8161 FirstRanker Page 24 of 42
Program:

--- Content provided by FirstRanker.com ---

def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]

--- Content provided by FirstRanker.com ---

righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):

--- Content provided by FirstRanker.com ---

if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1

--- Content provided by FirstRanker.com ---

k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1

--- Content provided by FirstRanker.com ---

#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 26 of 42

Aim:
To write a Python program to find first n prime numbers.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break
else print the value of num

--- Content provided by FirstRanker.com ---

4. Repeat step 3 for i in range(2,num)

Program:

n = int(input("Enter the upper limit: "))

--- Content provided by FirstRanker.com ---


print("Prime numbers are")

for num in range(0,n + 1):
# prime numbers are greater than 1

--- Content provided by FirstRanker.com ---

if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:

--- Content provided by FirstRanker.com ---

print(num)




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 8
Date:

FIRST N PRIME NUMBERS

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 27 of 42
Output:
Enter the upper limit:100

--- Content provided by FirstRanker.com ---

Prime numbers are
2
3
5
7

--- Content provided by FirstRanker.com ---

11
13
17
19
23

--- Content provided by FirstRanker.com ---

29
31
37
41
43

--- Content provided by FirstRanker.com ---

47
53
59
61
67

--- Content provided by FirstRanker.com ---

71
73
79
83
89

--- Content provided by FirstRanker.com ---

97




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 28 of 42

Aim:
To write a Python program to multiply matrices.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):
i. for j in range(len(Y[0])):
a) for k in range(len(Y))

--- Content provided by FirstRanker.com ---

b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

Program:

--- Content provided by FirstRanker.com ---

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],

--- Content provided by FirstRanker.com ---

[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for i in range(len(X)):

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---


Ex. No. : 9
Date:

MATRIX MULTIPLICATION

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 29 of 42
Output:

--- Content provided by FirstRanker.com ---

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to multiply matrices is executed successfully and the output is

--- Content provided by FirstRanker.com ---

verified.

GE8161 FirstRanker Page 30 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program for command line arguments.
Algorithm:
1. Add arguments to find the words and lines
2. Add the filename as argument
3. Parse the arguments to get the values

--- Content provided by FirstRanker.com ---

4. Format and print the words and lines

Program:
parser=argparse.ArgumentParser()
parser.add_argument('--words','-w',action='store_true')

--- Content provided by FirstRanker.com ---

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))

--- Content provided by FirstRanker.com ---

elif args.lines:
print("lines {}".format(print_lines(args.filename))
else:
print ("words {}".format(print_words(args.filename))
print("lines {}".format(print_lines(args.filename))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. :10
Date:

--- Content provided by FirstRanker.com ---


COMMAND LINE ARGUMENTS(WORD COUNT)

GE8161 FirstRanker Page 31 of 42
Output:

--- Content provided by FirstRanker.com ---



python main.py ?i input.txt
words 23
lines 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program for command line arguments is executed successfully and the output is

--- Content provided by FirstRanker.com ---

verified.

GE8161 FirstRanker Page 32 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find the most frequent words in a text read from a file.
Algorithm:
1. Read the filename
2. Open the file in read mode
3. Read each line from the file to count the lowers and words

--- Content provided by FirstRanker.com ---

4. Read each line from the file to replace the punctuations
5. Split each line into words and count them
6. Print the words and counts

Program:

--- Content provided by FirstRanker.com ---

def main():
filename=raw_input("enter the file").strip()
infile=open(filename,"r")
wordcounts={}
for line in infile:

--- Content provided by FirstRanker.com ---

processLine(line.lower(),wordcounts)
pairs=list(wordcounts.items())
items=[[x,y] for (y,x) in pairs]
items.sort()
for i in range(len(items)-1,len(items)-11,-1):

--- Content provided by FirstRanker.com ---

print(items[i][1]+"\t"+str(items[i][0]))
def processLine(line,wordcounts):
line=replacePunctuations(line)
words=line.split()
for word in words:

--- Content provided by FirstRanker.com ---

if word in wordcounts:
wordcounts[word]+=1
else:
wordcounts[word]=1
def replacePunctuations(line):

--- Content provided by FirstRanker.com ---

for ch in line:
if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":
line=line.replace(ch," ")
return line
main()

--- Content provided by FirstRanker.com ---



Ex. No. : 11
Date:

--- Content provided by FirstRanker.com ---

FINDING MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

GE8161 FirstRanker Page 33 of 42
Output:

--- Content provided by FirstRanker.com ---


Enter a filename:a.txt


Hi 1

--- Content provided by FirstRanker.com ---

How 1
Are 1
You 1


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to find the most frequent words in a text read from a file is

--- Content provided by FirstRanker.com ---

executed successfully and the output is verified.

GE8161 FirstRanker Page 34 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to simulate elliptical orbits in Pygame.
Algorithm:
1. Import the required packages
2. Set up the colours for the elliptical orbits
3. Define the parameters to simulate elliptical orbits

--- Content provided by FirstRanker.com ---

4. Display the created orbits

Program:
import math
import random

--- Content provided by FirstRanker.com ---

import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):
self.x = x
self.y = y

--- Content provided by FirstRanker.com ---

self.vx = 0
self.vy = 0
self.colour = colour
def apply_gravity (self, target):
dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2

--- Content provided by FirstRanker.com ---

#g = G*m/dsqd * normalized (self - target)
if dsqd == 0:
return
self.vx += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5
self.vy += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5

--- Content provided by FirstRanker.com ---

def update (self):
self.x += self.vx
self.y += self.vy
pygame.init()
window = pygame.display.set_mode ((600, 400))

--- Content provided by FirstRanker.com ---

main_surface = pygame.Surface ((600, 400))
colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666,
0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] + [0xFF0000, 0x00FF00, 0x0000FF,
0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888, 0xFFFFFF, 0x808000, 0x008080, 0x800080,
0x800000]

--- Content provided by FirstRanker.com ---

#colours = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,
Ex. No. : 12
Date:

SIMULATE ELLIPTICAL ORBITS IN PYGAME

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 35 of 42
0xFFFFFF, 0x808000, 0x008080, 0x800080, 0x800000]
particles = [Particle (200, 100, colours [i]) for i in range (20)]
earth = Particle (200, 200)

--- Content provided by FirstRanker.com ---

for i, p in enumerate (particles):
p.vx = i / 100
while (True):
# main_surface.fill(0x000000)
pygame.draw.circle (main_surface, 0x00FF00, (earth.x, earth.y), 5, 2)

--- Content provided by FirstRanker.com ---


for p in particles:
p.apply_gravity (earth)
p.update ()
pygame.draw.circle (main_surface, p.colour, (int (p.x), int (p.y)), 5, 2)

--- Content provided by FirstRanker.com ---

window.blit(main_surface, (0, 0))
pygame.display.flip()



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




GE8161 FirstRanker Page 36 of 42
Output:

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to simulate elliptical orbits using Pygame is executed
successfully and the output is verified.
FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

--- Content provided by FirstRanker.com ---


?



--- Content provided by FirstRanker.com ---

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY



--- Content provided by FirstRanker.com ---





Name : _______________________________________

--- Content provided by FirstRanker.com ---

Register No : _______________________________________
Section : _______________________________________


LABORATORY MANUAL

--- Content provided by FirstRanker.com ---


FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING


--- Content provided by FirstRanker.com ---


is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---

? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




VISION
MISSION

--- Content provided by FirstRanker.com ---


FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


--- Content provided by FirstRanker.com ---

1. FUNDAMENTALS
To impart students with fundamental knowledge in Mathematics, Science and
fundamentals of engineering that will mould them to be successful professionals

2. CORE COMPETENCE

--- Content provided by FirstRanker.com ---

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
To provide relevant training and experience to bridge the gap between theory and practice

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

will build up their communication skills, individual leadership and supportive qualities, and to enable
them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---

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


--- Content provided by FirstRanker.com ---





FirstRanker Page 4 of 42

--- Content provided by FirstRanker.com ---

PROGRAM OUTCOMES



a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in

--- Content provided by FirstRanker.com ---

Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution

--- Content provided by FirstRanker.com ---

e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable

--- Content provided by FirstRanker.com ---

h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning

--- Content provided by FirstRanker.com ---

k) To develop and manage projects in multidisciplinary environments




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)

--- Content provided by FirstRanker.com ---

SYLLABUS



To write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---

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. Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices

--- Content provided by FirstRanker.com ---

10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---




Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---




COURSE OUTCOMES
COURSE OBJECTIVES

--- Content provided by FirstRanker.com ---


FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

--- Content provided by FirstRanker.com ---


Sl.No. Name of the Experiment
Page
No.
1

--- Content provided by FirstRanker.com ---

Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)
09

--- Content provided by FirstRanker.com ---

3
Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers

--- Content provided by FirstRanker.com ---

13
5(a)
Linear search
15
5(b)

--- Content provided by FirstRanker.com ---

Binary search
17
6(a)
Selection sort
19

--- Content provided by FirstRanker.com ---

6(b)
Insertion sort
21
7
Merge sort

--- Content provided by FirstRanker.com ---

23
8
First n prime numbers
26
9

--- Content provided by FirstRanker.com ---

Multiply matrices
28
10
Programs that take command line arguments (word count)
30

--- Content provided by FirstRanker.com ---

11
Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find GCD of two numbers.

Algorithm:

--- Content provided by FirstRanker.com ---


1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i

--- Content provided by FirstRanker.com ---

4. Print the value of gcd
Program:

def computeGCD(x, y):
if x > y:

--- Content provided by FirstRanker.com ---

smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):

--- Content provided by FirstRanker.com ---

gcd = i

return gcd

num1 = 54

--- Content provided by FirstRanker.com ---

num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

--- Content provided by FirstRanker.com ---


print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))



--- Content provided by FirstRanker.com ---





Ex. No. :01

--- Content provided by FirstRanker.com ---

Date:

GCD OF TWO NUMBERS

FirstRanker Page 8 of 42

--- Content provided by FirstRanker.com ---

Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---

Program:

def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---

print('The square root is' ,newtonSqrt(81))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---

FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program for finding the square root of a given number by Newton ?s

--- Content provided by FirstRanker.com ---

Method is executed successfully and the output is verified.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---

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
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

4. Print the result

Program:
def power(base,exp):
if(exp==1):

--- Content provided by FirstRanker.com ---

return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))

--- Content provided by FirstRanker.com ---

print("Result:",power(base,exp))




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2

--- Content provided by FirstRanker.com ---

Result:49




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---


FirstRanker Page 13 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n

--- Content provided by FirstRanker.com ---

4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---

Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))

--- Content provided by FirstRanker.com ---

l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]

--- Content provided by FirstRanker.com ---

print("The maximum number is %d"%maxno)




--- Content provided by FirstRanker.com ---




Ex. No. :
04

--- Content provided by FirstRanker.com ---

Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9
Enter the numbers 90

--- Content provided by FirstRanker.com ---

The maximum number is 90




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

FirstRanker Page 15 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform Linear Search
Algorithm:
1. Read n elements into the list

--- Content provided by FirstRanker.com ---

2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):

--- Content provided by FirstRanker.com ---

pos=0
found=False
stop=False
while pos if alist[pos]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:

--- Content provided by FirstRanker.com ---

pos=pos+1
return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

e=int(input("enter the elements"))
a.append(e)
x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)

--- Content provided by FirstRanker.com ---

Date:

LINEAR SEARCH

FirstRanker Page 16 of 42

--- Content provided by FirstRanker.com ---


Output:
Enter upper limit 5
Enter the elements 6
Enter the elements 45

--- Content provided by FirstRanker.com ---

Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6
Element found in position 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---



FirstRanker Page 17 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list

--- Content provided by FirstRanker.com ---

3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element

--- Content provided by FirstRanker.com ---

5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---

Program:
def bsearch(alist,item):
first=0
last=len(alist)-1
found=False

--- Content provided by FirstRanker.com ---

while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)

--- Content provided by FirstRanker.com ---

else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---

BINARY SEARCH

FirstRanker Page 18 of 42
else:
first=mid+mid-1

--- Content provided by FirstRanker.com ---

return found

a=[]
n=int(input("enter upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---


FirstRanker Page 19 of 42

Aim:
To write a Python Program to perform selection sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location

--- Content provided by FirstRanker.com ---

5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):

--- Content provided by FirstRanker.com ---

pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]

--- Content provided by FirstRanker.com ---

alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---

SELECTION SORT

FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1

--- Content provided by FirstRanker.com ---

6. alist[position]=currentvalue
7. Print the sorted list

Program:
def insertionSort(alist):

--- Content provided by FirstRanker.com ---

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]

--- Content provided by FirstRanker.com ---

position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 6(b)
Date:

INSERTION SORT

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 22 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 23 of 42

Aim:
To write a Python Program to perform Merge sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0

--- Content provided by FirstRanker.com ---

5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j

--- Content provided by FirstRanker.com ---

Increment k
8. Print the sorted list



--- Content provided by FirstRanker.com ---



Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---

MERGE SORT


GE8161 FirstRanker Page 24 of 42
Program:

--- Content provided by FirstRanker.com ---

def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]

--- Content provided by FirstRanker.com ---

righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):

--- Content provided by FirstRanker.com ---

if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1

--- Content provided by FirstRanker.com ---

k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1

--- Content provided by FirstRanker.com ---

#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 26 of 42

Aim:
To write a Python program to find first n prime numbers.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break
else print the value of num

--- Content provided by FirstRanker.com ---

4. Repeat step 3 for i in range(2,num)

Program:

n = int(input("Enter the upper limit: "))

--- Content provided by FirstRanker.com ---


print("Prime numbers are")

for num in range(0,n + 1):
# prime numbers are greater than 1

--- Content provided by FirstRanker.com ---

if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:

--- Content provided by FirstRanker.com ---

print(num)




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 8
Date:

FIRST N PRIME NUMBERS

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 27 of 42
Output:
Enter the upper limit:100

--- Content provided by FirstRanker.com ---

Prime numbers are
2
3
5
7

--- Content provided by FirstRanker.com ---

11
13
17
19
23

--- Content provided by FirstRanker.com ---

29
31
37
41
43

--- Content provided by FirstRanker.com ---

47
53
59
61
67

--- Content provided by FirstRanker.com ---

71
73
79
83
89

--- Content provided by FirstRanker.com ---

97




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 28 of 42

Aim:
To write a Python program to multiply matrices.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):
i. for j in range(len(Y[0])):
a) for k in range(len(Y))

--- Content provided by FirstRanker.com ---

b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

Program:

--- Content provided by FirstRanker.com ---

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],

--- Content provided by FirstRanker.com ---

[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for i in range(len(X)):

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---


Ex. No. : 9
Date:

MATRIX MULTIPLICATION

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 29 of 42
Output:

--- Content provided by FirstRanker.com ---

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to multiply matrices is executed successfully and the output is

--- Content provided by FirstRanker.com ---

verified.

GE8161 FirstRanker Page 30 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program for command line arguments.
Algorithm:
1. Add arguments to find the words and lines
2. Add the filename as argument
3. Parse the arguments to get the values

--- Content provided by FirstRanker.com ---

4. Format and print the words and lines

Program:
parser=argparse.ArgumentParser()
parser.add_argument('--words','-w',action='store_true')

--- Content provided by FirstRanker.com ---

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))

--- Content provided by FirstRanker.com ---

elif args.lines:
print("lines {}".format(print_lines(args.filename))
else:
print ("words {}".format(print_words(args.filename))
print("lines {}".format(print_lines(args.filename))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. :10
Date:

--- Content provided by FirstRanker.com ---


COMMAND LINE ARGUMENTS(WORD COUNT)

GE8161 FirstRanker Page 31 of 42
Output:

--- Content provided by FirstRanker.com ---



python main.py ?i input.txt
words 23
lines 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program for command line arguments is executed successfully and the output is

--- Content provided by FirstRanker.com ---

verified.

GE8161 FirstRanker Page 32 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find the most frequent words in a text read from a file.
Algorithm:
1. Read the filename
2. Open the file in read mode
3. Read each line from the file to count the lowers and words

--- Content provided by FirstRanker.com ---

4. Read each line from the file to replace the punctuations
5. Split each line into words and count them
6. Print the words and counts

Program:

--- Content provided by FirstRanker.com ---

def main():
filename=raw_input("enter the file").strip()
infile=open(filename,"r")
wordcounts={}
for line in infile:

--- Content provided by FirstRanker.com ---

processLine(line.lower(),wordcounts)
pairs=list(wordcounts.items())
items=[[x,y] for (y,x) in pairs]
items.sort()
for i in range(len(items)-1,len(items)-11,-1):

--- Content provided by FirstRanker.com ---

print(items[i][1]+"\t"+str(items[i][0]))
def processLine(line,wordcounts):
line=replacePunctuations(line)
words=line.split()
for word in words:

--- Content provided by FirstRanker.com ---

if word in wordcounts:
wordcounts[word]+=1
else:
wordcounts[word]=1
def replacePunctuations(line):

--- Content provided by FirstRanker.com ---

for ch in line:
if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":
line=line.replace(ch," ")
return line
main()

--- Content provided by FirstRanker.com ---



Ex. No. : 11
Date:

--- Content provided by FirstRanker.com ---

FINDING MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

GE8161 FirstRanker Page 33 of 42
Output:

--- Content provided by FirstRanker.com ---


Enter a filename:a.txt


Hi 1

--- Content provided by FirstRanker.com ---

How 1
Are 1
You 1


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to find the most frequent words in a text read from a file is

--- Content provided by FirstRanker.com ---

executed successfully and the output is verified.

GE8161 FirstRanker Page 34 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to simulate elliptical orbits in Pygame.
Algorithm:
1. Import the required packages
2. Set up the colours for the elliptical orbits
3. Define the parameters to simulate elliptical orbits

--- Content provided by FirstRanker.com ---

4. Display the created orbits

Program:
import math
import random

--- Content provided by FirstRanker.com ---

import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):
self.x = x
self.y = y

--- Content provided by FirstRanker.com ---

self.vx = 0
self.vy = 0
self.colour = colour
def apply_gravity (self, target):
dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2

--- Content provided by FirstRanker.com ---

#g = G*m/dsqd * normalized (self - target)
if dsqd == 0:
return
self.vx += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5
self.vy += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5

--- Content provided by FirstRanker.com ---

def update (self):
self.x += self.vx
self.y += self.vy
pygame.init()
window = pygame.display.set_mode ((600, 400))

--- Content provided by FirstRanker.com ---

main_surface = pygame.Surface ((600, 400))
colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666,
0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] + [0xFF0000, 0x00FF00, 0x0000FF,
0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888, 0xFFFFFF, 0x808000, 0x008080, 0x800080,
0x800000]

--- Content provided by FirstRanker.com ---

#colours = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,
Ex. No. : 12
Date:

SIMULATE ELLIPTICAL ORBITS IN PYGAME

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 35 of 42
0xFFFFFF, 0x808000, 0x008080, 0x800080, 0x800000]
particles = [Particle (200, 100, colours [i]) for i in range (20)]
earth = Particle (200, 200)

--- Content provided by FirstRanker.com ---

for i, p in enumerate (particles):
p.vx = i / 100
while (True):
# main_surface.fill(0x000000)
pygame.draw.circle (main_surface, 0x00FF00, (earth.x, earth.y), 5, 2)

--- Content provided by FirstRanker.com ---


for p in particles:
p.apply_gravity (earth)
p.update ()
pygame.draw.circle (main_surface, p.colour, (int (p.x), int (p.y)), 5, 2)

--- Content provided by FirstRanker.com ---

window.blit(main_surface, (0, 0))
pygame.display.flip()



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




GE8161 FirstRanker Page 36 of 42
Output:

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to simulate elliptical orbits using Pygame is executed
successfully and the output is verified.

GE8161 FirstRanker Page 37 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to bouncing ball in Pygame.
Algorithm:
1. Import the required packages

--- Content provided by FirstRanker.com ---

2. Define the required variables
3. Define the screen space to display the bouncing balls in that space

Program:
import sys

--- Content provided by FirstRanker.com ---

import pygame
pygame.init()

size = width, height = 320, 240
speed = [2, 2]

--- Content provided by FirstRanker.com ---

black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load('C:\\Users\\admin\\Desktop//ball.jpg')

--- Content provided by FirstRanker.com ---

ballrect = ball.get_rect()

while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()

--- Content provided by FirstRanker.com ---


ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:

--- Content provided by FirstRanker.com ---

speed[1] = -speed[1]

screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 13
Date:

SIMULATE BOUNCING BALL USING PYGAME
FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---


FirstRanker Page 1 of 42

?

--- Content provided by FirstRanker.com ---



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


--- Content provided by FirstRanker.com ---

I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

--- Content provided by FirstRanker.com ---


LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---




is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education

--- Content provided by FirstRanker.com ---

and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at
different levels

--- Content provided by FirstRanker.com ---

? 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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

VISION
MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---



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

--- Content provided by FirstRanker.com ---

3. BREADTH
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

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

5. 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 4 of 42
PROGRAM OUTCOMES


--- Content provided by FirstRanker.com ---


a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career

--- Content provided by FirstRanker.com ---

c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society

--- Content provided by FirstRanker.com ---

g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with

--- Content provided by FirstRanker.com ---

coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS


--- Content provided by FirstRanker.com ---


To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort

--- Content provided by FirstRanker.com ---

8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

13.Simulate bouncing ball in Pygame




--- Content provided by FirstRanker.com ---

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.




--- Content provided by FirstRanker.com ---

COURSE OUTCOMES
COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
CONTENTS

Sl.No. Name of the Experiment
Page

--- Content provided by FirstRanker.com ---

No.
1
Compute the GCD of two numbers.
07
2

--- Content provided by FirstRanker.com ---

Find the square root of a number (Newton ?s method)
09
3
Exponentiation (power of a number)
11

--- Content provided by FirstRanker.com ---

4
Find the maximum of a list of numbers
13
5(a)
Linear search

--- Content provided by FirstRanker.com ---

15
5(b)
Binary search
17
6(a)

--- Content provided by FirstRanker.com ---

Selection sort
19
6(b)
Insertion sort
21

--- Content provided by FirstRanker.com ---

7
Merge sort
23
8
First n prime numbers

--- Content provided by FirstRanker.com ---

26
9
Multiply matrices
28
10

--- Content provided by FirstRanker.com ---

Programs that take command line arguments (word count)
30
11
Find the most frequent words in a text read from a file
32

--- Content provided by FirstRanker.com ---

12
Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---

37




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 7 of 42


Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---


Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y

--- Content provided by FirstRanker.com ---

3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

--- Content provided by FirstRanker.com ---

def computeGCD(x, y):
if x > y:
smaller = y
else:
smaller = x

--- Content provided by FirstRanker.com ---

for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---


num1 = 54
num2 = 24

# take input from the user

--- Content provided by FirstRanker.com ---

# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 8 of 42
Sample output :


--- Content provided by FirstRanker.com ---

The GCD of 54 and 24 is 6




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

--- Content provided by FirstRanker.com ---

FirstRanker Page 9 of 42

Aim:
To write a Python Program to find the square root of a number by Newton ?s Method.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

4. Print the value of approx

Program:

def newtonSqrt(n):

--- Content provided by FirstRanker.com ---

approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

return approx

print('The square root is' ,newtonSqrt(81))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :02
Date:

--- Content provided by FirstRanker.com ---

SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42
Output:

--- Content provided by FirstRanker.com ---

The square root is 9




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

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 ---

FirstRanker Page 11 of 42

Aim:
To write a Python program to find the exponentiation of a number.

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:

--- Content provided by FirstRanker.com ---

def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---


EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:

--- Content provided by FirstRanker.com ---

Enter base: 7
Enter exponential value: 2
Result:49


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

FirstRanker Page 13 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l

--- Content provided by FirstRanker.com ---

2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1

--- Content provided by FirstRanker.com ---

7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:
l=[]
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=l[0]
for i in range(0,len(l)):

--- Content provided by FirstRanker.com ---

if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :
04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6

--- Content provided by FirstRanker.com ---

Enter the numbers 9
Enter the numbers 90
The maximum number is 90


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---


FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list

--- Content provided by FirstRanker.com ---

Program:
def search(alist,item):
pos=0
found=False
stop=False

--- Content provided by FirstRanker.com ---

while pos if alist[pos]==item:
found=True
print("element found in position",pos)
else:
if alist[pos]>item:

--- Content provided by FirstRanker.com ---

stop=True
else:
pos=pos+1
return found
a=[]

--- Content provided by FirstRanker.com ---

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"))

--- Content provided by FirstRanker.com ---

search(a,x)
Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---


FirstRanker Page 16 of 42

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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to perform linear search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified.



FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python Program to perform binary search.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read the search element
2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element

--- Content provided by FirstRanker.com ---

4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list

--- Content provided by FirstRanker.com ---

7. If element is not found, loop terminates

Program:
def bsearch(alist,item):
first=0

--- Content provided by FirstRanker.com ---

last=len(alist)-1
found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)

--- Content provided by FirstRanker.com ---

Date:

BINARY SEARCH

FirstRanker Page 18 of 42

--- Content provided by FirstRanker.com ---

else:
first=mid+mid-1
return found

a=[]

--- Content provided by FirstRanker.com ---

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"))

--- Content provided by FirstRanker.com ---

bsearch(a,x)

Output:
enter upper limit 6
enter the elements 2

--- Content provided by FirstRanker.com ---

enter the elements 3
enter the elements 5
enter the elements 7
enter the elements 14
enter the elements 25

--- Content provided by FirstRanker.com ---

enter element to search 5
element found in position 2


Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to perform binary search is executed successfully and the output
is verified

FirstRanker Page 19 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0

--- Content provided by FirstRanker.com ---

3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]
6. 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]:

--- Content provided by FirstRanker.com ---

pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

--- Content provided by FirstRanker.com ---

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)


--- Content provided by FirstRanker.com ---





Ex. No. : 6(a)

--- Content provided by FirstRanker.com ---

Date:

SELECTION SORT

FirstRanker Page 20 of 42

--- Content provided by FirstRanker.com ---

Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to perform selection sort is successfully executed and the

--- Content provided by FirstRanker.com ---

output is verified.

GE8161 FirstRanker Page 21 of 42

Aim:

--- Content provided by FirstRanker.com ---

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. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)

--- Content provided by FirstRanker.com ---

4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

--- Content provided by FirstRanker.com ---

Program:
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index

--- Content provided by FirstRanker.com ---

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]

--- Content provided by FirstRanker.com ---

insertionSort(alist)
print(alist)



--- Content provided by FirstRanker.com ---




Ex. No. : 6(b)
Date:

--- Content provided by FirstRanker.com ---


INSERTION SORT


GE8161 FirstRanker Page 22 of 42

--- Content provided by FirstRanker.com ---

Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

GE8161 FirstRanker Page 23 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list

--- Content provided by FirstRanker.com ---

3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0
5. 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 ---

6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]
Increment i
Increment k
7. while j < len(righthalf), perform the following

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
Increment j
Increment k
8. Print the sorted list

--- Content provided by FirstRanker.com ---





Ex. No. : 7

--- Content provided by FirstRanker.com ---

Date:

MERGE SORT


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 24 of 42
Program:
def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:

--- Content provided by FirstRanker.com ---

mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)

--- Content provided by FirstRanker.com ---

i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1

--- Content provided by FirstRanker.com ---

else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

j=j+1
k=k+1
#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)


GE8161 FirstRanker Page 25 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to perform merge sort is successfully executed and the output is

--- Content provided by FirstRanker.com ---

verifie

GE8161 FirstRanker Page 26 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find first n prime numbers.

Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following

--- Content provided by FirstRanker.com ---

3. if num%i is 0 then break
else print the value of num
4. 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")

--- Content provided by FirstRanker.com ---

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:

--- Content provided by FirstRanker.com ---

break
else:
print(num)


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. : 8
Date:

--- Content provided by FirstRanker.com ---


FIRST N PRIME NUMBERS


GE8161 FirstRanker Page 27 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limit:100
Prime numbers are
2
3

--- Content provided by FirstRanker.com ---

5
7
11
13
17

--- Content provided by FirstRanker.com ---

19
23
29
31
37

--- Content provided by FirstRanker.com ---

41
43
47
53
59

--- Content provided by FirstRanker.com ---

61
67
71
73
79

--- Content provided by FirstRanker.com ---

83
89
97


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to find the first n prime numbers is executed successfully and

--- Content provided by FirstRanker.com ---

the output is verified.

GE8161 FirstRanker Page 28 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to multiply matrices.
Algorithm:
1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):

--- Content provided by FirstRanker.com ---

i. for j in range(len(Y[0])):
a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

--- Content provided by FirstRanker.com ---

Program:

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]

--- Content provided by FirstRanker.com ---

Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],

--- Content provided by FirstRanker.com ---

[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]

--- Content provided by FirstRanker.com ---

for r in result:
print(r)

Ex. No. : 9
Date:

--- Content provided by FirstRanker.com ---


MATRIX MULTIPLICATION


GE8161 FirstRanker Page 29 of 42

--- Content provided by FirstRanker.com ---

Output:

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to multiply matrices is executed successfully and the output is
verified.

GE8161 FirstRanker Page 30 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program for command line arguments.
Algorithm:
1. Add arguments to find the words and lines

--- Content provided by FirstRanker.com ---

2. Add the filename as argument
3. Parse the arguments to get the values
4. Format and print the words and lines

Program:

--- Content provided by FirstRanker.com ---

parser=argparse.ArgumentParser()
parser.add_argument('--words','-w',action='store_true')
parser.add_argument('--lines','-l',action='store_true')
parser.add_argument('filename')
args=parser.parse_args()

--- Content provided by FirstRanker.com ---

if args.words:
print("words {}".format(print_words(args.filename))
elif args.lines:
print("lines {}".format(print_lines(args.filename))
else:

--- Content provided by FirstRanker.com ---

print ("words {}".format(print_words(args.filename))
print("lines {}".format(print_lines(args.filename))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :10
Date:

COMMAND LINE ARGUMENTS(WORD COUNT)

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 31 of 42
Output:


python main.py ?i input.txt

--- Content provided by FirstRanker.com ---

words 23
lines 1



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program for command line arguments is executed successfully and the output is
verified.

GE8161 FirstRanker Page 32 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find the most frequent words in a text read from a file.
Algorithm:
1. Read the filename

--- Content provided by FirstRanker.com ---

2. Open the file in read mode
3. Read each line from the file to count the lowers and words
4. Read each line from the file to replace the punctuations
5. Split each line into words and count them
6. Print the words and counts

--- Content provided by FirstRanker.com ---


Program:
def main():
filename=raw_input("enter the file").strip()
infile=open(filename,"r")

--- Content provided by FirstRanker.com ---

wordcounts={}
for line in infile:
processLine(line.lower(),wordcounts)
pairs=list(wordcounts.items())
items=[[x,y] for (y,x) in pairs]

--- Content provided by FirstRanker.com ---

items.sort()
for i in range(len(items)-1,len(items)-11,-1):
print(items[i][1]+"\t"+str(items[i][0]))
def processLine(line,wordcounts):
line=replacePunctuations(line)

--- Content provided by FirstRanker.com ---

words=line.split()
for word in words:
if word in wordcounts:
wordcounts[word]+=1
else:

--- Content provided by FirstRanker.com ---

wordcounts[word]=1
def replacePunctuations(line):
for ch in line:
if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":
line=line.replace(ch," ")

--- Content provided by FirstRanker.com ---

return line
main()


Ex. No. : 11

--- Content provided by FirstRanker.com ---

Date:

FINDING MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

GE8161 FirstRanker Page 33 of 42

--- Content provided by FirstRanker.com ---

Output:


Enter a filename:a.txt

--- Content provided by FirstRanker.com ---


Hi 1
How 1
Are 1
You 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to find the most frequent words in a text read from a file is
executed successfully and the output is verified.

GE8161 FirstRanker Page 34 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to simulate elliptical orbits in Pygame.
Algorithm:
1. Import the required packages

--- Content provided by FirstRanker.com ---

2. Set up the colours for the elliptical orbits
3. Define the parameters to simulate elliptical orbits
4. Display the created orbits

Program:

--- Content provided by FirstRanker.com ---

import math
import random
import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):

--- Content provided by FirstRanker.com ---

self.x = x
self.y = y
self.vx = 0
self.vy = 0
self.colour = colour

--- Content provided by FirstRanker.com ---

def apply_gravity (self, target):
dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2
#g = G*m/dsqd * normalized (self - target)
if dsqd == 0:
return

--- Content provided by FirstRanker.com ---

self.vx += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5
self.vy += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5
def update (self):
self.x += self.vx
self.y += self.vy

--- Content provided by FirstRanker.com ---

pygame.init()
window = pygame.display.set_mode ((600, 400))
main_surface = pygame.Surface ((600, 400))
colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666,
0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] + [0xFF0000, 0x00FF00, 0x0000FF,

--- Content provided by FirstRanker.com ---

0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888, 0xFFFFFF, 0x808000, 0x008080, 0x800080,
0x800000]
#colours = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,
Ex. No. : 12
Date:

--- Content provided by FirstRanker.com ---


SIMULATE ELLIPTICAL ORBITS IN PYGAME

GE8161 FirstRanker Page 35 of 42
0xFFFFFF, 0x808000, 0x008080, 0x800080, 0x800000]

--- Content provided by FirstRanker.com ---

particles = [Particle (200, 100, colours [i]) for i in range (20)]
earth = Particle (200, 200)
for i, p in enumerate (particles):
p.vx = i / 100
while (True):

--- Content provided by FirstRanker.com ---

# main_surface.fill(0x000000)
pygame.draw.circle (main_surface, 0x00FF00, (earth.x, earth.y), 5, 2)

for p in particles:
p.apply_gravity (earth)

--- Content provided by FirstRanker.com ---

p.update ()
pygame.draw.circle (main_surface, p.colour, (int (p.x), int (p.y)), 5, 2)
window.blit(main_surface, (0, 0))
pygame.display.flip()

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 36 of 42
Output:



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to simulate elliptical orbits using Pygame is executed
successfully and the output is verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 37 of 42


Aim:
To write a Python program to bouncing ball in Pygame.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Import the required packages
2. Define the required variables
3. Define the screen space to display the bouncing balls in that space

--- Content provided by FirstRanker.com ---

Program:
import sys
import pygame
pygame.init()

--- Content provided by FirstRanker.com ---

size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

--- Content provided by FirstRanker.com ---


ball = pygame.image.load('C:\\Users\\admin\\Desktop//ball.jpg')
ballrect = ball.get_rect()

while 1:

--- Content provided by FirstRanker.com ---

for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()

ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:

--- Content provided by FirstRanker.com ---

speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]

screen.fill(black)

--- Content provided by FirstRanker.com ---

screen.blit(ball, ballrect)
pygame.display.flip()



--- Content provided by FirstRanker.com ---



Ex. No. : 13
Date:

--- Content provided by FirstRanker.com ---

SIMULATE BOUNCING BALL USING PYGAME

GE8161 FirstRanker Page 38 of 42


--- Content provided by FirstRanker.com ---

Output:




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to simulate bouncing ball using Pygame is executed
successfully and the output is verified.

FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---


FirstRanker Page 1 of 42

?

--- Content provided by FirstRanker.com ---



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


--- Content provided by FirstRanker.com ---

I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________

--- Content provided by FirstRanker.com ---


LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---




is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education

--- Content provided by FirstRanker.com ---

and training.

? To provide competent technical manpower capable of meeting requirements of the industry
? To contribute to the promotion of Academic excellence in pursuit of Technical Education at
different levels

--- Content provided by FirstRanker.com ---

? 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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

VISION
MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---



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

--- Content provided by FirstRanker.com ---

3. BREADTH
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

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

5. 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


FirstRanker Page 4 of 42
PROGRAM OUTCOMES


--- Content provided by FirstRanker.com ---


a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career

--- Content provided by FirstRanker.com ---

c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society

--- Content provided by FirstRanker.com ---

g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with

--- Content provided by FirstRanker.com ---

coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS


--- Content provided by FirstRanker.com ---


To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort

--- Content provided by FirstRanker.com ---

8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame

--- Content provided by FirstRanker.com ---

13.Simulate bouncing ball in Pygame




--- Content provided by FirstRanker.com ---

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.




--- Content provided by FirstRanker.com ---

COURSE OUTCOMES
COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY
CONTENTS

Sl.No. Name of the Experiment
Page

--- Content provided by FirstRanker.com ---

No.
1
Compute the GCD of two numbers.
07
2

--- Content provided by FirstRanker.com ---

Find the square root of a number (Newton ?s method)
09
3
Exponentiation (power of a number)
11

--- Content provided by FirstRanker.com ---

4
Find the maximum of a list of numbers
13
5(a)
Linear search

--- Content provided by FirstRanker.com ---

15
5(b)
Binary search
17
6(a)

--- Content provided by FirstRanker.com ---

Selection sort
19
6(b)
Insertion sort
21

--- Content provided by FirstRanker.com ---

7
Merge sort
23
8
First n prime numbers

--- Content provided by FirstRanker.com ---

26
9
Multiply matrices
28
10

--- Content provided by FirstRanker.com ---

Programs that take command line arguments (word count)
30
11
Find the most frequent words in a text read from a file
32

--- Content provided by FirstRanker.com ---

12
Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---

37




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 7 of 42


Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---


Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y

--- Content provided by FirstRanker.com ---

3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

--- Content provided by FirstRanker.com ---

def computeGCD(x, y):
if x > y:
smaller = y
else:
smaller = x

--- Content provided by FirstRanker.com ---

for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---


num1 = 54
num2 = 24

# take input from the user

--- Content provided by FirstRanker.com ---

# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 8 of 42
Sample output :


--- Content provided by FirstRanker.com ---

The GCD of 54 and 24 is 6




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

--- Content provided by FirstRanker.com ---

FirstRanker Page 9 of 42

Aim:
To write a Python Program to find the square root of a number by Newton ?s Method.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Define a function named newtonSqrt().
2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)

--- Content provided by FirstRanker.com ---

4. Print the value of approx

Program:

def newtonSqrt(n):

--- Content provided by FirstRanker.com ---

approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)

--- Content provided by FirstRanker.com ---

return approx

print('The square root is' ,newtonSqrt(81))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :02
Date:

--- Content provided by FirstRanker.com ---

SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42
Output:

--- Content provided by FirstRanker.com ---

The square root is 9




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

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 ---

FirstRanker Page 11 of 42

Aim:
To write a Python program to find the exponentiation of a number.

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:

--- Content provided by FirstRanker.com ---

def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))

--- Content provided by FirstRanker.com ---

base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---


EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:

--- Content provided by FirstRanker.com ---

Enter base: 7
Enter exponential value: 2
Result:49


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

FirstRanker Page 13 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l

--- Content provided by FirstRanker.com ---

2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1

--- Content provided by FirstRanker.com ---

7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:
l=[]
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=l[0]
for i in range(0,len(l)):

--- Content provided by FirstRanker.com ---

if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :
04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---


FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6

--- Content provided by FirstRanker.com ---

Enter the numbers 9
Enter the numbers 90
The maximum number is 90


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---


FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list

--- Content provided by FirstRanker.com ---

Program:
def search(alist,item):
pos=0
found=False
stop=False

--- Content provided by FirstRanker.com ---

while pos if alist[pos]==item:
found=True
print("element found in position",pos)
else:
if alist[pos]>item:

--- Content provided by FirstRanker.com ---

stop=True
else:
pos=pos+1
return found
a=[]

--- Content provided by FirstRanker.com ---

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"))

--- Content provided by FirstRanker.com ---

search(a,x)
Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---


FirstRanker Page 16 of 42

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



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to perform linear search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified.



FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python Program to perform binary search.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read the search element
2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element

--- Content provided by FirstRanker.com ---

4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list

--- Content provided by FirstRanker.com ---

7. If element is not found, loop terminates

Program:
def bsearch(alist,item):
first=0

--- Content provided by FirstRanker.com ---

last=len(alist)-1
found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)

--- Content provided by FirstRanker.com ---

Date:

BINARY SEARCH

FirstRanker Page 18 of 42

--- Content provided by FirstRanker.com ---

else:
first=mid+mid-1
return found

a=[]

--- Content provided by FirstRanker.com ---

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"))

--- Content provided by FirstRanker.com ---

bsearch(a,x)

Output:
enter upper limit 6
enter the elements 2

--- Content provided by FirstRanker.com ---

enter the elements 3
enter the elements 5
enter the elements 7
enter the elements 14
enter the elements 25

--- Content provided by FirstRanker.com ---

enter element to search 5
element found in position 2


Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to perform binary search is executed successfully and the output
is verified

FirstRanker Page 19 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0

--- Content provided by FirstRanker.com ---

3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]
6. 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]:

--- Content provided by FirstRanker.com ---

pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

--- Content provided by FirstRanker.com ---

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)


--- Content provided by FirstRanker.com ---





Ex. No. : 6(a)

--- Content provided by FirstRanker.com ---

Date:

SELECTION SORT

FirstRanker Page 20 of 42

--- Content provided by FirstRanker.com ---

Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to perform selection sort is successfully executed and the

--- Content provided by FirstRanker.com ---

output is verified.

GE8161 FirstRanker Page 21 of 42

Aim:

--- Content provided by FirstRanker.com ---

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. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)

--- Content provided by FirstRanker.com ---

4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

--- Content provided by FirstRanker.com ---

Program:
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index

--- Content provided by FirstRanker.com ---

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]

--- Content provided by FirstRanker.com ---

insertionSort(alist)
print(alist)



--- Content provided by FirstRanker.com ---




Ex. No. : 6(b)
Date:

--- Content provided by FirstRanker.com ---


INSERTION SORT


GE8161 FirstRanker Page 22 of 42

--- Content provided by FirstRanker.com ---

Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

GE8161 FirstRanker Page 23 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list

--- Content provided by FirstRanker.com ---

3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0
5. 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 ---

6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]
Increment i
Increment k
7. while j < len(righthalf), perform the following

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
Increment j
Increment k
8. Print the sorted list

--- Content provided by FirstRanker.com ---





Ex. No. : 7

--- Content provided by FirstRanker.com ---

Date:

MERGE SORT


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 24 of 42
Program:
def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:

--- Content provided by FirstRanker.com ---

mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)

--- Content provided by FirstRanker.com ---

i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1

--- Content provided by FirstRanker.com ---

else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

j=j+1
k=k+1
#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)


GE8161 FirstRanker Page 25 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to perform merge sort is successfully executed and the output is

--- Content provided by FirstRanker.com ---

verifie

GE8161 FirstRanker Page 26 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find first n prime numbers.

Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following

--- Content provided by FirstRanker.com ---

3. if num%i is 0 then break
else print the value of num
4. 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")

--- Content provided by FirstRanker.com ---

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:

--- Content provided by FirstRanker.com ---

break
else:
print(num)


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Ex. No. : 8
Date:

--- Content provided by FirstRanker.com ---


FIRST N PRIME NUMBERS


GE8161 FirstRanker Page 27 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter the upper limit:100
Prime numbers are
2
3

--- Content provided by FirstRanker.com ---

5
7
11
13
17

--- Content provided by FirstRanker.com ---

19
23
29
31
37

--- Content provided by FirstRanker.com ---

41
43
47
53
59

--- Content provided by FirstRanker.com ---

61
67
71
73
79

--- Content provided by FirstRanker.com ---

83
89
97


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to find the first n prime numbers is executed successfully and

--- Content provided by FirstRanker.com ---

the output is verified.

GE8161 FirstRanker Page 28 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to multiply matrices.
Algorithm:
1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):

--- Content provided by FirstRanker.com ---

i. for j in range(len(Y[0])):
a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

--- Content provided by FirstRanker.com ---

Program:

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]

--- Content provided by FirstRanker.com ---

Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],

--- Content provided by FirstRanker.com ---

[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]

--- Content provided by FirstRanker.com ---

for r in result:
print(r)

Ex. No. : 9
Date:

--- Content provided by FirstRanker.com ---


MATRIX MULTIPLICATION


GE8161 FirstRanker Page 29 of 42

--- Content provided by FirstRanker.com ---

Output:

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to multiply matrices is executed successfully and the output is
verified.

GE8161 FirstRanker Page 30 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program for command line arguments.
Algorithm:
1. Add arguments to find the words and lines

--- Content provided by FirstRanker.com ---

2. Add the filename as argument
3. Parse the arguments to get the values
4. Format and print the words and lines

Program:

--- Content provided by FirstRanker.com ---

parser=argparse.ArgumentParser()
parser.add_argument('--words','-w',action='store_true')
parser.add_argument('--lines','-l',action='store_true')
parser.add_argument('filename')
args=parser.parse_args()

--- Content provided by FirstRanker.com ---

if args.words:
print("words {}".format(print_words(args.filename))
elif args.lines:
print("lines {}".format(print_lines(args.filename))
else:

--- Content provided by FirstRanker.com ---

print ("words {}".format(print_words(args.filename))
print("lines {}".format(print_lines(args.filename))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :10
Date:

COMMAND LINE ARGUMENTS(WORD COUNT)

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 31 of 42
Output:


python main.py ?i input.txt

--- Content provided by FirstRanker.com ---

words 23
lines 1



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program for command line arguments is executed successfully and the output is
verified.

GE8161 FirstRanker Page 32 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find the most frequent words in a text read from a file.
Algorithm:
1. Read the filename

--- Content provided by FirstRanker.com ---

2. Open the file in read mode
3. Read each line from the file to count the lowers and words
4. Read each line from the file to replace the punctuations
5. Split each line into words and count them
6. Print the words and counts

--- Content provided by FirstRanker.com ---


Program:
def main():
filename=raw_input("enter the file").strip()
infile=open(filename,"r")

--- Content provided by FirstRanker.com ---

wordcounts={}
for line in infile:
processLine(line.lower(),wordcounts)
pairs=list(wordcounts.items())
items=[[x,y] for (y,x) in pairs]

--- Content provided by FirstRanker.com ---

items.sort()
for i in range(len(items)-1,len(items)-11,-1):
print(items[i][1]+"\t"+str(items[i][0]))
def processLine(line,wordcounts):
line=replacePunctuations(line)

--- Content provided by FirstRanker.com ---

words=line.split()
for word in words:
if word in wordcounts:
wordcounts[word]+=1
else:

--- Content provided by FirstRanker.com ---

wordcounts[word]=1
def replacePunctuations(line):
for ch in line:
if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":
line=line.replace(ch," ")

--- Content provided by FirstRanker.com ---

return line
main()


Ex. No. : 11

--- Content provided by FirstRanker.com ---

Date:

FINDING MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

GE8161 FirstRanker Page 33 of 42

--- Content provided by FirstRanker.com ---

Output:


Enter a filename:a.txt

--- Content provided by FirstRanker.com ---


Hi 1
How 1
Are 1
You 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to find the most frequent words in a text read from a file is
executed successfully and the output is verified.

GE8161 FirstRanker Page 34 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to simulate elliptical orbits in Pygame.
Algorithm:
1. Import the required packages

--- Content provided by FirstRanker.com ---

2. Set up the colours for the elliptical orbits
3. Define the parameters to simulate elliptical orbits
4. Display the created orbits

Program:

--- Content provided by FirstRanker.com ---

import math
import random
import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):

--- Content provided by FirstRanker.com ---

self.x = x
self.y = y
self.vx = 0
self.vy = 0
self.colour = colour

--- Content provided by FirstRanker.com ---

def apply_gravity (self, target):
dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2
#g = G*m/dsqd * normalized (self - target)
if dsqd == 0:
return

--- Content provided by FirstRanker.com ---

self.vx += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5
self.vy += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5
def update (self):
self.x += self.vx
self.y += self.vy

--- Content provided by FirstRanker.com ---

pygame.init()
window = pygame.display.set_mode ((600, 400))
main_surface = pygame.Surface ((600, 400))
colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666,
0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] + [0xFF0000, 0x00FF00, 0x0000FF,

--- Content provided by FirstRanker.com ---

0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888, 0xFFFFFF, 0x808000, 0x008080, 0x800080,
0x800000]
#colours = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,
Ex. No. : 12
Date:

--- Content provided by FirstRanker.com ---


SIMULATE ELLIPTICAL ORBITS IN PYGAME

GE8161 FirstRanker Page 35 of 42
0xFFFFFF, 0x808000, 0x008080, 0x800080, 0x800000]

--- Content provided by FirstRanker.com ---

particles = [Particle (200, 100, colours [i]) for i in range (20)]
earth = Particle (200, 200)
for i, p in enumerate (particles):
p.vx = i / 100
while (True):

--- Content provided by FirstRanker.com ---

# main_surface.fill(0x000000)
pygame.draw.circle (main_surface, 0x00FF00, (earth.x, earth.y), 5, 2)

for p in particles:
p.apply_gravity (earth)

--- Content provided by FirstRanker.com ---

p.update ()
pygame.draw.circle (main_surface, p.colour, (int (p.x), int (p.y)), 5, 2)
window.blit(main_surface, (0, 0))
pygame.display.flip()

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 36 of 42
Output:



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python Program to simulate elliptical orbits using Pygame is executed
successfully and the output is verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 37 of 42


Aim:
To write a Python program to bouncing ball in Pygame.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Import the required packages
2. Define the required variables
3. Define the screen space to display the bouncing balls in that space

--- Content provided by FirstRanker.com ---

Program:
import sys
import pygame
pygame.init()

--- Content provided by FirstRanker.com ---

size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

--- Content provided by FirstRanker.com ---


ball = pygame.image.load('C:\\Users\\admin\\Desktop//ball.jpg')
ballrect = ball.get_rect()

while 1:

--- Content provided by FirstRanker.com ---

for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()

ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:

--- Content provided by FirstRanker.com ---

speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]

screen.fill(black)

--- Content provided by FirstRanker.com ---

screen.blit(ball, ballrect)
pygame.display.flip()



--- Content provided by FirstRanker.com ---



Ex. No. : 13
Date:

--- Content provided by FirstRanker.com ---

SIMULATE BOUNCING BALL USING PYGAME

GE8161 FirstRanker Page 38 of 42


--- Content provided by FirstRanker.com ---

Output:




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to simulate bouncing ball using Pygame is executed
successfully and the output is verified.


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 39 of 42
VIVAVOCE QUESTIONS:
1. What it the syntax of print function?
2. What is the usage of input function?
3. Define a variable.

--- Content provided by FirstRanker.com ---

4. What is type conversion?
5. Mention the data types in Python
6. What are the attributes of the complex datatype?
7. Mention a few escape sequences.
8. Define an expression

--- Content provided by FirstRanker.com ---

9. What is the usage of ** operator in Python?
10. Give the syntax of if else statement.
11. Give the syntax of for statement.
12. How is range function used in for?
13. Give the syntax of while statement.

--- Content provided by FirstRanker.com ---

14. What are multi way if statements?
15. How is random numbers generated?
16. Define a function.
17. Give the syntax of function.
18. What are the types of arguments in function.?

--- Content provided by FirstRanker.com ---

19. What is a recursive function?
20. What are anonymous functions?
21. What are default arguments?
22. What are variable length arguments?
23. What are keyword arguments?

--- Content provided by FirstRanker.com ---

24. Mention the use of map().
25. Mention the use of filter().
26. Mention the use of reduce().
27. Define a string.
28. How is string slicing done?

--- Content provided by FirstRanker.com ---

29. What is the usage of repetition operator?
30. How is string concatenation done using + operator>
31. Mention some string methods
32. How is length of a string found?
33. How is a string converted to its upper case?

--- Content provided by FirstRanker.com ---

34. `Differentiate isalpha() and isdigit().
35. What is the use of split()?
36. Define a file.
37. Give the syntax for opening a file.
38. Give the syntax for closing a file.

--- Content provided by FirstRanker.com ---

39. How is reading of file done?
40. How is writing of file done?
41. What is a list?
42. Lists are mutable-Justify.
43. How is a list created?

--- Content provided by FirstRanker.com ---

44. How can a list be sorted?
45. How are elements appended to the list?
46. How is insert() used in list?
FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---

FirstRanker Page 1 of 42

?


--- Content provided by FirstRanker.com ---


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---


GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________


--- Content provided by FirstRanker.com ---

LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---



is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---


? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---

tag on heart and soul




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





VISION

--- Content provided by FirstRanker.com ---

MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---

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. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 4 of 42
PROGRAM OUTCOMES



--- Content provided by FirstRanker.com ---

a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications

--- Content provided by FirstRanker.com ---

d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as

--- Content provided by FirstRanker.com ---

economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination

--- Content provided by FirstRanker.com ---

j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS



--- Content provided by FirstRanker.com ---

To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---


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)

--- Content provided by FirstRanker.com ---

4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers

--- Content provided by FirstRanker.com ---

9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---





Upon completion of the course, students will be able to:

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---





COURSE OUTCOMES

--- Content provided by FirstRanker.com ---

COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

CONTENTS

Sl.No. Name of the Experiment
Page
No.

--- Content provided by FirstRanker.com ---

1
Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)

--- Content provided by FirstRanker.com ---

09
3
Exponentiation (power of a number)
11
4

--- Content provided by FirstRanker.com ---

Find the maximum of a list of numbers
13
5(a)
Linear search
15

--- Content provided by FirstRanker.com ---

5(b)
Binary search
17
6(a)
Selection sort

--- Content provided by FirstRanker.com ---

19
6(b)
Insertion sort
21
7

--- Content provided by FirstRanker.com ---

Merge sort
23
8
First n prime numbers
26

--- Content provided by FirstRanker.com ---

9
Multiply matrices
28
10
Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

30
11
Find the most frequent words in a text read from a file
32
12

--- Content provided by FirstRanker.com ---

Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---

Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1

--- Content provided by FirstRanker.com ---

Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

def computeGCD(x, y):

--- Content provided by FirstRanker.com ---

if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):

--- Content provided by FirstRanker.com ---

if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---

num1 = 54
num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))

--- Content provided by FirstRanker.com ---

# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 8 of 42
Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---


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().

--- Content provided by FirstRanker.com ---

2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---


Program:

def newtonSqrt(n):
approx = 0.5 * n

--- Content provided by FirstRanker.com ---

better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---


print('The square root is' ,newtonSqrt(81))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

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.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---


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
i. if exp is equal to 1, then return base

--- Content provided by FirstRanker.com ---

ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:
def power(base,exp):

--- Content provided by FirstRanker.com ---

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))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:
Enter base: 7

--- Content provided by FirstRanker.com ---

Enter exponential value: 2
Result:49



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to find the exponentiation of a number is executed successfully

--- Content provided by FirstRanker.com ---

and the output is verified.

FirstRanker Page 13 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n

--- Content provided by FirstRanker.com ---

3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---


Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:

--- Content provided by FirstRanker.com ---

maxno=l[i]
print("The maximum number is %d"%maxno)



--- Content provided by FirstRanker.com ---





Ex. No. :

--- Content provided by FirstRanker.com ---

04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9

--- Content provided by FirstRanker.com ---

Enter the numbers 90
The maximum number is 90



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:

--- Content provided by FirstRanker.com ---

def search(alist,item):
pos=0
found=False
stop=False
while pos if alist[pos]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",pos)
else:
if alist[pos]>item:
stop=True

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---

FirstRanker Page 16 of 42

Output:
Enter upper limit 5
Enter the elements 6

--- Content provided by FirstRanker.com ---

Enter the elements 45
Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6

--- Content provided by FirstRanker.com ---

Element found in position 1




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---




FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element

--- Content provided by FirstRanker.com ---

2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the

--- Content provided by FirstRanker.com ---

left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---


Program:
def bsearch(alist,item):
first=0
last=len(alist)-1

--- Content provided by FirstRanker.com ---

found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---


BINARY SEARCH

FirstRanker Page 18 of 42
else:

--- Content provided by FirstRanker.com ---

first=mid+mid-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"))
bsearch(a,x)

--- Content provided by FirstRanker.com ---


Output:
enter upper limit 6
enter the elements 2
enter the elements 3

--- Content provided by FirstRanker.com ---

enter the elements 5
enter the elements 7
enter the elements 14
enter the elements 25
enter element to search 5

--- Content provided by FirstRanker.com ---

element found in position 2


Result:
Thus the Python Program to perform binary search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified

FirstRanker Page 19 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,

--- Content provided by FirstRanker.com ---

4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]

--- Content provided by FirstRanker.com ---

selectionSort(alist)
print(alist)



--- Content provided by FirstRanker.com ---




Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---


SELECTION SORT

FirstRanker Page 20 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]

--- Content provided by FirstRanker.com ---

5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

Program:

--- Content provided by FirstRanker.com ---

def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:

--- Content provided by FirstRanker.com ---

alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(b)
Date:

--- Content provided by FirstRanker.com ---

INSERTION SORT


GE8161 FirstRanker Page 22 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to perform insertion sort is successfully executed and the output

--- Content provided by FirstRanker.com ---

is verified.

GE8161 FirstRanker Page 23 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]

--- Content provided by FirstRanker.com ---

4. Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i

--- Content provided by FirstRanker.com ---

else
alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

Increment j
Increment k
8. Print the sorted list


--- Content provided by FirstRanker.com ---




Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---


MERGE SORT


GE8161 FirstRanker Page 24 of 42

--- Content provided by FirstRanker.com ---

Program:
def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2

--- Content provided by FirstRanker.com ---

lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0

--- Content provided by FirstRanker.com ---

while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1

--- Content provided by FirstRanker.com ---

k=k+1
#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 26 of 42

Aim:
To write a Python program to find first n prime numbers.

--- Content provided by FirstRanker.com ---


Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break

--- Content provided by FirstRanker.com ---

else print the value of num
4. 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):

--- Content provided by FirstRanker.com ---

# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break

--- Content provided by FirstRanker.com ---

else:
print(num)



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 8
Date:

--- Content provided by FirstRanker.com ---

FIRST N PRIME NUMBERS


GE8161 FirstRanker Page 27 of 42
Output:

--- Content provided by FirstRanker.com ---

Enter the upper limit:100
Prime numbers are
2
3
5

--- Content provided by FirstRanker.com ---

7
11
13
17
19

--- Content provided by FirstRanker.com ---

23
29
31
37
41

--- Content provided by FirstRanker.com ---

43
47
53
59
61

--- Content provided by FirstRanker.com ---

67
71
73
79
83

--- Content provided by FirstRanker.com ---

89
97



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 28 of 42

Aim:
To write a Python program to multiply matrices.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):
i. for j in range(len(Y[0])):

--- Content provided by FirstRanker.com ---

a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

Program:

--- Content provided by FirstRanker.com ---


X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],

--- Content provided by FirstRanker.com ---

[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]

--- Content provided by FirstRanker.com ---

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:

--- Content provided by FirstRanker.com ---

print(r)

Ex. No. : 9
Date:

--- Content provided by FirstRanker.com ---

MATRIX MULTIPLICATION


GE8161 FirstRanker Page 29 of 42
Output:

--- Content provided by FirstRanker.com ---


[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to multiply matrices is executed successfully and the output is
verified.

GE8161 FirstRanker Page 30 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program for command line arguments.
Algorithm:
1. Add arguments to find the words and lines
2. Add the filename as argument

--- Content provided by FirstRanker.com ---

3. Parse the arguments to get the values
4. 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:

--- Content provided by FirstRanker.com ---

print("words {}".format(print_words(args.filename))
elif args.lines:
print("lines {}".format(print_lines(args.filename))
else:
print ("words {}".format(print_words(args.filename))

--- Content provided by FirstRanker.com ---

print("lines {}".format(print_lines(args.filename))




--- Content provided by FirstRanker.com ---





Ex. No. :10

--- Content provided by FirstRanker.com ---

Date:

COMMAND LINE ARGUMENTS(WORD COUNT)

GE8161 FirstRanker Page 31 of 42

--- Content provided by FirstRanker.com ---

Output:


python main.py ?i input.txt
words 23

--- Content provided by FirstRanker.com ---

lines 1




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program for command line arguments is executed successfully and the output is
verified.

GE8161 FirstRanker Page 32 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find the most frequent words in a text read from a file.
Algorithm:
1. Read the filename
2. Open the file in read mode

--- Content provided by FirstRanker.com ---

3. Read each line from the file to count the lowers and words
4. Read each line from the file to replace the punctuations
5. Split each line into words and count them
6. Print the words and counts

--- Content provided by FirstRanker.com ---

Program:
def main():
filename=raw_input("enter the file").strip()
infile=open(filename,"r")
wordcounts={}

--- Content provided by FirstRanker.com ---

for line in infile:
processLine(line.lower(),wordcounts)
pairs=list(wordcounts.items())
items=[[x,y] for (y,x) in pairs]
items.sort()

--- Content provided by FirstRanker.com ---

for i in range(len(items)-1,len(items)-11,-1):
print(items[i][1]+"\t"+str(items[i][0]))
def processLine(line,wordcounts):
line=replacePunctuations(line)
words=line.split()

--- Content provided by FirstRanker.com ---

for word in words:
if word in wordcounts:
wordcounts[word]+=1
else:
wordcounts[word]=1

--- Content provided by FirstRanker.com ---

def replacePunctuations(line):
for ch in line:
if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":
line=line.replace(ch," ")
return line

--- Content provided by FirstRanker.com ---

main()


Ex. No. : 11
Date:

--- Content provided by FirstRanker.com ---


FINDING MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

GE8161 FirstRanker Page 33 of 42
Output:

--- Content provided by FirstRanker.com ---



Enter a filename:a.txt


--- Content provided by FirstRanker.com ---

Hi 1
How 1
Are 1
You 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to find the most frequent words in a text read from a file is
executed successfully and the output is verified.

GE8161 FirstRanker Page 34 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to simulate elliptical orbits in Pygame.
Algorithm:
1. Import the required packages
2. Set up the colours for the elliptical orbits

--- Content provided by FirstRanker.com ---

3. Define the parameters to simulate elliptical orbits
4. Display the created orbits

Program:
import math

--- Content provided by FirstRanker.com ---

import random
import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):
self.x = x

--- Content provided by FirstRanker.com ---

self.y = y
self.vx = 0
self.vy = 0
self.colour = colour
def apply_gravity (self, target):

--- Content provided by FirstRanker.com ---

dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2
#g = G*m/dsqd * normalized (self - target)
if dsqd == 0:
return
self.vx += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5

--- Content provided by FirstRanker.com ---

self.vy += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5
def update (self):
self.x += self.vx
self.y += self.vy
pygame.init()

--- Content provided by FirstRanker.com ---

window = pygame.display.set_mode ((600, 400))
main_surface = pygame.Surface ((600, 400))
colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666,
0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] + [0xFF0000, 0x00FF00, 0x0000FF,
0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888, 0xFFFFFF, 0x808000, 0x008080, 0x800080,

--- Content provided by FirstRanker.com ---

0x800000]
#colours = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,
Ex. No. : 12
Date:

--- Content provided by FirstRanker.com ---

SIMULATE ELLIPTICAL ORBITS IN PYGAME

GE8161 FirstRanker Page 35 of 42
0xFFFFFF, 0x808000, 0x008080, 0x800080, 0x800000]
particles = [Particle (200, 100, colours [i]) for i in range (20)]

--- Content provided by FirstRanker.com ---

earth = Particle (200, 200)
for i, p in enumerate (particles):
p.vx = i / 100
while (True):
# main_surface.fill(0x000000)

--- Content provided by FirstRanker.com ---

pygame.draw.circle (main_surface, 0x00FF00, (earth.x, earth.y), 5, 2)

for p in particles:
p.apply_gravity (earth)
p.update ()

--- Content provided by FirstRanker.com ---

pygame.draw.circle (main_surface, p.colour, (int (p.x), int (p.y)), 5, 2)
window.blit(main_surface, (0, 0))
pygame.display.flip()


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





GE8161 FirstRanker Page 36 of 42

--- Content provided by FirstRanker.com ---

Output:




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to simulate elliptical orbits using Pygame is executed
successfully and the output is verified.

GE8161 FirstRanker Page 37 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python program to bouncing ball in Pygame.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Import the required packages
2. Define the required variables
3. Define the screen space to display the bouncing balls in that space

Program:

--- Content provided by FirstRanker.com ---

import sys
import pygame
pygame.init()

size = width, height = 320, 240

--- Content provided by FirstRanker.com ---

speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

--- Content provided by FirstRanker.com ---

ball = pygame.image.load('C:\\Users\\admin\\Desktop//ball.jpg')
ballrect = ball.get_rect()

while 1:
for event in pygame.event.get():

--- Content provided by FirstRanker.com ---

if event.type == pygame.QUIT: sys.exit()

ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]

--- Content provided by FirstRanker.com ---

if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]

screen.fill(black)
screen.blit(ball, ballrect)

--- Content provided by FirstRanker.com ---

pygame.display.flip()




--- Content provided by FirstRanker.com ---


Ex. No. : 13
Date:

SIMULATE BOUNCING BALL USING PYGAME

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 38 of 42


Output:

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to simulate bouncing ball using Pygame is executed
successfully and the output is verified.


GE8161 FirstRanker Page 39 of 42

--- Content provided by FirstRanker.com ---

VIVAVOCE QUESTIONS:
1. What it the syntax of print function?
2. What is the usage of input function?
3. Define a variable.
4. What is type conversion?

--- Content provided by FirstRanker.com ---

5. Mention the data types in Python
6. What are the attributes of the complex datatype?
7. Mention a few escape sequences.
8. Define an expression
9. What is the usage of ** operator in Python?

--- Content provided by FirstRanker.com ---

10. Give the syntax of if else statement.
11. Give the syntax of for statement.
12. How is range function used in for?
13. Give the syntax of while statement.
14. What are multi way if statements?

--- Content provided by FirstRanker.com ---

15. How is random numbers generated?
16. Define a function.
17. Give the syntax of function.
18. What are the types of arguments in function.?
19. What is a recursive function?

--- Content provided by FirstRanker.com ---

20. What are anonymous functions?
21. What are default arguments?
22. What are variable length arguments?
23. What are keyword arguments?
24. Mention the use of map().

--- Content provided by FirstRanker.com ---

25. Mention the use of filter().
26. Mention the use of reduce().
27. Define a string.
28. How is string slicing done?
29. What is the usage of repetition operator?

--- Content provided by FirstRanker.com ---

30. How is string concatenation done using + operator>
31. Mention some string methods
32. How is length of a string found?
33. How is a string converted to its upper case?
34. `Differentiate isalpha() and isdigit().

--- Content provided by FirstRanker.com ---

35. What is the use of split()?
36. Define a file.
37. Give the syntax for opening a file.
38. Give the syntax for closing a file.
39. How is reading of file done?

--- Content provided by FirstRanker.com ---

40. How is writing of file done?
41. What is a list?
42. Lists are mutable-Justify.
43. How is a list created?
44. How can a list be sorted?

--- Content provided by FirstRanker.com ---

45. How are elements appended to the list?
46. How is insert() used in list?

GE8161 FirstRanker Page 40 of 42
47. What is the usage of pop() in list?

--- Content provided by FirstRanker.com ---

48. Define a tuple.
49. Are tuples mutable or immutable?
50. Mention the use of return statement.
51. What is a Boolean function?
52. How is main function defined?

--- Content provided by FirstRanker.com ---

53. What is a dictionary?
54. How are tuples created?
55. How is a dictionary created?
56. How to print the keys of a dictionary?
57. How to print the values of a dictionary?

--- Content provided by FirstRanker.com ---

58. How is del statement used?
59. Can tuple elements be deleted?
60. What is Python interpreter?
61. Why is Python called an interpreted language?
62. Mention some features of Python

--- Content provided by FirstRanker.com ---

63. What is Python IDLE?
64. Mention some rules for naming an identifier in Python.
65. Give points about Python Numbers.
66. What is bool datatype?
67. Give examples of mathematical functions.

--- Content provided by FirstRanker.com ---

68. What is string formatting operator?
69. Mention about membership operators inPython.
70. How is expression evaluated in Python?
71. What are the loop control statements in Python?
72. What is the use of break statement?

--- Content provided by FirstRanker.com ---

73. What is the use of continue statement?
74. What is the use of pass statement?
75. What is assert statement?
76. Differentiate fruitful function s and void functions.
77. What are required arguments ?

--- Content provided by FirstRanker.com ---

78. Differentiate pass by value and pass by reference.
79. Mention few advantages of function.
80. How is lambda function used?
81. What is a local variable?
82. What are global variables?

--- Content provided by FirstRanker.com ---

83. What are Python decorators?
84. Are strings mutable or immutable?
85. What is join()?
86. What is replace() method?
87. What is list comprehension?

--- Content provided by FirstRanker.com ---

88. Define multidimensional list.
89. How to create lists using range()?
90. What is swapcase() method?
91. What is linear search?
92. How is binary search done?

--- Content provided by FirstRanker.com ---


FirstRanker.com - FirstRanker's Choice

FirstRanker Page 1 of 42

--- Content provided by FirstRanker.com ---

?



DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

--- Content provided by FirstRanker.com ---



I SEMESTER - R 2017

GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING

--- Content provided by FirstRanker.com ---

LABORATORY




--- Content provided by FirstRanker.com ---




Name : _______________________________________
Register No : _______________________________________

--- Content provided by FirstRanker.com ---

Section : _______________________________________


LABORATORY MANUAL

--- Content provided by FirstRanker.com ---

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING



--- Content provided by FirstRanker.com ---

is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

? 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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



VISION
MISSION

--- Content provided by FirstRanker.com ---

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES


1. FUNDAMENTALS

--- Content provided by FirstRanker.com ---

To impart students with fundamental knowledge in Mathematics, Science and
fundamentals of engineering that will mould them to be successful professionals

2. CORE COMPETENCE
To provide students with sound knowledge in engineering and experimental skills to

--- Content provided by FirstRanker.com ---

identify complex software problems in industry and to develop practical solutions for them

3. BREADTH
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

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

them to adapt and work in ever changing technologies

5. LIFELONG LEARNING

To develop the ability of students to establish themselves as professionals in Computer

--- Content provided by FirstRanker.com ---

Science and Engineering and to create awareness about the need for lifelong learning and
pursuing advanced degrees



--- Content provided by FirstRanker.com ---




FirstRanker Page 4 of 42
PROGRAM OUTCOMES

--- Content provided by FirstRanker.com ---




a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field

--- Content provided by FirstRanker.com ---

b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT

--- Content provided by FirstRanker.com ---

tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as
economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to

--- Content provided by FirstRanker.com ---

convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination
j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS

--- Content provided by FirstRanker.com ---




To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.

--- Content provided by FirstRanker.com ---

Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

LIST OF EXPERIMENTS:

--- Content provided by FirstRanker.com ---

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. Find the maximum of a list of numbers
5. Linear search and Binary search

--- Content provided by FirstRanker.com ---

6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame


--- Content provided by FirstRanker.com ---



Upon completion of the course, students will be able to:
Write, test, and debug simple Python programs.
Implement Python programs with conditionals and loops.

--- Content provided by FirstRanker.com ---

Develop Python programs step-wise by defining functions and calling them.
Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python.


--- Content provided by FirstRanker.com ---



COURSE OUTCOMES
COURSE OBJECTIVES

--- Content provided by FirstRanker.com ---

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY
CONTENTS

--- Content provided by FirstRanker.com ---

Sl.No. Name of the Experiment
Page
No.
1
Compute the GCD of two numbers.

--- Content provided by FirstRanker.com ---

07
2
Find the square root of a number (Newton ?s method)
09
3

--- Content provided by FirstRanker.com ---

Exponentiation (power of a number)
11
4
Find the maximum of a list of numbers
13

--- Content provided by FirstRanker.com ---

5(a)
Linear search
15
5(b)
Binary search

--- Content provided by FirstRanker.com ---

17
6(a)
Selection sort
19
6(b)

--- Content provided by FirstRanker.com ---

Insertion sort
21
7
Merge sort
23

--- Content provided by FirstRanker.com ---

8
First n prime numbers
26
9
Multiply matrices

--- Content provided by FirstRanker.com ---

28
10
Programs that take command line arguments (word count)
30
11

--- Content provided by FirstRanker.com ---

Find the most frequent words in a text read from a file
32
12
Simulate elliptical orbits in Pygame
34

--- Content provided by FirstRanker.com ---

13
Simulate bouncing ball in Pygame
37


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 7 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find GCD of two numbers.

Algorithm:

--- Content provided by FirstRanker.com ---

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1
Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd

--- Content provided by FirstRanker.com ---

Program:

def computeGCD(x, y):
if x > y:
smaller = y

--- Content provided by FirstRanker.com ---

else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
gcd = i

--- Content provided by FirstRanker.com ---


return gcd

num1 = 54
num2 = 24

--- Content provided by FirstRanker.com ---


# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))

--- Content provided by FirstRanker.com ---

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))




--- Content provided by FirstRanker.com ---




Ex. No. :01
Date:

--- Content provided by FirstRanker.com ---


GCD OF TWO NUMBERS

FirstRanker Page 8 of 42
Sample output :

--- Content provided by FirstRanker.com ---



The GCD of 54 and 24 is 6


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to compute GCD of two numbers is executed successfully and

--- Content provided by FirstRanker.com ---

the output is verified.

FirstRanker Page 9 of 42

Aim:

--- Content provided by FirstRanker.com ---

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. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,

--- Content provided by FirstRanker.com ---

i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

Program:

--- Content provided by FirstRanker.com ---


def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:

--- Content provided by FirstRanker.com ---

approx = better
better = 0.5 * (approx + n/approx)
return approx

print('The square root is' ,newtonSqrt(81))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Ex. No. :02

--- Content provided by FirstRanker.com ---

Date:

SQUARE ROOT OF A NUMBER

FirstRanker Page 10 of 42

--- Content provided by FirstRanker.com ---

Output:

The square root is 9


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



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 ---



FirstRanker Page 11 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python program to find the exponentiation of a number.

Algorithm:

1. Define a function named power()

--- Content provided by FirstRanker.com ---

2. Read the values of base and exp
3. Use ?if ? to check if exp is equal to 1 or not
i. if exp is equal to 1, then return base
ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

--- Content provided by FirstRanker.com ---


Program:
def power(base,exp):
if(exp==1):
return(base)

--- Content provided by FirstRanker.com ---

if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 03
Date:

EXPONENTIATION OF A NUMBER

--- Content provided by FirstRanker.com ---

FirstRanker Page 12 of 42
Output:
Enter base: 7
Enter exponential value: 2
Result:49

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to find the exponentiation of a number is executed successfully
and the output is verified.

--- Content provided by FirstRanker.com ---

FirstRanker Page 13 of 42


Aim:
To write a Python Program to find the maximum from a list of numbers.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create an empty list named l
2. Read the value of n
3. Read the elements of the list until n
4. Assign l[0] as maxno

--- Content provided by FirstRanker.com ---

5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

Program:

--- Content provided by FirstRanker.com ---

l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):
a=int(input("enter the numbers"))
l.append(a)

--- Content provided by FirstRanker.com ---

maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]
print("The maximum number is %d"%maxno)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :
04
Date:

--- Content provided by FirstRanker.com ---


FINDING MAXIMUM FROM A LIST OF NUMBERS

FirstRanker Page 14 of 42
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 ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus a Python Program to find the maximum from the given list of numbers is successfully
executed and the output is verified.

FirstRanker Page 15 of 42

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):
pos=0

--- Content provided by FirstRanker.com ---

found=False
stop=False
while pos if alist[pos]==item:
found=True
print("element found in position",pos)

--- Content provided by FirstRanker.com ---

else:
if alist[pos]>item:
stop=True
else:
pos=pos+1

--- Content provided by FirstRanker.com ---

return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))

--- Content provided by FirstRanker.com ---

a.append(e)
x=int(input("enter element to search"))
search(a,x)
Ex. No. : 5(a)
Date:

--- Content provided by FirstRanker.com ---


LINEAR SEARCH

FirstRanker Page 16 of 42

--- Content provided by FirstRanker.com ---

Output:
Enter upper limit 5
Enter the elements 6
Enter the elements 45
Enter the elements 2

--- Content provided by FirstRanker.com ---

Enter the elements 61
Enter the elements 26
Enter element to search 6
Element found in position 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.


--- Content provided by FirstRanker.com ---


FirstRanker Page 17 of 42


Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform binary search.
Algorithm:
1. Read the search element
2. Find the middle element in the sorted list
3. Compare the search element with the middle element

--- Content provided by FirstRanker.com ---

i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the
left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the

--- Content provided by FirstRanker.com ---

right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. 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:

--- Content provided by FirstRanker.com ---

mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)
else:

--- Content provided by FirstRanker.com ---

if item last=mid-1
EX. No. : 5(b)
Date:

BINARY SEARCH

--- Content provided by FirstRanker.com ---


FirstRanker Page 18 of 42
else:
first=mid+mid-1
return found

--- Content provided by FirstRanker.com ---


a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))

--- Content provided by FirstRanker.com ---

a.append(e)
x=int(input("enter element to search"))
bsearch(a,x)

Output:

--- Content provided by FirstRanker.com ---

enter upper limit 6
enter the elements 2
enter the elements 3
enter the elements 5
enter the elements 7

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

FirstRanker Page 19 of 42

Aim:
To write a Python Program to perform selection sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,
4. Set pos=location
5. Swap alist[i] and alist[pos]

--- Content provided by FirstRanker.com ---

6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0

--- Content provided by FirstRanker.com ---

for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]

--- Content provided by FirstRanker.com ---

alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. : 6(a)
Date:

SELECTION SORT

--- Content provided by FirstRanker.com ---


FirstRanker Page 20 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

GE8161 FirstRanker Page 21 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform insertion sort.
Algorithm:
1. Create a function named insertionsort

--- Content provided by FirstRanker.com ---

2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]
5. position = position-1
6. alist[position]=currentvalue

--- Content provided by FirstRanker.com ---

7. Print the sorted list

Program:
def insertionSort(alist):
for index in range(1,len(alist)):

--- Content provided by FirstRanker.com ---

currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1

--- Content provided by FirstRanker.com ---

alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 6(b)
Date:

INSERTION SORT

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 22 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Result:
Thus the Python program to perform insertion sort is successfully executed and the output
is verified.

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 23 of 42

Aim:
To write a Python Program to perform Merge sort.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4. Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following

--- Content provided by FirstRanker.com ---

if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

Increment j
Increment k
6. while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]
Increment i

--- Content provided by FirstRanker.com ---

Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]
Increment j
Increment k

--- Content provided by FirstRanker.com ---

8. Print the sorted list




--- Content provided by FirstRanker.com ---


Ex. No. : 7
Date:

MERGE SORT

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 24 of 42
Program:
def mergeSort(alist):

--- Content provided by FirstRanker.com ---

# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]

--- Content provided by FirstRanker.com ---

mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1

--- Content provided by FirstRanker.com ---

k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1

--- Content provided by FirstRanker.com ---

while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
#print("Merging ",alist)

--- Content provided by FirstRanker.com ---

alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

GE8161 FirstRanker Page 26 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find first n prime numbers.

Algorithm:

--- Content provided by FirstRanker.com ---

1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break
else print the value of num
4. Repeat step 3 for i in range(2,num)

--- Content provided by FirstRanker.com ---


Program:

n = int(input("Enter the upper limit: "))

--- Content provided by FirstRanker.com ---

print("Prime numbers are")

for num in range(0,n + 1):
# prime numbers are greater than 1
if num > 1:

--- Content provided by FirstRanker.com ---

for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. : 8
Date:

FIRST N PRIME NUMBERS

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 27 of 42
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 ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

GE8161 FirstRanker Page 28 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to multiply matrices.
Algorithm:
1. Define two matrices X and Y

--- Content provided by FirstRanker.com ---

2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):
i. for j in range(len(Y[0])):
a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]

--- Content provided by FirstRanker.com ---

4. for r in result, print the value of r

Program:

X = [[12,7,3],

--- Content provided by FirstRanker.com ---

[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]

--- Content provided by FirstRanker.com ---

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])):

--- Content provided by FirstRanker.com ---

for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)

--- Content provided by FirstRanker.com ---

Ex. No. : 9
Date:

MATRIX MULTIPLICATION

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 29 of 42
Output:

[114, 160, 60, 27]

--- Content provided by FirstRanker.com ---

[74, 97, 73, 14]
[119, 157, 112, 23]



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to multiply matrices is executed successfully and the output is
verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 30 of 42

Aim:
To write a Python program for command line arguments.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Add arguments to find the words and lines
2. Add the filename as argument
3. Parse the arguments to get the values
4. Format and print the words and lines

--- Content provided by FirstRanker.com ---


Program:
parser=argparse.ArgumentParser()
parser.add_argument('--words','-w',action='store_true')
parser.add_argument('--lines','-l',action='store_true')

--- Content provided by FirstRanker.com ---

parser.add_argument('filename')
args=parser.parse_args()
if args.words:
print("words {}".format(print_words(args.filename))
elif args.lines:

--- Content provided by FirstRanker.com ---

print("lines {}".format(print_lines(args.filename))
else:
print ("words {}".format(print_words(args.filename))
print("lines {}".format(print_lines(args.filename))

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. :10
Date:

--- Content provided by FirstRanker.com ---

COMMAND LINE ARGUMENTS(WORD COUNT)

GE8161 FirstRanker Page 31 of 42
Output:

--- Content provided by FirstRanker.com ---


python main.py ?i input.txt
words 23
lines 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program for command line arguments is executed successfully and the output is
verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 32 of 42

Aim:
To write a Python program to find the most frequent words in a text read from a file.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Read the filename
2. Open the file in read mode
3. Read each line from the file to count the lowers and words
4. Read each line from the file to replace the punctuations

--- Content provided by FirstRanker.com ---

5. Split each line into words and count them
6. Print the words and counts

Program:
def main():

--- Content provided by FirstRanker.com ---

filename=raw_input("enter the file").strip()
infile=open(filename,"r")
wordcounts={}
for line in infile:
processLine(line.lower(),wordcounts)

--- Content provided by FirstRanker.com ---

pairs=list(wordcounts.items())
items=[[x,y] for (y,x) in pairs]
items.sort()
for i in range(len(items)-1,len(items)-11,-1):
print(items[i][1]+"\t"+str(items[i][0]))

--- Content provided by FirstRanker.com ---

def processLine(line,wordcounts):
line=replacePunctuations(line)
words=line.split()
for word in words:
if word in wordcounts:

--- Content provided by FirstRanker.com ---

wordcounts[word]+=1
else:
wordcounts[word]=1
def replacePunctuations(line):
for ch in line:

--- Content provided by FirstRanker.com ---

if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":
line=line.replace(ch," ")
return line
main()

--- Content provided by FirstRanker.com ---


Ex. No. : 11
Date:

FINDING MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 33 of 42
Output:


--- Content provided by FirstRanker.com ---

Enter a filename:a.txt


Hi 1
How 1

--- Content provided by FirstRanker.com ---

Are 1
You 1



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to find the most frequent words in a text read from a file is
executed successfully and the output is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 34 of 42

Aim:
To write a Python program to simulate elliptical orbits in Pygame.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Import the required packages
2. Set up the colours for the elliptical orbits
3. Define the parameters to simulate elliptical orbits
4. Display the created orbits

--- Content provided by FirstRanker.com ---


Program:
import math
import random
import pygame

--- Content provided by FirstRanker.com ---

class Particle ():
def __init__ (self, x, y, colour=0x000000):
self.x = x
self.y = y
self.vx = 0

--- Content provided by FirstRanker.com ---

self.vy = 0
self.colour = colour
def apply_gravity (self, target):
dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2
#g = G*m/dsqd * normalized (self - target)

--- Content provided by FirstRanker.com ---

if dsqd == 0:
return
self.vx += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5
self.vy += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5
def update (self):

--- Content provided by FirstRanker.com ---

self.x += self.vx
self.y += self.vy
pygame.init()
window = pygame.display.set_mode ((600, 400))
main_surface = pygame.Surface ((600, 400))

--- Content provided by FirstRanker.com ---

colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666,
0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] + [0xFF0000, 0x00FF00, 0x0000FF,
0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888, 0xFFFFFF, 0x808000, 0x008080, 0x800080,
0x800000]
#colours = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,

--- Content provided by FirstRanker.com ---

Ex. No. : 12
Date:

SIMULATE ELLIPTICAL ORBITS IN PYGAME

--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 35 of 42
0xFFFFFF, 0x808000, 0x008080, 0x800080, 0x800000]
particles = [Particle (200, 100, colours [i]) for i in range (20)]
earth = Particle (200, 200)
for i, p in enumerate (particles):

--- Content provided by FirstRanker.com ---

p.vx = i / 100
while (True):
# main_surface.fill(0x000000)
pygame.draw.circle (main_surface, 0x00FF00, (earth.x, earth.y), 5, 2)

--- Content provided by FirstRanker.com ---

for p in particles:
p.apply_gravity (earth)
p.update ()
pygame.draw.circle (main_surface, p.colour, (int (p.x), int (p.y)), 5, 2)
window.blit(main_surface, (0, 0))

--- Content provided by FirstRanker.com ---

pygame.display.flip()




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 36 of 42
Output:

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python Program to simulate elliptical orbits using Pygame is executed

--- Content provided by FirstRanker.com ---

successfully and the output is verified.

GE8161 FirstRanker Page 37 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to bouncing ball in Pygame.
Algorithm:
1. Import the required packages
2. Define the required variables

--- Content provided by FirstRanker.com ---

3. Define the screen space to display the bouncing balls in that space

Program:
import sys
import pygame

--- Content provided by FirstRanker.com ---

pygame.init()

size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0

--- Content provided by FirstRanker.com ---


screen = pygame.display.set_mode(size)

ball = pygame.image.load('C:\\Users\\admin\\Desktop//ball.jpg')
ballrect = ball.get_rect()

--- Content provided by FirstRanker.com ---


while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()

--- Content provided by FirstRanker.com ---

ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]

--- Content provided by FirstRanker.com ---


screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()

--- Content provided by FirstRanker.com ---





Ex. No. : 13

--- Content provided by FirstRanker.com ---

Date:

SIMULATE BOUNCING BALL USING PYGAME

GE8161 FirstRanker Page 38 of 42

--- Content provided by FirstRanker.com ---



Output:


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to simulate bouncing ball using Pygame is executed
successfully and the output is verified.

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 39 of 42
VIVAVOCE QUESTIONS:
1. What it the syntax of print function?

--- Content provided by FirstRanker.com ---

2. What is the usage of input function?
3. Define a variable.
4. What is type conversion?
5. Mention the data types in Python
6. What are the attributes of the complex datatype?

--- Content provided by FirstRanker.com ---

7. Mention a few escape sequences.
8. Define an expression
9. What is the usage of ** operator in Python?
10. Give the syntax of if else statement.
11. Give the syntax of for statement.

--- Content provided by FirstRanker.com ---

12. How is range function used in for?
13. Give the syntax of while statement.
14. What are multi way if statements?
15. How is random numbers generated?
16. Define a function.

--- Content provided by FirstRanker.com ---

17. Give the syntax of function.
18. What are the types of arguments in function.?
19. What is a recursive function?
20. What are anonymous functions?
21. What are default arguments?

--- Content provided by FirstRanker.com ---

22. What are variable length arguments?
23. What are keyword arguments?
24. Mention the use of map().
25. Mention the use of filter().
26. Mention the use of reduce().

--- Content provided by FirstRanker.com ---

27. Define a string.
28. How is string slicing done?
29. What is the usage of repetition operator?
30. How is string concatenation done using + operator>
31. Mention some string methods

--- Content provided by FirstRanker.com ---

32. How is length of a string found?
33. How is a string converted to its upper case?
34. `Differentiate isalpha() and isdigit().
35. What is the use of split()?
36. Define a file.

--- Content provided by FirstRanker.com ---

37. Give the syntax for opening a file.
38. Give the syntax for closing a file.
39. How is reading of file done?
40. How is writing of file done?
41. What is a list?

--- Content provided by FirstRanker.com ---

42. Lists are mutable-Justify.
43. How is a list created?
44. How can a list be sorted?
45. How are elements appended to the list?
46. How is insert() used in list?

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 40 of 42
47. What is the usage of pop() in list?
48. Define a tuple.
49. Are tuples mutable or immutable?

--- Content provided by FirstRanker.com ---

50. Mention the use of return statement.
51. What is a Boolean function?
52. How is main function defined?
53. What is a dictionary?
54. How are tuples created?

--- Content provided by FirstRanker.com ---

55. How is a dictionary created?
56. How to print the keys of a dictionary?
57. How to print the values of a dictionary?
58. How is del statement used?
59. Can tuple elements be deleted?

--- Content provided by FirstRanker.com ---

60. What is Python interpreter?
61. Why is Python called an interpreted language?
62. Mention some features of Python
63. What is Python IDLE?
64. Mention some rules for naming an identifier in Python.

--- Content provided by FirstRanker.com ---

65. Give points about Python Numbers.
66. What is bool datatype?
67. Give examples of mathematical functions.
68. What is string formatting operator?
69. Mention about membership operators inPython.

--- Content provided by FirstRanker.com ---

70. How is expression evaluated in Python?
71. What are the loop control statements in Python?
72. What is the use of break statement?
73. What is the use of continue statement?
74. What is the use of pass statement?

--- Content provided by FirstRanker.com ---

75. What is assert statement?
76. Differentiate fruitful function s and void functions.
77. What are required arguments ?
78. Differentiate pass by value and pass by reference.
79. Mention few advantages of function.

--- Content provided by FirstRanker.com ---

80. How is lambda function used?
81. What is a local variable?
82. What are global variables?
83. What are Python decorators?
84. Are strings mutable or immutable?

--- Content provided by FirstRanker.com ---

85. What is join()?
86. What is replace() method?
87. What is list comprehension?
88. Define multidimensional list.
89. How to create lists using range()?

--- Content provided by FirstRanker.com ---

90. What is swapcase() method?
91. What is linear search?
92. How is binary search done?


--- Content provided by FirstRanker.com ---

GE8161 FirstRanker Page 41 of 42
93. How is merge sort performed?
94. What is sorting?
95. How is insertion sort done?
96. How is selection sort done?

--- Content provided by FirstRanker.com ---

97. What are command line arguments?
98. Name some built in functions with dictionary.
99. What is an exception?
100. How is exception handled in python?

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




FirstRanker.com - FirstRanker's Choice

--- Content provided by FirstRanker.com ---

FirstRanker Page 1 of 42

?


--- Content provided by FirstRanker.com ---


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I SEMESTER - R 2017

--- Content provided by FirstRanker.com ---


GE8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Name : _______________________________________
Register No : _______________________________________
Section : _______________________________________


--- Content provided by FirstRanker.com ---

LABORATORY MANUAL

FirstRanker Page 2 of 42
DHANALAKSHMI COLLEGE OF ENGINEERING

--- Content provided by FirstRanker.com ---



is committed to provide highly disciplined, c onscientious and
enterprising professionals conforming to global standards through value based quality education
and training.

--- Content provided by FirstRanker.com ---


? To provide competent technical manpower capable of meeting requirements of the industry
? 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

--- Content provided by FirstRanker.com ---

tag on heart and soul




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





VISION

--- Content provided by FirstRanker.com ---

MISSION

FirstRanker Page 3 of 42
PROGRAM EDUCATIONAL OBJECTIVES

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---

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. LIFELONG LEARNING

--- Content provided by FirstRanker.com ---


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

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

FirstRanker Page 4 of 42
PROGRAM OUTCOMES



--- Content provided by FirstRanker.com ---

a) To apply the basic knowledge of Mathematics, Science and engineering fundamentals in
Computer Science and Engineering field
b) To design and conduct experiments as well as to analyze and interpret data and apply the
same in the career
c) To design and develop innovative and creative software applications

--- Content provided by FirstRanker.com ---

d) To understand a complex real world problem and develop an efficient practical solution
e) To create, select and apply appropriate techniques, resources, modern engineering and IT
tools
f) To understand their roles as a professionals and give the best to the society
g) To develop a system that will meet expected needs within realistic constraints such as

--- Content provided by FirstRanker.com ---

economical, environmental, social, political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to
convey in both verbal and written forms
i) To work in a team as team member or a leader and make unique contributions and work with
coordination

--- Content provided by FirstRanker.com ---

j) To exhibit confidence in self-education and ability for lifelong learning
k) To develop and manage projects in multidisciplinary environments



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



FirstRanker Page 5 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

(Common to all branches of B.E. / B.Tech Programmes)
SYLLABUS



--- Content provided by FirstRanker.com ---

To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, dictionaries.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---


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)

--- Content provided by FirstRanker.com ---

4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers

--- Content provided by FirstRanker.com ---

9. Multiply matrices
10. Programs that take command line arguments (word count)
11. Find the most frequent words in a text read from a file
12. Simulate elliptical orbits in Pygame
13.Simulate bouncing ball in Pygame

--- Content provided by FirstRanker.com ---





Upon completion of the course, students will be able to:

--- Content provided by FirstRanker.com ---

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.
Read and write data from/to files in Python.

--- Content provided by FirstRanker.com ---





COURSE OUTCOMES

--- Content provided by FirstRanker.com ---

COURSE OBJECTIVES

FirstRanker Page 6 of 42
GE 8161 ? PROBLEM SOLVING AND PYTHON PROGRAMMING
LABORATORY

--- Content provided by FirstRanker.com ---

CONTENTS

Sl.No. Name of the Experiment
Page
No.

--- Content provided by FirstRanker.com ---

1
Compute the GCD of two numbers.
07
2
Find the square root of a number (Newton ?s method)

--- Content provided by FirstRanker.com ---

09
3
Exponentiation (power of a number)
11
4

--- Content provided by FirstRanker.com ---

Find the maximum of a list of numbers
13
5(a)
Linear search
15

--- Content provided by FirstRanker.com ---

5(b)
Binary search
17
6(a)
Selection sort

--- Content provided by FirstRanker.com ---

19
6(b)
Insertion sort
21
7

--- Content provided by FirstRanker.com ---

Merge sort
23
8
First n prime numbers
26

--- Content provided by FirstRanker.com ---

9
Multiply matrices
28
10
Programs that take command line arguments (word count)

--- Content provided by FirstRanker.com ---

30
11
Find the most frequent words in a text read from a file
32
12

--- Content provided by FirstRanker.com ---

Simulate elliptical orbits in Pygame
34
13
Simulate bouncing ball in Pygame
37

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





FirstRanker Page 7 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python program to find GCD of two numbers.

--- Content provided by FirstRanker.com ---

Algorithm:

1. Define a function named computeGCD()
2. Find the smallest among the two inputs x and y
3. Perform the following step till smaller+1

--- Content provided by FirstRanker.com ---

Check if ((x % i == 0) and (y % i == 0)), then assign GCD=i
4. Print the value of gcd
Program:

def computeGCD(x, y):

--- Content provided by FirstRanker.com ---

if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):

--- Content provided by FirstRanker.com ---

if((x % i == 0) and (y % i == 0)):
gcd = i

return gcd

--- Content provided by FirstRanker.com ---

num1 = 54
num2 = 24

# take input from the user
# num1 = int(input("Enter first number: "))

--- Content provided by FirstRanker.com ---

# num2 = int(input("Enter second number: "))

print("The GCD. of", num1,"and", num2,"is", computeGCD(num1, num2))


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Ex. No. :01
Date:

GCD OF TWO NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 8 of 42
Sample output :


The GCD of 54 and 24 is 6

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python program to compute GCD of two numbers is executed successfully and
the output is verified.

FirstRanker Page 9 of 42

--- Content provided by FirstRanker.com ---


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().

--- Content provided by FirstRanker.com ---

2. Initialize approx as 0.5*n and better as 0.5*(approx.+n/approx.)
3. Use a while loop with a condition better!=approx to perform the following,
i. Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print the value of approx

--- Content provided by FirstRanker.com ---


Program:

def newtonSqrt(n):
approx = 0.5 * n

--- Content provided by FirstRanker.com ---

better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx

--- Content provided by FirstRanker.com ---


print('The square root is' ,newtonSqrt(81))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


Ex. No. :02
Date:

SQUARE ROOT OF A NUMBER

--- Content provided by FirstRanker.com ---


FirstRanker Page 10 of 42
Output:

The square root is 9

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

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.


FirstRanker Page 11 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python program to find the exponentiation of a number.

Algorithm:

--- Content provided by FirstRanker.com ---


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
i. if exp is equal to 1, then return base

--- Content provided by FirstRanker.com ---

ii.if exp is not equal to 1, then return (base*power(base,exp-1))
4. Print the result

Program:
def power(base,exp):

--- Content provided by FirstRanker.com ---

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))



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 03
Date:

--- Content provided by FirstRanker.com ---

EXPONENTIATION OF A NUMBER

FirstRanker Page 12 of 42
Output:
Enter base: 7

--- Content provided by FirstRanker.com ---

Enter exponential value: 2
Result:49



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to find the exponentiation of a number is executed successfully

--- Content provided by FirstRanker.com ---

and the output is verified.

FirstRanker Page 13 of 42


--- Content provided by FirstRanker.com ---

Aim:
To write a Python Program to find the maximum from a list of numbers.
Algorithm:
1. Create an empty list named l
2. Read the value of n

--- Content provided by FirstRanker.com ---

3. Read the elements of the list until n
4. Assign l[0] as maxno
5. If l[i]>maxno then set maxno=l[i]
6. Increment i by 1
7. Repeat steps 5-6 until i8. Print the value of maximum number

--- Content provided by FirstRanker.com ---


Program:
l=[]
n=int(input("enter the upper limit"))
for i in range(0,n):

--- Content provided by FirstRanker.com ---

a=int(input("enter the numbers"))
l.append(a)
maxno=l[0]
for i in range(0,len(l)):
if l[i]>maxno:

--- Content provided by FirstRanker.com ---

maxno=l[i]
print("The maximum number is %d"%maxno)



--- Content provided by FirstRanker.com ---





Ex. No. :

--- Content provided by FirstRanker.com ---

04
Date:

FINDING MAXIMUM FROM A LIST OF NUMBERS

--- Content provided by FirstRanker.com ---

FirstRanker Page 14 of 42
Output:
Enter the upper limt 3
Enter the numbers 6
Enter the numbers 9

--- Content provided by FirstRanker.com ---

Enter the numbers 90
The maximum number is 90



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---

FirstRanker Page 15 of 42

Aim:
To write a Python Program to perform Linear Search
Algorithm:

--- Content provided by FirstRanker.com ---

1. Read n elements into the list
2. Read the element to be searched
3. If alist[pos]==item, then print the position of the item
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:

--- Content provided by FirstRanker.com ---

def search(alist,item):
pos=0
found=False
stop=False
while pos if alist[pos]==item:

--- Content provided by FirstRanker.com ---

found=True
print("element found in position",pos)
else:
if alist[pos]>item:
stop=True

--- Content provided by FirstRanker.com ---

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)

--- Content provided by FirstRanker.com ---

Ex. No. : 5(a)
Date:

LINEAR SEARCH

--- Content provided by FirstRanker.com ---

FirstRanker Page 16 of 42

Output:
Enter upper limit 5
Enter the elements 6

--- Content provided by FirstRanker.com ---

Enter the elements 45
Enter the elements 2
Enter the elements 61
Enter the elements 26
Enter element to search 6

--- Content provided by FirstRanker.com ---

Element found in position 1




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform linear search is executed successfully and the output
is verified.

--- Content provided by FirstRanker.com ---




FirstRanker Page 17 of 42

--- Content provided by FirstRanker.com ---


Aim:
To write a Python Program to perform binary search.
Algorithm:
1. Read the search element

--- Content provided by FirstRanker.com ---

2. Find the middle element in the sorted list
3. Compare the search element with the middle element
i. if both are matching, print element found
ii. else then check if the search element is smaller or larger than the middle element
4. If the search element is smaller than the middle element, then repeat steps 2 and 3 for the

--- Content provided by FirstRanker.com ---

left sublist of the middle element
5. If the search element is larger than the middle element, then repeat steps 2 and 3 for the
right sublist of the middle element
6. Repeat the process until the search element if found in the list
7. If element is not found, loop terminates

--- Content provided by FirstRanker.com ---


Program:
def bsearch(alist,item):
first=0
last=len(alist)-1

--- Content provided by FirstRanker.com ---

found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True

--- Content provided by FirstRanker.com ---

print("element found in position",mid)
else:
if item last=mid-1
EX. No. : 5(b)
Date:

--- Content provided by FirstRanker.com ---


BINARY SEARCH

FirstRanker Page 18 of 42
else:

--- Content provided by FirstRanker.com ---

first=mid+mid-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"))
bsearch(a,x)

--- Content provided by FirstRanker.com ---


Output:
enter upper limit 6
enter the elements 2
enter the elements 3

--- Content provided by FirstRanker.com ---

enter the elements 5
enter the elements 7
enter the elements 14
enter the elements 25
enter element to search 5

--- Content provided by FirstRanker.com ---

element found in position 2


Result:
Thus the Python Program to perform binary search is executed successfully and the output

--- Content provided by FirstRanker.com ---

is verified

FirstRanker Page 19 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform selection sort.
Algorithm:
1. Create a function named selectionsort
2. Initialise pos=0
3. If alist[location]>alist[pos] then perform the following till i+1,

--- Content provided by FirstRanker.com ---

4. Set pos=location
5. Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):

--- Content provided by FirstRanker.com ---

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

--- Content provided by FirstRanker.com ---

temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp

alist = [54,26,93,17,77,31,44,55,20]

--- Content provided by FirstRanker.com ---

selectionSort(alist)
print(alist)



--- Content provided by FirstRanker.com ---




Ex. No. : 6(a)
Date:

--- Content provided by FirstRanker.com ---


SELECTION SORT

FirstRanker Page 20 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 21 of 42

Aim:
To write a Python Program to perform insertion sort.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the following till len(alist)
4. alist[position]=alist[position-1]

--- Content provided by FirstRanker.com ---

5. position = position-1
6. alist[position]=currentvalue
7. Print the sorted list

Program:

--- Content provided by FirstRanker.com ---

def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:

--- Content provided by FirstRanker.com ---

alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)

--- Content provided by FirstRanker.com ---

print(alist)




--- Content provided by FirstRanker.com ---



Ex. No. : 6(b)
Date:

--- Content provided by FirstRanker.com ---

INSERTION SORT


GE8161 FirstRanker Page 22 of 42
Output:

--- Content provided by FirstRanker.com ---

[17, 20, 26, 31, 44, 54, 55, 77, 93]




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---




Result:
Thus the Python program to perform insertion sort is successfully executed and the output

--- Content provided by FirstRanker.com ---

is verified.

GE8161 FirstRanker Page 23 of 42

Aim:

--- Content provided by FirstRanker.com ---

To write a Python Program to perform Merge sort.
Algorithm:
1. Create a function named mergesort
2. Find the mid of the list
3. Assign lefthalf = alist[:mid] and righthalf = alist[mid:]

--- Content provided by FirstRanker.com ---

4. Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
Increment i

--- Content provided by FirstRanker.com ---

else
alist[k]=righthalf[j]
Increment j
Increment k
6. while i < len(lefthalf),perform the following

--- Content provided by FirstRanker.com ---

alist[k]=lefthalf[i]
Increment i
Increment k
7. while j < len(righthalf), perform the following
alist[k]=righthalf[j]

--- Content provided by FirstRanker.com ---

Increment j
Increment k
8. Print the sorted list


--- Content provided by FirstRanker.com ---




Ex. No. : 7
Date:

--- Content provided by FirstRanker.com ---


MERGE SORT


GE8161 FirstRanker Page 24 of 42

--- Content provided by FirstRanker.com ---

Program:
def mergeSort(alist):
# print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2

--- Content provided by FirstRanker.com ---

lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0

--- Content provided by FirstRanker.com ---

while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:

--- Content provided by FirstRanker.com ---

alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]

--- Content provided by FirstRanker.com ---

i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1

--- Content provided by FirstRanker.com ---

k=k+1
#print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 25 of 42
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Result:
Thus the Python program to perform merge sort is successfully executed and the output is
verifie

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 26 of 42

Aim:
To write a Python program to find first n prime numbers.

--- Content provided by FirstRanker.com ---


Algorithm:
1. Read the value of n
2. for num in range(0,n + 1), perform the following
3. if num%i is 0 then break

--- Content provided by FirstRanker.com ---

else print the value of num
4. 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):

--- Content provided by FirstRanker.com ---

# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break

--- Content provided by FirstRanker.com ---

else:
print(num)



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---



Ex. No. : 8
Date:

--- Content provided by FirstRanker.com ---

FIRST N PRIME NUMBERS


GE8161 FirstRanker Page 27 of 42
Output:

--- Content provided by FirstRanker.com ---

Enter the upper limit:100
Prime numbers are
2
3
5

--- Content provided by FirstRanker.com ---

7
11
13
17
19

--- Content provided by FirstRanker.com ---

23
29
31
37
41

--- Content provided by FirstRanker.com ---

43
47
53
59
61

--- Content provided by FirstRanker.com ---

67
71
73
79
83

--- Content provided by FirstRanker.com ---

89
97



--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- 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.

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 28 of 42

Aim:
To write a Python program to multiply matrices.

--- Content provided by FirstRanker.com ---

Algorithm:
1. Define two matrices X and Y
2. Create a resultant matrix named ?result ?
3. for i in range(len(X)):
i. for j in range(len(Y[0])):

--- Content provided by FirstRanker.com ---

a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r

Program:

--- Content provided by FirstRanker.com ---


X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],

--- Content provided by FirstRanker.com ---

[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]

--- Content provided by FirstRanker.com ---

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:

--- Content provided by FirstRanker.com ---

print(r)

Ex. No. : 9
Date:

--- Content provided by FirstRanker.com ---

MATRIX MULTIPLICATION


GE8161 FirstRanker Page 29 of 42
Output:

--- Content provided by FirstRanker.com ---


[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to multiply matrices is executed successfully and the output is
verified.

GE8161 FirstRanker Page 30 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program for command line arguments.
Algorithm:
1. Add arguments to find the words and lines
2. Add the filename as argument

--- Content provided by FirstRanker.com ---

3. Parse the arguments to get the values
4. 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:

--- Content provided by FirstRanker.com ---

print("words {}".format(print_words(args.filename))
elif args.lines:
print("lines {}".format(print_lines(args.filename))
else:
print ("words {}".format(print_words(args.filename))

--- Content provided by FirstRanker.com ---

print("lines {}".format(print_lines(args.filename))




--- Content provided by FirstRanker.com ---





Ex. No. :10

--- Content provided by FirstRanker.com ---

Date:

COMMAND LINE ARGUMENTS(WORD COUNT)

GE8161 FirstRanker Page 31 of 42

--- Content provided by FirstRanker.com ---

Output:


python main.py ?i input.txt
words 23

--- Content provided by FirstRanker.com ---

lines 1




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program for command line arguments is executed successfully and the output is
verified.

GE8161 FirstRanker Page 32 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to find the most frequent words in a text read from a file.
Algorithm:
1. Read the filename
2. Open the file in read mode

--- Content provided by FirstRanker.com ---

3. Read each line from the file to count the lowers and words
4. Read each line from the file to replace the punctuations
5. Split each line into words and count them
6. Print the words and counts

--- Content provided by FirstRanker.com ---

Program:
def main():
filename=raw_input("enter the file").strip()
infile=open(filename,"r")
wordcounts={}

--- Content provided by FirstRanker.com ---

for line in infile:
processLine(line.lower(),wordcounts)
pairs=list(wordcounts.items())
items=[[x,y] for (y,x) in pairs]
items.sort()

--- Content provided by FirstRanker.com ---

for i in range(len(items)-1,len(items)-11,-1):
print(items[i][1]+"\t"+str(items[i][0]))
def processLine(line,wordcounts):
line=replacePunctuations(line)
words=line.split()

--- Content provided by FirstRanker.com ---

for word in words:
if word in wordcounts:
wordcounts[word]+=1
else:
wordcounts[word]=1

--- Content provided by FirstRanker.com ---

def replacePunctuations(line):
for ch in line:
if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":
line=line.replace(ch," ")
return line

--- Content provided by FirstRanker.com ---

main()


Ex. No. : 11
Date:

--- Content provided by FirstRanker.com ---


FINDING MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

GE8161 FirstRanker Page 33 of 42
Output:

--- Content provided by FirstRanker.com ---



Enter a filename:a.txt


--- Content provided by FirstRanker.com ---

Hi 1
How 1
Are 1
You 1

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python program to find the most frequent words in a text read from a file is
executed successfully and the output is verified.

GE8161 FirstRanker Page 34 of 42

--- Content provided by FirstRanker.com ---

Aim:
To write a Python program to simulate elliptical orbits in Pygame.
Algorithm:
1. Import the required packages
2. Set up the colours for the elliptical orbits

--- Content provided by FirstRanker.com ---

3. Define the parameters to simulate elliptical orbits
4. Display the created orbits

Program:
import math

--- Content provided by FirstRanker.com ---

import random
import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):
self.x = x

--- Content provided by FirstRanker.com ---

self.y = y
self.vx = 0
self.vy = 0
self.colour = colour
def apply_gravity (self, target):

--- Content provided by FirstRanker.com ---

dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2
#g = G*m/dsqd * normalized (self - target)
if dsqd == 0:
return
self.vx += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5

--- Content provided by FirstRanker.com ---

self.vy += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5
def update (self):
self.x += self.vx
self.y += self.vy
pygame.init()

--- Content provided by FirstRanker.com ---

window = pygame.display.set_mode ((600, 400))
main_surface = pygame.Surface ((600, 400))
colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666,
0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] + [0xFF0000, 0x00FF00, 0x0000FF,
0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888, 0xFFFFFF, 0x808000, 0x008080, 0x800080,

--- Content provided by FirstRanker.com ---

0x800000]
#colours = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,
Ex. No. : 12
Date:

--- Content provided by FirstRanker.com ---

SIMULATE ELLIPTICAL ORBITS IN PYGAME

GE8161 FirstRanker Page 35 of 42
0xFFFFFF, 0x808000, 0x008080, 0x800080, 0x800000]
particles = [Particle (200, 100, colours [i]) for i in range (20)]

--- Content provided by FirstRanker.com ---

earth = Particle (200, 200)
for i, p in enumerate (particles):
p.vx = i / 100
while (True):
# main_surface.fill(0x000000)

--- Content provided by FirstRanker.com ---

pygame.draw.circle (main_surface, 0x00FF00, (earth.x, earth.y), 5, 2)

for p in particles:
p.apply_gravity (earth)
p.update ()

--- Content provided by FirstRanker.com ---

pygame.draw.circle (main_surface, p.colour, (int (p.x), int (p.y)), 5, 2)
window.blit(main_surface, (0, 0))
pygame.display.flip()


--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





GE8161 FirstRanker Page 36 of 42

--- Content provided by FirstRanker.com ---

Output:




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---

Result:
Thus the Python Program to simulate elliptical orbits using Pygame is executed
successfully and the output is verified.

GE8161 FirstRanker Page 37 of 42

--- Content provided by FirstRanker.com ---



Aim:
To write a Python program to bouncing ball in Pygame.
Algorithm:

--- Content provided by FirstRanker.com ---

1. Import the required packages
2. Define the required variables
3. Define the screen space to display the bouncing balls in that space

Program:

--- Content provided by FirstRanker.com ---

import sys
import pygame
pygame.init()

size = width, height = 320, 240

--- Content provided by FirstRanker.com ---

speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

--- Content provided by FirstRanker.com ---

ball = pygame.image.load('C:\\Users\\admin\\Desktop//ball.jpg')
ballrect = ball.get_rect()

while 1:
for event in pygame.event.get():

--- Content provided by FirstRanker.com ---

if event.type == pygame.QUIT: sys.exit()

ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]

--- Content provided by FirstRanker.com ---

if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]

screen.fill(black)
screen.blit(ball, ballrect)

--- Content provided by FirstRanker.com ---

pygame.display.flip()




--- Content provided by FirstRanker.com ---


Ex. No. : 13
Date:

SIMULATE BOUNCING BALL USING PYGAME

--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 38 of 42


Output:

--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---





Result:

--- Content provided by FirstRanker.com ---

Thus the Python Program to simulate bouncing ball using Pygame is executed
successfully and the output is verified.


GE8161 FirstRanker Page 39 of 42

--- Content provided by FirstRanker.com ---

VIVAVOCE QUESTIONS:
1. What it the syntax of print function?
2. What is the usage of input function?
3. Define a variable.
4. What is type conversion?

--- Content provided by FirstRanker.com ---

5. Mention the data types in Python
6. What are the attributes of the complex datatype?
7. Mention a few escape sequences.
8. Define an expression
9. What is the usage of ** operator in Python?

--- Content provided by FirstRanker.com ---

10. Give the syntax of if else statement.
11. Give the syntax of for statement.
12. How is range function used in for?
13. Give the syntax of while statement.
14. What are multi way if statements?

--- Content provided by FirstRanker.com ---

15. How is random numbers generated?
16. Define a function.
17. Give the syntax of function.
18. What are the types of arguments in function.?
19. What is a recursive function?

--- Content provided by FirstRanker.com ---

20. What are anonymous functions?
21. What are default arguments?
22. What are variable length arguments?
23. What are keyword arguments?
24. Mention the use of map().

--- Content provided by FirstRanker.com ---

25. Mention the use of filter().
26. Mention the use of reduce().
27. Define a string.
28. How is string slicing done?
29. What is the usage of repetition operator?

--- Content provided by FirstRanker.com ---

30. How is string concatenation done using + operator>
31. Mention some string methods
32. How is length of a string found?
33. How is a string converted to its upper case?
34. `Differentiate isalpha() and isdigit().

--- Content provided by FirstRanker.com ---

35. What is the use of split()?
36. Define a file.
37. Give the syntax for opening a file.
38. Give the syntax for closing a file.
39. How is reading of file done?

--- Content provided by FirstRanker.com ---

40. How is writing of file done?
41. What is a list?
42. Lists are mutable-Justify.
43. How is a list created?
44. How can a list be sorted?

--- Content provided by FirstRanker.com ---

45. How are elements appended to the list?
46. How is insert() used in list?

GE8161 FirstRanker Page 40 of 42
47. What is the usage of pop() in list?

--- Content provided by FirstRanker.com ---

48. Define a tuple.
49. Are tuples mutable or immutable?
50. Mention the use of return statement.
51. What is a Boolean function?
52. How is main function defined?

--- Content provided by FirstRanker.com ---

53. What is a dictionary?
54. How are tuples created?
55. How is a dictionary created?
56. How to print the keys of a dictionary?
57. How to print the values of a dictionary?

--- Content provided by FirstRanker.com ---

58. How is del statement used?
59. Can tuple elements be deleted?
60. What is Python interpreter?
61. Why is Python called an interpreted language?
62. Mention some features of Python

--- Content provided by FirstRanker.com ---

63. What is Python IDLE?
64. Mention some rules for naming an identifier in Python.
65. Give points about Python Numbers.
66. What is bool datatype?
67. Give examples of mathematical functions.

--- Content provided by FirstRanker.com ---

68. What is string formatting operator?
69. Mention about membership operators inPython.
70. How is expression evaluated in Python?
71. What are the loop control statements in Python?
72. What is the use of break statement?

--- Content provided by FirstRanker.com ---

73. What is the use of continue statement?
74. What is the use of pass statement?
75. What is assert statement?
76. Differentiate fruitful function s and void functions.
77. What are required arguments ?

--- Content provided by FirstRanker.com ---

78. Differentiate pass by value and pass by reference.
79. Mention few advantages of function.
80. How is lambda function used?
81. What is a local variable?
82. What are global variables?

--- Content provided by FirstRanker.com ---

83. What are Python decorators?
84. Are strings mutable or immutable?
85. What is join()?
86. What is replace() method?
87. What is list comprehension?

--- Content provided by FirstRanker.com ---

88. Define multidimensional list.
89. How to create lists using range()?
90. What is swapcase() method?
91. What is linear search?
92. How is binary search done?

--- Content provided by FirstRanker.com ---



GE8161 FirstRanker Page 41 of 42
93. How is merge sort performed?
94. What is sorting?

--- Content provided by FirstRanker.com ---

95. How is insertion sort done?
96. How is selection sort done?
97. What are command line arguments?
98. Name some built in functions with dictionary.
99. What is an exception?

--- Content provided by FirstRanker.com ---

100. How is exception handled in python?




--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---






--- Content provided by FirstRanker.com ---


GE8161 FirstRanker Page 42 of 42

LIST OF MINI PROJECTS
1.ATM Generation

--- Content provided by FirstRanker.com ---

2.Dice Rolling simulator
3.Library Management System
4.Restaurant Management System
5.Calendar Application
6.Guessing Birthdays

--- Content provided by FirstRanker.com ---

7.Loan Calculator
8.Text Animation
9. Spell Checker
10.Data Retrieval from Web

--- Content provided by FirstRanker.com ---



FirstRanker.com - FirstRanker's Choice