Csc 1302, Honors Principles of Computer Science II (Fall 2023)
Week 5 (22 September 2023)
Tuple Concatenate, Rename and Times Operators
During this week you will write the following methods:- (Tuple.py):
## This method combines two tuples into one and assigns a new schema to the ## result tuple; the method returns the new tuple ## e.g. t1 = <"jones",20,200> and t2=<1,2,2.5> ## then t1.concatenate(t2,attr,dom) will be <"jones",20,200,1,2,2.5> ## with schema attr = <A, R.B, R.C, S.B, S.C, D> ## and dom = <VARCHAR,INTEGER,INTEGER,INTEGER,INTEGER,DECIMAL> def concatenate(self, t, attrs, doms): Construct new Tuple object, tup, with attrs and doms for each component in the "self" tuple: add the component to tup for each component in the Tuple t: add the component to tup return tup
- (Relation.py) Rename Operator:
## The rename method takes as parameter an array list of new column names, cnames, ## and returns a new relation object that contains the same set of tuples, but with ## new columns names. We can assume that the size of cnames is same as size of self.attributes def rename(self,cnames): Construct new empty attrs and doms array lists Copy all column names from cnames into attrs and all domains from self.domains to doms Construct new Relation object, rel for each tuple in self.table: clone the tuple and add to rel.table return rel
- (Relation.py) Times Operator:
## The times method returns the cartesian product of two relations. ## As an example, let R and S be the following two relations: ## R(A:VARCHAR, B:INTEGER, C:INTEGER) and S(B:INTEGER, C:INTEGER, D:DECIMAL) ## and let R contain the tuples {<jones",20,200>, <smith",30,300> and ## let S contian the tuples {<1,2,2.5>, <100,200,3.86>} ## The R times S would have the schema ## R_TIMES_S(A:VARCHAR, R.B:INTEGER, R.C:INTEGER, S.B:INTEGER, S.C:INTEGER, D:DECIMAL) ## and the tuples: {<jones",20,200,1,2,2.5>, <jones",20,200,100,200,3.86>, ## <smith",30,300,1,2,2.5>, <smith",30,300,100,200,3.86>} ## Notice the tuples in the output are formed by combining tuples in the ## input relations in all possible ways, maintaining the order of columns def times(self,r2): Construct new empty attrs and doms array lists Copy all column names from self.attributes to attrs, renaming any attribute that also appears in r2.attributes; copy corresponding self.domains value to doms Copy all column names from r2.attributes to attrs, renaming any attribute that also appears in self.attributes; copy corresponding r2.domains value to doms Construct new Relation object, rel Using nested for-loops, get tuple t1 from self.table and t2 from r2.table and concatenate t1 and t2 using the method written earlier; Add the concatenated tuple to rel return rel }
Download the Driver Program and implement all the methods in this assignment. You should see the following output when you run Driver.py:
Mac-mini:week5 raj$ python3 Driver.py REL1(COL1:INTEGER,COL2:VARCHAR) Number of tuples:5 1111:Robert Adams: 1112:Charles Bailey: 1114:Richard Johnson: 1115:Graham Gooch: 1116:John Miller: RENAMECOLS(NEWCOL1:INTEGER,NEWCOL2:VARCHAR) Number of tuples:5 1111:Robert Adams: 1112:Charles Bailey: 1114:Richard Johnson: 1115:Graham Gooch: 1116:John Miller: REL1(COL1:INTEGER,COL2:VARCHAR) Number of tuples:5 1111:Robert Adams: 1112:Charles Bailey: 1114:Richard Johnson: 1115:Graham Gooch: 1116:John Miller: REL2(COL1:INTEGER,COL2:VARCHAR) Number of tuples:5 1113:John Smith: 1112:Charles Bailey: 1115:Graham Gooch: 1116:John Miller: 1117:Hugh Howell: R1TIMESR2(REL1.COL1:INTEGER,REL1.COL2:VARCHAR,REL2.COL1:INTEGER,REL2.COL2:VARCHAR) Number of tuples:25 1111:Robert Adams:1113:John Smith: 1111:Robert Adams:1112:Charles Bailey: 1111:Robert Adams:1115:Graham Gooch: 1111:Robert Adams:1116:John Miller: 1111:Robert Adams:1117:Hugh Howell: 1112:Charles Bailey:1113:John Smith: 1112:Charles Bailey:1112:Charles Bailey: 1112:Charles Bailey:1115:Graham Gooch: 1112:Charles Bailey:1116:John Miller: 1112:Charles Bailey:1117:Hugh Howell: 1114:Richard Johnson:1113:John Smith: 1114:Richard Johnson:1112:Charles Bailey: 1114:Richard Johnson:1115:Graham Gooch: 1114:Richard Johnson:1116:John Miller: 1114:Richard Johnson:1117:Hugh Howell: 1115:Graham Gooch:1113:John Smith: 1115:Graham Gooch:1112:Charles Bailey: 1115:Graham Gooch:1115:Graham Gooch: 1115:Graham Gooch:1116:John Miller: 1115:Graham Gooch:1117:Hugh Howell: 1116:John Miller:1113:John Smith: 1116:John Miller:1112:Charles Bailey: 1116:John Miller:1115:Graham Gooch: 1116:John Miller:1116:John Miller: 1116:John Miller:1117:Hugh Howell: Mac-mini:week5 raj$