Vous êtes sur la page 1sur 4

POO GRP6 http://localhost:8888/notebooks/POO GRP6.

ipynb

In [31]: class Graphe:

def __init__(self):
"""
__init__ est une méthode magique automatiquement invoquée
par Python lors de l'instanciation d'une nouveau Graphe.
g = Graphe()
==> ...... Graphe.__init__(g)
"""
self.g = dict()

def nb_sommets(self):
"""
retourne le nombre de sommets du graphe
"""
return len(self.g)

def nb_arcs(self):
"""
retourne le nombre total d'arcs du graphe self
"""
return sum(len(v) for v in self.g.values())

def sommets(self):
"""
retourne un frozenset contenant les sommets du graphe
self
"""
return frozenset(self.g)

def ajouter_sommet(self, s):


"""
ajoute le sommet s au graphe self, déclenche une
erreur si s est déjà dans self.
"""
if s in self.g:
raise Exception(" Sommet déjà présent dans le graphe")
self.g[s] = set()

def ajouter_arc(self, dp, df):


"""
ajouter l'arc (dp --- > df) au graphe courant self.
Déclenche des erreurs si :
- dp ou df ne sont pas dans le graphe
- dp est déjà relié à df
"""
assert dp in self.g
assert df in self.g
assert df not in self.g[dp]
self.g[dp].add(df) #self.g => dict, self.d[dp] => set

def supprimer_arc(self, dp, df):


"""
supprimer l'arc (dp --> df) du graphe self.
Déclenche des erreurs si :
- dp ou df ne sont pas dans le graphe.
- dp n'est pas relié à df
"""
assert dp in self.g and df in self.g and df in self.g[dp]
self.g[dp].discard(df)

def supprimer_sommet(self, s):


"""

1 sur 4 25/10/2019 à 12:39


POO GRP6 http://localhost:8888/notebooks/POO GRP6.ipynb

In [33]: #instanciation de gr
gr = Graphe()
#ajout des sommets
for s in range(1, 7):
gr.ajouter_sommet(s)
gr.ajouter_arc(1,1)
gr.ajouter_arc(1,2)
gr.ajouter_arc(1,3)
gr.ajouter_arc(2,4)
gr.ajouter_arc(3,1)
gr.ajouter_arc(3,4)
gr.ajouter_arc(3,5)
gr.ajouter_arc(4,6)
gr.ajouter_arc(6,4)

In [34]: gr.g

Out[34]: {1: {1, 2, 3}, 2: {4}, 3: {1, 4, 5}, 4: {6}, 5: set(), 6: {4}}

In [35]: def ParcoursLargeur(gr, s):


d = {n:False for n in gr.sommets()}
f = list()
v = list()
d[s] = True
f.append(s)
while len(f) != 0:
s = f.pop(0)
v.append(s)
for suc in gr[s]:
if not d[suc]:
d[suc] = True
f.append(suc)
return v

In [37]: ParcoursLargeur(gr,2)

Out[37]: [2, 4, 6]

In [6]: def factorielle(n):


if not isinstance(n, int): #type(n) is not int:
raise Exception(" Uniquement les valeur entières sont acceptées")
if n < 0:
raise Exception(" Entier positif attendu !")
if n == 0:
return 1
else:
return n * factorielle(n-1)

In [ ]: def factorielle(n):
assert isinstance(n,int) and n >= 0, "Entier naturel attendu!"
if n == 0:
return 1
else:
return n * factorielle(n-1)

In [7]: factorielle(5)

Out[7]: 120

2 sur 4 25/10/2019 à 12:39


POO GRP6 http://localhost:8888/notebooks/POO GRP6.ipynb

In [9]: factorielle(-1)

---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-9-71a9c5f638b5> in <module>()
----> 1 factorielle(-1)

<ipython-input-6-d549e107e4bc> in factorielle(n)
3 raise Exception(" Uniquement les valeur entières sont acceptées")
4 if n < 0:
----> 5 raise Exception(" Entier positif attendu !")
6 if n == 0:
7 return 1

Exception: Entier positif attendu !

In [10]: isinstance(5, int)

Out[10]: True

In [11]: s = {1,2,3,4}

In [12]: s -= {1}

In [13]: s

Out[13]: {2, 3, 4}

In [14]: s.discard(3)

In [15]: s

Out[15]: {2, 4}

In [16]: s.remove(2)

In [17]: s

Out[17]: {4}

In [18]: d = {1:5,3:5,10:11}

In [19]: d.pop(1)
Out[19]: 5

In [20]: d

Out[20]: {3: 5, 10: 11}

In [21]: del d[10]

In [22]: d

Out[22]: {3: 5}

In [23]: d.popitem()

Out[23]: (3, 5)

3 sur 4 25/10/2019 à 12:39


POO GRP6 http://localhost:8888/notebooks/POO GRP6.ipynb

In [24]: d

Out[24]: {}

In [25]: l = ["med", "ali", "salah"]

In [26]: l[0]

Out[26]: 'med'

In [27]: l.__getitem__(0)

Out[27]: 'med'

In [28]: list.__getitem__(l,0)
Out[28]: 'med'

In [29]: "med" in l

Out[29]: True

In [30]: list.__contains__(l, "med")

Out[30]: True

In [ ]:

4 sur 4 25/10/2019 à 12:39

Vous aimerez peut-être aussi