A Door With Legal Transitions Only

Medium ⏱ 12 min 62% acceptance ★★★★★ 4.7
Model a door: states 'open', 'closed', 'locked'. Door starts closed; open(), close(), lock(), unlock() mutate state but illegal moves raise ValueError (can't open a locked door, can't lock an open one, unlock only works when locked). Each method returns the new state.

Examples

Example 1
Input
d = Door(); d.lock(); d.open()
Output
ValueError after 'locked'
Explanation

Opening a locked door is illegal.

Example 2
Input
Door().open()
Output
'open'
Explanation

A closed door opens fine.

Constraints

  • Exactly the three states.
  • Illegal transitions raise ValueError.

Topics

OOPstate modeling

Companies

TeslaBoschHoneywell

Hints

Hint 1

Guard each method with a state check.

Hint 2

Return self.state at the end of each.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.