MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / get_distance

Function get_distance

cellular_automata/nagel_schrekenberg.py:60–78  ·  view source on GitHub ↗

Get the distance between a car (at index car_index) and the next car >>> get_distance([6, -1, 6, -1, 6], 2) 1 >>> get_distance([2, -1, -1, -1, 3, 1, 0, 1, 3, 2], 0) 3 >>> get_distance([-1, -1, -1, -1, 2, -1, -1, -1, 3], -1) 4

(highway_now: list, car_index: int)

Source from the content-addressed store, hash-verified

58
59
60def get_distance(highway_now: list, car_index: int) -> int:
61 """
62 Get the distance between a car (at index car_index) and the next car
63 >>> get_distance([6, -1, 6, -1, 6], 2)
64 1
65 >>> get_distance([2, -1, -1, -1, 3, 1, 0, 1, 3, 2], 0)
66 3
67 >>> get_distance([-1, -1, -1, -1, 2, -1, -1, -1, 3], -1)
68 4
69 """
70
71 distance = 0
72 cells = highway_now[car_index + 1 :]
73 for cell in range(len(cells)): # May need a better name for this
74 if cells[cell] != -1: # If the cell is not empty then
75 return distance # we have the distance we wanted
76 distance += 1
77 # Here if the car is near the end of the highway
78 return distance + get_distance(highway_now, -1)
79
80
81def update(highway_now: list, probability: float, max_speed: int) -> list:

Callers 1

updateFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected