Organising information: ordered structures

Communication 1

The GitHub repository of the course now includes also the links to the laboratory lectures, that will be presented by Marilena Daquino

Communication 2

Starting from the previous lecture, Python files to the main listings included in each lecture notes are also published

They are linked in the lecture notes and in the GitHub repository of the course

Any question about the previous lecture?

Historic hero: Donald Knuth

Donald Knuth is a Computer Scientist

Main contributions: theoretical and practical development of the analysis of the computational complexity of algorithms, The Art of Computer Programming series of monographs, TeX typesetting system for writing academic documents

The Art of Computer Programming is an ongoing work (3 volumes and an half out of 7 have been published so far)

Preliminaries: Python functions

Definition of functions:
def <function name>(<param_1>, ...)

It is the mechanism for implementing functions in Python, i.e. a mechanism for listing a sequence of instructions under a particular name

  • Built-in functions: made available by the programming language itself, e.g. len() or list()

  • User-defined functions: written by a user of the language for addressing some specific requirements or tasks that are not addressable by means of one built-in function directly, like the algorithm implemented in the previous lecture

Preliminaries: executions

def add_one(n):
    return n + 1

result = add_one(41)
print(result)

print(<object_1>, <object_2>, ...) allows one to print on screen one or more values (that can be referred by a variable)

Preliminaries: packages

Module: a Python file (extension .py) that contains the definition of variables, functions, and even runnable code

Package: a mechanism to expose one or more Python modules, functions, variables

from <package> import <module or function or variable>

Example:

from collections import deque

Data structures

The first volume of Knuth's series of books is entirely dedicated to a comprehensive introduction of all the basic data structures

A data structure is a way in which we can organise the information to process and returned by a computer, so as it can be accessed and modified in an efficient and computational manner

Broadly speaking, it is a sort of bucket where we can place some information, that provides some methods to add and retrieve pieces of such information

Order and repeatability

Among the simplest data structures, there are those ones that organise their element in a specific order and allow the repeatability of the values they contain

Order: the sequence in which the elements are added to these data structures matters

Repeatability: the same value can appear twice or more times in the same data structure

List: example

List: definition

A list is a countable sequence of ordered and repeatable elements

Countable: it is possible to know the length of the list (i.e. how many elements it contains) – in Python, we can use the function len(<countable_object>)

Ordered: the elements are placed in the list in a specific order, which is preserved

Repeatable: the elements may appear more than one time in the list

List: methods

Create a new list: list()

Add new element: <list>.append(<element>)

Remove element: <list>.remove(<element>)

Add elements from another list: <list>.extend(<another_list>)

Use methods on list

my_first_list = list()
my_first_list.append(34)
my_first_list.append(15)
my_first_list.append("Silvio")
my_first_list.remove(34)
my_first_list.extend(my_first_list)

my_first_list = 34 15 "Silvio" 15 "Silvio"

Stack: example

Stack: definition

A stack is a is a kind of list seen from a particular perspective, i.e. from bottom to top, and with a specific set of operations

The elements follow a last in first out strategy (LIFO) for addition and removal

The last element inserted in the structure is placed in the top of the stack and, thus, it is also the first one that will be removed when requested

For removing the element in the middle of the stack one has to remove all the elements that have been added after such middle element

Stack: methods

Create a new stack: deque()

Add new element: <stack>.append(<element>)

Remove (and return) latest element: <stack>.pop()

Add elements from another stack: <stack>.extend(<another_stack>)

Use methods on stack

my_first_stack = deque()
my_first_stack.append(34)
my_first_stack.append(15)
my_first_stack.extend(my_first_stack)
my_first_stack.append("Silvio")
my_first_stack.pop()

my_first_stack =

"Silvio"
15
34
15
34

Queue: example

Queue: definition

A queue is a is a kind of list seen from a particular perspective, i.e. from left to right, and with a specific set of operations

The elements follow a first in first out strategy (FIFO) for addition and removal

The first element is placed in the left-most part of the queue and, thus, it is also the first one that will be removed when requested

For removing the element in the middle of the queue one has to remove all the elements that have been added before such middle element

Queue: methods

Create a new queue: deque()

Add new element: <queue>.append(<element>)

Remove (and return) first element: <queue>.leftpop()

Add elements from another queue: <queue>.extend(<another_queue>)

Use methods on queue

my_first_queue = deque()
my_first_queue.append(34)
my_first_queue.append(15)
my_first_queue.append("Silvio")
my_first_queue.leftpop()
my_first_queue.extend(my_first_queue)

my_first_queue = 34 15 "Silvio" 15 "Silvio"

END Organising information: ordered structures