Académique Documents
Professionnel Documents
Culture Documents
ipynb
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)
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 [37]: ParcoursLargeur(gr,2)
Out[37]: [2, 4, 6]
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
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
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
In [22]: d
Out[22]: {3: 5}
In [23]: d.popitem()
Out[23]: (3, 5)
In [24]: d
Out[24]: {}
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
Out[30]: True
In [ ]: