Class Lane

A lane consists of a number of positions that can either contain a vehicle or be empty (i.e. None).

Vehicles move into one end (the largest index) and out of the other end (index 0).

bild

Simplifications of reality: A lane consists of a number of discrete positions and all vehicles take up equal space.

What happens to the lane as time increases?

bild

bild

bild

bild

bild

Observe that a vehicle is moved no more than one position per time step!

Simplification of reality: A vehicle either stands still or moves one place per time step.

What happens at the ends of Lane?

The Lane class does not know what is in front or behind. It is thus "someone else" who makes sure that vehicles enter or leave the lane.

Lane must provide a method to take out the vehicle on the first position and then return the vehicle that was previously there:

bild

There must also be a method that puts a vehicle to the last place:

bild

If you put a vehicle to the last place and it is not free, you will lose what was on that place:

bild

It must also be possible to investigate whether the last place is vacant or not.

The class must include the following methods:

In the main program, the Lane objects must be handled with these methods and thus be independent of the internal representation, i.e. you're not allowed to access or modify the lane's attribute from outside the object, use the methods instead!

Below you can find code with print outs that demonstrate the different methods:

Kod Utskrift
def demo_lane(): """Demonstration of the class Lane""" a_lane = Lane(10) print(a_lane) v = Vehicle('N', 34) a_lane.enter(v) print(a_lane) a_lane.step() print(a_lane) for i in range(20): if i % 2 == 0: u = Vehicle('S', i) a_lane.enter(u) a_lane.step() print(a_lane) if i % 3 == 0: print(' out: ', a_lane.remove_first()) print('Number in lane:', a_lane.number_in_lane()) [..........] [.........N] [........N.] [.......NS.] out: None [......NS..] [.....NS.S.] [....NS.S..] out: None [...NS.S.S.] [..NS.S.S..] [.NS.S.S.S.] out: None [NS.S.S.S..] [NSS.S.S.S.] [NSSS.S.S..] out: Vehicle(N, 34) [SSS.S.S.S.] [SSSS.S.S..] [SSSSS.S.S.] out: Vehicle(S, 0) [SSSS.S.S..] [SSSSS.S.S.] [SSSSSS.S..] out: Vehicle(S, 2) [SSSSS.S.S.] [SSSSSS.S..] [SSSSSSS.S.] out: Vehicle(S, 4) [SSSSSS.S..] Number in lane: 7

Tillbaka

Valid CSS!