0% found this document useful (0 votes)
29 views

Python Unit 1

Uploaded by

Sabarigiri Vason
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Python Unit 1

Uploaded by

Sabarigiri Vason
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

PROBLEM SOLVING AND PYTHON PROGRAMMING

1
UNIT I

1. Algorithms,
2. building blocks of algorithms (statements, state, control flow, functions),
notation (pseudo code, flow chart, programming language),
3. algorithmic problem solving,
4. simple strategies for developing algorithms (iteration, recursion).
5. Illustrative problems:
a).find minimum in a list,
b).insert a card in a list of sorted cards,
c).Guess an integer number in a range,
d).Towers of Hanoi.
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
2 PROBLEM SOLVING

 Problem solving is the systematic approach to define the problem and creating number of
solutions.

 The problem solving process starts with the problem specifications and ends with a correct
program.

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
3 PROBLEM SOLVING TECHNIQUES

 Problem solving technique is a set of techniques that helps in providing logic


for solving a problem.
 Problem solving can be expressed in the form of

1. Algorithms.
2. Flowcharts.
3. Pseudo codes.
4. programs

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
4 ALGORITHM

 It is defined as a sequence of instructions that describe a method for solving a


problem. In other words it is a step by step procedure for solving a problem.

 Properties of Algorithms

Should be written in simple English


Each and every instruction should be precise and unambiguous.
Instructions in an algorithm should not be repeated infinitely.
Algorithm should conclude after a finite number of steps.
Should have an end point
 Derived results should be obtained only after the algorithm terminates

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
5 Qualities of a good algorithm

 The following are the primary factors that are often used to judge the quality of the
algorithms.

 Time – To execute a program, the computer system takes some amount of time. The lesser is
the time required, the better is the algorithm.

 Memory – To execute a program, computer system takes some amount of memory space.
The lesser is the memory required, the better is the algorithm.

 Accuracy – Multiple algorithms may provide suitable or correct solutions to a given


problem, some of these may provide more accurate results than others, and such algorithms
may be suitable.

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
6 Example:

Example

Write an algorithm to print „Good Morning”

Step 1: Start
Step 2: Print “Good Morning”
Step 3: Stop

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
7 BUILDING BLOCKS OF ALGORITHMS

 Algorithms can be constructed from basic building blocks namely, sequence,


selection and iteration.

1. statements
2. state
3. control flow
4. functions

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
8 Statements:

 Statement is a single action in a computer.

 In a computer statements might include some of the following actions

 input data-information given to the program

 process data-perform operation on a given input

 output data-processed result

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
9 State

 Transition from one process to another process under specified condition


with in a time is called state.

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
10 Control flow:

 The process of executing the individual statements in a given order is called


control flow.

 The control can be executed in three ways

1.Sequence
2.selection
3.iteration

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
11 Sequence:

 All the instructions are executed one after another is called sequence
execution.
Example:
Add two numbers:
Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b Step 4:
Display c
Step 5: Stop

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
12 Selection

A selection statement causes the program control to be transferred to a


specific part of the program based upon the condition.

If the conditional test is true, one part of the program will be executed,
otherwise it will execute the other part of the program.
Example

Write an algorithm to check whether he is eligible to vote?

Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 5: Stop

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
13 Iteration:

 In some programs, certain set of statements are executed again and again based upon
conditional test.
 i.e. executed more than one time.
 This type of execution is called looping or iteration.
Example
Write an algorithm to print all natural numbers up to n
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
14
Functions:

 Function is a sub program which consists of block of code (set of instructions) that
performs a particular task.
 For complex problems, the problem is been divided into smaller and simpler tasks
during algorithm design.

 Benefits of Using Functions


 Reduction in line of code
 code reuse
 Better readability
 Information hiding
 Easy to debug and test
 Improved maintainability

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
15 Example:
Algorithm for addition of two numbers using function

Main function()
Step 1: Start
Step 2: Call the function add()
Step 3: Stop
Sub function add()
Step 1: Function start
Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
Step 5: Return
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
16

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
17 NOTATIONS

 FLOW CHART
 PSEUDO CODE
 PROGRAMMING LANGUAGE

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
18
FLOW CHART
 Flow chart is defined as graphical representation of the logic for problem solving.
 The purpose of flowchart is making the logic of the program clear in a visual representation.

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
19
Rules for drawing a flowchart
 The flowchart should be clear, neat and easy to follow
 The flowchart must have a logical start and finish.
 Only one flow line should come out from a process symbol
 Only one flow line should enter a decision symbol. However, two or three flow lines may
leave the decision symbol.

 Only one flow line is used with a terminal symbol.

 Within standard symbols, write briefly and precisely.


 Intersection of flow lines should be avoided.

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
Advantages of flowchart
20

1. Communication: - Flowcharts are better way of communicating the logic of a system to all
concerned.
2. Effective analysis: - With the help of flowchart, problem can be analyzed in more effective
way.
3. Proper documentation: - Program flowcharts serve as a good
program documentation, which is needed for various purposes.
4. Efficient Coding: - The flowcharts act as a guide or blueprint during the systems analysis
and program development phase.
5. Proper Debugging: - The flowchart helps in debugging process.
6. Efficient Program Maintenance: - The maintenance of operating program becomes easy
with the help of flowchart. It helps the programmer to put efforts more efficiently on that
part

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
Disadvantages of flow chart
21

1. Complex logic: - Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.

2. Alterations and Modifications: - If alterations are required the flowchart may require re-
drawing completely.

3. Reproduction: - As the flowchart symbols cannot be typed, reproduction of flowchart


becomes a problem.

4. Cost: For large application the time and cost of flowchart drawing becomes costly.

Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
22
PSEUDO CODE
 Pseudo code consists of short, readable and formally styled English languages used for
explain an algorithm.

 It does not include details like variable declaration, subroutines.

 It is easier to understand for the programmer or non programmer to understand the


general working of the program, because it is not based on any programming language.

 It gives us the sketch of the program before actual coding.

 It is not a machine readable

 Pseudo code can’t be compiled and executed.

 There is no standard syntax for pseudo code


Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming

You might also like