Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Class notes Python

Rating
-
Sold
-
Pages
40
Uploaded on
03-04-2024
Written in
2023/2024

This document are very useful and gain more knowledge by this book

Institution
Course

Content preview

UNIT - 1

ALGORITHMIC PROBLEM SOLVING


1.1 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


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

1.1.2 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. Eg: ASCII
character of 0-225.

,1.2 Problem Solving and Python Programming


Output
An algorithm has one or more output.

Effectiveness
An algorithm should be effective that means each of the operation to be preformed 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.

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

1.2 BUILDING BLOCKS OF ALGORITHM
1.2.1 Statements/Instructions
The statements/instructions enable us to manipulate the data items. The instructions in
Python or indeed in any high-level language are designed as components for algorithmic problem
solving rather than as one-to-one translations of the underlying machine language instruction set of
the computer. Thus, they allow the programmer to work at a higher level of abstraction.

Input/output statements make up one type of statement. An input statement collects a
specific value from the user for a variable within the program. An output statement writes a message
or the value of a program variable to the user’s screen. Assignment statement, which assigns a
value to a program variable. This is similar to what an input statement does, except that the value
is not collected directly from the user, but is computed by the program. Control statements, the
third type of statement, affect the order in which instructions are executed. A program executes
one instruction or program statement at a time. Without directions to the contrary, instructions are
executed sequentially from first to last in the program. Control statements direct the flow of control.

1.2.2 State
Stored data are regarded as part of the internal state of the entity performing the algorithm.
The state is stored in one or more data structures. For such computational process, the algorithm must

, Algorithmic Problem Solving 1.3


be rigorously defined and it must be specified in the way that it applies in all possible circumstances.
Data Structure specify the arrangement of data in a particular format.

1.2.3 Control Flow
Control flow (or flow of control) is the order in which individual statements, instructions or
function calls of a program are executed or evaluated. At the level of machine language or assembly
language, control flow instructions usually work by altering the program counter. For some central
processing units (CPUs), control flow instructions available are conditional or unconditional branch
instructions, also termed jumps.

kinds of control flow statements
•• conditional branch
•• loop - the same as conditional branch
•• subroutines, co-routines, and continuations
•• unconditional halt

1.2.4 Function
A function is a sequence of instructions that perform a task, bundled as a unit. Functions
can accept input arguments and produce output values. A function in Python is defined by using the
keyword def, after which the name of the function follows, terminated by a pair of braces (which
may or may not contain input parameters) and, finally, a colon (:) signals the end of the function
definition line. Immediately afterwards, indented by four spaces, we find the body of the function,
which is the set of instructions that the function will execute when called.

Reason for using Function
•• Hide the implementation details from their users.
•• Reduce code duplication in a program.
•• Help in splitting a complex task into smaller blocks, each of which becomes a function.
•• Improve traceability.
•• Improve readability.

1.2.5 Basic building blocks of Algorithm
Three basic building blocks of algorithm are Sequence, Selection, and Iteration.

Building Block Common name Description
Sequence Action Instructions are executed in sequential order (from
first to last).
Selection Decision or A Decision is making a choice among several
Branching actions (condition checking).
Iteration Repetition or Loop A Loop is one or more instructions that the
computer performs repeatedly.

, 1.4 Problem Solving and Python Programming


Sequence Structure
This is the most common form of control structure. Each statement in the program is
executed one after another in the given order. The flow of sequence structure is shown in Figure.1.1.


Statement 1



Statement 2



Statement n



Figure 1.1. Sequence structure.

An algorithm to add two numbers entered by user
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop

Selection Structure
The selection control structure is the presentation of a condition and the choice between two
(or sometimes more) actions. The choice made depends on whether the condition is true or false. It is
also called a decision, one of the three basic logic structures in computer. This structure is sometimes
referred to as an if-then-else because it directs the program to perform in this way: If Condition A
is True then perform Action X else perform Action Y. The flow of sequence structure is shown in
Figure.1.2.


True False




Figure 1.2. Selection structure.

Written for

Course

Document information

Uploaded on
April 3, 2024
Number of pages
40
Written in
2023/2024
Type
Class notes
Professor(s)
Harish
Contains
Problem solving anf python program

Subjects

$10.29
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
h14harish7

Get to know the seller

Seller avatar
h14harish7 Published
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions