amanpy >. Tarminal 38 ***Supports traversal with a for loop, return ster(self. stons) sendbas t oythond y. FIle "arrays.py", ine e for count in rare(len. (selt.itens), lentsels items) + 2): 41 defetiten.(elf. inden): 42 **Subseript operator for secess at index. 43 Precandition: e e index size() Syntatrror: invalid syntex rite your code hare sandbax ) 45 46 def setitem.(self. inder, etten) 47 **Subseript operator fer replacerent at index Precondition: e e index size() 40 Rrite your code hare 51 def stze(self): The nunbar of stans in the array. zaturn self.logicalsize 54 def grow(self): temparray l1at() far i in range(len(self.itoma)): tenearray. eppene(aelf.itena() self.itens temp.array far count in range(len. (self itens), lan(self.Stans) 2): self.itans.append(self.fillvalua) def sheink (self: 1f(self.capacity (len(self.itera)/2)): tenparray list() for i in zange(len(self.items)/r2): tenp.erray.append(self.ites(]) self.Itens tenp.array

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Getting the attached error when inputting the below syntax...help

 

ORIGINAL QUESTION: 

In the Array class of the arrays.py file complete the following:

  1. Add preconditions to the methods __getitem__ and __setitem__.
    • The precondition of each method is 0 <= index < size().
    • Be sure to raise an IndexError exception if the precondition is not satisfied.

To test your program run the main() method in the arrays.py file.

Your program's output should look like the following:

Physical size: 10 Logical size: 0 Items: [None, None, None, None, None, None, None, None, None, None] Traceback (most recent call last): File ".solution/arrays.py", line 69, in <module> main() File ".solution/arrays.py", line 66, in main print(a[0]) File ".solution/arrays.py", line 45, in __getitem__ raise IndexError("Array index out of bounds") IndexError: Array index out of bounds
 
 
SYNTAX

 

Adds a precondition on __getitem__ and __setitem__
and raises an exception if it is not satisfied.

An Array is a restricted list whose clients can use
only [], len, iter, and str.

To instantiate, use

<variable> = array(<capacity>, <optional fill value>)

The fill value is None by default.
"""

class Array(object):
    """Represents an array."""

    def __init__(self, capacity, fillValue = None):
        """Capacity is the static size of the array.
        fillValue is placed at each position."""
        self.items = list()
        self.logicalSize = 0
        for count in range(capacity):
            self.items.append(fillValue)

    def __len__(self):
        """-> The capacity of the array."""
        return len(self.items)

    def __str__(self):
        """-> The string representation of the array."""
        return str(self.items)

    def __iter__(self):
        """Supports traversal with a for loop."""
        return iter(self.items)

    def __getitem__(self, index):
        """Subscript operator for access at index.
        Precondition: 0 <= index < size()"""
        # Write your code here

    def __setitem__(self, index, newItem):
        """Subscript operator for replacement at index.
        Precondition: 0 <= index < size()"""
        # Write your code here 

    def size(self):
        """-> The number of items in the array."""
        return self.logicalSize
    
    def grow(self):
        temp_array = list()
        for i in range(len(self.items)):
            temp_array.append(self.items[i])
        self.items = temp_array
        for count in range(len.(self.items), len(self.items) * 2):
            self.items.append(self.fillValue)

    def shrink(self):
        if(self.capacity <= (len(self.items)/2)):
            temp_array = list()
            for i in range(len(self.items)//2):
                temp_array.append(self.items[i])
            self.items = temp_array

def main():
    """Test code for modified Array class."""
    a = Array(10)
    print("Physical size:", len(a))
    print("Logical size:", a.size())
    print("Items:", a)
    print(a[0])

if __name__ == "__main__":
    main()

amay
. Teinal
38
**Supports traversal witha for loop.
Teturn Ster(self.stens)
sendbas t pythond ys.
FIle "arrays.py", ine e
for count in care (len. (self. itens), lenelf
itema) • 2):
-40
defgetiten.(elf, inden)
42
**Subseript operator for sccess at index.
43
Precandition: e e index size()
41
Syntatrror: invalid syntex
sandaa 0
Rrite your code here
def setiten.(self, inder, etten)
**subseript operator for replacenent at index.
Precandition: e index size()
46
47
40
rite your code hare
def slze(self):
The nunbar of stens in the array.
zaturn self.logicalsize
54
def ero(sel):
temparray l1at)
far i in range(ien(aelf.itoma)):
tenp aray. eppend(aelf.itena()
self.itens temp.array
far count in range(len. (self itens), lan(self.Stans) 2):
self.itans.append(self.fillvalua)
57
se
def sheink (self)
1f(self.capacity (len(self.itera)/2)):
tenparay liat()
for i in zange(len(self.items)/r2):
tenp.erray.append(self.ites(i]]
self. Itens tenparray
Transcribed Image Text:amay . Teinal 38 **Supports traversal witha for loop. Teturn Ster(self.stens) sendbas t pythond ys. FIle "arrays.py", ine e for count in care (len. (self. itens), lenelf itema) • 2): -40 defgetiten.(elf, inden) 42 **Subseript operator for sccess at index. 43 Precandition: e e index size() 41 Syntatrror: invalid syntex sandaa 0 Rrite your code here def setiten.(self, inder, etten) **subseript operator for replacenent at index. Precandition: e index size() 46 47 40 rite your code hare def slze(self): The nunbar of stens in the array. zaturn self.logicalsize 54 def ero(sel): temparray l1at) far i in range(ien(aelf.itoma)): tenp aray. eppend(aelf.itena() self.itens temp.array far count in range(len. (self itens), lan(self.Stans) 2): self.itans.append(self.fillvalua) 57 se def sheink (self) 1f(self.capacity (len(self.itera)/2)): tenparay liat() for i in zange(len(self.items)/r2): tenp.erray.append(self.ites(i]] self. Itens tenparray
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY