I am new to programming (less than a year), and I would like to improve.
I solved a problem on CodeWars and copied both my solution and the top voted solution below. I listed specific questions after my solution.
I would like to know, what would be considered the better overall solution in a real production environment? Is abstracting the way I did better (even if I did not execute correctly)? Could someone provide advice to help me improve?
Description:
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.
levels can be only numbers:
0,1,2,3
buttons can be strings:
'0','1','2','3'
possible return values are 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 number.
Top Voted Solution
def goto(level, button) return 0 unless (0..3).include?(level) && ('0'..'3').include?(button) button.to_i - level end
My Solution
def goto(level, button)
inputs_types_are_accepted = (level.is_a? Integer) && (button.is_a? String)
if inputs_types_are_accepted
current_floor, desired_floor = level.to_i, button.to_i
if current_floor.between?(0, 3) && desired_floor.between?(0, 3)
floor_change = desired_floor - current_floor
return floor_change
else
return 0
end
else
return 0
end
end
- With a method this small, is it correct to abstract the way I did?
- If I change
if current_floor.between?(0, 3) && desired_floor.between?(0, 3)
toif (current_floor && desired_floor).between?(0, 3)
then I failed some tests. Is there a clearer way to represent this? - Is there a way to remove one set of
else return 0 end
? I tried and failed tests.
if
and there is usually a better solution than anif
statement, period. what the top solution highlights is the essence of the algorithm, which is just a single subtraction statement. that essence is completely lost in your solution. \$\endgroup\$