Modularisation Damian Gordon. Modularisation Let’s imagine we had code as follows:

Slides:



Advertisements
Similar presentations
Session Objectives# 24 COULD code the solution for an algorithm
Advertisements

Tutorial #6 PseudoCode (reprise)
Flow Charting, Structured English and PseudoCode
Flow Charting Damian Gordon. Introduction We mentioned it already, that if we thing of an analyst as being analogous to an architect, and a developer.
Unit 171 Algorithms and Problem Solving  Introduction  Algorithm Design  Algorithm Properties  Algorithm Control Flow  Examples  Comparing Algorithms.
1.9 Methods academy.zariba.com 1. Lecture Content 1.What is a method? Why use methods? 2.Void Methods and methods with parameters 3.Methods which return.
Python quick start guide
Programming Training Main Points: - Working with Functions in Python
PYTHON PROGRAMMING Week 10 – Wednesday. TERMS – CHAPTER 1 Write down definitions for these terms:  Computation  Computability  Computing  Artificial.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Iteration: WHILE Loop Damian Gordon. WHILE Loop Consider the problem of searching for an entry in a phone book with only SELECTION:
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Searching Damian Gordon. Google PageRank Damian Gordon.
1 What We Will Learn  Introduction  Passing input parameters  Producing output  Scope of variables  Storage Class of variables  Function usage example.
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Data Structures: Arrays Damian Gordon. Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each.
Python: Arrays Damian Gordon. Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Introduction to Python Damian Gordon. The Python Programming Language Python was developed by Guido van Rossum in the Netherlands in Van Rossum.
Programming for GCSE 1.0 Beginning with Python T eaching L ondon C omputing Margaret Derrington KCL Easter 2014.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Recursion Damian Gordon. Recursion Recursion: Factorial Recursion is a feature of modularisation, when a module can call itself to complete a solution.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Recursion Damian Gordon. Recursion Factorial Fibonacci Decimal to Binary conversion Travelling Salesman Problem Knight’s Tour.
Introduction to Python Damian Gordon e:
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
Iteration: FOR, DO, LOOP Loop Damian Gordon. FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a.
Introduction to Python
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Recall: d is a divisor of n  n % d == 0
Pseudocode (Revision)
Functions and Procedures
Linked Lists Damian Gordon.
CMPT 201 Functions.
While Loops in Python.
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Coding Concepts (Sub- Programs)
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Queues: Implemented using Arrays
Topic 1: Problem Solving
Loops CIS 40 – Introduction to Programming in Python
Python: Algorithms Damian Gordon.
Programming Training Main Points: - Working with Functions in Python
Stacks: Implemented using Linked Lists
Problem Solving Designing Algorithms.
Some Common Issues: Iteration
The structure of programming
Structured Programming
Circular Queues: Implemented using Arrays
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
National Central University, Taiwan
COMPUTER PROGRAMMING SKILLS
Some Common Issues: Common Algorithms
Modules Programming Guides.
Queues: Implemented using Linked Lists
Inductive Reasoning.
COMPUTING.
Presentation transcript:

Modularisation Damian Gordon

Modularisation Let’s imagine we had code as follows:

Modularisation # Python program to mail merger # Names are in the file names.txt # Body of the mail is in body.txt # open names.txt for reading with open("names.txt",'r',encoding = 'utf-8') as names_file: # open body.txt for reading with open("body.txt",'r',encoding = 'utf-8') as body_file: # read entire content of the body body = body_file.read() # iterate over names for name in names_file: mail = "Hello "+name+body # write the mails to individual files with open(name.strip()+".txt",'w',encoding = 'utf-8') as mail_file: mail_file.write(mail) # Python program to mail merger # Names are in the file names.txt # Body of the mail is in body.txt # open names.txt for reading with open("names.txt",'r',encoding = 'utf-8') as names_file: # open body.txt for reading with open("body.txt",'r',encoding = 'utf-8') as body_file: # read entire content of the body body = body_file.read() # iterate over names for name in names_file: mail = "Hello "+name+body # write the mails to individual files with open(name.strip()+".txt",'w',encoding = 'utf-8') as mail_file: mail_file.write(mail)

Modularisation And some bits of the code are repeated a few times

Modularisation # Python program to mail merger # Names are in the file names.txt # Body of the mail is in body.txt # open names.txt for reading with open("names.txt",'r',encoding = 'utf-8') as names_file: # open body.txt for reading with open("body.txt",'r',encoding = 'utf-8') as body_file: # read entire content of the body body = body_file.read() # iterate over names for name in names_file: mail = "Hello "+name+body # write the mails to individual files with open(name.strip()+".txt",'w',encoding = 'utf-8') as mail_file: mail_file.write(mail) # Python program to mail merger # Names are in the file names.txt # Body of the mail is in body.txt # open names.txt for reading with open("names.txt",'r',encoding = 'utf-8') as names_file: # open body.txt for reading with open("body.txt",'r',encoding = 'utf-8') as body_file: # read entire content of the body body = body_file.read() # iterate over names for name in names_file: mail = "Hello "+name+body # write the mails to individual files with open(name.strip()+".txt",'w',encoding = 'utf-8') as mail_file: mail_file.write(mail)

Modularisation # Python program to mail merger # Names are in the file names.txt # Body of the mail is in body.txt # open names.txt for reading with open("names.txt",'r',encoding = 'utf-8') as names_file: # open body.txt for reading with open("body.txt",'r',encoding = 'utf-8') as body_file: # read entire content of the body body = body_file.read() # iterate over names for name in names_file: mail = "Hello "+name+body # write the mails to individual files with open(name.strip()+".txt",'w',encoding = 'utf-8') as mail_file: mail_file.write(mail) # Python program to mail merger # Names are in the file names.txt # Body of the mail is in body.txt # open names.txt for reading with open("names.txt",'r',encoding = 'utf-8') as names_file: # open body.txt for reading with open("body.txt",'r',encoding = 'utf-8') as body_file: # read entire content of the body body = body_file.read() # iterate over names for name in names_file: mail = "Hello "+name+body # write the mails to individual files with open(name.strip()+".txt",'w',encoding = 'utf-8') as mail_file: mail_file.write(mail)

Modularisation It would be good if there was some way we could wrap up frequently used commands into a single package, and instead of having to rewrite the same code over and over again, we could just call the package name. We can call these packages methods or functions (or subroutines or procedures)

Modularisation Let’s revisit our prime number algorithm again:

Modularisation PROGRAM CheckPrime: Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; IF (IsPrime = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”; ENDIF; END.

Modularisation There’s two parts to the program:

Modularisation PROGRAM CheckPrime: Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; IF (IsPrime = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”; ENDIF; END.

Modularisation The first part checks if it’s prime… The second part just prints out the result…

Modularisation So we can create a module from the checking bit:

Modularisation MODULE PrimeChecker: Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END.

Modularisation MODULE PrimeChecker: Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END.

Modularisation Let’s remind ourselves of what the algorithm was initially.

Modularisation PROGRAM CheckPrime: Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; IF (IsPrime = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”; ENDIF; END.

Modularisation Now that we have a module to do the check we can rewrite as follows:

Modularisation PROGRAM CheckPrime: IF (PrimeChecker = FALSE) THEN Print “Not Prime”; ELSE Print “Prime”; ENDIF; END.

Modularisation MODULE PrimeChecker: Read A; B <- A - 1; IsPrime <- TRUE; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime <- FALSE; ENDIF; B <- B – 1; ENDWHILE; RETURN IsPrime; END.

Modularisation Modularisation make life easier for a lot of reasons: – It easier for someone else to understand how the code works – It makes team programming a lot easier, different programmers can work on different methods – Can improve the quality of the code – Can reuse the same code over and over again (“don’t reinvent the wheel”).

Modularisation If we were writing programs about primes, it would be useful to have a pre-packaged prime test, e.g. If we were writing a program to explore Goldbach's conjecture, that all even integers are sums of two primes, if would be useful. Also if we were exploring twin primes, which are prime numbers that has a gap of two, (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73), (101, 103), (107, 109), (137, 139), it would be useful.

etc.