Let’s model parallel parking of cars. Suppose there is a street, on which if cars parked in the most efficient manner with no unnecessary gaps, the street would fit n cars (we need only concern ourselves with one side of the street). We can consider this street to be n car-lengths long (where the minimum necessary gap between cars gets incorporated into the unit of “one car length”). For ease, we can assume that all the cars we consider take up the same space along a street, i.e. 1 car-length.
Suppose the street is initially empty. Then a car parks, and given the entire street is available, the car parks randomly anywhere along the street — the car has an equal probability of selecting any position along the street. Then a second car comes along, and also parks randomly, with the only constraint that the space the second car occupies cannot overlap with the space occupied by the first car (a physical impossibility). Thus it has an equal probability of selecting any position along the street provided its viable, i.e. it doesn’t overlap with the already parked car. The third car also parks randomly, but this time it must not overlap with either of the previous two cars and so on … until there are no gaps large enough to fit one more car.
The process then terminates, with a number of cars necessarily ≤n .
A) Write a function that simulates the process of an n-car-length street filling up with parallel-parked cars. The function should return:
An array pos of floats containing the positions of the front of the cars along the street (note that all cars are assumed to be the same length).
An integer occ that represents the final occupancy of the street number of cars that managed to park along the street.
I dont know how to go about it
Imelda Masika is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.