Python UNIT 1
Python UNIT 1
Pseudocode
READ name, hoursWorked, payRate
gross = hoursWorked * payRate
WRITE name, hoursWorked, gross
Capitalize Initial Keyword
• In the example below note the words:
Pseudocode
READ name, hoursWorked, payRate
gross = hoursWorked * payRate
WRITE name, hoursWorked, gross
Indent to Show Hierarchy
Each design structure uses a particular indentation pattern
• Loop:
Indent statements that fall inside the loop but not keywords that form
the loop
READ name, grossPay, taxes
IF taxes > 0
net = grossPay – taxes
ELSE
net = grossPay
ENDIF
WRITE name, net
End Multiline Structures
READ name, grossPay, taxes
IF taxes > 0
net = grossPay – taxes
ELSE
net = grossPay
ENDIF
WRITE name, net
Read n,
Initialize i=1
No While
(i<=n)
yes
Print i
i=i+1
Stop
Common keywords
• Input: READ, OBTAIN, GET
• Output: PRINT, DISPLAY, SHOW
• Compute: COMPUTE, CALCULATE, DETERMINE
• Initialize: SET, INIT
• Add one: INCREMENT
Example- repitition structure
Advantages
• To understand logic clearly
• Better Communication
• Effective analysis
• Effective synthesis
• Effective coding
• Proper program documentation
• Systematic Debugging
• Systematic Testing
• Efficient program maintenance
Dis-Advantages/Limitations
• Complex logic
• Alterations and modifications
• Reproduction
• Cost
• Not known
Programming languages
• Computer programming languages are used to
communicate instructions to a computer.
• They are based on certain syntatic and
semantic rules, which define the meaning of
each of the programming language constructs.
Simple strategies for developing algorithms
(Iteration, Recursion)
Read digit,
Initialize
reverse =0
No
While
(digit>0)
yes
Print
reverse
Stop
pseodocode
• Set the variable reverse, digit
• Initialize reverse=0
• READ the value of digit
• WHILE (digit>0) then
reverse =reverse *10
reverse=reverse+digit%10
digit =digit/10
Repeat while until the condition fail
ENDWHILE
• PRINT reverse
• Stop
Iteration:- factorial
step 1. Start
step 2. Read the number n
step 3. [Initialize]
i=1, fact=1
step 4. if or check whether (i<=n) else goto
step 5. fact=fact*i
step 6. i=i+1
step 7: goto step 4
step 8. Print fact
step 9. Stop
PSEUDOCODE
Start
READ the value of n
Initialize i and fact
IF(i<=n) THEN
fact=fact*i
i=i+1
Repeat while until the condition fail
Print fact
END WHILE
Stop
IIUSTRATIVE EXAMPLES
Iteration: minimum number in a list
1. Start the program
2. Read the upper limit array ‘n’
3. Read an array a[] of n numbers.
4. Assign first array value to variable ‘min’ min-
>a[0]
5. For i=1 to n
6. If a[i]<min then min=a[i]
7. Repeat step 5 and 6 until condition fails
8. Print min element
9. stop
start
Read n
for i=0 to n
Read i
Min=a[0]
no
for i=1 to n
yes
If(a[i]< no
min)
yes
Min=a[i]
Print
min
stop
pseudocode
• Set initial i
• READ n
• READ array a[] of n numbers
• Set min=a[0]
• FOR i=0 to n
– IF a[]i]<min
– then min=a[i]
• ENDFOR
• Print min
• stop
Recursion:- factorial
• Step 1: Start
• Step 2: Read number n
• Step 3: Call factorial(n)
• Step 4: Print factorial f
• Step 5: Stop
factorial(n)
• Step 1: If n==1 then return 1
• Step 2: Else f=n*factorial(n-1)
• Step 3: Return f
Flowchart
start Call factorial n
Read n
If(n==1) yes Return 1
Call factorial n no
f=n*factorial(n-1)
Print f
Return f
stop
pseudocode
Start
READ number n
Call factorial(n)
Factorial(n)
IF n==1 THEN return 1
ELSE f=n*factorial(n-1)
RETURN f
ENDIF
WRITE factorial f
Stop
Difference between recursion and
iteration:
PROBLEM SOLVING METHODOLOGY
Definition
Analysis
Design
Coding
Testing
Documentation
Maintanence
ALGORITHMIC PROBLEM SOLVING
STEPS FOR ALGORITHMIC PROBLEM
SOLVING
1.Understanding the Problem
⮚ The problem given should be clearly and completely
understood.
2.Ascertaining the Capabilities of the Computational
Device
⮚ Sequential algorithms
⮚ Parallel algorithms
⮚ The capabilities of the computing device should be
known.
⮚ Speed, Memory availability of the device are noted.
STEPS FOR ALGORITHMIC PROBLEM SOLVING
(Contd)
3. Choosing between Exact and Approximate Problem Solving
⮚ Solving the problem exactly is called an exact algorithm.
⮚ Solving it approximately is called an approximation algorithm.
4.Deciding on appropriate Data Structures
⮚ Data type is a well-defined collection of data with a well
defined set of operations.
⮚ Data structure is basically a group of data elements that are
put together under one name.
Elementary data structures:
List: Allow fast access of data.
Sets: Treats data as element of a set. The operations such as
intersection , union and equivalence.
Dictionaries: Allows data to be stored as a key-value pair.
STEPS FOR ALGORITHMIC PROBLEM
SOLVING (Contd)
5. Algorithm Design Techniques
⮚ An algorithm design technique is a general approach
to solving problems algorithmically that is applicable
to a variety of problems from different areas of
computing.
6. Methods of Specifying an Algorithm
⮚ Free and also a step-by-step form
⮚ Pseudocode.
⮚ Flowcharts.
STEPS FOR ALGORITHMIC PROBLEM
SOLVING (Contd)
7. Proving an Algorithm’s Correctness
⮚ Writing an algorithm is not enough.
⮚ You need to prove that it computes solutions for all the possible
valid inputs.
⮚ This process is called algorithm validation.
⮚ Algorithm validation ensures that the algorithm will work correctly
irrespective of the programming language in which it will be
implemented.
8. Analyzing an Algorithm
⮚ Efficiency is an important characteristic of any algorithm.
There are two kinds of algorithm efficiency:
⮚ Time efficiency, indicating how fast the algorithm runs.
⮚ Space efficiency, indicating how much extra memory it uses.
9. Coding an Algorithm
Inserting a element from a sorted element
Algorithm:
• Step 1: Start the program.
• Step 2: Read a [], n, item, i=n-1
• Step 3:Check whether WHILE (item<a[i] && i>=0) then a
[i+1] = a[i] else go to Step 4
• Step 4: a [i+1] = item
• Step 5:Increment n
• Step 6:For i=1 to n
• Step 7:Write a[i]
• Step 8:Stop the program
Pseudocode
READ a [20], n, item, i.
Get the value of array a [].
Get the value to be inserted in the sorted elements.
i= n-1;
WHILE (item<a[i] && i>=0)
a [i+1] = a[i]
i--
ENDWHILE
a [i+1] = item
n++
WRITE the Insertion Array.
FOR i = 0 to n
WRITE a[i].
ENDFOR
Start
Read a[],item
i=n-1
While(item
<a[i]&&i>0 no a[i+1]=item
) n++
yes
a[i+1]=a[1]
i--
for i=0 to n
write a[i]
Stop
Guess an integer number in a range
Algorithm:
Step 1: Start the program
Step 2: Read an ‘n’ number.
Step 3: Read an Guess number.
Step 4: if Guess > n ; Print “ Your Guess too high”.
Step 5: elseif Guess < n ; Print “ Your Guess too low”.
Step 6: elseif Guess = = n; Print “Good Job”.
Step 7: else print “Nope”.
Step 8:Stop the program.
Pseudocode:
READ an ‘n’ number
READ an Guess number
Guess = 20
IF Guess > n
WRITE “ Your Guess too high”
ELSEIF Guess < n
WRITE “ Your Guess too low”
ElSEIF Guess = = 20
WRITE “Good Job”
ElSE
WRITE “NOPE”
Towers of hanoi
• Rules:
– Only one disk can be moved at a time.
– Each move consists of taking the upper disk from
one of the stacks and placing it on top of another
stack.
– No disk may be placed on top of a smaller disk.
Towers of Hanoi
Algorithm:
Step 1: Start the program
Step 2: procedure Hanoi(disk, source, dest, aux)
Step 3: if disk = = 1
Step 3.1 : Move disk from source to dest
Step 4: else
Step 4.1 : Hanoi(disk -1, source, aux, dest)
Step 4.2 : Move disk from source to dest
Step 4.3: Hanoi(disk – 1, aux, dest, source)
Step 5:endif
Step 6:Stop the program
Towers of Hanoi
Pseudocode:
Procedure Hanoi(disk, source, dest, aux)
IF disk = =1 then
Move disk form source to dest
ElSE Hanoi(disk -1, source, aux, dest)
Move disk from source to dest
Hanoi(disk – 1, aux, dest, source)
ENDIF
END Procedure
start
Hanoi(disk,source,
Read
dest,aux
disk,sourcr,dest,aux
Display result No
Hanoi(disk-
1,source,aux,dest)
Stop
Hanoi(disk-
1,aux,dest,source)
Assignment
• to check whether the given year is leap or not.
• Find the Largest Number Among Three Numbers
• to Check if a given Integer is Odd or Even
• to Check if a given Integer is Positive or Negative
• to Read Two Integers M and N & Swap their Values