Csc 1302, Honors Principles of Computer Science II (Fall 2023)
Week 4 (17 September 2023)
Pseudo Code for Union, Intersect, and Minus
During class, you will complete coding of the Tuple.clone(), Relation.member(), and the
union, intersect, and minus operators. Pseudo code is given for some of these methods:
def union(self,r2):
## Schema part of output relation
Create empty array list for attributes of output relation, call it attrs
Create empty array list for domains of output relation, call it doms
Copy values from attributes of "self" relation into attrs
Copy values from domains of "self" relation into doms
Create a new Relation object, called rel, with attributes attrs and domains doms, name can be null
## Instance part of output relation
Clone each tuple from the "self" relation and add to rel
Clone each tuple from r2 and add to rel
remove duplicates from rel
return rel
def intersect(self,r2):
## Schema part of output relation
Create empty array list for attributes of output relation, call it attrs
Create empty array list for domains of output relation, call it doms
Copy values from attributes of "self" relation into attrs
Copy values from domains of "self" relation into doms
Create a new Relation object, called rel, with attributes attrs and domains doms, name can be null
## Instance part of output relation
for each tuple in "self" relation:
if tuple also belongs to r2 then
Clone tuple add to rel
return rel
def minus(self,r2):
## Schema part of output relation
Create empty array list for attributes of output relation, call it attrs
Create empty array list for domains of output relation, call it doms
Copy values from attributes of "self" relation into attrs
Copy values from domains of "self" relation into doms
Create a new Relation object, called rel, with attributes attrs and domains doms, name can be null
## Instance part of output relation
for each tuple in "self" relation:
if tuple does not belong to r2 then
Clone tuple add to rel
return rel