Our DynamicArray class, as given in Code Fragment 5.3, does not support use of negative indices with
Question:
Our DynamicArray class, as given in Code Fragment 5.3, does not support use of negative indices with _ _getitem_ _. Update that method to better match the semantics of a Python list.
Code Fragment 5.3
Transcribed Image Text:
1 import ctypes # provides low-level arrays 3 class DynamicArray: "A dynamic array class akin to a simplified Python list." 4 def --init.--(self): "Create an empty array." self._n = 0 6. # count actual elements # default array capacity # low-level array self.capacity = 1 self.A = self.make_array(self._capacity) 9. 10 11 def --len -(self): """ Return number of elements stored in the array." 12 13 return self._n 14 15 def --getitem --(self, k): """' Return element at index k." 16 17 if not 0 <= k < self._n: 18 raise IndexError('invalid index') return self._A[k] 19 # retrieve from array 20 21 def append(self, obj): """ Add object to end of the array." if self.n == self._capacity: self._resize(2 * self.capacity) self._A[self._n] = obj self._n += 1 22 23 # not enough room 24 # so double capacity 25 26 27 28 def -resize(self, c): 'Resize internal array to capacity c.' B = self._make_array(c) for k in range(self.n): B[k] = self.A[k] self._A = B # nonpublic utitity 29 30 # new (bigger) array # for each existing value 31 32 33 # use the bigger array 34 self._capacity = c 35 36 def -make.array(self, c): Return new array with capacity c.' return (c * ctypes.py_object)( ) # nonpublic utitity 37 38 # see ctypes documentation 39
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 62% (8 reviews)
An index such as 3 is equivalent to t...View the full answer
Answered By
Abigael martinez
I have been a tutor for over 3 years and have had the opportunity to work with students of all ages and backgrounds. I have a strong belief that all students have the ability to learn and succeed if given the right tools and support. I am patient and adaptable, and I take the time to get to know each student's individual learning style in order to best support their needs. I am confident in my ability to help students improve their grades and reach their academic goals.
5.00+
1+ Reviews
10+ Question Solved
Related Book For
Data Structures and Algorithms in Python
ISBN: 978-1118290279
1st edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Question Posted:
Students also viewed these Computer science questions
-
Although keys in a map are distinct, the binary search algorithm can be applied in a more general setting in which an array stores possibly duplicative elements in nondecreasing order. Consider the...
-
What is the running time of parenthesize(T, T.root( )), as given in Code Fragment 8.26, for a tree T with n nodes? Fragment 8.26 1 /** Prints parenthesized representation of subtree of T rooted at p....
-
Consider the following variant of the findIndex method of the SortedTableMap class, originally given in Code Fragment 10.11: Does this always produce the same result as the original version? Justify...
-
In the game of chess, if a board position is repeated three times with the same side to move, the side to move can declare a draw. Describe how you could test this condition using a computer program.
-
A hospital administrator is faced with the problem of having a limited amount of funds available for capital projects. He has narrowed his choice down to two pieces of x-ray equipment, since the...
-
A random variable has P (X = x) = x/15 for x = 1, 2, 3, 4, 5, and 0 otherwise. Find the mean and variance of X.
-
Outline some of the different small business models in the current industry. LO.1
-
Starbucks Corporationlike all other businessesmakes adjusting en-tries at year- end in order to measure assets, liabilities, revenues, and expenses properly. Examine Starbucks Corporations Balance...
-
Kennel Waste Management has a subsidiary that disposes of hazardous waste and a subsidiary that collects and disposes of residential garbage. Information related to the two subsidiaries follows:...
-
Aliza Grajek is a self-employed nurse with 2017 gross income of $68,000 and taxable annual income (federal and state) of $55,000 after adjustments, exemptions, and deductions. Calculate her total...
-
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...
-
The constructor for the CaesarCipher class in Code Fragment 5.11 can be implemented with a two-line body by building the forward and backward strings using a combination of the join method and an...
-
Compute the discrete convolution \(y(n)=x(m) \bullet h(m)\), where \(\{x(k), k=-1,0,1,2\}=[-1.9\), \(0.5,1.2,1.5]\{h(k), k=0,1,2\}=[-2.1,1.2,0.8]\).
-
How do you demonstrate resilience as a leader during times of crisis or uncertainty, and what steps do you take to bolster your team's resilience ?
-
What would you do if it becomes clear to you that the potential successor you were grooming is not going to make the grade as a supervisor? What are your next steps? Do you think this grooming is...
-
How do services and products differ? What kind of decisions do companies make regarding products and services? Why are brands important to marketers? How do marketing strategies change during the...
-
What leadership principles do you feel you possess that are important for APRNs to exhibit? What principles do you need to explore to be more confident in performing? Which leadership style do you...
-
Question 1- Where do you go in the Courier to find out your amount of leftover inventory for a specific product last round? Based on the production tab of the worksheet I gave you; how do you use...
-
A particle, moving in a straight-line, passes through a fixed-price O. Its velocity v ms -1 , t seconds after passing through O, is given by v = 32/(t+2) 2 . a. Find the acceleration of the particle...
-
Read the Forecasting Supply Chain Demand Starbucks Corporation case in your text Operations and Supply Chain Management on pages 484-485, then address the four questions associated with the...
-
Consider the example of a breadth-first traversal given in Figure 8.15. Using the annotated numbers from that figure, describe the contents of the queue before each pass of the while loop in Code...
-
Draw a binary tree T that simultaneously satisfies the following: Each internal node of T stores a single character. A preorder traversal of T yields EXAMFUN. An inorder traversal of T yields...
-
Let T be an ordered tree with more than one node. Is it possible that the preorder traversal of T visits the nodes in the same order as the postorder traversal of T? If so, give an example;...
-
Accounting changes fall into one of three categories. Identify and explain these categories and give an example of each one.
-
Machinery is purchased on May 15, 2015 for $120,000 with a $10,000 salvage value and a five year life. The half year convention is followed. What method of depreciation will give the highest amount...
-
Flint Corporation was organized on January 1, 2020. It is authorized to issue 14,000 shares of 8%, $100 par value preferred stock, and 514,000 shares of no-par common stock with a stated value of $2...
Study smarter with the SolutionInn App