Update the speed of the cars >>> update([-1, -1, -1, -1, -1, 2, -1, -1, -1, -1, 3], 0.0, 5) [-1, -1, -1, -1, -1, 3, -1, -1, -1, -1, 4] >>> update([-1, -1, 2, -1, -1, -1, -1, 3], 0.0, 5) [-1, -1, 3, -1, -1, -1, -1, 1]
(highway_now: list, probability: float, max_speed: int)
| 79 | |
| 80 | |
| 81 | def update(highway_now: list, probability: float, max_speed: int) -> list: |
| 82 | """ |
| 83 | Update the speed of the cars |
| 84 | >>> update([-1, -1, -1, -1, -1, 2, -1, -1, -1, -1, 3], 0.0, 5) |
| 85 | [-1, -1, -1, -1, -1, 3, -1, -1, -1, -1, 4] |
| 86 | >>> update([-1, -1, 2, -1, -1, -1, -1, 3], 0.0, 5) |
| 87 | [-1, -1, 3, -1, -1, -1, -1, 1] |
| 88 | """ |
| 89 | |
| 90 | number_of_cells = len(highway_now) |
| 91 | # Beforce calculations, the highway is empty |
| 92 | next_highway = [-1] * number_of_cells |
| 93 | |
| 94 | for car_index in range(number_of_cells): |
| 95 | if highway_now[car_index] != -1: |
| 96 | # Add 1 to the current speed of the car and cap the speed |
| 97 | next_highway[car_index] = min(highway_now[car_index] + 1, max_speed) |
| 98 | # Number of empty cell before the next car |
| 99 | dn = get_distance(highway_now, car_index) - 1 |
| 100 | # We can't have the car causing an accident |
| 101 | next_highway[car_index] = min(next_highway[car_index], dn) |
| 102 | if random() < probability: |
| 103 | # Randomly, a driver will slow down |
| 104 | next_highway[car_index] = max(next_highway[car_index] - 1, 0) |
| 105 | return next_highway |
| 106 | |
| 107 | |
| 108 | def simulate( |
no test coverage detected