3 kyu

The Lift

1,251 of 3,474dinglemouse

Description:

x 3

I bet you won't ever catch a Lift (a.k.a. elevator) again without thinking of this Kata !


Synopsis

A multi-floor building has a Lift in it.

People are queued on different floors waiting for the Lift.

Some people want to go up. Some people want to go down.

The floor they want to go to is represented by a number (i.e. when they enter the Lift this is the button they will press)

BEFORE (people waiting in queues)               AFTER (people at their destinations)
                   +--+                                          +--+ 
  /----------------|  |----------------\        /----------------|  |----------------\
10|                |  | 1,4,3,2        |      10|             10 |  |                |
  |----------------|  |----------------|        |----------------|  |----------------|
 9|                |  | 1,10,2         |       9|                |  |                |
  |----------------|  |----------------|        |----------------|  |----------------|
 8|                |  |                |       8|                |  |                |
  |----------------|  |----------------|        |----------------|  |----------------|
 7|                |  | 3,6,4,5,6      |       7|                |  |                |
  |----------------|  |----------------|        |----------------|  |----------------|
 6|                |  |                |       6|          6,6,6 |  |                |
  |----------------|  |----------------|        |----------------|  |----------------|
 5|                |  |                |       5|            5,5 |  |                |
  |----------------|  |----------------|        |----------------|  |----------------|
 4|                |  | 0,0,0          |       4|          4,4,4 |  |                |
  |----------------|  |----------------|        |----------------|  |----------------|
 3|                |  |                |       3|            3,3 |  |                |
  |----------------|  |----------------|        |----------------|  |----------------|
 2|                |  | 4              |       2|          2,2,2 |  |                |
  |----------------|  |----------------|        |----------------|  |----------------|
 1|                |  | 6,5,2          |       1|            1,1 |  |                |
  |----------------|  |----------------|        |----------------|  |----------------|
 G|                |  |                |       G|          0,0,0 |  |                |
  |====================================|        |====================================|

Rules

Lift Rules

  • The Lift only goes up or down!
  • Each floor has both UP and DOWN Lift-call buttons (except top and ground floors which have only DOWN and UP respectively)
  • The Lift never changes direction until there are no more people wanting to get on/off in the direction it is already travelling
  • When empty the Lift tries to be smart. For example,
    • If it was going up then it will continue up to collect the highest floor person wanting to go down
    • If it was going down then it will continue down to collect the lowest floor person wanting to go up
  • The Lift has a maximum capacity of people
  • When called, the Lift will stop at a floor even if it is full, although unless somebody gets off nobody else can get on!
  • If the lift is empty, and no people are waiting, then it will return to the ground floor

People Rules

  • People are in "queues" that represent their order of arrival to wait for the Lift
  • All people can press the UP/DOWN Lift-call buttons
  • Only people going the same direction as the Lift may enter it
  • Entry is according to the "queue" order, but those unable to enter do not block those behind them that can
  • If a person is unable to enter a full Lift, they will press the UP/DOWN Lift-call button again after it has departed without them

Kata Task

  • Get all the people to the floors they want to go to while obeying the Lift rules and the People rules
  • Return a list of all floors that the Lift stopped at (in the order visited!)

NOTE: The Lift always starts on the ground floor (and people waiting on the ground floor may enter immediately)


I/O

Input

  • queues a list of queues of people for all floors of the building.
    • The height of the building varies
    • 0 = the ground floor
    • Not all floors have queues
    • Queue index [0] is the "head" of the queue
    • Numbers indicate which floor the person wants go to
  • capacity maximum number of people allowed in the lift

Parameter validation - All input parameters can be assumed OK. No need to check for things like:

  • People wanting to go to floors that do not exist
  • People wanting to take the Lift to the floor they are already on
  • Buildings with < 2 floors
  • Basements

Output

  • A list of all floors that the Lift stopped at (in the order visited!)

Example

Refer to the example test cases.


Language Notes

  • Python : The object will be initialized for you in the tests

Good Luck -

DM

Algorithms
Queues
Data Structures

Stats:

CreatedJan 31, 2017
PublishedMar 24, 2017
Warriors Trained22591
Total Skips5954
Total Code Submissions74373
Total Times Completed3474
Java Completions581
Python Completions1251
JavaScript Completions718
CoffeeScript Completions9
Ruby Completions101
C++ Completions413
C# Completions286
Elixir Completions9
TypeScript Completions118
COBOL Completions9
Rust Completions64
Go Completions43
Total Stars1499
% of votes with a positive feedback rating96% of 698
Total "Very Satisfied" Votes642
Total "Somewhat Satisfied" Votes54
Total "Not Satisfied" Votes2
Total Rank Assessments3
Average Assessed Rank
3 kyu
Highest Assessed Rank
1 kyu
Lowest Assessed Rank
5 kyu
Ad
Contributors
  • dinglemouse Avatar
  • Blind4Basics Avatar
  • KataSideKick Avatar
  • FArekkusu Avatar
  • metalim Avatar
  • Awesome A.D. Avatar
  • hobovsky Avatar
  • user7531853 Avatar
  • MobulaKuhlii Avatar
  • akar-0 Avatar
  • Just4FunCoder Avatar
Ad