CS25C02- COMPUTER PROGRAMMING PYTHON
UNIT-1 INTRODUCTION TO PYTHON
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.
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
ALGORITHM
An algorithm is defined as a step by step procedure for solving a problem. It is a
ordered set of rules to solve a problem. An algorithm is a representation of a solution to a
problem. It is a well- defined computational procedure consisting of a set of instructions that
takes some value or set of values, as input, and produces some value or set of values, as
output.
Algorithm
Input A step by step method Output
for solving a problem
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
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.
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
Properties of Algorithms
Every algorithm must have five essential properties:
(1) Inputs specified: An algorithm must have zero or more inputs, We must specify
the type of the data, the amount of data, and the form that the data will take.
(2) Outputs specified: An algorithm has one or more outputs, which have a
specified relation to the inputs.
(3) Definiteness: Every detail of each step must be clearly specified.
(4) Effectiveness: All operations to be performed must be sufficiently basic that they
can be done exactly and in finite length.
(5) Finiteness: An algorithm must always terminate after a finite number of steps.
Characteristics of Algorithm Finiteness
Algorithm must terminate after a finite number of steps and further each steps must
be executable in finite amount of time.
Definiteness
Instruction must be clear, user defined & Precise. There should not be any ambiguity.
Input
An algorithm has zero or more, but only finite number of inputs, zero inputs.
Output
An algorithm has one or more output.
Effectiveness
An algorithm should be effective that means each of the operation to be performed
in algorithm should be computer programming language independent.
Uniqueness
Result or each steps are uniquely defined and only depend on the input and result of the
preceding steps.
Generality
Algorithm should apply to set of input, rather than single input.
Feasibility
It must be possible to perform each instructions.
Method for Developing an Algorithm
(1) Define the problem: State the problem to be solved in clear and concise manner.
(2) List the inputs and outputs
(3) Describe the steps needed to convert input to output
(4) Test the algorithm: Choose input data and verify that the algorithm works.
ALGORITHMIC PROBLEM SOLVING:
Algorithmic problem solving is solving problem that require the formulation of an
algorithm for the solution.
Understanding the Problem
It is the process of finding the input of the problem that the algorithm solves.
It is very important to specify exactly the set of inputs the algorithm needs to handle.
A correct algorithm is not one that works most of the time, but one that
works correctly for all legitimate inputs.
Ascertaining the Capabilities of the Computational Device
If the instructions are executed one after another, it is called sequential algorithm.
Choosing between Exact and Approximate Problem Solving
The next principal decision is to choose between solving the problem exactly or
solving it approximately.
Based on this, the algorithms are classified as exact algorithm and
approximation algorithm.
Data structure plays a vital role in designing and analysis the algorithms.
Some of the algorithm design techniques also depend on the structuring data specifying
a problem’s instance
Algorithm+ Data structure=programs.
Algorithm Design Techniques
An algorithm design technique (or “strategy” or “paradigm”) is a general approach
to solving problems algorithmically that is applicable to a variety of problems from
different areas of computing.
Learning these techniques is of utmost importance for the following reasons.
First, they provide guidance for designing algorithms for new
problems,
Second, algorithms are the cornerstone of computer science.
Methods of Specifying an Algorithm
Pseudocode is a mixture of a natural language and programming language-like
constructs. Pseudo code is usually more precise than natural language, and its usage
often yields more succinct algorithm descriptions. In the earlier days of computing, the
dominant vehicle for specifying algorithms was a flowchart, a method of expressing
an algorithm by a collection of connected geometric shapes containing descriptions of
the algorithm’s steps.
Programming language can be fed into an electronic computer directly. Instead, it
needs to be converted into a computer program written in a particular computer
language. We can look at such a program as yet another way of specifying the
algorithm, although it is preferable to consider it as the algorithm’s implementation.
Prove Correctness
Once an algorithm has been specified, you have to prove its correctness. That is,
you have to prove that the algorithm yields a required result for every legitimate
input in a finite amount of time.
A common technique for proving correctness is to use mathematical induction
because an algorithm’s iterations provide a natural sequence of steps needed for such
proofs.
Analyzing an Algorithm
1. Efficiency.
Time efficiency: indicating how fast the algorithm runs,
Space efficiency: indicating how much extra memory it uses
2. Simplicity.
An algorithm should be precisely defined and investigated with
mathematical expressions.
Simpler algorithms are easier to understand and easier to program.
Simple algorithms usually contain fewer bugs.
Coding an Algorithm
Most algorithms are destined to be ultimately implemented as computer
programs. Programming an algorithm presents both a peril and an opportunity.
A working program provides an additional opportunity in allowing an empirical
analysis of the underlying algorithm. Such an analysis is based on timing the program
on several inputs and then analyzing the results obtained.
PROBLEM ANALYSIS CHART (PAC)
A Problem Analysis Chart (PAC) is a structured tool used to break down a problem into key
components. It helps to systematically understand the problem, identify necessary data, and
develop a structured approach to solve it
Components of a Problem Analysis Chart:
Input: Data or information required to solve the problem.
Process: The steps or operations needed to transform the input into the desired output.
Output: The desired result or solution to the problem.
Constraints/Assumptions: Details of any limitations or specific requirements that the program
must adhere to.
Example:
# Problem Analysis Chart for Area of a Rectangle
# 1. Inputs:
- Length (float): The length of the rectangle.
- Width (float): The width of the rectangle.
# 2. Process/Calculations:
- Formula: Area = Length * Width
# 3. Outputs:
- Area (float): The calculated area of the rectangle.
# 4. Constraints/Assumptions:
- Length and Width must be non-negative numerical values.
- The input values are expected to be in a consistent unit (e.g., meters, feet).
DEVELOPING AN ALGORITHM
Step 1: Obtain a description of the
problem. Step 2: Analyze the problem.
Step 3: Develop a high-level algorithm.
Step 4: Refine the algorithm by adding more
detail. Step 5: Review the algorithm.
Step 1: Obtain a description of the problem.
This step is much more difficult than it appears. In the following discussion, the word client
refers to someone who wants to find a solution to a problem, the word developer refers to
someone who finds a way to solve the problem. The developer must create an algorithm that
will solve the client's problem.
Step 2: Analyze the problem.
The purpose of this step is to determine both the starting and ending points for solving
the problem. This process is analogous to a mathematician determining what is given and
what must be proven. A good problem description makes it easier to perform this step.
Step 3: Develop a high-level algorithm.
An algorithm is a plan for solving a problem, but plans come in several levels of
detail. It's usually better to start with a high-level algorithm that includes the major part of a
solution, but leaves the details until later.
Step 4: Refine the algorithm by adding more detail.
A high-level algorithm shows the major steps that need to be followed to solve a
problem. The technique of gradually working from a high-level to a detailed algorithm is
often called stepwise refinement. Stepwise refinement is a process for developing a detailed
algorithm by gradually adding detail to a high-level algorithm.
Step 5: Review the algorithm.
The final step is to review the algorithm. First, we need to work through the algorithm
step by step to determine whether or not it will solve the original problem.
FLOWCHART
A graphical representation of an algorithm. Flowcharts is a diagram made
up of boxes, diamonds, and other shapes, connected by arrows.
Each shape represents a step in process and arrows show the order in which they occur.
Table : Flowchart Symbols
[Link] Name of Symbol Type Description
symbol
1. Terminal Oval Represent the start and
Symbol stop of the program.
2. Input/ Output Parallelogram Denotes either input or
symbol output operation.
3. Process symbol Rectangle Denotes the process to be
carried
4. Decision symbol Diamond Represents decision
making and branching
5. Flow lines Arrow lines Represents the sequence
of steps and direction of
flow. Used to connect
symbols.
6. Connector Circle A connector symbol is
represented by a circle
and a letter or digit is
placed in the circle to
specify the link. This
symbol is used to
connect flowcharts.
Rules for drawing flowchart
1. In drawing a proper flowchart, all necessary requirements should be listed out in logical
order.
2. The flow chart should be clear, neat and easy to follow. There should not be any
room for ambiguity in understanding the flowchart.
3. The usual directions of the flow of a procedure or system is from left to right or top
to bottom. Only one flow line should come out from a process symbol.
4. Only one flow line should enter a decision symbol, but two or three flow lines, one
for each possible answer, cap leave the decision symbol.
<0 >0
=0
5. Only one flow line is used in conjunction with terminal symbol
6. If the flowchart becomes complex, it is better to use connector symbols to reduce
the number of flow lines.
7. Ensure that the flowchart has a logical start and finish.
Advantages of flow charts
• It is easy to understand
• A problem can be analysed easily with flowchart
• It gives clear idea of a program
• It acts as a guide during the program development
• It helps to clear the errors in coding
• It helps in maintenance of code
Disadvantages of flow charts
• It cannot be prepared for difficult programs
• Alterations and modifications cannot be done easily
• It is not typed, so its preparation is little difficult
Rules for drawing a flow chart is given below
• The standard symbols should only be used.
• The arrowheads in the flowchart represent the direction of flow of control in the problem.
• The usual direction of the flow of procedure is from top to bottom or left to right.
• The flow lines should not cross each other.
• Be consistent in using names and variables in the flowchart.
• Keep the flowchart as simple as possible.
• Words in the flowchart symbols should be common statements and easy to understand.
• Chart main line of logic, and then incorporate all the details of logic.
• If a new page is needed for flowcharting, then use connectors for better representation.
• Don’t chart every details or the flowchart will only be graphical represented.
Example: Flowchart representation to add two numbers
Start
Declare variables num1, num2 and
sum
Read num1and num2
sum ← a+b
Display sum
stop
Example 2: Finding the greatest number among two numbers
PSEUDOCODE
Pseudocode is an informal high-level description of the operating principle
of a computer program or algorithm. It uses the basic structure of a normal
programming language, but is intended for human reading rather than machine reading.
It is text based detail design tool. Pseudo means false and code refers to
instructions written in programming language.
Pseudocode cannot be compiled nor executed, and there are no real formatting
or syntax rules. The pseudocode is written in normal English language which cannot be
understood by the computer
Example:
Pseudocode: To find sum of two numbers
READ num1, num2
sum = num1+ num2
PRINT sum
Rules of Pseudo code
• Start with an algorithm and phrase it using words that are easily transcribed into
computer instructions.
• Indent when you are enclosing instructions within a loop or a conditional clause.
• Avoid words associated with a certain kind of computer language.
• Do not include data declarations in pseudo code.
Basic rules to write pseudocode:
1. Only one statement per line.
Statements represents single action is written on same line. For
example to read the input, all the inputs must be read using single
statement.
2. Capitalized initial keywords
The keywords should be written in capital letters. Eg: READ,
WRITE, IF, ELSE, ENDIF, WHILE, REPEAT, UNTIL
Example:
Pseudocode: Find the total and average of
three subjects RAED name, department,
mark1, mark2, mark3
Total=mark1+mark2+mark3
Average=Total/3
WRITE name, department,mark1, mark2, mark3
3. Indent to show hierarchy
Indentation is a process of showing the boundaries of the structure.
4. End multi-line structures
Each structure must be ended properly, which provides more clarity.
Example:
Pseudocode: Find greatest of two numbers
READ a, b
IF a>b then
PRINT a is greater
ELSE
PRINT b is greater
ENDIF
5. Keep statements language independent.
Pesudocode must never written or use any syntax of any programming
language.
Advantages of Pseudocode
Can be done easily on a word processor
Easily modified
Implements structured concepts well
It can be written easily
It can be read and understood easily
Converting pseudocode to programming language is easy
as compared with flowchart
Disadvantages of Pseudocode
It is not visual
There is no standardized style or format