The main function, it will simulate the evolution of the highway >>> simulate([[-1, 2, -1, -1, -1, 3]], 2, 0.0, 3) [[-1, 2, -1, -1, -1, 3], [-1, -1, -1, 2, -1, 0], [1, -1, -1, 0, -1, -1]] >>> simulate([[-1, 2, -1, 3]], 4, 0.0, 3) [[-1, 2, -1, 3], [-1, 0, -1, 0], [-1, 0, -1, 0],
(
highway: list, number_of_update: int, probability: float, max_speed: int
)
| 106 | |
| 107 | |
| 108 | def simulate( |
| 109 | highway: list, number_of_update: int, probability: float, max_speed: int |
| 110 | ) -> list: |
| 111 | """ |
| 112 | The main function, it will simulate the evolution of the highway |
| 113 | >>> simulate([[-1, 2, -1, -1, -1, 3]], 2, 0.0, 3) |
| 114 | [[-1, 2, -1, -1, -1, 3], [-1, -1, -1, 2, -1, 0], [1, -1, -1, 0, -1, -1]] |
| 115 | >>> simulate([[-1, 2, -1, 3]], 4, 0.0, 3) |
| 116 | [[-1, 2, -1, 3], [-1, 0, -1, 0], [-1, 0, -1, 0], [-1, 0, -1, 0], [-1, 0, -1, 0]] |
| 117 | """ |
| 118 | |
| 119 | number_of_cells = len(highway[0]) |
| 120 | |
| 121 | for i in range(number_of_update): |
| 122 | next_speeds_calculated = update(highway[i], probability, max_speed) |
| 123 | real_next_speeds = [-1] * number_of_cells |
| 124 | |
| 125 | for car_index in range(number_of_cells): |
| 126 | speed = next_speeds_calculated[car_index] |
| 127 | if speed != -1: |
| 128 | # Change the position based on the speed (with % to create the loop) |
| 129 | index = (car_index + speed) % number_of_cells |
| 130 | # Commit the change of position |
| 131 | real_next_speeds[index] = speed |
| 132 | highway.append(real_next_speeds) |
| 133 | |
| 134 | return highway |
| 135 | |
| 136 | |
| 137 | if __name__ == "__main__": |