Algo 2
Algo 2
Lecture 2
Cyrille Rosset
[email protected]
Université Paris Cité
7 November 2022
1 Variables
1 Variables
Example in Python
>>> a_number = 10
>>> type(a_number)
int
>>> greatings = 'Hello, World!'
>>> type(greatings)
str
1 Variables
Definition
Similar to function in mathematics
A function takes arguments as input, does some computations and returns a
value.
Example in Python
def f(x, y):
z = x**2 + y**2
return z
>>> result = f(2, 3)
>>> result
13
>>>
Definition
A procedure executes some statements but does not return any values
It can be seen as a function which returns nothing
In Python, there is no difference between a procedure and a function
Example in Python
def print_square(x):
print 'The square of', x, 'is', x**2
>>> print_square(4)
The square of 4 is 16
>>> x = print_square(3
The square of 3 is 9
>>> x
>>> print x
None
1 Variables
Pseudo-code
In some books you will see algorithms describe in pseudo-code.
Pseudo-code is a kind of language to describe algorithm, but it is not actually a
programming language.
In this lecture, we will use mostly Python to describe algorithm, adding comments
when necessary.
We can do that because Python syntax is very close to pseudo-code you could
find in books.
1 Variables
1 Variables
Conditions
The conditions can use:
comparison operators: ==, !=, <, >, <=, >=
boolean operators: and, or, not
Example
# if a number is even and positive, divide it by 2
if x > 0 and x % 2 == 0:
x = x / 2
1 Variables
Iterations
A for loop is a way to iterate over successive numbers.
In Python, the for loop allows looping over elemnets of a list.
The function range creates a list of numbers.
Notes
range(5) goes from 0 to 4!
range(5, 10) to go from 5 to 9
range(1, 10, 2) to get odd numbers 1, 3, 5, 7, 9.
Definition
A while loop will repeat a block of instructions as long as the given condition is
True.
Notes
Use a while loop when you don’t know a priori the number of iterations.
Definition
break Allows to stop a loop before the normal end of the loop
continue Allows to go directly to the next iteration
1 Variables
Given two integers, subtract the smaller to the bigger and repeat until the two
numbers are equal. The obtained value is the greatest common divisor.
Exercise
Write the algorithm using what we have learnt so far.
1 Variables
Definition
A data structure is defined by the operations which can be performed on it.
It introduces the notion of abstract data types (as opposed to an implementation
through a programming language).
Examples
We will study the following data structures: array, list, stack and tree.
1 Variables
Definition
An array a is defined by:
a fixed number of data items (N)
all data items have the same type
all data items are stored contiguously in memory
any data items can be accessed through an index i: a[i]
Example
In Python, there is no array by default (a list is used instead).
The package numpy provides this data type.
import numpy as np
gives:
a = np.array([0, 1, 2, 3, 4]) [0 1 2 3 4]
print a [ 0 1 999 3 4]
a[2] = 999
print a
Prime numbers
The Sieve of Eratosthenes is an algorithm to find all prime numbers up to some
given number N.
This is done by marking as non-prime the multiples of each prime, starting with
the multiples of 2.
Exercise
Write the algorithm using an array of boolean values called isprime, which, in
the end, will be True at the prime number indices (e.g. isprime[7] will be True,
but isprime[6] will be False).
1 Variables
Definition
A linked list contains multiple items (as an array), but they have the advantage
they can grow or shrink easily.
The data is organized in nodes which:
contain a data item
contain a pointer (or a link) to the next value (the arrow on the picture)
A L I S T
Creation
An empty list will have a head linking to a tail.
These are special nodes: the head contains no item, and the tail will point to
itself.
head tail
Insertion
X
head A L I S T tail
Deletion
head A L I X S T tail
Exercise
Assuming a node can be described with variables node.item and node.link,
write in details the algorithms for the functions to manipulate a list.
Definition
A stack is a data structure which piles up data items.
Operations on stack:
Push put data on the stack
Pop pop the topmost data off the stack
C C
C C
B B
A A
1 Variables
Memory
The array use exactly the memory taken by the data
The list use additionally a link for each item: if the items are small, you may
need twice the memory your data needs.
Speed
List: inserting and removing elements is easy and fast with list. For arrays,
you need to create a new array. . .
Array: accessing an element at any index i is fast (while you need to go
through all item and follow the links until you find your element with lists)