Question: 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.datdata_4.dat).

In [1]:

1

%%file data_1.dat

2

1

3

2 3

4

4 5 6

5

7 8 9 A

6

B C D E F

7

G H I J K L
Writing data_1.dat 

In [2]:

1

%%file data_2.dat

2

1

3

2 3

4

4 5 6

5

7 8 9 A

6

B C D E F

7

G H I J K L M
Writing data_2.dat 

In [3]:

1

%%file data_3.dat

2

a

3

b c

4

d e f

5

g h i j

6

k l m n o

7

p q r s t u
Writing data_3.dat 

In [4]:

1

%%file data_4.dat

2

0

3

1 2 

4

3 4 5

5

6 7 8 9
Writing data_4.dat 

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', 'J', '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

In [ ]:

1

# YOUR CODE HERE

2

raise NotImplementedError()

In [ ]:

1

#-------------------------------------------------------------------------------------- 

2

#  

3

#  W A R N I N G! 

4

#  

5

#

6

#  If you have not defined an instance field named `data` in your triangular class, 

7

# all remaining tests will fail!

8

#

9

#  Please follow the provided specifications!

10

#

11

#-------------------------------------------------------------------------------------- 

12

 

13

t = triangular( 'data_1.dat' )

14

 

15

'data' in dir( t.data )

16

 

17

assert hasattr( t, 'data' ), "Your triangular instance should have a 'data' field as specified!"

In [ ]:

1

t = triangular( 'data_1.dat' )

2

 

3

assert t.check() == True

4

 

5

t.data

In [ ]:

1

try:

2

 triangular( 'data_2.dat' )

3

 

4

except ValueError:

5

 print( "Got ValueError as expected!" )

6

 pass

7

 

8

else:

9

 assert False

10

 

In [ ]:

1

a = triangular( 'data_1.dat' )

2

c = triangular( 'data_3.dat' )

3

 

4

#print( a.data )

5

#print( c.data )

6

 

7

student_answer = a + c

8

correct_answer = \

9

[['1a'],

10

 ['2b', '3c'],

11

 ['4d', '5e', '6f'],

12

 ['7g', '8h', '9i', 'Aj'],

13

 ['Bk', 'Cl', 'Dm', 'En', 'Fo'],

14

 ['Gp', 'Hq', 'Ir', 'Js', 'Kt', 'Lu']]

15

 

16

#print( student_answer )

17

 

18

assert student_answer == correct_answer

19

 

In [ ]:

1

# Using ord() as the data_converter function

2

#

3

a = triangular( 'data_1.dat', ord )

4

d = triangular( 'data_4.dat', ord )

5

 

6

a.data = a.data[:4]

7

d.data = d.data[:4]

8

 

9

print( a.data )

10

print( d.data )

11

 

12

student_answer = a + d

13

 

14

#print( student_answer )

15

 

16

correct_answer = [[97], [99, 101], [103, 105, 107], [109, 111, 113, 122]]

17

 

18

#print( student_answer )

19

 

20

assert student_answer == correct_answer

21

 

In [ ]:

1

a = triangular( 'data_1.dat', ord )

2

d = triangular( 'data_4.dat', ord )

3

 

4

#print( a.data )

5

#print( d.data )

6

 

7

try:

8

 a + d

9

 

10

except AssertionError:

11

 print( 'Got AssertionError as expected' )

12

 pass

13

 

14

else:

15

 assert False

16

please solve it using python

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!