You are on page 1of 3

Machine Learning for Engineers (ME698A)

Malay K. Das, 210 Southern Lab, mkdas@iitk.ac.in

Office hours: T 1800-1900, SL-210

Today: Converting pseudocode to a computer program

.in
ac
1
k.
© Malay K. Das; mkdas@ iitk.ac.in
iit
@
as

Recall the Pseudocode for computing the greatest common divisor (gcd) of two non-
kd

negative integers, where at least one integer is nonzero

input: non-negative, at least one nonzero, integers Greatest common divisor


m

of
output: gcd of
where
while
if
swap the values of
We are now ready to code
the pseudocode in python
end while

We will use the jupyter lab tool within the anaconda (or miniconda) environment

2
Improving the pseudocode by creating a function
input: non-negative, at least one nonzero, integers function takes few values as
output: gcd of inputs and delivers one value
as output
function gcd(m,n)
function may be called from
while
other functions
if
swap the values of function improves modularity
of the program. Modern,
end while large programs rely heavily

.in
return on modularity
end function gcd(m,n)

ac
We will now code the pseudocode in python and call it from the main program

3
k.
© Malay K. Das; mkdas@ iitk.ac.in
iit
@
as

Improving the pseudocode by using recursion


kd

input: non-negative, at least one nonzero, integers we wish to run the else block
until the top if block is
output: gcd of satisfied (m = n is reached)
m

function gcd(m,n)
if this is achieved by calling the
return function gcd from the same
else function
if
swap the values of such idea, in programming, is
called recursion
endif
we can now implement the
endif
same in python
return gcd(m,n)
end function gcd(m,n)

4
Summary
Discussed three different ways of writing pseudocode and python program
for finding gcd

Exercise

Write an alternative (other than what we discussed) pseudocode for calculating


greatest common divisor (gcd). Write the python program for the same

.in
Thank You

ac
5
k.
© Malay K. Das; mkdas@ iitk.ac.in
iit
@
as
kd
m

You might also like