Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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). In [ ]: file data_1.dat 2 3 4 5 6 7 8 9 A BCDEF G H I J K L In (): %%file data_2.dat 1 2 3 4 5 6 7 8 9 A BCDEF GHIJKLM In [ ]: %%file data_3.dat bc def g hij klmno pqrstu In [ ]: %%file data_4.dat 1 2 3 4 5 6 7 8 9 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'), I'B', 'C', 'D', 'E', 'F'], ['G', 'H', 'I', 'J', 'K', 'L'll 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 In [ ]: # YOUR CODE HERE raise Not ImplementedError() In (): # # # 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!" In [ ]: t = triangular( 'data_1.dat' ) assert t.check() == True t.data In [ ]: try: triangular( 'data_2.dat' ) except ValueError: print( "Got ValueError as expected!" ) pass else: assert False In [ ]: a = triangular( 'data_1.dat' ) c = triangular 'data_3.dat' ) #print( a.data) #print( c.data) student_answer = a + c correct_answer = \ [['1a'], ['25', '3c'], ['4', '5e 'f'], ['7g' '8h', '9i', 'Aj'], ['Bk' 'Cl', Dm 'En', 'Fo'l, ['Gp', 'Hq' 'Ir', 'Js' 'Kt', 'Lu'll #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 = triangular( 'data_1.dat', ord) d = triangular( 'data_4.dat', ord) #print( a.data) #print( d.data) try: a + d except AssertionError: print( 'Got AssertionError as expected' ) pass else: assert False
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started