Debugging / Error Handling, Data Structures and Data Search
Discuss the ways in which data structures support object-oriented development. Use examples of three different data structures to contextualise your response.
In Object-oriented software, the data structures are associated with methods that operate on them. The Classes are a sort of template of objects that also define a data structure, a user-defined data structure. When classes use objects of another class, the predefined data structures can contain objects of the included class.
class HolidaySchedule():
holidays = []
class Person():
pass
def __init__(self, name, dob):
self.__name = name
self.__dob = dob
def get_name(self):
return self.__name
class Employee(Person):
pass
def __init__(self, name, dob):
Person.__init__(self, name, dob)
def request_holiday(self, date, ndays):
self.holiday = Holiday(date, ndays, self)
return self.holiday
In this example we use a list to store Holiday objects. The operations defined on list can be applied disregarding the type of data they store.
# Class to define the coordinates to be used by the Map
class Point:
pass
def __init__(self, x, y):
self.x = x # Represented as x in the simulator
self.y = y # Represented as y in the simulator
def __repr__(self):
return "(" + str(self.x) + "," + str(self.y) + ")"
# Class to define areas where the car will circulate. It uses a dictionary to store
# areas and their correspondent coordinates.
class Map:
map = dict()
pass
def __init__(self, size, incode):
self.generate_map(size, incode)
# Based on the accepted answer of
# https://stackoverflow.com/questions/4170054/generate-4000-unique-pseudo-random-cartesian-coordinates-faster
def generate_map(self, size, incode):
keys = range(1, size)
for i in keys:
self.map[incode + str(i)] = Point(random.randint(0, 99), random.randint(0, 99))
For our coding assessment, we will be using a dictionary to store point objects and an associated postcode. The flexibility of the dictionaries allow to store multiple types of data.
Create a nested dictionary of data on cars within a Car class. Extend the program to work with the dictionary by calling the following methods: items(), keys(), values()
class Car():
nested_dict = {}
def __init__(self, gear_box, engine):
gear_dict = dict(gear1=gear_box[0], gear2=gear_box[1])
engine_dict = dict(size=engine[0], n_cilinders=engine[1])
self.nested_dict['gear_box'] = gear_dict
self.nested_dict['engine'] = engine_dict
def get_nested_dict(self):
return self.nested_dict
def show_car_data(self):
for index, value in self.nested_dict.items():
print(value.values())
print(value.keys())
gear_box = ['3.00:1', '2.00:1']
engine = [1.6, 8]
obj_cheverolet = Car(gear_box, engine)
obj_cheverolet.show_car_data()
#dict_values(['3.00:1', '2.00:1'])
#dict_keys(['gear1', 'gear2'])
#dict_values([1.6, 8])
#dict_keys(['size', 'n_cilinders'])
# The same can be done by getting the dictionary using the get_nested_dict() method and looping through to print keys and values.
nested_dictionary = obj_cheverolet.get_nested_dict()
for index, value in nested_dictionary.items():
print(value.values())
print(value.keys())
The items() function is used to obtain the top level keys and values of the dictionary. We can see the values() function returns a list of all values in the dictionary and the keys() function a list of all keys in the dictionary.