Bonsoir,
Le but de ma classe "Intset" et de faire des opérations de symétrie entre des sets de nombre
Voici le corps de la classe
Je souhaitais faire pour la dernière méthode intersect retourner un objet de type InSet sans changer les paramètre de la méthode mais je n'arrive pas à crée un objet InSet dans cette méthode
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 # -*- coding: utf-8 -*- class IntSet(object): """An IntSet is a set of integers The values contained in the set are represented by a list of integers: self.vals Each integer in the set occurs in self.vals exactly once. """ def __init__(self): """Creates an empty set of integers input -- self : instance of class IntSet """ self.vals = [] def __str__(self): """Returns a string representation of self input -- self : instance of class IntSet output -- string: shows the list of elements in self (string type) """ # TODO message = "{" for i in range(len(self.vals)-1): message+= str(self.vals[i]) + ", " if(len(self.vals) > 0): message += str(self.vals[-1]) message += '}' return message def insert(self, e): """Inserts e into self input -- self : instance of class IntSet -- e : integer """ if not e in self.vals: self.vals.append(e) def member(self, e): """Returns True if e is in self, and False otherwise input -- self : instance of class IntSet -- e : integer, the value to be tested output -- bool, is True if e is in self, False otherwise """ return e in self.vals def remove(self, e): """Removes e from self. If e is not in self, do nothing Raises ValueError if e is not in self input -- self : instance of class IntSet -- e : integer output -- bool, True if e has been removed """ if self.member(e): self.vals.remove(e) return True return False def intersect(self, other): """Returns the intersection of the sets self and other input -- self : instance of class IntSet -- other : instance of class IntSet output -- instance of class IntSet, the intersection of self and other """ # TODO commun = Inset() for number in self.vals: if number in other.vals: commun.insert(number) return commun
Erreur reçu :
Je ne comprend donc pas comment faire un retour d'un nouvel objet de ce type "IntSet" ou si il y a une méthode pour faire l'équivalent
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 TP4\intset.py:68 in intersect commun = InSet() NameError: name 'InSet' is not define
Test unitaire :
Pouvais vous m'indiquer la notion à chercher ou la méthode car je n'arrive pas à trouver même après recherche sur internet car je n'ai surement pas les bons mots pour le problème.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 #%% Test de la méthode intersect s1 = iset.IntSet() for i in [1,5,4,3]: s1.insert(i) s2 = iset.IntSet() for i in [1,6,4,7]: s2.insert(i) s = s1.intersect(s2) print('s1:', s1) print('s2:', s2) print('s1 ∩ s2:', s)
Merci pour votre temps et bonne soirée à vous !
Partager