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

Function construct_highway

cellular_automata/nagel_schrekenberg.py:31–57  ·  view source on GitHub ↗

Build the highway following the parameters given >>> construct_highway(10, 2, 6) [[6, -1, 6, -1, 6, -1, 6, -1, 6, -1]] >>> construct_highway(10, 10, 2) [[2, -1, -1, -1, -1, -1, -1, -1, -1, -1]]

(
    number_of_cells: int,
    frequency: int,
    initial_speed: int,
    random_frequency: bool = False,
    random_speed: bool = False,
    max_speed: int = 5,
)

Source from the content-addressed store, hash-verified

29
30
31def construct_highway(
32 number_of_cells: int,
33 frequency: int,
34 initial_speed: int,
35 random_frequency: bool = False,
36 random_speed: bool = False,
37 max_speed: int = 5,
38) -> list:
39 """
40 Build the highway following the parameters given
41 >>> construct_highway(10, 2, 6)
42 [[6, -1, 6, -1, 6, -1, 6, -1, 6, -1]]
43 >>> construct_highway(10, 10, 2)
44 [[2, -1, -1, -1, -1, -1, -1, -1, -1, -1]]
45 """
46
47 highway = [[-1] * number_of_cells] # Create a highway without any car
48 i = 0
49 initial_speed = max(initial_speed, 0)
50 while i < number_of_cells:
51 highway[0][i] = (
52 randint(0, max_speed) if random_speed else initial_speed
53 ) # Place the cars
54 i += (
55 randint(1, max_speed * 2) if random_frequency else frequency
56 ) # Arbitrary number, may need tuning
57 return highway
58
59
60def get_distance(highway_now: list, car_index: int) -> int:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected