Question
COMPLETE THE FOLLWING PROGRAM IN PYTHON: initfunction.py (do not modify): class IntFunction: def __init__(self, f, length): self._f = f self._length = length def __getitem__(self, k):
COMPLETE THE FOLLWING PROGRAM IN PYTHON:
initfunction.py (do not modify):
class IntFunction: def __init__(self, f, length): self._f = f self._length = length
def __getitem__(self, k): if isinstance(k, slice): start = k.start or 0 step = k.step or 1 stop = k.stop or len(self) return [self[i] for i in range(start, stop, step)] if k != int(k): raise TypeError('indices must be integers') if k = len(self): raise IndexError('index out of range') return self._f(k)
def __iter__(self): for i in range(len(self)): yield self[i]
def __len__(self): return self._length
Organized Truck In this homework, you will implement an iterable class called OrganizedTruck that stores a truck of rotated sorted boxes and provides O(log n) time search operations. Here, we assume that the boxes are sorted by their volume and the boxes with the same volume are equal. Also, an OrganizedTruck may contain repeating elements. Rotated sorted truck We call a Truck rotated sorted if the rotation is applied on a sorted sequence of boxes. For example, the following code shows a rotated sorted truck L= [Box (5,1,1), t = Truck(L) Box (3,2,1), Box(1,1,1), Box ( 2, 1,1), Box ( 1,3,1), Box (2,1,2)] The volume of boxes in L are 5, 6, 1, 2, 3, 4 and it is a sorted sequence rotated for 2 steps to the right. A truck can store boxes with the same volume but different dimensions. Here, we consider the boxes with the same volume to be equal. A truck may contain repeating elements. However, all the repeating elements in a truck should be adjacent. For example, the following truck is a valid rotated sorted truck with repetition L = [Box (4,1,1), t Truck(L) Box ( 1,2,2), Box(1,1,1), Box ( 2,1,1), Box ( 1,3,1), Box (3,1,1)] The following code shows an invalid rotated sorted truck with repetition, since Box(4,1,1) and Box(1,2,2) are the same but not adjacent. L = [Box (1,2,2), t Truck(L) Box ( 1,1,1), Box(2,1,1), Box(1,3,1), Box (3,1,1), Box (4,1,1)] Organized Truck In this homework, you will implement an iterable class called OrganizedTruck that stores a truck of rotated sorted boxes and provides O(log n) time search operations. Here, we assume that the boxes are sorted by their volume and the boxes with the same volume are equal. Also, an OrganizedTruck may contain repeating elements. Rotated sorted truck We call a Truck rotated sorted if the rotation is applied on a sorted sequence of boxes. For example, the following code shows a rotated sorted truck L= [Box (5,1,1), t = Truck(L) Box (3,2,1), Box(1,1,1), Box ( 2, 1,1), Box ( 1,3,1), Box (2,1,2)] The volume of boxes in L are 5, 6, 1, 2, 3, 4 and it is a sorted sequence rotated for 2 steps to the right. A truck can store boxes with the same volume but different dimensions. Here, we consider the boxes with the same volume to be equal. A truck may contain repeating elements. However, all the repeating elements in a truck should be adjacent. For example, the following truck is a valid rotated sorted truck with repetition L = [Box (4,1,1), t Truck(L) Box ( 1,2,2), Box(1,1,1), Box ( 2,1,1), Box ( 1,3,1), Box (3,1,1)] The following code shows an invalid rotated sorted truck with repetition, since Box(4,1,1) and Box(1,2,2) are the same but not adjacent. L = [Box (1,2,2), t Truck(L) Box ( 1,1,1), Box(2,1,1), Box(1,3,1), Box (3,1,1), Box (4,1,1)]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started