예제 #1
0
 def __add__(self, other):
     """Returns a new bag containing the contents
     of self and other."""
     result = ArrayBag(self)
     for item in other:
         result.add(item)
     return result
예제 #2
0
def distributeBag(bag):
    redBag = ArrayBag()
    blueBag = ArrayBag()

    # Move the balls to the appropriate bags:

    # <your code>

    for item in bag:

        if (item.getColor() == 'red'):
            redBag.add(item)
        elif (item.getColor() == 'blue'):
            blueBag.add(item)
    bag.clear()

    # Return the 2 bags:
    return (redBag, blueBag)
예제 #3
0
def main():
    bag1 = ArrayBag([1, 2, 3, 4, 5, 6])

    print("bag1 is", bag1)

    bag2 = ArrayBag([7, 8, 9, 10, 11])
    print("bag2 is", bag2)

    bag1 = bag1 + bag2
    print("bag1 after bag1 + bag2 =", bag1, "length =", len(bag1))

    sep()

    for number in range(1, 12):
        if number in bag1:
            bag1.remove(number)

    print("bag1 after removing every number in range(1,12) =", bag1,
          "length =", (len(bag1)))
    bag1.remove(0)
예제 #4
0
def testResize():
    """Tests the resizing of an array-based bag,
    when space is wasted."""
    bag = ArrayBag(range(100))
    print("Added 100 items, length of bag =", len(bag))
    print("Length of array =", len(bag._items))
    for item in range(76):
        bag.remove(item)
    print("Removed 76 items, length of bag =", len(bag))
    print("Length of array =", len(bag._items))
    for item in range(76, 100):
        bag.remove(item)
    print("Removed remaining items, length of bag =", len(bag))
    print("Length of array =", len(bag._items))
예제 #5
0
    b1.add(25)
    b1.remove(25)
    print("b1.add(25), b1.remove(25): Expect // {} //", b1)
    b1 = bagType(lyst)
    b2 = bagType(b1)
    print("b1=bagType(lyst), b2=bagType(b1), b1==b2: Expect // True //",
          b1 == b2)
    print("b1 is b2: Expect // False //", b1 is b2)
    for item in lyst:
        b1.remove(item)
    print("b1.remove the items in lyst: Expect // {} //", b1)
    print("b2.remove(99): Expect crash with KeyError:")
    b2.remove(99)


if None:
    print("yeah")
b3 = ArrayBag()
b3.add(3)
b3.add(5)
b3Iter = iter(b3)
print(b3Iter)
b3b = next(b3Iter)
b3b = next(b3Iter)
print(b3Iter)
print(b3b)

#test(LinkBag)
test(ArrayBag)
#test(ArraySortedBag)
예제 #6
0
    for item in bag:
        str(item)
        print(item)

    if bag.isEmpty():
        print('empty')
    print()


# Test 1:
print("Test 1:")
bag1 = ArrayBag([
    Ball("red", 1),
    Ball("red", 2),
    Ball("red", 3),
    Ball("blue", 1),
    Ball("blue", 2),
    Ball("blue", 3)
])
print("Original mixed bag:")
printBag(bag1)
redBag, blueBag = distributeBag(bag1)
print("Red bag:")
printBag(redBag)
print("Blue bag:")
printBag(blueBag)
print("Final mixed bag:")
printBag(bag1)

# Test 2:
print("Test 2:")
예제 #7
0
'''
Ricky Cheah
Feb 26, 2020
Lab 4

This is a driver function to test the clone method in ArrayBag and LinkedBag
classes. 

'''
from arraybag import ArrayBag
from linkedbag import LinkedBag

print("Testing clone method on ArrayBag.")
arrayBag1 = ArrayBag([2, 3, 4, "abcd"])
print("arrayBag1 is", arrayBag1)
print("\nExecuting arrayBag2 = arrayBag1.clone()")

arrayBag2 = arrayBag1.clone()
print("arrayBag2 is", arrayBag2)
print()
print("arrayBag2 == arrayBag1:", arrayBag2 == arrayBag1)
print("arrayBag2 is arrayBag1:", arrayBag2 is arrayBag1)

print("_" * 70)

print("Testing clone method on LinkedBag.")
linkedBag1 = LinkedBag([4, 5, 6,
                        "defg"])  #element order will be reversed in list
print("linkedBag1 is", linkedBag1)
print("\nExecuting linkedBag2 = linkedBag1.clone()")