Question: In Code Fragment 5.1, we perform an experiment to compare the length of a Python list to its underlying memory usage. Determining the sequence of

In Code Fragment 5.1, we perform an experiment to compare the length of a Python list to its underlying memory usage. Determining the sequence of array sizes requires a manual inspection of the output of that program. Redesign the experiment so that the program outputs only those values of k at which the existing capacity is exhausted. For example, on a system consistent with the results of Code Fragment 5.2, your program should output that the sequence of array capacities are 0, 4, 8, 16, 25, . . . .

Code Fragment 5.1

1 import sys 2 data = [] 3 for k in range(n):

Code Fragment 5.2

4 # provides getsizeof function %3D # NOTE: must fix choice of

1 import sys 2 data = [] 3 for k in range(n): 4 # provides getsizeof function %3D # NOTE: must fix choice of n # number of elements # actual size in bytes Size in bytes: {1:4d}'.format(a, b)) # increase length by one a = len(data) b = sys.getsizeof(data) print('Length: {0:3d}; data.append(None) Length: 0; Size in bytes: Length: 1; Size in bytes: 104 Length: 2; Size in bytes: 104 Length: 3; Size in bytes: 104 Length: 4; Size in bytes: 104 Length: 5; Size in bytes: 136 Length: 6; Size in bytes: 136 Length: 7; Size in bytes: 136 Length: 8; Size in bytes: 136 Length: 9; Size in bytes: 200 Length: 10; Size in bytes: 200 Length: 11; Size in bytes: 200 Length: 12; Size in bytes: 200 Length: 13; Size in bytes: 200 Length: 14; Size in bytes: 200 Length: 15; Size in bytes: 200 Length: 16; Size in bytes: 200 Length: 17; Size in bytes: 272 Length: 18; Size in bytes: 272 Length: 19; Size in bytes: 272 Length: 20; Size in bytes: 272 Length: 21; Size in bytes: 272 Length: 22; Size in bytes: 272 Length: 23; Size in bytes: 272 Length: 24; Size in bytes: 272 Length: 25; Size in bytes: 272 Length: 26; Size in bytes: 352 72

Step by Step Solution

3.39 Rating (177 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Keep track of the maximum data size thus far import sys provides ... View full answer

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 Data Structures Algorithms Questions!