2021-06-09 07:05:40 +00:00
|
|
|
class Node:
|
|
|
|
"""La classe ```Node``` sera un noeud (une case, un emplacement, ...) où l'on stockera notre donnée
|
|
|
|
|
|
|
|
Les objets de cette classe ont deux attributs:
|
|
|
|
|
|
|
|
value: qui correspond à la valeur stockée
|
|
|
|
nxt: la référence vers le prochain élément
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, value=None, nxt=None) -> None:
|
|
|
|
"""Initialisation du "node"
|
|
|
|
|
|
|
|
:param value: valeur à stocker dans ce "node"
|
|
|
|
:param nxt: Node du prochain élément
|
|
|
|
|
|
|
|
:consignes: compléter l'initialisation de la classe.
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> n1 = Node(1)
|
|
|
|
>>> assert n1.value == 1
|
|
|
|
>>> assert n1.nxt is None
|
|
|
|
>>> n2 = Node(5, n1)
|
2021-06-09 08:49:28 +00:00
|
|
|
>>> assert n2.value == 5
|
|
|
|
>>> assert n2.nxt == n1
|
2021-06-09 07:05:40 +00:00
|
|
|
>>> n3 = Node("a", n2)
|
2021-06-09 08:49:28 +00:00
|
|
|
>>> assert n3.value == "a"
|
|
|
|
>>> assert n3.nxt == n2
|
2021-06-09 07:05:40 +00:00
|
|
|
"""
|
2021-06-09 16:46:47 +00:00
|
|
|
pass
|
2021-06-09 07:05:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class File:
|
|
|
|
"""
|
2021-06-09 08:49:28 +00:00
|
|
|
La classe ```File``` doit posséder un élément "courant" qui est l'élément sur le dessus de la file.
|
2021-06-09 07:05:40 +00:00
|
|
|
|
2021-06-09 08:49:28 +00:00
|
|
|
Fonctionnement d'une file
|
2021-06-09 07:05:40 +00:00
|
|
|
|
2021-06-09 08:49:28 +00:00
|
|
|
-----------
|
|
|
|
append ----> _ _ _ _ _
|
|
|
|
-----------
|
2021-06-09 07:05:40 +00:00
|
|
|
|
2021-06-09 08:49:28 +00:00
|
|
|
-----------
|
|
|
|
_ _ _ _ _ ----> pop
|
|
|
|
-----------
|
2021-06-09 07:05:40 +00:00
|
|
|
|
2021-06-09 08:49:28 +00:00
|
|
|
Elle doit aussi posséder un entier ```n``` qui compte le nombre d'éléments dans la file
|
2021-06-09 07:05:40 +00:00
|
|
|
|
2021-06-09 08:49:28 +00:00
|
|
|
Cette classe aura donc 2 attributs:
|
|
|
|
- current avec l'élément courant
|
|
|
|
- n avec le nombre d'éléments dans la file
|
2021-06-09 07:05:40 +00:00
|
|
|
|
2021-06-09 08:49:28 +00:00
|
|
|
:consignes: Compléter les méthodes suivantes."""
|
2021-06-09 07:05:40 +00:00
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
"""Initialisation de la File vide
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> file = File()
|
|
|
|
>>> assert file.n == 0
|
|
|
|
>>> assert file.current is None
|
|
|
|
"""
|
2021-06-09 16:46:47 +00:00
|
|
|
pass
|
2021-06-09 07:05:40 +00:00
|
|
|
|
|
|
|
def append(self, value):
|
|
|
|
"""Ajoute "value" dans la file
|
|
|
|
|
2021-06-09 08:49:28 +00:00
|
|
|
Comme nous sommes dans une file, l'élément doit être ajouté en dernière position.
|
2021-06-09 07:05:40 +00:00
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> file = File()
|
|
|
|
>>> file.append(1)
|
|
|
|
>>> assert file.n == 1
|
2021-06-09 08:49:28 +00:00
|
|
|
>>> assert file.current.value == 1
|
2021-06-09 07:05:40 +00:00
|
|
|
>>> file.append("a")
|
|
|
|
>>> assert file.n == 2
|
2021-06-09 08:49:28 +00:00
|
|
|
>>> assert file.current.value == 1
|
2021-06-09 07:05:40 +00:00
|
|
|
"""
|
2021-06-09 16:46:47 +00:00
|
|
|
pass
|
2021-06-09 07:05:40 +00:00
|
|
|
|
|
|
|
def get_current(self):
|
|
|
|
"""Renvoie la prochaine valeur sans l'enlever de la file
|
|
|
|
Si la liste est vide, la fonction renvoie None.
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> file = File()
|
|
|
|
>>> file.get_current()
|
|
|
|
>>> file.append(3)
|
|
|
|
>>> file.get_current()
|
|
|
|
3
|
|
|
|
>>> file.get_current()
|
|
|
|
3
|
|
|
|
"""
|
2021-06-09 16:46:47 +00:00
|
|
|
pass
|
2021-06-09 07:05:40 +00:00
|
|
|
|
|
|
|
def is_empty(self):
|
|
|
|
"""Vérifie si le file est vide
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> file = File()
|
|
|
|
>>> file.is_empty()
|
|
|
|
True
|
|
|
|
>>> file.append(1)
|
|
|
|
>>> file.is_empty()
|
|
|
|
False
|
|
|
|
|
|
|
|
"""
|
2021-06-09 16:46:47 +00:00
|
|
|
pass
|
2021-06-09 07:05:40 +00:00
|
|
|
|
|
|
|
def pop(self):
|
|
|
|
"""Renvoie le prochain élément de la file et l'enlève
|
|
|
|
|
|
|
|
Comme c'est une file, le prochain élément est le premier élément arrivé.
|
|
|
|
|
2021-06-09 08:49:28 +00:00
|
|
|
Si la liste est vide, la fonction renvoie rien
|
2021-06-09 07:05:40 +00:00
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> file = File()
|
|
|
|
>>> file.pop()
|
|
|
|
>>> file.append(2)
|
|
|
|
>>> file.pop()
|
|
|
|
2
|
|
|
|
>>> file.pop()
|
2021-06-09 08:49:28 +00:00
|
|
|
>>> file.append(2)
|
|
|
|
>>> file.append(5)
|
|
|
|
>>> file.append("a")
|
2021-06-09 07:05:40 +00:00
|
|
|
>>> file.pop()
|
|
|
|
2
|
|
|
|
>>> file.pop()
|
|
|
|
5
|
|
|
|
>>> file.pop()
|
|
|
|
'a'
|
|
|
|
>>>
|
|
|
|
|
|
|
|
"""
|
2021-06-09 16:46:47 +00:00
|
|
|
pass
|
2021-06-09 07:05:40 +00:00
|
|
|
|
|
|
|
def empty(self):
|
|
|
|
"""vide la file c'est à dire element tous les elements de cette dernière
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> file = File()
|
|
|
|
>>> file.append(3)
|
|
|
|
>>> file.append(5)
|
|
|
|
>>> file.append("a")
|
|
|
|
>>> file.get_current()
|
2021-06-09 08:49:28 +00:00
|
|
|
3
|
2021-06-09 07:05:40 +00:00
|
|
|
>>> assert file.n == 3
|
|
|
|
>>> file.empty()
|
|
|
|
>>> file.get_current()
|
|
|
|
>>> assert file.n == 0
|
|
|
|
"""
|
2021-06-09 16:46:47 +00:00
|
|
|
pass
|