Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python Question 1 100 points 95 mins Consider the following file with triangular data, if you will. Run the next set of four cells to

python

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Question 1 100 points 95 mins Consider the following file with "triangular" data, if you will. Run the next set of four cells to create the data files that will be needed in the test cases further below ( data_1.dat - data_4.dat ). ]: %%file data_1.dat 23 4 5 6 7 8 9A BCDEF GHIJKL ]: %%file data_2.dat 1 23 4 5 6 7 8 9 A BCDEF G H I J K L M ]: %%file data_3.dat a bc de f g hij klmno pqrstu ]: %%file data_4.dat 1 2 3 4 5 6 7 8 9 Now consider the following ADT named triangular : Now consider the following ADT named triangular : triangular( data_filepath, data_converter-str ): initializes a triangular object with the data provided in a file at data_filepath. Each row of data will be stored as a list of values converted to the data format that will be specified by the data_converter parameter, which must be a function that takes single parameter. The default value of data_converter function is str. That is, the data converter will be called on each piece of data to do the conversion. The data will be stored as a list of sublists in an instance variable called data. If the input data is not triangular, the initializer method should raise a ValueError For example, the content of data_1. dat should be represented as [['1'], ['2', '3'], ['4', '5', '6'], ['7', '8', '9', 'A'], ['B', 'C', 'D', 'E', 'F'], ['G', 'H', 'I', 'I', 'K', 'l']] in memory len( triangular ( ... )) should return the number of rows in the data. In data_1.dat, there are 6 rows, for example. triangular.check(): should return True if the input data specified for the initializer above is indeed triangular in shape. a + b should return the addition of two triangular objects, a and b, depending on their type. Assume that a and b will always be compatible. If the dimensions of a and b are not identical, this method should raise an AssertionError Implement this ADT in Python. PROVIDE YOUR ANSWER IN THE FOLLOWING CODE CELL ]: # YOUR CODE HERE raise NotImplementedError # WARNING! * * * # # # # # If you have not defined an instance field named 'data' in your triangular class, all remaining tests will fail! # Please follow the provided specifications! t = triangular('data_1.dat' ) 'data' in dir( t.data) assert hasattr( t, 'data' ), "Your triangular instance should have a 'data' field as specified!" triangular('data_1.dat' ) assert t.check == True t.data - try: triangular('data_2.dat' ) except ValueError: print("Got ValueError as expected!" ) pass else: assert False triangular( 'data_1.dat' ) triangular( 'data_3.dat' ) #print( a.data) #print( c.data) student_answer = a + c correct answer = \ [['la'] ['2b', '30'], ['40', '5e' '') ['7g', '8', '9i', 'Aj'), ['Bk', 'al', 'Dm', 'en', 'Fo'], ['Gp', 'Hq', 'Ir', 'Is', 'Kt', 'Lu']] #print( student_answer ) assert student_answer == correct_answer # Using ord() as the data_converter function # a = triangular( 'data_1.dat', ord) d = triangular( 'data_4.dat', ord) a.data = a.data[:4) d.data = d.data[:4] print(a.data) print( d.data) student_answer = a + d #print( student_answer ) correct answer [[97], [99, 101], [103, 105, 107], [109, 111, 113, 122]] #print( student_answer ) assert student_answer == correct answer a = d = triangular( 'data_1.dat', ord ) triangular( 'data_4.dat', ord ) #print( a.data) #print( d.data) try: except AssertionError: print( 'Got AssertionError as expected' ) pass else: assert False Question 1 100 points 95 mins Consider the following file with "triangular" data, if you will. Run the next set of four cells to create the data files that will be needed in the test cases further below ( data_1.dat - data_4.dat ). ]: %%file data_1.dat 23 4 5 6 7 8 9A BCDEF GHIJKL ]: %%file data_2.dat 1 23 4 5 6 7 8 9 A BCDEF G H I J K L M ]: %%file data_3.dat a bc de f g hij klmno pqrstu ]: %%file data_4.dat 1 2 3 4 5 6 7 8 9 Now consider the following ADT named triangular : Now consider the following ADT named triangular : triangular( data_filepath, data_converter-str ): initializes a triangular object with the data provided in a file at data_filepath. Each row of data will be stored as a list of values converted to the data format that will be specified by the data_converter parameter, which must be a function that takes single parameter. The default value of data_converter function is str. That is, the data converter will be called on each piece of data to do the conversion. The data will be stored as a list of sublists in an instance variable called data. If the input data is not triangular, the initializer method should raise a ValueError For example, the content of data_1. dat should be represented as [['1'], ['2', '3'], ['4', '5', '6'], ['7', '8', '9', 'A'], ['B', 'C', 'D', 'E', 'F'], ['G', 'H', 'I', 'I', 'K', 'l']] in memory len( triangular ( ... )) should return the number of rows in the data. In data_1.dat, there are 6 rows, for example. triangular.check(): should return True if the input data specified for the initializer above is indeed triangular in shape. a + b should return the addition of two triangular objects, a and b, depending on their type. Assume that a and b will always be compatible. If the dimensions of a and b are not identical, this method should raise an AssertionError Implement this ADT in Python. PROVIDE YOUR ANSWER IN THE FOLLOWING CODE CELL ]: # YOUR CODE HERE raise NotImplementedError # WARNING! * * * # # # # # If you have not defined an instance field named 'data' in your triangular class, all remaining tests will fail! # Please follow the provided specifications! t = triangular('data_1.dat' ) 'data' in dir( t.data) assert hasattr( t, 'data' ), "Your triangular instance should have a 'data' field as specified!" triangular('data_1.dat' ) assert t.check == True t.data - try: triangular('data_2.dat' ) except ValueError: print("Got ValueError as expected!" ) pass else: assert False triangular( 'data_1.dat' ) triangular( 'data_3.dat' ) #print( a.data) #print( c.data) student_answer = a + c correct answer = \ [['la'] ['2b', '30'], ['40', '5e' '') ['7g', '8', '9i', 'Aj'), ['Bk', 'al', 'Dm', 'en', 'Fo'], ['Gp', 'Hq', 'Ir', 'Is', 'Kt', 'Lu']] #print( student_answer ) assert student_answer == correct_answer # Using ord() as the data_converter function # a = triangular( 'data_1.dat', ord) d = triangular( 'data_4.dat', ord) a.data = a.data[:4) d.data = d.data[:4] print(a.data) print( d.data) student_answer = a + d #print( student_answer ) correct answer [[97], [99, 101], [103, 105, 107], [109, 111, 113, 122]] #print( student_answer ) assert student_answer == correct answer a = d = triangular( 'data_1.dat', ord ) triangular( 'data_4.dat', ord ) #print( a.data) #print( d.data) try: except AssertionError: print( 'Got AssertionError as expected' ) pass else: assert False

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions