Question

2. Create a class Compute. Your task is to perform operator overloading. Following are the operators...

2. Create a class Compute. Your task is to perform operator overloading. Following are the operators that would be overloaded. i. + The input for the operators would be same sized lists, same sized tuple or variable-sized dictionary. Following task needs to be completed: For + operator, if the caller is a list, then you need to add the contents of the two lists and return the same sized list. Similarly, you need to add the contents of the two tuples and return the same sized tuple. For a dictionary, you need to concatenate the two dictionaries. If the key is already present, then you need to merge the contents or values of those keys. Example: If input is list: [1,2,3,4] and [3.3,4,5,1.2], then for + operator output would be [4.4,6,8,5.2] Same applies for tuples. If input is dictionary: {1:’abc’,3:’xyz’,5:’test’} and {2:’test1’,3:’test2’,4:’test3’}, then for + operator   output would be {1:’abc’,3:’xyztest2’,5:’test’, 2:’test1’,4:’test3’}

Homework Answers

Answer #1
The above code has been implemented in Python 3 as per the details given above.
Appropriate comments have been added in the code for better understanding.
Below is the CODE, CODE SNIPPET (for indentation purposes) and OUTPUT.

CODE:

class Compute:
    def __init__(self, x_comp, y_comp):
        # Creating a constructor for class compute
        self.x_comp = x_comp
        self.y_comp = y_comp

    def list_add(self):
        # Adding the elements of two lists which are of same size
        list_result = []
        for i in range(0, len(self.x_comp)):
            list_result.append(self.x_comp[i] + self.y_comp[i])
        return list_result

    def tuple_add(self):
        # Adding the elements of two tuples which are of same size
        tuple_result = [sum(x) for x in zip(self.x_comp,self.y_comp)]
        return tuple_result

    def dict_add(self):
        # Adding the values of two dictionaries
        for key in self.y_comp:
            if key in self.x_comp:
                self.x_comp[key] = self.y_comp[key] + self.x_comp[key]
            else:
                self.x_comp[key] = self.y_comp[key]
        return self.x_comp


def main():
    # Operator Overloading is done on input1 and input2
    input1 = {1: 'abc', 3: 'xyz', 5: 'test'}
    input2 = {2: 'test1', 3: 'test2', 4: 'test3'}

    # Create object of the Compute class
    obj1 = Compute(input1, input2)

    # Check the type of input1 and input2 and call the appropriate method
    if type(input1) == list and type(input2) == list:
        result = obj1.list_add()
    elif type(input1) == tuple and type(input2) == tuple:
        result = obj1.tuple_add()
    elif type(input1) == dict and type(input2) == dict:
        result = obj1.dict_add()
    else:
        # If the input is not correct, exit the program.
        print("Please enter the correct type")
        exit()

    print(result)


if __name__ == '__main__':
    main()

CODE SNIPPET:

OUTPUT:

1) LIST 

input1 = [1,2,3,4]
input2 = [3.3,4,5,1.2]

2) TUPLE

3) DICT

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a currency class, up to 5 individual currencies, with four attributes - currency name, whole...
Create a currency class, up to 5 individual currencies, with four attributes - currency name, whole parts and fractional parts and fractional's name such that 100 fractional parts equals 1 whole part, e.g. Pound, 1, 55, pence or Euro, 3, 33, cent. Define overloaded operators to add or subtract different currency objects - care should be taken that you can only perform these operation on objects of the same currency. Also, take care that fractional parts roll into whole parts....
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...