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...
-
State with reasons whether the following propositions are correct. (a). In an undertaking with a high fixed cost, break-even point can be attained at a lower level of activity. (b). Profit is...
-
Show that during the early part of the electron-positron annihilation era, the ratio of the electron number density to the photon number density scaled with temperature as \[\frac{n_{-}}{n_{\gamma}}...
-
Emelio and Charita are married taxpayers with 2 dependent children. Emelio starts a computer consulting business in 2011. Charita works as a real estate broker. During 2011, they have the following...
-
A small electric immersion heater is used to heat 87 g of water for a cup of instant coffee. The heater is labeled "120 watts" (it converts electrical energy to thermal energy at this rate)....
-
You will need to use VerilUOC Desktop to answer this question. Please, complete the module "VerilUOC_Desktop tools", inside this week 2, before performing this exercise. Write the Boolean expression...
-
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...
-
Citrus Company is considering a project that has estimated annual net cash flows of $32,000 for six years and is estimated to cost $150,000. Citruss cost of capital is 8 percent. Calculate the net...
-
What is the purpose of a risk pool?
-
How can futures contracts be used to hedge interest rate risk?
-
Briefly, describe one common approach to risk management.
-
How do options and futures differ?
-
What is the difference between a hostile and a friendly merger?
-
What is the difference between the articles of incorporation and a charter?
-
KD Insurance Company specializes in term life insurance contracts. Cash collection experience shows that 20 percent of billed premiums are collected in the month before they are due, 60 percent are...
-
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;...
-
Randy Sneaker Company is a maker of mid-priced sneakers. They have recently made a new running sneaker but it has not met its sales expectations. As a result, the company president has asked you to...
-
a. Factorize the following expression 6x+13x-5 b. Express 20% as a decimal number c. Simplify (3y+5) (2y-3) - (y+7)(5y - 1). d. Simplify 7x+x-x-5x-3x-6x4 e. Solve -3x+4 <11 hence represent it...
-
Q4(a) Personal protective equipment (PPE) refers to the equipment worn to minimize the worker's exposure to the hazards which causes occupational injuries and illness. Explain three (3) factors which...
Study smarter with the SolutionInn App