def test_insert(self):
        d = HashMap()
        d.insert(lower_to_upper.items())
        self.assertEqual(len(d), len(lowercase))

        for ll, ul in zip(lowercase, uppercase):
            self.assertEqual(d[ll], ul)
Exemple #2
0
class Environment(object):
    def __init__(self):
        self.__objects = [] # World objects
        self.__hashmap = HashMap()

    def addObject(self, object):
        """
        Add an object to the environment
        """
        object.setEnvironment(self)
        self.__objects.append(object)

    def removeObject(self, object):
        """
        Remove an object from the environment
        """
        self.__objects.remove(object)

    def step(self, time):
        """
        Step forward with controllers attached to each object
        """
        for object in self.__objects:
            object.processControllers(time)

        # After processing the controllers, check for collisions
        self.checkCollisions()

    def inputStep(self, mouse_x, mouse_y):
        """
        Pass inputs to the objects
        """
        for object in self.__objects:
            object.mouseEvent(mouse_x, mouse_y)
            #TODO: Add a keyboard event here

    def addBlock(self, x, y):
        self.__hashmap.insert((x, y))

    def clearMap(self):
        self.__hashmap.clear()

    def checkCollisions(self):
        position = self.__objects[0].getPosition()
        print "Character position: ", position.x, position.y
        print "Hash output: ", self.__hashmap.get((position.x, position.y))
        if self.__hashmap.get((position.x, position.y)):
            print "collision!"
            self.__objects[0].gravity.stop()
            self.__objects[0].gravity.reset()

            self.__objects[0].jump.stop()
        else:
            self.__objects[0].gravity.start()
Exemple #3
0
def main():
    m = HashMap()

    ops = [
        ('insert', (5, 6), (True, )),
        ('lookup', (5, ), (6, )),
        ('insert', (7, 3), (True, )),
        ('lookup', (7, ), (3, )),
        ('lookup', (5, ), (6, )),
        ('insert', (5, 2), (False, )),
        ('lookup', (5, ), (2, )),
        ('erase', (5, ), (True, )),
        ('lookup', (5, ), (None, )),
        ('erase', (3, ), (False, )),
    ]

    for op, input, expected_output in ops:
        if op == 'insert':
            output = (m.insert(*input), )
        elif op == 'lookup':
            output = (m.lookup(*input), )
        else:
            output = (m.erase(*input), )
        assert output == expected_output, 'Expected {} == {}'.format(
            output, expected_output)
        print('Succeed')
Exemple #4
0
def main():
    m = HashMap()

    ops = [
        ('insert', (5, 6), (True,)), ('lookup', (5,), (6,)),
        ('insert', (7, 3), (True,)), ('lookup', (7,), (3,)),
        ('lookup', (5,), (6,)), ('insert', (5, 2), (False,)),
        ('lookup', (5,), (2,)), ('erase', (5,), (True,)),
        ('lookup', (5,), (None,)), ('erase', (3,), (False,)),
    ]

    for op, input, expected_output in ops:
        if op == 'insert':
            output = (m.insert(*input),)
        elif op == 'lookup':
            output = (m.lookup(*input),)
        else:
            output = (m.erase(*input),)
        assert output == expected_output, 'Expected {} == {}'.format(
            output, expected_output)
        print('Succeed')
Exemple #5
0
class Distance:
    def __init__(self):
        self.address = HashMap()
        self.distance = []
        self.address_manager = []
        self.combo = {}

    def add_address(self, address, zip_code):
        # Big O: O(1)
        # inserts address into address list
        self.address.insert(address, zip_code)

    def print_addresses(self):
        # Big O: O(1)
        # keys to address list
        self.address.keys()

    def add_distance(self, distance):
        # Big O: O(1)
        # appends distance to list
        self.distance.append(distance)

    def combine(self):
        # Big O: O(N^2)
        # combines the two separate lists (addresses and distances)
        # into a usable dictionary
        keys = self.address.keys()
        distances = self.distance
        for i in keys:
            for j in distances:
                if i in j:
                    del j[0]
                    self.combo[i] = j
        return self.combo

    def distances_from_address(self, address):
        # Big O: O(1)
        # return all distances from address
        return self.combo[address]

    def distance_to_specific(self, address_a, index_b):
        # Big O: O(1)
        # Retrieve the distance between two addresses
        return float(self.combo[address_a][index_b - 1])

    def manage_address(self, address):
        # Big O: O(1)
        # appends address to an address list
        self.address_manager.append(address)
        return self.address_manager

    def index_to_address(self, index):
        # Big O: O(1)
        # finds the address associated with an index
        return self.address_manager[index]

    def address_to_index(self, address):
        # Big O: O(N)
        index_number = 0
        for i in self.address_manager:
            index_number += 1
            if i == address:
                return index_number

    def closest_neighbor(self, address):
        # Big O: O(N)
        # return index # of closest distance
        testing = []
        for i in self.combo[address]:
            testing.append(float(i))
        m = min(i for i in testing if i > 0)
        min_index = testing.index(m)
        closest = self.index_to_address(min_index)
        print('Between {} and {} is {}'.format(address, closest, m))

    def distance_traveled(self, time, list):
        # Big O: O(N)
        # calculates the distances between each address in the list
        # returns the distances in list form
        a = list
        distance_between = []
        for i, nexti in zip(a, a[1::]):
            next_index = self.address_to_index(nexti)
            distance_between.append(self.distance_to_specific(i, next_index))
        return distance_between

    def load_distances(self, node_list, combo, distances):
        # Big O: O(N)
        # read files, sets up addresses and distances between each address
        with open(node_list) as set_address:
            for line in set_address:
                line = line.strip().split('\t')
                distances.add_address(line[0], line)
                distances.manage_address(line[0])
        with open(combo) as set_distance:
            for line in set_distance:
                split = line.strip().split('\t')
                distances.add_distance(split)
        distances.combine()
Exemple #6
0
        # Delivery One
        if package[6] != 'EOD' and ('Must' in package[8]
                                    or 'None' in package[8]):
            deliveries[0].append(package)
        # Delivery Two
        if 'Can only be' in package[8] or 'Delayed' in package[8]:
            deliveries[1].append(package)

        # Puts remaining packages into delivery lists
        if package not in deliveries[0] and package not in deliveries[
                1] and package not in deliveries[2]:
            if len(deliveries[1]) < len(deliveries[2]):
                deliveries[1].append(package)
            else:
                deliveries[2].append(package)

        # Insert package value into hash table
        hashmap.insert(package_id, package)


# Returns packages for the specified delivery number
# Space-time complexity -> O(1)
def get_deliveries(num):
    return deliveries[num]


# Returns full list of packages
# Space-time complexity -> O(1)
def get_hashmap():
    return hashmap