4 kyu
Elevator Action!
6 of 66ITSOES
Loading description...
Algorithms
Performance
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Binary search was overkill here. Perfomance tag means not to check every floor one by one.
Sorry man, it's a duplicate. https://www.codewars.com/kata/58905bfa1decb981da00009e
This same kata was mentioned earlier in a different comment.
I'll repeat what I mentioned there:
These are completely different algorithms that just both happened to involve elevators.
suggested tags:
algorithms
,performance
Sounds good, added!
I am passing all the sample tests and failing most of the random tests, which makes me feel that the random tests cover some extra cases that I am missing from the description and the sample tests. Maybe you could check my current solution and add a few extra sample tests (one or two), which would help to debug the kata?
Okey, I was finally able to solve it. :D These were the test cases that helped me debug and find the problem:
puzzle = [{'from': 39, 'to': 14}, {'from': 6, 'to': 0}, {'from': 10, 'to': 6}, {'from': 8, 'to': 43}, {'from': 20, 'to': 2}, {'from': 7, 'to': 32}, {'from': 41, 'to': 37}, {'from': 37, 'to': 21}, {'from': 19, 'to': -1}, {'from': 21, 'to': 4}, {'from': 1, 'to': 23}, {'from': 34, 'to': -3}, {'from': 19, 'to': -3}, {'from': 9, 'to': 39}, {'from': 0, 'to': 26}, {'from': 26, 'to': 18}, {'from': 36, 'to': 18}, {'from': 6, 'to': 33}] solution = [39, 37, 36, 34, 26, 21, 18, 14, 10, 6, 4, 0, -3, 0, 1, 6, 7, 8, 23, 26, 32, 33, 43, 41, 37, 20, 19, 2, -1, -3, 9, 39] tester(29, puzzle, solution)
puzzle = [{'from': 4, 'to': 20}, {'from': 12, 'to': 2}, {'from': 22, 'to': 24}, {'from': 9, 'to': 39}, {'from': 30, 'to': 45}, {'from': 5, 'to': 26}, {'from': -3, 'to': 43}, {'from': 36, 'to': 25}, {'from': 10, 'to': 13}, {'from': 42, 'to': 24}, {'from': 25, 'to': 28}, {'from': 39, 'to': 6}, {'from': 4, 'to': 3}, {'from': 7, 'to': 9}, {'from': 33, 'to': 0}, {'from': 24, 'to': 10}, {'from': 2, 'to': 41}, {'from': 36, 'to': 38}, {'from': 16, 'to': 27}, {'from': 12, 'to': 19}, {'from': 24, 'to': 31}, {'from': 14, 'to': 42}, {'from': 30, 'to': 34}, {'from': 33, 'to': 0}, {'from': 0, 'to': 10}, {'from': 4, 'to': 32}, {'from': 2, 'to': 16}, {'from': -1, 'to': 14}, {'from': 24, 'to': 8}, {'from': 14, 'to': 3}, {'from': 39, 'to': -3}, {'from': 8, 'to': 45}, {'from': 36, 'to': -1}, {'from': 3, 'to': -2}, {'from': 42, 'to': 30}, {'from': 7, 'to': 39}, {'from': 11, 'to': -3}, {'from': 45, 'to': 13}, {'from': 33, 'to': 28}, {'from': 24, 'to': 19}, {'from': 38, 'to': 41}, {'from': 32, 'to': 34}, {'from': 24, 'to': 40}, {'from': 16, 'to': 34}, {'from': 22, 'to': 39}, {'from': 30, 'to': 39}, {'from': 45, 'to': 4}, {'from': 25, 'to': 0}, {'from': 11, 'to': 8}, {'from': 37, 'to': 36}, {'from': 45, 'to': -2}, {'from': 38, 'to': 6}, {'from': 18, 'to': 1}, {'from': -2, 'to': 22}] solution = [4, 5, 7, 9, 20, 22, 24, 26, 30, 31, 32, 34, 36, 38, 39, 41, 45, 42, 30, 25, 24, 13, 12, 10, 4, 3, 2, 0, -2, -3, -2, -1, 0, 2, 10, 13, 14, 16, 22, 27, 30, 34, 39, 41, 42, 43, 39, 38, 37, 36, 25, 24, 8, 6, -1, -3, 2, 8, 12, 16, 19, 24, 25, 28, 30, 34, 39, 40, 45, 33, 28, 24, 19, 18, 14, 11, 3, 1, 0, -3, 11, 8] tester(7, puzzle, solution)
Okay, first of all, I apologize. The Python "small building" tests were supposed to span under 15 different floors. I fixed that now so hopefully the issue you're having is easier to see.
Anyway, I added only 1 of your examples to the Python tests, plus another one that I think illustrates the problem you're having. I.e. this one:
Feel free to let me know if that's enough!
Yep, very good, thanks. My solution fails only 2 tests now, one of them being Day 14 (exactly this one you've pointed out) from the sample tests, so very easy to debug it. :)
This comment has been hidden.
This comment has been hidden.
I added more test cases. I even had to optimize some of my solutions further. I think it's in a good place now.
Yes, now you have a really challenging kata.
Day 8 shows that once you reach the current target floor, you won't pick up any additional people going in the same direction anymore, instead you'll prioritise the next person in queue. I'm not sure this situation is explained in the rules.
Hmm, these 3 rules are supposed to explain that situation:
Maybe I can better emphasize "enter or leave" as being one or the other but not both at the same time. The second rule is meant to apply anytime you're empty including during a stop, which would therefore imply a possible change in direction.
Yes, the enter or leave part is crucial in this case. It's the only edge case I found that could be interpreted ambiguously (imo).
Okay, I updated the rules to be clearer (hopefully)!
I suggest you add a second batch of random tests with small queues (max 15 people) and low amount of floors (max 10 floors buildings). This would help users focus on their algorithm for edge cases they may encounter. I would add up to 500 of such random tests. The current random tests are a good way to address the performance constraints.
Good suggestion! I added a bunch more "small building" tests. Some languages run the tests in random order, and I may have to change the labeling in some cases to fix it unfortunately.
This kata looks amazing, especially because the description seems very realistic. Once a week, I go to a place where I ride the elevators a lot. As I ride, I often begin thinking about how an elevator control algorithm would look. But soon I get to my destination, get off the elevator, and forget about it until the next time. So I never finish my solution. Maybe this kata will help me concentrate long enough to solve it.
UPDATE: Passing 443 tests so far, then timing out. Pretty happy with that, but I might need to take an entirely more efficient approach to pass 'em all.
What happens if, for example, the first in the queue is something like (85, 3) and the elevator starts from level 1, and during the road 1-85, more than 5 people will get in and they will continue going to a higher floor than 85. Would the first person from the queue have to wait until the elevator comes back empty? Will the elevator stop?
The elevator won't stop due to:
When the elevator reaches floor 85 and no one is getting off and the elevator is too full to hold any more, then there will be no change of passengers.
It is quite possible for the first person queued to be the last passenger of the day/run according to the rules. There should be such an example in the test cases in fact.
Cool, thanks. I thought it may be an exception because in this case 85 was the exact destination of the elevator.
You don't explain whether the elevator gives priority to its own direction or to the next person in queue.
For instance, in the example below, elevator moves UP to 4. Then what happens,
Or, the following:
This is what these 2 rules apply to:
Empty ≠ Holding passengers
What a disappointing answer :/ You haven't answered a thing. I'm not even going to bother raising an issue about missing and conflicting specs. I'm done here.
I was trying to give you a hint. The rule you cited was referring to when you're holding passengers. I pointed out the 2 rules that can force you to change directions. Also, sorry, I was still planning to give the answer as a spoiler comment.
The answer for the first one (
[ { from: 4, to: 2}, { from 5, to: 4 } ]
) is as you described it in your second bullet:[4, 2, 5, 2]
Hopefully, I illustrated that well. For
[ { from: 4, to: 2}, { from 2, to: 5 } ]
, it would be as you described it in your first bullet. Your stops would be[2, 5, 4, 2]
. I.e. you pick the person traveling in your direction, which then forces you to continue traveling past the first in queue since you're now holding passengers and can't switch directions.I hope that's clear, and I'm sorry for upsetting you.
Marking the comment as a spoiler is not helpful for people who didn't solve the kata.
Oh okay, I think I'm starting to get it. I thought of it as a hint that you had to confirm to see, but now I realize that you have to unlock all solutions just to see one spoiler. Thanks for pointing that out!
I think the comment is more reasonable to show in that case so I unmarked it then.
Ok it makes sense now. I was able to pass the kata, but my solution doesn't fully synchronise with the ref sol. I need to investigate which solution is not entirely correct. It was a bit surprising when people get out of the elevator, and it becomes empty, that other people on the same floor that want to take the elevator have to wait, if the next person in queue is not on this floor. I'm sure these people would curse.
EDIT: new solution added, in sync with ref sol. There was 1 edge case I didn't take into account. I added a comment on the top to check if this edge case is explained in the rules (well enough).
Yes, sorry about the confusion.
Although I took some creative liberties here, I tried to model it around how I found some elevators to work. But mainly, I started from the perspective of the elevator and I had a vague idea of respecting the order of when UP or DOWN buttons across all floors are pushed. Also since many elevators indicate its next direction on arrival, I continued with the idea that it ALWAYS indicates its direction as long as there was somewhere it knew it should go to. That's roughly how the rules here ended up forming.
Actually, I noticed people that can still hop on to the floor of the next person in queue, are allowed so. So it is a fair system nonetheless.
What happens if the elevator is full but there is a floor along the way where someone wants to go in that direction? Does it still stop at that floor without picking up anyone?
Or is it one of those "smart" elevators that I saw in a city center hotel once that knows to keep moving in that scenario?
If you can't pick someone because you're full then you have no reason to stop.
If the elevator is full, you can only go unload someone.
Indeed, the elevator is "smart" enough not to stop :)
If no one gets off, then no one can get on when you're full, and therefore, according to this rule, you can't stop because you're not changing passengers. Hopefully, that answers your question.
isn't that too close to this ?
I'd say it's different enough, there are more than a few differents rules.
When I was searching for similar katas, I never found this. I'll have to try it :)
Although I may be biased, I would still argue that this kata is considerably different given the rules and data structure. E.g. there's an arbitrarily large range of floors to handle, basements are allowed, the elevator stops in less places, etc. I would imagine solutions would need to be quite different.
I agree, if we allow different kata's for different type of sorting algorithms, we should also about different type of elevator algorithms.
Random tests generate negative
from
/to
, which is unphysical, and certainly unexpected.I actually thought it would be expected and physical, hence why I added it. Some buildings have floors below 1 or negative floors, although they are typically thought of as basements.
I figured more people would find it odd if negative numbers weren't tested so I'm not sure how to respond to your comment.
Yes, but it was never specified either, and whether floors are non-negative or not (as well as how high floors can go) will affect implementation approaches.
So at least the floor range should be specified.
Okay, I added a note to the description. The floor range is meant to be arbitrarily large. The main limitation is the number of people in the
queue
(<200).EDIT: It's now raised to up to 500 allowed in the
queue
.JS is full of issues:
Thanks! I was bound to miss something. Should be fixed.