Professional Documents
Culture Documents
Information Technology 2
Meeting #5
Block 1 (Part 4)
Patterns, algorithms and programs 1
OU Materials, PPT prepared by Dr. Khaled Suwais
• Introduction
• 4.1 Calculate
• 4.2 Document and test
• Lists
• Summary
2
Introduction
• We will study several types of problems and the
corresponding patterns that you can use to solve them.
• A pattern will be a template to be filled in to obtain a
concrete algorithm.
• When given a problem, if you recognise the type of the
problem, you can then use the corresponding pattern to
create a concrete algorithm for the given problem. That is
much easier and faster than having to design an algorithm
from scratch.
3
Introduction
• The problem-solving process you saw in Part 2 becomes more
structured, with intermediate steps to go from problem to
algorithm, illustrated in this Figure.
4
4.1 Calculate
• Throughout this part, we will show you various kinds of
numeric problems,
• i.e. where the inputs and outputs are numbers.
5
4.1.1 Numeric expressions
7
4.1.1 Numeric expressions
• Python also provides the remainder (modulus) operator,
confusingly written as the percentage sign (%).
• The % operator calculates the remainder of the floor
division of the first number by the second,
• For example, 7%2 (read as “7 modulo 2”) is 1 because 2 fits 3 times
in 7 (3 * 2 = 6) and 1 remains (7 – 6 = 1).
8
4.1.2 Formula problems: pattern
Problem 4.1 Brick volume
9
4.1.2 Formula problems: pattern
Problem 4.1 Brick volume
Decompose the problem
12
4.1.2 Formula problems: pattern
4.1.3 Formula problems: algorithm
13
4.1.2 Formula problems: pattern
4.1.3 Formula problems: algorithm
16
4.2 Document and test
• For complicated or less familiar problems, the reader can struggle
to understand what’s going on in the code, even with descriptive
variable names.
17
4.2 Document and test
• No matter how simple your program is, you should get
into the habit of testing it to catch any errors.
18
4.2 Document and test
• You only have to test your program for admissible input
values and document in the code which values are not.
• If the user inputs an inadmissible value, it’s their fault, and
all bets are off: your program may crash, output a
nonsensical value – or, even worse, output a value that
seems correct.
• For numeric problems, some of the typical conditions for
an input value to be admissible are: the value is an integer;
positive; negative; non-zero; a multiple of some number n;
a percentage (i.e. a floating-point number from 0 to 1 or
an integer from 0 to 100); larger than another input; etc.
19
How to solve computational problems-
Summary
As a conclusion, we briefly summarize a process to solve
computational problems:
20
Lists
• There are a number of things which work the same way for lists as for
strings.
• len(): where you can use len(L) to know the number of items in
a list L.
• in: operator which tells you if a list contains something.
• Examples:
if 2 in L:
print('Your list contains number 2.')
if 0 not in L:
print('Your list has no zeroes.')
22
Lists
• A list is a data structure in Python that is a mutable
(or changeable), ordered sequence of elements.
23
Lists
• To access the items of the list inside a list, you have to use double squared
brackets.
24
Lists
25
Lists
In Python, for loop is used to iterate over the items of any sequence
including the Python list, string, tuple etc. The for loop is also used to access
elements from a container (for example list, string, tuple) using built-in
function range().
Both of the following examples print out the items of a list, one-by-one, on
separate lines.
26
Lists
27
Lists
28
Lists
More operations on lists:
• Concatenation:
>>> L1 = [0,1,2]; L2 = [3,4,5]
>>> L1+L2
[0,1,2,3,4,5]
• Repetition:
>>> L1*3
[0,1,2,0,1,2,0,1,2]
• Appending:
>>> L1.append(10)
[0,1,2,10]
• Sorting:
>>> L3 = [2,1,4,3]
>>> L3.sort()
[1,2,3,4] 29
Lists
• Reversal:
>>> L4 = [4,3,2,1]
>>> L4.reverse()
>>> L4
[1,2,3,4]
• Shrinking:
>>> del L4[0] # L4 will be [2,3,4]
>>> L4 = [] # L4 will be []
• Index and slice assignment:
>>> L1 = [10,20,30,40]
>>> L1[1] = 0 # L1 will be [10,0,30,40]
>>> L1[1:4] = [4,5,6] # L1 will be [10,4,5,6]
• Making a list of integers:
>>> list(range(4))
[0,1,2,3]
>>> list(range(1,5))
[1,2,3,4]
30
Example- List of lists
• Suppose we have the following list:
List=[['Ali', 5, 10,15],['Naji',12,12,15],['Fadi',10,14,12],['Rajaa',18,16,14]]
- Print the average of the second grade for all students.
- Print the name of the student and his/her average grade
OR
31
Summary
In this part, you learned further techniques to solve
computational problems by:
• looking in the problem statement for the input(s) and the
output(s)
• thinking about which inputs are not allowed, e.g. negative
numbers
• writing tests (pairs of admissible inputs and their
expected outputs)
• recognising the type of the problem or the types of the
sub-problems
• instantiating the patterns for those problem types
• combining the algorithms for the sub-problems to solve
the whole problem. 32