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...
-
Street, Rhode and Close carried on business in partnership sharing profits and losses, in the ratio 5 : 4 : 3. Their draft statement of financial position as on 31 March 20X9 was as follows: Street...
-
Name two performance measures useful in evaluating investment centers.
-
Equity Securities Lexington Co. has the following available-for-sale securities outstanding on December 31, 2010 (its first year of operations). During 2011 Summerset Company stock was sold for...
-
Select from the option list provided the word or phrase that correctly completes each statement related to GAAP and non-GAAP measures. A B 1 Statement Missing Term 2 The U.S. Securities and Exchange...
-
Given a password that is 15 characters long where each character can be one of the 52 upper- and lower-case letters, 10 digits or 32 punctuation symbols and assuming each hash requires 1 ns, compute...
-
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...
-
Production systems can be divided into two categories or levels. Name and briefly define the two levels.
-
A sailor on a wharf is hauling on a line pulling in a ship. The rope makes an angle of 25 with the horizontal and the force the sailor is using is 220 N. What amount of the force is being used to...
-
Grammer and Language, Culture, and Community Review Questions 1. What sign movement is being used to communicate that something is done regularly or habitually? 2. Look at Figures 5.3-5.6. How are...
-
The McDonald Manufacturing of Wichita Falls, Texas is evaluating a proposal to open a manufacturing plant in Diekirch, Luxembourg to permit European production and distribution of its products....
-
Please this using Java OVERVIEW The programs we've discussed so far are generally structured as methods that call one another in a hierarchical manner. For some problems, it's useful to have a method...
-
Warf Computers has decided to proceed with the manufacture and distribution of the virtual keyboard (VK) the company has developed. To undertake this venture, the company needs to obtain equipment...
-
What is a Gantt chart and what are its benefits?
-
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;...
-
O que Coaching Executivo? Escolha uma opo: a. Anteriormente era o treinador de um time de futebol. b. Terapia neuropsicolgica para superar um trauma de infncia que afeta o nosso trabalho ou ambiente...
-
QUESTION 1 ( 2 0 Marks ) The fundamental ethical principles that all chartered accountants and registered auditors should comply with are laid down by the SAICA and IRBA code of professional conduct....
-
Hoytsville Company had the following account balances for 20X1. Account Amount Net Income $25,000 Retained Earnings (beginning of year) 40,000 Dividends 10,000 Unrealized Gain on Available-for-Sale...
Study smarter with the SolutionInn App