Unit-1 Python Notes PDF
Unit-1 Python Notes PDF
SYLLABUS
Python interpreter and interactive mode, debugging; values and types: int, float, boolean, string , and list;
variables, expressions, statements, tuple assignment, precedence of operators, comments; Illustrative
programs: exchange the values of two variables, circulate the values of n variables, distance between two
points.
Conditionals:Boolean values and operators, conditional (if), alternative (if-else),chained conditional (if-
elif-else);Iteration: state, while, for, break, continue, pass; Fruitful functions: return values,parameters,
local and global scope, function composition, recursion; Strings: string slices,immutability, string
functions and methods, string module; Lists as arrays. Illustrative programs: square root, gcd,
exponentiation, sum an array of numbers, linear search, binary search.
Lists: list operations, list slices, list methods, list loop, mutability, aliasing, cloning lists, list parameters;
Tuples: tuple assignment, tuple as return value; Dictionaries: operations and methods; advanced list
processing - list comprehension; Illustrative programs: simple sorting, histogram, Students marks
statement, Retail bill preparation.
Files and exceptions: text files, reading and writing files, format operator; command line arguments, errors
and exceptions, handling exceptions, modules, packages; Illustrative programs: word count, copy file,
Voter’s age validation, Marks range validation (0-100). TOTAL : 45 PERIODS
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
2
I. FUNDAMENTALS OF COMPUTING
COMPUTER:
Computer is an electronic device that accepts data as input, processes the input data by performing
mathematical and logical operations on it, and gives the desired output.
Characteristics Of Computers
• Speed
• Accuracy
• Diligence
• Storage capability
• Versatility
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
3
Speed:
• Speed is measured as number of instructions executed per second.
• The computer can process data very fast, at the rate of millions of instructions per second.
• Speed can be measured by milli seconds/ micro seconds/ nano seconds/ pico seconds.
Accuracy:
• Computer provides a high degree of accuracy.
• For example, the computer can accurately give the result of a division of any two numbers up to 10
decimal places.
Deligence:
• When used for a longer period of time, the computer does not get tired or lack of concentration.
• It can perform long and complex calculations with the same speed and accuracy from the start till
the end.
Storage capability:
• Large volumes of data and information can be stored in the computer and also retrieved whenever
required.
• A limited amount of data can be stored, temporarily, in the primary (main) memory.
• Secondary storage devices like floppy disk and compact disk can store a large amount of data
permanently.
Versatility:
• Computer is versatile in nature.
• It can perform different types of tasks with the same ease.
GENERATIONS OF COMPUTERS
• The computer has evolved from a large-sized simple calculating machine to a simple but much more
powerful machine. The evolution of computer to the current state is defined in terms of the generations
of the computers.
• Currently there are five generations of the computers.
• Generations are categorized on the basis of
1. Technology used (hardware and software).
2. Computing characteristics (speed).
3. Physical appearance (size).
4. Applications (Uses).
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
4
➢ Machine language uses 0s and 1s for coding of the instructions.
3. Computing characteristics:
➢ Computation time was in milliseconds(10-3Sec).
4. Physical appearance:
➢ Computers were enormous in size and required a large room for installation.
5. Applications:
➢ For scientific applications
6. Examples:
➢ Universal automatic computer(UNIVAC)
➢ Electronic numeric integrator and computer (ENIAC)
➢ Electronic discrete variable automatic computer(EDVAC)
Limitations:
➢ Used a large number of vacuum tubes and thus generated a lot of heat.
➢ They consumed a great deal of electricity.
➢ Expensive to operate.
➢ Required constant maintenance.
➢ Since it used machine language, they were difficult to program.
➢ One program at a time.
Advantages:
➢ Use less power and heat than second generation.
➢ Cost of the computer is reduced.
➢ Less maintenance cost.
4. Physical appearance:
➢ Smaller than computers in previous generation.
5. Applications:
➢ Widely available for commercial purpose.
➢ PC became available for home user.
Advantages:
➢ Computers are smaller and cheaper.
➢ Portable and reliable.
➢ Generates much lesser heat and less maintenance.
➢ Easy to use and learn.
➢ Networking has resulted in resource sharing and easy communication.
Fifth Generation (Present and next) – using Artificial Intelligence.
1. Hardware Technology:
➢ To develop computers those are capable of learning and self-organization.
➢ They use Super Large Scale Integrated Chips (SLSI) to store millions of components in a
single chip.
➢ It also uses parallel processing that allows several instructions to be executed in parallel.
➢ It consists of large memory.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
7
2. Software Technology:
➢ Fifth generation computers based on Artificial Intelligence (AI)
➢ They try to simulate the human way of thinking and reasoning.
➢ It includes areas likeExpert system (ES), Natural Language Processing (NLP), Speech
recognition, voice recognition, robotics etc.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
8
Examples UNIVAC, PDP-8, IBM 370, Intel 4004,
ENIAC, IBM 1401, PDP 11 IBM, Apple’s
EDVAC CDC 1604 Macintosh
Computer is an electronic device that accepts data as input, processes the input data by performing
mathematical and logical operations on it, and gives the desired output.
Input-Process-Output Concept:
A computer is an electronic device that (1) accepts data (2) processes data (3) generates output and
(4) stores data. The concept of generating output information from the input is also referred to as input-
process-output concept.
Components of Computer Hardware:
The computer system hardware comprises of three main components,
1. Input/output unit(I/O Unit)
2. Central Processing unit (CPU Unit)
3. Memory unit
Input/output Unit:
• Users interact with the computer via I/O unit.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
9
• Input unit accepts data from user.
• Some of the input devices are keyboard, trackball, mouse.
• Output unit provides processed data to user.
• Some of the output devices are monitor and printer.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
10
1. Arithmetic Unit
- Performs arithmetic operations on the data
- E.g.Addition, subtraction, multiplication, division.
2. Logic Unit
- Performs logic operations.
- E.g. Greater than, less than or equal to condition.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
11
Memory Unit
• Memory unit stores the data, instructions, intermediate results and output.
• Memory unit consists of three types
1. Cache Memory
2. Primary or Main memory
3. Secondary memory
1. Cache Memory
• Cache memory is a very high speed memory placed in between RAM and CPU.
• Cache memory stores the frequently accessed data; it increases the speed of processing.
• CPU first checks cache for data in the cache, if data is not found in cache, then looks for it in
RAM.
2.Primary Memory:
• Primary memory is the main memory of computer. It is used to store data and instructions
temporarily during the processing of data.
• Also called as Main memory / System memory / Temporary memory / Volatile memory.
• It is a Semiconductor memory.
• Two types:
1. RAM (Random Access Memory)
2. ROM (Read Only Memory)
3.Secondary Memory:
• Secondary memory stores data and instructions permanently. The information can be stored in
secondary memory for a long time and it is permanent in nature.
• Also called as Permanent memory / Non-volatile memory / External memory.
• It provides back-up storage for data and instructions.
• It has high storage capacity than primary memory.
• It is also cheaper than primary memory.
• Examples: Hard disk, Magnetic tapes, Magnetic disks, Floppy disks, Optical disk
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
13
ALGORITHM
Rules
• 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.
Statements
Example:
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
14
• Comment statements are given after // symbol, which is used to tell the purpose of the line.
States
Control Flow
• Control flow which is also stated as flow of control, determines what section of
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
15
• The name suggests the sequential control structure is used to perform the action one after another.
• Only one step is executed once.
• The logic is top to bottom approach.
Example
1. Start
4. Calculate sum=a+b
6. Stop
• Selection flow allows the program to make choice between two alternate paths
Basic structure:
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
16
Example:
1. Start
2. Read a
3. Read b
4. If a>b then
4.1. Print a is
greater else
5. Stop
2. Write an algorithm to find the largest among three different numbers entered by user.
Step 1: Start
Step 2: Declare variables a,b and c. Step 3: Read variables a,b and c.
Step 4: if a>b and if a>c
Display a is the largest number.
Step 5: Else If b>c
Display b is the largest number.
Step 6: Else
Step 7: Stop
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
17
Step 4: Read n from user.
Step 5: Repeat the steps till i<(n/2)
5.1 If n%i==0 equals then flag=0
Go to step 6
5.2 i=i+1
Step 6: If flag=0 Display n is not prime else
Display n is prime
Step 7: Stop
• Repetition control flow means that one or more steps are performed repeatedly
until some condition is reached.
• This logic is used for producing loops in program logic when one more instructions may need
to beexecuted several times or depending on condition.
Basic Structure:
Statements
Iteration
Example 1
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
18
Example 2
1. Start
3. Initialize i =1
4.1. Print i
5. Stop
Function
• A function is a block of organized, reusable code that is used to perform a single, related action.
• Function is also named as methods, sub-routines.
Elements of functions:
3. Formal parameter
Basic Syntax
function_name(parameters)
function statements
end function
1. PSEUDOCODE
• The pseudocode is written in normal English language which cannot be understood by the
computer.
• It uses the basic structure of a normal programming language, but is intended for human
reading ratherthan 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.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
20
Example:
Find the total and average of three subjects
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.
ELSE
PRINT b is greater
ENDIF
5. Keep statements language independent.
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
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
21
Disadvantages of Pseudocode
• It is not visual
• There is no standardized style or format
2. 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.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
22
3. The usual directions of the flow of a procedure or system is from left to right or top top to bottom.
Only oneflow 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.
6. If flowchart becomes complex, it is better to use connector symbols to reduce the number of flow
lines.
7. Ensure that flowchart has logical start and stop
Advantages of
Flowchart
Communication:
• Flowcharts are better way of communicating the logic of the system.
Effective Analysis
• With the help of flowchart, a problem can be analyzed in more effective way.
Proper Documentation
• Flowcharts are used for good program documentation, which is needed for various purposes.
Efficient Coding
• The flowcharts act as a guide or blue print during the system analysis and program development
phase.
Systematic Testing and Debugging
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
23
• The flowchart helps in testing and debugging the program
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.
Disadvantages of Flowchart
Complex Logic:
• Sometimes, the program logic is quite complicated. In that case flowchart becomes complex
anddifficult to use.
Alteration and Modification:
• If alterations are required the flowchart may require redrawing completely.
Reproduction:
• As the flowchart symbols cannot be typed, reproduction becomes problematic.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
24
Conditional Structure
• Conditional structure is used to check the condition. It will be having two outputs only (True or
False)
➢ IF and IF…ELSE are the conditional structures used in Python language.
➢ CASE is the structure used to select multi way selection control. It is not
supported inPython.
IF… ELSE
IF…THEN…ELSE is the structure used to specify, if the condition is true, then execute
Process1, else, that is condition is false then execute Process2
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
25
Example:
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
26
Machine Language:
• Machine language is the lowest-level programming language (except for computers that utilize
programmable microcode). Machine languages are the only languages understood by computers.
It is also called as low level language.
Example code:100110011 111001100
Assembly Language:
• An assembly language contains the same instructions as a machine language, but the instructions
and variables have names instead of being just numbers. An assembler language consists of
mnemonics, mnemonics that corresponds unique machine instruction.
Example code:
startAdd x,y
Sub x,y
High – level Language:
• A high-level language (HLL) is a programming language such as C, FORTRAN, or Pascal that
enables a programmer to write programs that are more or less independent of a particular type of
computer.
• programs written in a high-level language must be translated into machine language by a
compiler or interpreter.
Interpreted Programming Language:
• Interpreter is a program that executes instructions written in a high-level language.
• An interpreter reads the source code one instruction or one line at a time, converts this line into
machinecode and executes it.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
28
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
29
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
30
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.
Learningthese techniques is of utmost importance for the following reasons.
➢ First, they provide guidance for designing algorithms for new problems,
➢ Second, algorithms are the corner stone of computer science.
Methods of Specifying an Algorithm:
1. Algorithms.
2. Flowcharts.
3. Pseudo codes.
4. Programs
➢ Once an algorithm has been specified, you have to prove its correctness.
➢ 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.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
31
IV. SIMPLE STRATEGIES FOR DEVELOPING ALGORITHM
They are two commonly used strategies used in developing algorithm
1. Iteration
2. Recursion
Iteration
• The iteration is when the set of statement repeatedly executes till the controlling condition becomes
false
• The iteration is applied to the set of instructions which we want to get repeatedly executed.
• Iteration includes initialization, condition, and execution of statement within loop and
update (increments and decrements) the control variable.
➢ A sequence of statements is executed until a specified condition is true is called iterations.
1. for loop
2. While loop
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
32
Flowchart:
Recursions:
❖ A function that calls itself is known as recursion.
❖ Recursion is a process by which a function calls itself repeatedly until some specified condition
has been satisfied.
Algorithmfor factorial of n numbers using recursion:
Main function:
Step1: Start
Step2: Get n
Step3: call factorial(n)
Step4: print fact
Step5: Stop
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
33
Sub function factorial(n):
Step1: if(n==1) then fact=1 return fact
Step2: else fact=n*factorial(n-1) and return fact
Flowchart
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
34
V. ILLUSTRATIVE PROBLEMS
1. Find minimum in a list
➢ A user has a list of numbers and wishes to find the minimum value in the list.
➢ Take the first element and compare its value against the values of all other elements.
Once wefind a smaller element we continue the comparisons with its value. Finally we
find the minimum.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
35
Algorithm:
Step 1: Start
Step 2: Read n
Step 3:Initialize i=0
Step 4: If i<n, then goto step 4.1, 4.2 else goto step 5
Pseudocode:
BEGIN READ n
FOR i=0 to n, then READ a[i]
INCREMENT i
END FOR
COMPUTE min=a[0]
FOR i=1 to n, then
IF a[i]<min, then
CALCULATE min=a[i]
INCREMENT i
ELSE INCREMENT i
END IF-ELSE
END FOR
PRINT min
END
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
36
Flowchart:
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
37
Algorithm:
Step 1: Start
Step 2: Read n
Step 3:Initialize i=0
Step 4: If i<n, then goto step 4.1, 4.2 else goto step 5
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
38
Step 7.1: a[i+1]=a[i]
Step 7.2: i=i-1 goto step 7
Step 8: Compute a[i+1]=item
Step 9: Compute n=n+1
Step 10: If i<n, then goto step 10.1, 10.2 else goto step 11
Step10.1: Print a[i] Step10.2: i=i+1 goto step 10
Step 11: Stop
Pseudocode:
BEGIN READ n
FOR i=0 to n,
thenREAD a[i]
INCREMENT i
END FOR
READ item
FOR i=n-1 to 0 and item<a[i], then
CALCULATE a[i+1]=a[i]
DECREMENT i
END FOR
COMPUTE a[i+1]=a[i]
COMPUTE n=n+1
FOR i=0 to n, then
PRINT a[i]
INCREMENT i
END FOR
END
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
39
Flowchart:
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
40
ELSE IF guess<n
ELSE IF guess=n
ELSE
PRINT nope
END
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
41
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
42
4. TOWER OF HANOI
➢ Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one
rings.
➢ Tower of Hanoi is one of the best example for recursive problem solving.
Pre-condition:
➢ These rings are of different sizes and stacked upon in an ascending order, i.e. the
smaller one sits over the larger one.
➢ There are other variations of the puzzle where the number of disks increase, but the tower count
remainsthe same.
Post-condition:
➢ All the disk should be moved to the last pole and placed only in ascending order as shown below.
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.
➢ Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. This presentation
shows thata puzzle with 3 disks has taken 23 - 1 = 7 steps.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
43
Algorithm
➢ To write an algorithm for Tower of Hanoi, first we need to learn how to solve this problem
with lesser amount of disks, say → 1 or 2.
➢ We mark three towers with name, source, aux (only to help moving the disks) and
destination.Input: one disk
➢ If we have only one disk, then it can easily be moved from source to destination peg.
Input: two disks
If we have 2 disks −
• First, we move the smaller (top) disk to aux peg.
• Then, we move the larger (bottom) disk to destination peg.
• And finally, we move the smaller disk from aux to destination peg.
Input: more than two disks
➢ 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
auxStep 2 − Move nth disk from source
to dest Step 3 − Move n-1 disks from
aux to dest
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
44
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)
END IF
END
STOP Procedure
******************************************************************************
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
45
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
46
Draw a flow chart to find whether the given number is leap year or not.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
47
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
48
2 MARKS
1. What is an algorithm?
An algorithm is a finite number of clearly described, unambiguous do able steps that can be
systematically followed to produce a desired results for given input in the given amount of time. In other
word, an algorithm is a step by step procedure to solve a problem with finite number of steps.
2. What is Pseudo code?
Pseudocode is an informal high-level description of the operating principle of a computer
program or
algorithm. Pseudo means false and code refers to instructions written in programming language.
3. What is Problem Solving?
Problem solving is the systematic approach to define the problem and creating number of
solutions. Theproblem solving process starts with the problem specifications and ends with a correct
program.
4. Distinguish between algorithm and program.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
49
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC