Sample Input 0 1 9 User create a create b clone a c hash a hash b hash c is_equal a b is_equal a c is_equal b c

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Kindly refer to the pictures for the instructions (Python language)

 

Code Template:

 

import uuid
import random

def make_class(attrs, base=None):
    def make_instance(cls):
        def bind(val, instance):
            if callable(val):
                def method(*args):
                    return val(instance, *args)

                return method
            
            return val

        def get_v(attr_name):
            if attr_name in attrs:
                # Object attribute
                return attrs[attr_name]

            # Class attribute
            val = cls['get'](attr_name)
            return bind(val, instance)

        def set_v(attr_name, val):
            attrs[attr_name] = val
            
        def get_uuid():
            return uuid.UUID(bytes=bytes(random.getrandbits(8) for _ in range(16)), version=4)

        # FIXME: Edit `get_hash()` to return hash of object
        def get_hash():
            return None

        # FIXME: Edit `is_same(other)` to return whether this object is the
        #        same as other
        def is_same(other):
            return None

        # FIXME: Edit `clone_o()` to return the clone of this object
        def clone_o():
            return None

        # FIXME: Generate a hash for this function using the `get_uuid()` function
        hashcode = None

        # FIXME: Edit the instance dictionary to add `hash`, `is_same` and `clone`
        #        functions
        attrs = {}
        instance = {
            'get': get_v,
            'set': set_v,
        }

        return instance

    def init(cls, *args):
        obj = make_instance(cls)
        init_m = cls['get']('__init__')

        if init_m:
            init_m(obj, *args)

        return obj

    def get_a(attr_name):
        if attr_name == 'super':
            return base
        elif attr_name in attrs:
            return attrs[attr_name]
        elif base is not None:
            return base['get'](attr_name)

    def set_a(attr_name, val):
        attrs[attr_name] = val

    def new_o(*args):
        return init(cls, *args)
    
    random.seed('EEE111')

    cls = {
        'get': get_a,
        'set': set_a,
        'new': new_o,
    }

    return cls

# Don't edit anything below this to make your life easier. :)
def main():
    def DynamicClassFactoryMaker(fun_name, props_dict):
        def f():
            def __init__(self):
                for prop_name, (prop_val, is_get, is_set) in props_dict.items():
                    self['set'](prop_name, prop_val)
                    self['modify'](prop_name, is_get, is_set)
            
            return make_class(locals())
            
        f.__name__ = fun_name
        return f
        
    n_testcases = int(input())

    for t in range(n_testcases):
        obj_dict = {}

        n_commands = int(input())
        class_name = input()
        
        DynamicClassFactory = DynamicClassFactoryMaker(class_name, {})
        DynamicClass = DynamicClassFactory()

        print(f'Case #{t + 1}:')
        
        for _ in range(n_commands):
            cmd_line = input().split()

            if cmd_line[0] == 'create':
                obj_name = cmd_line[1]

                class_obj = DynamicClass['new']()
                obj_dict[obj_name] = class_obj
            elif cmd_line[0] == 'hash':
                obj_name = cmd_line[1]

                print(obj_dict[obj_name]['hash']())
            elif cmd_line[0] == 'clone':
                a, b = cmd_line[1:]

                obj_dict[b] = obj_dict[a]['clone']()
            elif cmd_line[0] == 'is_equal':
                a, b = cmd_line[1:]
                print(obj_dict[a]['is_same'](obj_dict[b]))

if __name__ == '__main__':
    main()

Constraints
Input Constraints
1<T< 5
0 < ne < 50
op E {create, hash, is_equal, clone}
class_name is written in CamelCase while object names are in snake_case.
Max running time of code should be <5 seconds for each test input.
There will be no case wherein a non-existent object is operated on.
You can assume that all of the inputs are well-formed and are always provided within these constraints. You
are not required to handle any errors.
Functional Constraints
You are required to make sure that each object instantiated will have a hash, is_same, and clone keys in
its dispatch dictionary. Failure to do so will mark your code with a score of zero. For the OJ to work, you are
also required to use the template code and only edit the parts marked with a FIXME comment before them.
The template code contains the hash-generating function get_uuid (), which generates a pseudorandom
UUIDV4 ID. For replication purposes, the random seed is always set to the string EEE111, which is reinitialized
everytime a new class is created (i.e. new testcase processed).
Output Format
The output will consist of several blocks for each testcase. Each block shall start with a line Case #t: with t
being the serial number of the testcase starting at 1. The next n, lines denote the corresponding values of the
hash or is_equal operations queried from the corresponding commands in the testcase.
Sample Input 0
User
create a
create b
clone a c
hash a
hash b
hash c
is_equal a b
is_equal a c
is_equal b c
Sample Output 0
Case #1:
656c7a06-049e-434c-95e7-03290203de98
e37lec9d-f02d-4ee3-a80b-f2d524c85ed5
656c7a06-049e-434c-95e7-03290203de98
False
True
False
Explanation 0
User objects a and c are the same, while b is a different object altogether.
Transcribed Image Text:Constraints Input Constraints 1<T< 5 0 < ne < 50 op E {create, hash, is_equal, clone} class_name is written in CamelCase while object names are in snake_case. Max running time of code should be <5 seconds for each test input. There will be no case wherein a non-existent object is operated on. You can assume that all of the inputs are well-formed and are always provided within these constraints. You are not required to handle any errors. Functional Constraints You are required to make sure that each object instantiated will have a hash, is_same, and clone keys in its dispatch dictionary. Failure to do so will mark your code with a score of zero. For the OJ to work, you are also required to use the template code and only edit the parts marked with a FIXME comment before them. The template code contains the hash-generating function get_uuid (), which generates a pseudorandom UUIDV4 ID. For replication purposes, the random seed is always set to the string EEE111, which is reinitialized everytime a new class is created (i.e. new testcase processed). Output Format The output will consist of several blocks for each testcase. Each block shall start with a line Case #t: with t being the serial number of the testcase starting at 1. The next n, lines denote the corresponding values of the hash or is_equal operations queried from the corresponding commands in the testcase. Sample Input 0 User create a create b clone a c hash a hash b hash c is_equal a b is_equal a c is_equal b c Sample Output 0 Case #1: 656c7a06-049e-434c-95e7-03290203de98 e37lec9d-f02d-4ee3-a80b-f2d524c85ed5 656c7a06-049e-434c-95e7-03290203de98 False True False Explanation 0 User objects a and c are the same, while b is a different object altogether.
Composing Programs has introduced us to a way to implement an OOP system in programming languages
that don't have it. Together with the current lesson, the OOP system can do the following:
• Make a class
• Bind the self object to a class method
• Instantiate objects of a class
• Run an --init_() routine during instantiation of an object
• Make a class inherit from a base class
• Get its superclass from a class method
Now, we are interested in augmenting our system to be able to compare two objects. Recognizing that (at
least initially) no two objects from the same class are created, we can assign an ID to each object instantiated.
This hashcode is used by some languages, such as Java, to implement the equality operator between objects.
In this challenge, we will be emulating this in Python.
We will be assigning each object with a hash based on a pseudorandom universally unique identifier (UUID).
This hash can be retrieved using the hash function. To check whether two objects are the same, we will be
implementing an is_same function to check whether their hashes are the same. Finally, we will be
implementing a clone function to duplicate an object. There are several ways to "clone" an object, but we will
only implement a very simple one that makes a new object with the same hash as itself.
Input Format
The first line of the input is an integer T denoting the number of classes (or testcases) that will follow. Each
testcase starts with a line containing a single number denoting the number of commands ne to execute for
this testcase.
The next line contains the name of the class class_name to create.
The next ne lines denote the commands to execute. Each line consists of an operation op followed by some
operands. op can only be from this set: create, hash, is_equal, clone. The format of a line with each
keyword is shown below, with a and b as snake-case variable names.
create a - create an object a of class class_name
hash a - get hash of object a
• is_equal a b - compare two objects a and b
• clone a b - copy object a into a new object c
Note that each line in a set of commands is executed in order. For example, the code below shows means that
object a is created first, then its hash is printed, and then the object b is then created.
create a
hash a
create b
hash b
Transcribed Image Text:Composing Programs has introduced us to a way to implement an OOP system in programming languages that don't have it. Together with the current lesson, the OOP system can do the following: • Make a class • Bind the self object to a class method • Instantiate objects of a class • Run an --init_() routine during instantiation of an object • Make a class inherit from a base class • Get its superclass from a class method Now, we are interested in augmenting our system to be able to compare two objects. Recognizing that (at least initially) no two objects from the same class are created, we can assign an ID to each object instantiated. This hashcode is used by some languages, such as Java, to implement the equality operator between objects. In this challenge, we will be emulating this in Python. We will be assigning each object with a hash based on a pseudorandom universally unique identifier (UUID). This hash can be retrieved using the hash function. To check whether two objects are the same, we will be implementing an is_same function to check whether their hashes are the same. Finally, we will be implementing a clone function to duplicate an object. There are several ways to "clone" an object, but we will only implement a very simple one that makes a new object with the same hash as itself. Input Format The first line of the input is an integer T denoting the number of classes (or testcases) that will follow. Each testcase starts with a line containing a single number denoting the number of commands ne to execute for this testcase. The next line contains the name of the class class_name to create. The next ne lines denote the commands to execute. Each line consists of an operation op followed by some operands. op can only be from this set: create, hash, is_equal, clone. The format of a line with each keyword is shown below, with a and b as snake-case variable names. create a - create an object a of class class_name hash a - get hash of object a • is_equal a b - compare two objects a and b • clone a b - copy object a into a new object c Note that each line in a set of commands is executed in order. For example, the code below shows means that object a is created first, then its hash is printed, and then the object b is then created. create a hash a create b hash b
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY