Professional Documents
Culture Documents
The Elevator Problem
The Elevator Problem
1. (2 pts) First, write a function level_search that should process a list of requested
floors (calls) and a specific target floor (target). It will remove all instances of the
target floor from the list and count the number of stops made at this specific floor.
The function should return the modified calls list and the number of stops count.
Example:
b. Constraints:
i. The elevator starts from the ground floor (0).
ii. The elevator moves to the last call of the calls list and continues to do
so until the calls list is empty.
iii. The time increments differently depending on the elevator's action:
moving one floor takes 2 seconds, and stopping at a floor (when it
reaches a call) takes 10 seconds.
iv. In this function, the elevator tackles one step at a time. It only stops at
the current target floor and doesn’t consider the intermediate ones.
v. When the elevator reaches the target floor, remove all the
appearances of the target floor in the calls list by using the
level_search function.
c. Outputs: At each step, the function should display the current time, the
current position, the current targeted floor and the remaining floors.
Example of output:
Example of output:
From now, you will consider that there are 13 floors from -2 to 10 included.
Example:
2. (2 pts) Write a function elevator_C, which integrates both external floor calls (calls)
and internal user requests (inside_calls). It will simulate a more realistic elevator
operation by processing external calls to pick up users and then attending to their
individual floor requests made from inside the elevator. It will track and display the
elevator's journey, showing its position, the time elapsed, and the remaining calls to
be attended in real-time.
a. Inputs: a list of integers (calls) containing floors that the elevator is called to.
b. Constraints:
i. The elevator starts from the ground floor (0).
ii. The time increments differently depending on the elevator's action:
moving one floor takes 2 seconds, and stopping at a floor (when it
reaches a call) takes 10 seconds.
iii. The elevator tackles one step at a time. It only stops at the current
target floor and doesn’t consider the intermediate ones.
iv. The function maintains two lists: one for the external calls (calls) and
another for the internal calls (inside_calls) which will be generated
based on the number of users entering the elevator at each stop.
v. First, the elevator moves to the closest call of the calls list but:
1. When the elevator reaches a floor from the calls list, remove
that call and all the appearances in the calls list using the
level_search function (part A).
2. Then, use the user_request function to simulate new users
entering the elevator and choosing their destination floors.
3. Process all the user requests (inside_calls) by moving to the
closest call of the inside_calls list until it is empty.
4. While processing the user requests, if the elevator stops at a
level where there are calls from the calls list make them enter
and add their requests to the inside_calls list.
c. Outputs: At each step, the function should display the current time, the
current position, the current targeted floor and the remaining floors.
Example of output:
Tips: When the elevator reaches a floor, check both lists (calls and inside_calls) for any
required stops at that floor. Use the level_search and the user_request functions. Don’t
forget to adjust the time increments to account for these additional stops.
Example of output:
Example:
b. Constraints:
i. The elevator starts from the ground floor (0) and can move up or down
based on calls.
ii. The elevator has two types of calls: internal (inside_calls) and level
calls (level_calls), initially empty.
iii. In each cycle of the simulation:
1. Use the generate_random_calls function to add new level
calls randomly over time:
a. Waiting (no calls) takes 1 second.
b. Moving from one floor to the next one takes 2 seconds.
c. Stopping at a floor takes 10 seconds.
2. At any floor, search for matching calls in both level and inside
call lists using level_search. If there are people entering the
elevator, add their request to the inside_calls list using the
user_request function.
3. Determine the elevator's next move based on the current calls
and direction. If moving 'up' and the highest call is above the
current position, continue up; otherwise, change direction.
4. Repeat similar logic for the 'down' direction.
iv. Be sure to use the random library and random.seed(42) in order to
have comparable results between runs.
c. Outputs: At each step, the function should display the current time, the
current position and the remaining floors.
Example: