256. Python Program to Count the Floors a Lift Travels
MediumPhase 2 · Practice
This one takes a few steps, all with tools you know: split(), a for loop and a dictionary that counts.
Overview
Building managers track how far a lift travels, because a lift needs a check-up after many floors. They also want to know which floor is the busiest.
Problem Statement
Ibrahim looks after the lift in an office building. Floor 0 is the ground floor, and the basement floors are -1 and -2. The first input line is the floor where the lift starts. The second line lists the floors where it stops, in order. The lift goes straight from each floor to the next stop.
Print three lines:
Floors travelled: <total>
Busiest floor: <floor>
Stops there: <count>
- Floors travelled is the total number of floors the lift passes. Going from floor 3 to floor -1 is 4 floors, and so is going from -1 back to 3.
- The busiest floor is the floor in the stop list that appears most often. If two or more floors tie, the lowest floor wins. The start floor does not count as a stop.
- A stop on the floor the lift is already on adds 0 floors, but it still counts as a stop.
- Stops there is how many times the lift stopped on the busiest floor.
Examples
Example 1. Input:
0
5 2 8 5 0 5
gives:
Floors travelled: 27
Busiest floor: 5
Stops there: 3
Why: the trips are 0 to 5, 5 to 2, 2 to 8, 8 to 5, 5 to 0 and 0 to 5, so 5 + 3 + 6 + 3 + 5 + 5 = 27 floors. Floor 5 is in the list 3 times.
Example 2. Input:
3
-1 6 -1 6
gives:
Floors travelled: 25
Busiest floor: -1
Stops there: 2
Why: 3 to -1 is 4 floors, and -1 to 6 is 7 floors, three times, so 4 + 7 + 7 + 7 = 25. Floors -1 and 6 both have 2 stops, and -1 is the lower floor.
Input
Two lines. The first line is the start floor. The second line holds 1 or more stop floors, with a space between them. Every floor is a whole number from -2 to 20. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
Three lines, as above. The tests try 7 different inputs, also one stop, a stop on the start floor and a tie where the higher floor comes first.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.