There is a house with 4 levels.
In that house there is an elevator.
You can program this elevator to go up or down,
depending on what button the user touches inside the elevator.

Valid levels must be only these numbers: `0,1,2,3`

Valid buttons must be only these strings: `'0','1','2','3'` 

Possible return values are these numbers: `-3,-2,-1,0,1,2,3`

If the elevator is on the ground floor(0th level) 
and the user touches button '2'
the elevator must go 2 levels up,
so our function must return 2.

If the elevator is on the 3rd level
and the user touches button '0'
the elevator must go 3 levels down, so our function must return -3.

If the elevator is on the 2nd level,
and the user touches button '2'
the elevator must remain on the same level,
so we return 0.

We cannot endanger the lives of our passengers, 
so if we get erronous inputs, 
our elevator must remain on the same level.
So for example:

- `goto(2,'4')` must return 0, because there is no button '4' in the elevator.
- `goto(4,'0')` must return 0, because there is no level 4.
- `goto(3,undefined)` must return 0.
- `goto(undefined,'2')` must return 0.
- `goto([],'2')` must return 0 because the type of the input level is array instead of a number.
- `goto(3,{})` must return 0 because the type of the input button is object instead of a string.

Source: codewars | Difficulty: EASY
Tags: Dynamic programming

Fundamentals

Starter code: def goto(level,button):
	

Solutions:
levels = [0, 1, 2, 3]
buttons = ['0', '1', '2', '3']

def goto(level, button):
	if level not in levels or button not in buttons:
		return 0
	else:
		return int(button) - level


def goto(l, b):
	if b in ('0', '1', '2', '3') and l in (0, 1, 2, 3):
		return int(b) - l
	return 0


def goto(level, button):
	if type(level) is not int or level > 3 or type(button) is not str or (int(button) > 3):
		return 0
	return int(button) - level


def goto(level, button):
	try:
		return int(button) - level if button in '0123' and level in {0, 1, 2, 3} else 0
	except (TypeError, ValueError):
		return 0


def goto(level, button):
	return int(button) - level if isinstance(level, int) and isinstance(button, str) and (level >= 0) and (level <= 3) and (int(button) >= 0) and (int(button) <= 3) else 0


def goto(level=None, button=None):
	if level not in (0, 1, 2, 3) or button not in ('0', '1', '2', '3'):
		return 0
	else:
		move = int(button) - level
		return move


def goto(level, button):
	return level in range(4) and button in list('0123') and int(button) - level


goto = lambda level, button: 0 if level not in [0, 1, 2, 3] or button not in ['0', '1', '2', '3'] else int(button) - level


goto = lambda l, b: l in (0, 1, 2, 3) and b in list('0123') and int(b) - l