def union(*args): res = [] for seq in args: for x in seq: if not x in res: res.append(x) return Set(res)
def union(*args): # self is args[0] res = [] for seq in args: # For all args for x in seq: # For all nodes if not x in res: res.append(x) # Add new items to result return Set(res)
def union(*args): # self - args[0] res = [] for seq in args: # Для всех аргументов for x in seq: # Для всех узлов if not x in res: res.append(x) # Добавить новый элемент в результат return Set(res)
def union(*arg): res = [] for other in arg: for x in other: if x not in res: res.append(x) return Set(res)
def intersect(self, *others): res = [] for x in self: # Scan first sequence for other in others: # For all other args if x not in other: break # Item in each one? else: # No: break out of loop res.append(x) # Yes: add item to end return Set(res)
def intersect(self, *arg): res = [] for x in self.data: for other in arg: if x not in other: break else: res.append(x) return Set(res)
def intersect(self, *others): res = [] for x in self: # Сканировать первую последов-ть for other in others: # Для всех остальных аргументов if x not in other: # Элемент присутствует во всех? break # Нет: прервать цикл else: res.append(x) # Да: добавить элемент в конец return Set(res)
def intersect(self, *others): res = [] for x in self: for other in others: if x not in other: break else: res.append(x) return Set(res)
class MultiSet(Set): def intersect(self, *others): res = [] for x in self: for other in others: if x not in other: break else: res.append(x) return Set(res) def union(*args): res = [] for seq in args: for x in seq: if not x in res: res.append(x) return Set(res)
def union(self, other): # other is any sequence res = self.data[:] # Copy of my list for x in other: # Add items in other if not x in res: res.append(x) return Set(res)
def intersect(self, other): # other is any sequence res = [] # self is the subject for x in self.data: if x in other: # Pick common items res.append(x) return Set(res) # Return a new Set
return Set(res) # Return a new Set def union(self, other): # other is any sequence res = self.data[:] # Copy of my list for x in other: # Add items in other if not x in res: res.append(x) return Set(res) def concat(self, value): # value: list, Set... for x in value: # Removes duplicates if not x in self.data: self.data.append(x) def __len__(self): return len(self.data) # len(self), if self def __getitem__(self, id_sli): return self.data[id_sli] # self[i], self[i:j] def __and__(self, other): return self.intersect(other) # self & other def __or__(self, other): return self.union(other) # self | other def __repr__(self): return 'Set:' + repr(self.data) # print(self),... def __iter__(self): return iter(self.data) # for x in self,... if __name__ == '__main__': from setwrapper import Set x = Set([1, 3, 5, 7]) print(x.union(Set([1, 4, 7]))) # prints Set:[1, 3, 5, 7, 4] print(x | Set([1, 4, 6])) # prints Set:[1, 3, 5, 7, 4, 6] print(x[1:4])
def union(self, other): # other는 시퀀스 res = self.data[:] # my list의 사본 for x in other: # other에 아이템 추가 if not x in res: res.append(x) return Set(res)
def intersect(self, other): # other는 시퀀스 res = [] # self가 대상 for x in self.data: if x in other: # 중복 아이템 선택 res.append(x) return Set(res) # 신규 집합 반환
return Set(res) def concat(self, value): # value: 리스트, 집합... for x in value: # 중복 삭제 if not x in self.data: self.data.append(x) def __len__(self): return len(self.data) # self면, len(self) def __getitem__(self, key): return self.data[key] # self[i], self[i:j] def __and__(self, other): return self.intersect(other) # self & other def __or__(self, other): return self.union(other) # self | other def __repr__(self): return 'Set:' + repr(self.data) # print(self).... def __iter__(self): return iter(self.data) # for x in self.... from setwrapper import Set x = Set([1, 3, 5, 7]) print(x.union(Set([1, 4, 7]))) # 집합 [1, 3, 5, 7, 4]을 출력 print(x | Set([1, 4, 6])) # 집합 [1, 3, 5, 7, 4, 6]을 출력