GE8151-PROBLEM SOLVING AND
PYTHON PROGRAMMING
INTRODUCTION TO COMPUTER
A computer is a machine that manipulates data
according to a set of instructions.
Computer is fast operating electronic device that
receives data (input), processes the data, stores data,
and produces resultant data (output).
OVERVIEW OF COMPUTER
Computers are the Combination of two things. They
are
Hardware
Software
HARDWARE
Computer hardware is a visible components of the
Computer that enables the Computer System to
function properly.
Examples- CPU, Monitor, keyboard, Mouse., etc
COMPONENTS OF A COMPUTER
SOFTWARE
A set of programs that are executed sequentially is
called software.
Software is generic term for organized collection of
computer data and instruction.
TYPES OF SOFTWARE
Software's can be classified into two types:
They are
(i) System Software
(ii) Application Software.
BASIC PROCESS OF COMPUTER
CHARACTERISTICS OF A COMPUTER
i) Speed
ii) Accuracy
iii) Storage
iv) Versatility
v) Automation
vi) Resource sharing
vii) High Reliability
viii) Reduction in working cost and man power
APPLICATIONS OF COMPUTERS
i) Education
ii) Online Education
iii) Business Marketing
iv) Banking Sector
v) Video Games
vi) Training
vii) Hospitals
viii) Service Sector
ix) Science
UNIT-1
ALGORITHMIC PROBLEM SOLVING
ALGORITHM
Algorithm is defined as a step by step procedure for
solving any problem.
An algorithm is a sequence of finite instructions, often
used for calculation and data processing.
CHARACTERISTIC OF ALGORITHM
Algorithm has a finite number of inputs.
Every instruction should be precise and
unambiguous.
Ensure that the algorithm has proper termination.
Effectiveness of each step is very important.
Algorithm should be written in sequences
Desired output must be obtained only after the
algorithm terminates.
QUALITIES OF AN ALGORITHM
Accuracy
Memory
Time
Sequence
REPRESENTATION OF ALGORITHM
Algorithm has a starting point and a final point.
Between these two points are the instructions that
solve the problem.
Algorithms often have steps that repeat or require
decisions (such as logic or comparison).
Example: Area of the circle
Start the algorithm
Read the value of radius r
Calculate Area= 3.14*r*r
Print the area of the circle
Stop
Convert the temperature from fahrenheit to
celsius
formula: (fahrenheit-32)*(5/9)
Circumference of circle
formula: 2*3.14*r
BUILDING BLOCKS OF ALGORITHM
(STATEMENTS, STATE, CONTROL FLOW,
FUNCTIONS)
STATEMENTS
Statement is an instruction written in high level
language that command the computer to performed a
specific action.
A program written in such a language is formed by a
sequence of one or more statements.
A statement may have internal components (e.g.,
expressions).
KINDS OF STATEMENTS
Simple statements
Example: assignment:
A:= A + 5
Compound statements
Example: block: begin ------- end
do-loop: do -------- while (i < 10);
if statement: if (condition)
statements
STATE
An algorithm is deterministic automaton for
accomplishing a goal which, given an initial state,
will terminate in a defined end-state.
CONTROL FLOW
It defined as the program statements that specifies the
order in which statements are executed.
Flow of control (or) control flow (when referring to
computer programming) is the order function calls,
instructions, and statements are executed or evaluated
when a program is running.
Sequence Control Structure
Selection Control Structures
IF..THEN Structures
IF..THEN...ELSE structure
Case Structure
Sequence Control Structure
Selection Control Structures
IF..THEN Structures
IF..THEN...ELSE structure
Case Structure
FUNCTIONS
Functions are “self contained” modules of code that
accomplish a specific task.
Functions usually “take in” data, process it, and
“return” a result.
Functions can be “called” from the inside of other
functions.
Advantage
Reduction in code redundancy
Code Reuse
Better readability
Improved maintainability
Improved debugging and testing
FUNCTION WORKSPACE
Every function has its own Workspace.
This means that every variable inside the function is
only usable during the execution of the function (and
then the variables go away).
NOTATION
(PSEUDO CODE, FLOW CHART,
PROGRAMMING LANGUAGE)
PSEUDOCODES
Pseudocode is a kind of structure English for
designing algorithm.
Pseudocode cannot be compiled nor executed, and
there are no real formatting or syntax rules.
Example
A pseudocode to add two numbers and display the
results:
READ num1, num2
result = num1 + num2
WRITE result.
BASIC GUIDELINES FOR WRITING
PSEUDOCODE
Write only one Statement per Line
Capitalize initial keyword
Indent to show hierarchy
End multiline structures
Keep statements language independent
ADVANTAGES OF PSEUDOCODE
It can be done easily on a word processor.
Easily modified.
Implements structured concepts well.
It is simple because it uses English-like statements.
No special symbols are used.
DISADVANTAGES OF PSEUDOCODE
It’s not visual.
There is no accepted standard, so it varies widely
from company to company.
Cannot be compiled not executed.
FLOWCHART
A flow chart is a diagrammatic representation, that
illustrates the sequence of operations to be performed
to arrive at the solution.
The flow chart symbols are linked together with
arrows showing the flow direction of the process.
FLOWCHARTS SYMBOLS
ADVANTAGES OF FLOWCHARTS
Communication
Effective analysis
Proper documentation
Efficient Coding
Proper Testing & Debugging
Efficient Program Maintenance
DISADVANTAGES OF FLOWCHARTS
Complex logic
Alterations and Modifications
No Update
PROGRAMMING LANGUAGES
A computer is the ideal machine to execute
computational algorithms .
The computer can perform arithmetic operations
It can also perform an operation only when some
condition is satisfied (using the conditional branch
instruction)
TYPES OF LANGUAGES USED IN
COMPUTER PROGRAMMING
Machine language
Assembly language or low level
programming language
High level programming language
SOME WELL KNOWN PROGRAMMING
LANGUAGES
ALGORITHMIC PROBLEM
SOLVING
ALGORITHMIC PROBLEM SOLVING
Algorithms are procedural solutions to problems.
These solutions are not answers but specific
instructions for getting answers.
ALGORITHMIC PROBLEM SOLVING
STEPS FOR DESIGNING AND ANALYZING AN
ALGORITHM
Understanding the Problem
Ascertaining the Capabilities of the Computational
Device
Choosing between Exact and Approximate Problem
Solving
Deciding on appropriate Data Structures
Algorithm Design Techniques
Methods of Specifying an Algorithm
Proving an Algorithm’s Correctness
Analyzing an Algorithm
EXAMPLES
Sum of two numbers
Algorithm:
Step 1 : Start
Step 2 : Input the value of A and B.
Step 3 : Find the sum of A and B. sum=A+B
Step 4 : Print the value of sum
Step 5 : Stop.
SUM OF TWO NUMBERS
Find the area and circumference of circle.
Algorithm
Step 1 : Start
Step 2 : Input the radius of the circle
Step 3 : Find the area and circumference using the
formula
Area = 3.14 * r *r
Circumference = 2*3.14*r
Step 4 : Print the area and circumference of the circle.
Step 5 : Stop
FIND THE AREA AND CIRCUMFERENCE OF CIRCLE.
SIMPLE STRATEGIES FOR
DEVELOPING ALGORITHMS
(ITERATION, RECURSION)
SIMPLE STRATEGIES FOR
DEVELOPING ALGORITHMS
Algorithms are used to manipulate the data for a given
problem.
For complex problem its algorithm is often divided into
smaller units called modules.
Two approaches to design an algorithm
Top down approach
Bottom up approach
Top down approach
A top down approach starts by dividing the complex
algorithm into one or more modules.
Top down design begins with top most module and
incrementally add modules that it calls.
Bottom up approach
In Bottom up design, start designing the most basic
bottom modules and then proceed towards designing
higher level modules.
Iteration
Iteration that involves executing one or more steps for
a number of times until some condition is true.
It can be implemented using constructs such as while,
do while, and for loop.
Example
Step 1: [initialise] set I=1,N=10
Step 2: Repeat step 3&4 while I<=N
Step 3: Print I
Step 4: set I=I+1
Step 5: end
Recursion
Recursion is a technique of solving a problem by
breaking it down into smaller and smaller sub
problems.
This breaking process will continue until you get a
small enough problem that can be easily solved.
Example
Step 1: start
Step 2: input number as n
Step 3:call factorial(n)
Step 4: stop
User defined function
Step 1: set f=1
Step 2 :if n==1 then return 1
else set f=n*factorial(n-1)
Step 3: Print f
Illustrative problems
Find minimum in a list
To find a minimum value into an array of items, take
the first element and compare its value against value of
other elements. Finally we find the minimum
Algorithm
Step 1: start
Step 2: declare and read elements of a list.
Step 3: declare and set a variable min as first
element of the list.
Step 4: traverse the list index(i) from 1 st
position to n-1st position.
Step 5: if list[i]<min then
min = list[i]
Step 6: Repeat the step until I becomes n.
Step 7: display min is the smaller number,
Step 8: stop
Insert a card in a list of sorted cards
To insert a card in the sorted card, we must increase the
list size with 1.
we can insert a new card in the appropriate position by
comparing each element value
with the new card.
When the position is found we have to move the
remaining elements by one position up and the card can
be inserted
Linear search
Linear search is a very simple search
algorithm. In this type of search, a
sequential search is made over all items
one by one.
Every item is checked and if a match is
found then that particular item is returned,
otherwise the search continues till the end
of the data collection.
Linear Search Example
Algorithm
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step
8
Step 7: Print element not found
Step 8: Exit
Pseudocode
procedure linear_search (list, value)
for each item in the list
if match item == value
return the item's location
end if
end for
end procedure
Binary search
Binary search is a fast search algorithm with run-time complexity of Ο(log
n).
This search algorithm works on the principle of divide and conquer. For this
algorithm to work properly, the data collection should be in the sorted form.
Binary search looks for a particular item by comparing the middle most
item of the collection.
If a match occurs, then the index of item is returned.
If the middle item is greater than the item, then the item is searched in the
sub-array to the left of the middle item.
Otherwise, the item is searched for in the sub-array to the right of the
middle item.
This process continues on the sub-array as well until the size of the
subarray reduces to zero.
Binary Search Example
The following is our sorted array and let us
assume that we need to search the location
of value 31 using binary search.
Step-1
First, we shall determine half of the array by using this
formula −
mid = low + (high - low) / 2
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5).
So, 4 is the mid of the array.
Step-2
Now we compare the value stored at location 4, with the value
being searched, i.e. 31.
We find that the value at location 4 is 27, which is not a match.
As the value is greater than 27 and we have a sorted array, so
we also know that the target value must be in the upper
portion of the array.
Step-3
We change our low to mid + 1 and find the new mid
value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored
at location 7 with our target value 31.
Step-4
The value stored at location 7 is not a match, rather it
is more than what we are looking for. So, the value
must be in the lower part from this location.
Step-5
Hence, we calculate the mid again. This time it is 5.
Step-6
We compare the value stored at location 5 with our
target value. We find that it is a match.
Step-7
We conclude that the target value 31 is stored at
location 5.
Binary search halves the searchable items and thus
reduces the count of comparisons to be made to
very less numbers.
Pseudo Code (Binary Search)
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
Assignment
To guess an integer number in a range
Towers of hanoi
TOWERS OF HANOI
RULES
The mission is to move all the disks to some another
tower without violating the sequence of arrangement.
A few rules to be followed for Tower of Hanoi are −
Only one disk can be moved among the towers at
any given time.
Only the "top" disk can be removed.
No large disk can sit over a small disk.
So now, we are in a position to design an
algorithm for Tower of Hanoi with more than
two disks. We divide the stack of disks in two
parts. The largest disk (nth disk) is in one part
and all other (n-1) disks are in the second
part.
Our ultimate aim is to move disk n from
source to destination and then put all other
(n1) disks onto it. We can imagine to apply
the same in a recursive way for all given set
of disks.
The steps to follow are −
Step 1 − Move n-1 disks from source to
aux
Step 2 − Move nth disk from source to
dest
Step 3 − Move n-1 disks from aux to dest
A recursive algorithm for Tower of Hanoi
can be driven as follows −
START
Procedure Hanoi(disk, source, dest, aux)
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
END Procedure
STOP