3 kyu
The Lift
575 of 3,451dinglemouse
Loading description...
Algorithms
Queues
Data Structures
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.
This comment has been hidden.
TypeScript fork
let
instead ofvar
caused to async bug (Fixing issues [1] [2] [3] [4] )Approved. Thanks!
Me: this kata looks too easy for 3kyu
Me 7 days later of non stoping coding: hold on, I almost solved it
I was submitting my code 10 times, and finally it passed random tests, lol
I'm not proud of it, but I skipped solving the fire drill case as my whole logic was built apon the assumption, that we don't do those stops when we're full, otherwise a cool kata, I was banging my head against this kata for days, but at the end I learned quite a few things like logging and testing, data structures, thinking of quees and matrixes, refactoring a lot, and feel like I leveled up after solving it
This comment has been hidden.
You are sort of right, do not expect this one to be easy, I'm stuck on this for +5 days, and still a couple of final tests are failing
Haha, I guess so, I managed to have a few tests succesful and mostly have the good behavior for the elevator, but I have some troubles and I don't even know where or why nor how to ask a question about it.
Very nice problem - thanks!
One question: Why do Python solutions need to deal with the mysterious Dinglemouse object? Why not just define a function with parameters queues and capacity, as in the other languages? That's how I wrote the code in my IDE, only to discover that I needed to insert self. everywhere to make it work.
Finally after days of trying managed to get it! I really liked the idea of this kata and it was really fun solving it. Only thing I would point out is maybe the rules aren't that clear. Wasted a few days just because I misinterpreted some of the rules and their priorities. Other than that it was great!
This comment has been hidden.
Probably it is best if you stay away from tall building for a while. Getting into a lift might trigger a relapse.
Javascript
why this var queues = [[], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], ] with capacity of 5 should equal to [ +0, 6, 5, 4, 3, 2, 1, +0, 5, 4, 3, 2, 1, +0, 4, 3, 2, 1, +0, 3, 2, 1, +0, 1, +0 ] ? first lift goes to 6th floor, takes 4 guys, then to the 5th, takes 1 guy, and after goes direclty to 0, no? why stopping at 4 ?
The Kata description has a rule for this case:
Basically, my lift doesn't have any sensors to know how many people are in it... so it stops at every floor it is called then everybody waiting will say "Oh, it is full. I will catch it next time"
Dear Dinglemouse,
thank you foryour reply. I figured it out just after pressing the "post" button. Anyway, I hope it may be useful to somebody in the future.
Cool kata, as a novice, it helps a lot making a lot of things more clear
Dinglemouse, really enjoyed this one, but seemed a little too easy for 3kyu. Found a pretty straightforward solution in about an hour where I've spent a week on some of your others.
This kata is 6 years old. The overall difficulty levels increased drastically since then.
I don't get to decide the kyu difficulty -- whoever approves the first language version makes that decision and it is stuck forever-after whether you or I like it or not.
cool kata, I enjoy it.
Passed basic and several random test. Remaining several random test failed. I think I have the clue "prioritize the caller on other floor if its same direction with previous direction", "queue of the current floor not always granted to enter although the lift is empty" But still confuse how to implement this clue. Using python, took many hours with my level of coding but still not solved :)
Amazing kata, enjoying it so far. Sadly, reloaded the page without saving and lost some progress. Don't forget to save your work, people! :)
You don't use an IDE or other editor? Madman!
Love it. Really enjoyed this kata. Thanks for your effort!
can someone explain me what does this mean : "When called, the Lift will stop at a floor even if it is full, although unless somebody gets off nobody else can get on!" I dont get it
I found it
headdesk
*Every* *single* example I try works fine, but some random test gets me, or maybe a "tricky" one. But that's the key, maybe.
Pro tip: Reset your variables on each pass. I wrote this as a
class
first, noticed it was expectingstatic
, shrugged and threw static in front of everything. Who cares, nobody is going to create multiple objects anyway since the kata only calls a static method.Well, turns out I care since I'm using the static constructor.
Roll back to my first Git commit, toss ^^^ in, and it passes!
Still my fault, clearing variables is Programming for n00bs 101, but I guess I'm finally thinking in objects now...
I did decide to leave in some of the extra edgecase handling that I wrote while trying to figure out where I was going wrong, I'll leave you with this one:
Although the Kata doesn't require it, I highly recommend it.
My lift doors won't open unless the lift has arrived at the floor, so there is no way to push people down shafts. Maybe try pushing them down stairs, or through a window.
Don't know if it's the heat of the summer or what but this kata got me stuck for a little while. I won't look at elevators the same way indeed ;-)
Ruby
All Fixed Tests have the same input:
[[], [], [], [], [], [], []], 5
so they all pass anyway.
This comment has been hidden.
Q. Well, you might know the lift is full, but how does the lift know that it is full and so can skip stops? A. It doesn't.
From the Kata rules:
A very well thought kata indeed. Appreciate it very much . I got to the solution ,but the real problem for me was "Server Timeout" on submition.
very fun kata, nice challenge
I write on Python. Tests passed. I attempt and the first 4 tests passed but test "Up and down" Execution Timed Out (12000 ms) I tried to optimiszed my code. But the error have occures yet.
What should I do?
debug your code: you're most likely running into an infinite loop
See: https://docs.codewars.com/training/troubleshooting
[Rust]
Second test case has a person wanting to go the the floor he's already in.
Forked and fixed. Also added more helpful assertion messages while I was at it.
Let me know if the issue somehow persists.
Hi, I have a problem with the highlander test, perhaps I messed something up? It is [ [], [2], [3,3,3], [1], [], [], [] ] (capacity = 1). My lift goes
Problem1: Test tells me, I'm wrong on index 4 (expected 1 instead of 2) ??? Problem2: If I don't stop at 4. the test gets passed but then I miss the FireDrill-Test :-(
Could someone tell me where I'm wrong? Thanks a lot!
Why does your step 4 stop at level 2 on the way back down?
On the way back down the button at level 2 is pressed (there are two 3s waiting). And a rule is
"When called, the Lift will stop at a floor even if it is full, although unless somebody gets off nobody else can get on!"
Ok, think I got it - it stops only if the button "into the same direction" is pressed. It's working now, sorry :-)
interesting kata! every elevator trip will look different now
Great problem!
[Typescript] I'm having the same issue as @metawort. Basic & other tests pass. First random test R#0 passes. All other random tests fail;
Fixed in this fork
This comment has been hidden.
What language?
Sorry, language is python
My only complaint is with one slightly unclear part of the instructions. Suppose I have the following setup: G:[1], 1:[0], 2:[0] After the passenger from the ground floor disembarks on floor 1, the lift is empty. I felt it was unclear whether or not the passenger going down from floor 1 was allowed to enter (they are not). Other than that, this kata was a joy to work on.
It's tricky, but the rules are there in the Kata description:
So the empty lift at floor 1 continues going UP (to collect the person on floor 2)
So the person going DOWN from floor 1 cannot enter the empty lift while it is still going UP...
There is a possible issue with the TypeScript tests; In my solution, everything works fine - except the FIRST random test R#0. It fails every time, no matter of the input values, while all other random tests succeed. This behaviour is always consistent.
Fixed in this fork
One thing is not clear : If elevator is going up, left the last passenger and became empty he has to check if there is anybody going up on the floors above, before checking who is the person coming down from the highest floor.
But, it's not clear what happens if there is a second passenger going up. For example :
Thank you
This comment has been hidden.
Fixed in this fork
Lua translation!
D translation
final int[][] queues2 = { new int[0], // 0 new int[0], // 1 new int[]{4,4,4,4}, // 2 new int[0], // 3 new int[]{2,2,2,2}, // 4 new int[0], // 5 new int[]{0,2,4,4,2,2,4,4,2,0}, array lengths differed, expected.length=7 actual.length=10; arrays first differed at element [3]; expected:<2> but was:<4> capacity is 2; why 2 expected. lift goes up picks up people on floor 2 than they go off on floor 4 then continue go up to flooor 6 and then goes down but stops at floor 4 again cause there are people waiting to go down even if lift is fuull
That's not going to happen.
Kata description says
There is nobody above floor 4 so the lift won't keep going up...
[0]
[0,2]
[0,2,4]
[0,2,4,2]
... etc"The Lift never changes direction until there are no more people wanting to get on/off in the direction it is already travelling" I'm not a native speaker. I understood that because in that test case there are people on last floor all wanting to go down lift should continue go up
final int[][] queues2 = { new int[0], // 0 new int[0], // 1 new int[]{4,4,4,4}, // 2 new int[0], // 3 new int[]{2,2,2,2}, // 4 new int[0], // 5 new int[]{0,2,4,4,2,2,4,4,2,0} last floor has plenty of people waiting to go down. that's why in my solution lift goes there first. and in my solution i think i don't take those 2's cause lift is going up not down. so i first take down people from higest floor and when lift passes by the floor in down direction and is empty i take down those 2's.
final int[][] queues2 = { new int[0], // 0 new int[0], // 1 new int[]{4,4,4,4}, // 2 new int[0], // 3 new int[]{2,2,2,2}, // 4 new int[0], // 5 new int[]{0,2,4,4,2,2,4,4,2,0}, capacity 2 array lengths differed, expected.length=7 actual.length=10; arrays first differed at element [3]; expected:<2> but was:<4> why it expected 2 when lift should go up till the end and on the way down stop if someone waiting to go down even if it is full so it should stop at 4. it goes first to 2 picks up then to 4 then to 6 and then has stop at 4 cause there are people there waiting to go to floor 2?
Rather confusing rule. More logically do not stop when it full, otherwise our lift not so "smart".
In real life would a lift know its capacity? I'm not sure it would, but I don't know for sure. If it could then I agree with your point!
In real-life I guess capacity doesn't mean very much other than as a rough guide...
e.g. Capacity = 8 but Snow White and the Seven Dwarfs in a lift is not the same as Snow White and the Seven Sumo Wrestlers in a lift!
Maybe I've got it a bit wrong -- as if smart a machine(Lift) ruling the roost. But it turns our more about interaction between Queue and Lift as two simple logics.
Rust translation
Approved. Thanks!
Go translation
Approved :-)
You didn't approve Go :)
Oops. Done now.
It's my first time writing here on codewars. I liked this kata, the idea is very creative, so congratulation to dinglemouse for his or her brainchild! Sadly, for me was very hard to understand how this lift works, because of my so-so English. I misanderstood some instructions at first and I could pass 11 out of 15 tests, therefore I had to changed a lot of things in my code. Eventually, I was able to pass this kata, but I think that I could do better, because something in my code could have written better without repetitions, but I have a headache at the moment, so I leave it as it is.
Bye :)
Someone can explain for me what's wrong with the fire drill test. I really straggle on that (
This was fun. Took me a few hours because I changed a few things and was confused for a bit.
To clear up the instrucitons, I would reword them:
-The lift stops if someone needs off or someone needs on (going in same direction) -The lift only turns around if empty and no one reamining on floors in current direction -The next stop is always either the next floor someone needs off, the next floor with someone needing on going the same direction, or the last floor with a person going the opposite direction.
I just don't get the logic behind some of the tests. For instance in 'Yoyo' case we get:
[(), (), (4, 4, 4, 4), (), (2, 2, 2, 2), (), ()] with capacity 2
It is clear to me that we should first pick up people on floor 4, because at the beginning the lift is empty, and is obviously going up, because there is not other way to go from the ground floor. Now, from the task we are told that: "When empty the Lift tries to be smart. For example, If it was going up then it may continue up to collect the highest floor person wanting to go down"
Clearly, people on floor 2 want to go up, and floor 2 contains highest floor people wanting to go down, yet the answer suggests the following path:
[0, 2, 4, 2, 4, 2, 0]
My solution would be:
[0, 4, 2, 4, 2, 4, 0]
The same goes for the test: [(), (0, 0, 0, 6), (), (), (), (6, 6, 0, 0, 0, 6), ()] with capacity 5. Why should we stop at 1 if there are people who want to get from 5 to 0?
I cannot understand the logic behind this, and, therefore, cannot solve this kata. Can anyone explain?
Hmmm...
The previous rule "The Lift never changes direction until there are no more people wanting to get on/off in the direction it is already travelling" comes first.
That rule about "When empty the Lift tries to be smart" was intended only for when the previous rule was exhausted. e.g. some people got out making the lift empty with nobody left wanting to get on/off in the direction of travel...
So, in the "yo-yo" test the people on floor 2 want to go up, so the upwards moving lift stops to get them (by the first rule).
If you feel there is some better wording which would make it clearer please let me know. OTOH maybe it is not so bad because 300+ people could solve it and somehow this question never came up before.
Your solution is actually less efficient than the correct solution. In total, your solution is passing through 12 floors from the start to the point the queues are exhausted and the elevator is empty, ie. 0>4=4, 4>2=2, 2>4=2, 4>2=2, 2>4=2.
The correct solution only passes through 10 floors in total, because it picks up 2 people from the second floor on the way up to the fourth floor, eliminating the need to do another trip from second floor to the fourth floor at the end, ie. 0>2=2, 2>4=2, 4>2=2, 2>4=2, 4>2=2.
Just imagine you are waiting on an elevator which is empty, and you see it go past your floor and stop at the floor you want to go to without stopping to pick you up. Hope this helps you make sense of it.
How come the last test showed the lift stops on the 1st floor?! there is neither queue nor a request to go for the first floor in the queue list?
A person on 1st floor called the lift.
Aha Ok Thank you, I didn't notice that.
[Python]
On basic test "Highlander" in python, the test is : Queues : ((), (2,), (3, 3, 3), (1,), (), (), ()) Capacity : 1
And I've the following result : [0, 1, 2, 3, 2, 1, 2, 3, 2, 3, 0] should equal [0, 1, 2, 3, 1, 2, 3, 2, 3, 0]
But according to lift rule "When called, the Lift will stop at a floor even if it is full, although unless somebody gets off nobody else can get on!"
the elevator should stop at the second floor after having picked the people at the 3rd floor.
Read Fchilling's post below, he asked the same question.
yeah, that was fun.
What I found confusing in the description was
I'd rather phrase that like:
it should really be changed, as it is a hard rule, not a possibility
I meant "may" only in the sense that it won't happen if there is no such person ... but i see the point, and since there are so many votes to change it, i changed it.
COBOL translation.
approved
fun kata ,this kata took me about four days ,i really enjoy it
Thanks :-)
my code is a mess...but i dont understand why i pass all regular tests i attempt and i get mistakes because result is None...as it is not returning anything...if that is the case and the code is not returning the result...how is possible that regular tests are ok?
Because your code is a mess: sometimes it returns something, and sometimes nothing (if I believe you). You need some debugging: https://docs.codewars.com/training/troubleshooting. This is a desserved 3 kyu kata, not an easy one.
Fun kata, the task is simpler than you think
How come there is no solution from you to view? I wanted to know how you got a simple implementation out of this kata.
@epicshaner IIUC you can only "View Solution" if you also solved in the same language...
@epicshaner If you did with JS or TS, you can check mine. I'm learning clean code stuff and did this kata for training so it should be quite easy to understand.
Funny little kata - was expecting some kind of animal though ;) Evening definitely well spent!
First of all thank you for this wonderful kata! It's very realistic and of so much use for other problems.
The instructions are amazing. But there is one thing I don't really like: The hint the lift tries to be smart part. It may continue in the same direction. (I think this is misleading and make things more complicated than neccessary) It actually continues in the same direction until it isn't possible anymore. The lift just doesn't stop, unless it has to (somebody wants to leave or enter the lift).
Example: lift is moving upwards and is in floor 6 of 7, the lift is empty. It always moves to floor 7 (even if there isn't anybody) and then turns around and moves downwards it just doesn't stop until it reaches a floor in which someone is waiting for the elevator moving downwards.
The Kata output is:
So, if what you say was true then the top floor of the building should be part of every answer.
But it isn't.
Hmmm, I don't what I should respond to this. I've mentioned twice that it goes there but doesn't stop. Sorry, but your reply isn't really an answer.
Perhaps you are only describing how you own implementation works... but not actually a requirement of the rules?
Yes, you're right! But that's exactly the point. I just stated out that this hint was for me misleading and pushed me towards a wrong direction. And if it pushes someone (in this case me) into a wrong direction, then something about this hint isn't great. This whole thing is just a suggestion. Take it or leave it. It's entirely up to you.
For me, description looks fine. Actually, YOUR POST pushed me towards an easy
ground-roof-ground
solution, haha. Even though clearly it's not how the lift is supposed to move. Idk if we should hide this thread. It felt like cheatingSo i have quick question.
I am getting time out error and if someone could tell me if this kata is getting computation heavy later or am i just having infinity loop somewhere, just wanna figure out what's wrong before i start changing stuff
@arison. No, this Kata is has no intended "perfomance" tests. So any timeout is likely due to some problem in your code. Cheers, DM.
This comment has been hidden.
Haha totally feel you! I have two such challenges which I've been thinking on for years! Managed to complete one, but Voronoi cells kata is still waiting :D
@wayne1512. LOL. Well, I warned you about that in the Kata description!
Kudos for seeing it to completion.
good job on the persistence
[C++]
maybe someone can explain why this test is considered correct?
capacity: 5; input ppls: [] [0 0 0 0 ] [0 0 0 0 ] [0 0 0 0 ] [0 0 0 0 ] [0 0 0 0 ] [0 0 0 0 ];
Expected: equal to [ 0, 6, 5, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 0, 4, 3, 2, 1, 0, 3, 2, 1, 0, 1, 0 ] - i think it's wrong
Well, it isn't.
Is this was a random test then it might warrant time to figure what is going on... But this is expected results for the fixed test case "Fire Drill" AFAIK. Please note that 165 people already passed this Kata for
C++
- seriously what are the chances that you are right but those 165 people are not?"The Lift never changes direction until there are no more people wanting to get on/off in the direction it is already travelling" "When called, the Lift will stop at a floor even if it is full, although unless somebody gets off nobody else can get on!" I think these two rules explain why this will happen
"When called, the Lift will stop at a floor even if it is full, although unless somebody gets off nobody else can get on!"
Ah, right, makes sense now. Without that it would be way more efficient... but usually an elevator does not really know if there is still capacity.
I don't think this is a reasonable rule. It makes tests cases wired. And, the lift can be equipped with sensors to detect the number of people.
Sure, it "can be". But in the real world have you ever seen such a thing? I think my current rule is closer to reality. The lift stops because someone called it... doors open... but sorry no more room... doors close.. lift continues.
C++ fork removing warnings generated by tests.
It should be checked that the default language version doesn't revert to C++ 14 because this happens inside the kumite editor for some reason.
Approved
After the random TypeScript test cases have been updated, the function is only called once per test case (as should be). They still fail though, because the expected result is always
[ 0 ]
...Still the first test passes (apparently with a different expected result) and the following test cases use the identical input values as the first one. Test case titles are still BS as well.
Example:
R#0: 10 floors, 23 people, lift holds 2
theLift([ [ 2, 3, 1, 2 ], [ 0, 0, 2 ], [], [ 2 ] ], 3, [], 1) -> [ 0, 1, 2, 3, 2, 1, 0, 2, 0 ]
passesR#1: 7 floors, 19 people, lift holds 5 (and following)
theLift([ [ 2, 3, 1, 2 ], [ 0, 0, 2 ], [], [ 2 ] ], 3, [], 1) -> [ 0, 1, 2, 3, 2, 1, 0, 2, 0 ]
fails withexpected [ 0, 1, 2, 3, 2, 1, 0, 2, 0 ] to have the same members as [ 0 ]
I fixed this by clearing the original queue arrays in the
queues
array given to the function, though it wasn't necessary for my algorithm to produce the correct result.But apparently the random test cases use the same
queues
array for each run and the sample solution clears it as well, logically producing the result[ 0 ]
for every consecutive run (since there are no people wanting to go anywhere anymore). This behavior of the random test runs seems buggy to me, though, and should be fixed!@metawork. Sorry for the trouble. I don't know typescript and the transalator seems to have run away. Looking at the random tests I don't immediately see the bug you are referring to. But since you already passed the Kata can you have a quick look at the kata Test code and point me to the bug you think it there - then I can fix it for others. Thanks.
I guess that the random tests are run asynchronously. Since they all use the same variables vor
queues1
,queues2
andliftMax
that could explain why the are always the same for every run. I'd try moving the code in thefor
-loop into the function passed toit()
.What does this mean? How do you "clear the original queue arrays in the queues array given to the function"
a
This comment has been hidden.
Fixed in this fork
COMMENT UPDATED AND CHANGED FROM QUESTION TO ISSUE (original comment is below, found issue described in reply)
Coding in TypeScript - I am still getting first random test failed (all the other test are always passed, just first random test
R#0
is failed) and no idea why, because sometimes it is really simple test case. Here is the example with logs:It looks that there are actually 2 tests, and result from the first is the expected one ([0,2,1,0]), also second result is correct based on the input
queues
, but the second result is compared with expected result from first test and it is obviously failed.Another thing, I don't understand why there are required input parameters
result
anddirection
, as you can see in example above, in first test the input parameterresult
has assigned value[ 0, 1 ]
and in second test it is undefined. Maybe this causes the issue, because the second result always contains thisresult
parameter from first test at the beginning. Another example just to see it clearly:You can see that result from first test is expected result, and result from second test starts with the input parameter
result
from first test ([ 0, 1, 2, 3, 4 ]).It is really wierd that all the other complex tests are always passed, only this one fails. Am I missing something here or is this an issue of the test?
I had luck and one of my attempt tries was successful - all tests passed, so I was able to submit my solution and check the test cases, and I think there is an issue in random test cases for Typescript: For the user result, function
theLift
is called and for the expected result, functionmyAnswer
is called, but inside of functionmyAnswer
functiontheLift
is also called, that means that user implementation is used for expected results.I guess this is wrong and function
myAnswer
should call itself instead of functiontheLift
. Am I right?Well, maybe it is by purpose, to test if the solution works correctly also when the lift had already done some progress before calling the function. If yes, mark this issue as resolved.
Anyway, I was able to update my solution to pass all the tests everytime and this was a great kata!
I'm also using TypeScript and am very confused by these parameters! Please at least add and describe them in the instructions.
@pacman. Yeah that looks bogus to me also. What was supposed to be a recusive call turned into a weird symbiotic relationship with the user solution. Anyway, it's fixed now. Thanks very much for reporting this, and sorry for all the trouble it caused you. The translator will be shot at dawn.
I'm also seeing the phenomenon that
theLift
is called twice in the random test cases... Unlike pacman09 I don't see how I can sensibly fix this. I get results like this:The first test case always passes, for example:
R#0: 20 floors, 41 people, lift holds 4
theLift([[],[0],[0,1],[],[]], 4, [0,1,2,3,4], 0) -> [0,1,2,3,4,2,1,0] // expected result
theLift([[],[4,3,0,2],[3,0,1],[],[]], 4,, [], 1) -> [0,1,2,3,4,2,1,0] // still expected result
Noticeably the title of the test cases matches only the capacity used... The first call looks like a intermediate result from the second run (though my algorithm is not recursive). It gets stranger in the subsequent (failing) cases:
R#1: 15 floors, 17 people, lift holds 3
theLift([[],[0],[0,1],[],[]], 4, [0], 0) -> [0,2,1,0] // expected result
theLift([[],[4,3,0,2],[3,0,1],[],[]], 4, [], 1) -> [0,1,2,3,4,2,1,0] // seems correct to me for the input, though
Despite the different title, the test cases uses the same parameters as the first one, except the third parameter. Fhe input and outpt for all following test cases look identical to this one, including an identical expected result...
@dinglemouse - no problem, it was also kind of fun to figure out what is wrong there :)
@metawort - all test cases are ok now, I tried also my original solution (where the problem appeared) and it works (all test cases passed)
For me the problem with the random tests persists... see my issue above
5 or 6 hours across three days and i get Passed: 23 Failed: 11. Fire drill is killing me now and the random tests. Can't figure out what the queue must be for the fire drill and what the capacity is to give that result to test it. Is this too long to spend on this?
Nevermind i just finished it!!!
.
When I started solving this thing, I thought: "Hm... is this really a 3 kyu?" Two days later I thought the same thing, but for the opposite reason. Almost went crazy trying to untangle a mess of if - then - else if - else statements. It took me two long walks with dogs to figure out how to do this, with a completely different approach to the problem.
I love this one!
So the dogs helped you?
Yes, they did. :-D When I'm stuck, I walk. More often than not, the solution just pops up in my head.
[], [0, 0, 0, 6], [], [], [], [6, 6, 0, 0, 0, 6], []
I need your help:) Looking at the instructions on this "testTrickyQueues" input I understand (and this is what my code does) is that:
From the bottom, the first "passenger" in queue seeks to go down - thus the Direction of the Lift is set to DOWN. Since there is someone on the 5th floor (3 "passengers" actually) who also seek to go down, lift should me smart enough to regonize this and: firstly go to floor 5, pick 3 "passengers" than on the way down on the floor 1 collect 2 additonal "passengers" and let 5 pepole out on the ground floor.
So now the queues on each floor look like this: [], [0, 6], [], [], [], [6, 6, 6], []
With this logic, the output is at this stage is: [0, 5, 1, 0] and there are still "passengers" awaiting.
I'm failing 2 from 16 tests, one of them is "testTrickyQueues" and it says that: arrays first differed at element [1]; expected:<1> but was:<5>
Meaning that we should ignore the fact that on the 5th floor three pepole are willing to go down and pick up the passengers from floor 1. Which doesn't make sense considering the instructions but I MIGHT simply misread them
The testTrickyQueues test data looks like this
And expecting
new int[]{0,1,5,6,5,1,0,1,0}
Let's work thru the first few floors...
0
going UP1
who wants to go to floor6
. They get in. Lift contains {6}5
wanting to go to floor6
. Three people get in. Lift contains {6,6,6,6}. Still going up.6
. Everybody out. This is top so now lift will go DOWN.5
wanting to go to ground. Those three get in. Lift contains {0,0,0} and is going DOWN*...
You can see that so far the lift has stopped at FLOORS
0,1,5,6,5
.... which is the first part of the required Kata answer.~
I leave the rest as exercise for the reader ;-)
Cheers!
Good task! It is very important to read carefully here. The first time was not attentive. The elevator stops for all calls, even if there is no room and no one gets out - it turns out this is important!
All of the rules are important! ;-)
Wrong tests.
It is map of floors: [[], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] Capacity is 5 My lift is: [0, 6, 5, 0, 5, 4, 0, 4, 3, 0, 3, 2, 0, 1, 0] Test lift is: [0, 6, 5, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 0, 4, 3, 2, 1, 0, 3, 2, 1, 0, 1, 0]
Test is ubnormal.
Read Kaboms's post below.
Thank You!
I was extremely astonished when I realized "'tuple' object has no attribute 'pop'" and I had to push down all my solutions and do again :(
One of the best Kata I've solved (or even looked at) on this website. Struggled a bit in the beginning (mainly because I was misunderstanding the question), but I completed it with a semi-decent solution. Really cool question, thanks!
Thanks. Congrats. Merry Xmas :-)
One of the most enjoyable questions I've solved on Codewars. Nice question.
Really enjoyed this kata. Very interesting and fun to code
Great Kata!! thanks.
I liked this kata, thanks! I'm fairly happy with my solution but I guess it could be a bit more efficient removing some unnecessary comparisons when selecting the next floor.
What I did not like is the solution stub: why do I have to start from
class Dinglemouse(object)
? In hindsight since I did not need objects I should have ignored the class stub and written my solution as an independent function.Very interesting kata. Thanks!
Potential issue with Typescript tests.
I have this test case:
which has this solution
According to the question, people can get on the ground floor at the start of the problem. So why does this lift not stop at Floor 3 before proceeding to Floor 5? And in that case, it would also stop at 2 and 4 on the way up.
I am not sure if you have confused yourself. (OTOH, I am sure you have confused me).
When I add and execute a Typescript test using the input you gave if gives expected result
[ 0, 2, 3, 4, 5, 4, 3, 2, 1, 0 ]
which at first glance looks ok.Well my solution seems to have deleted itself, so I can't check anymore! But that solution you got seems to match what I got as a result, but the test failed.
you can find it in the trainer ("past solutions" panel) or in the "Solutions/my solutions" page.
Thanks for that, I don't remember that being there!
As I thought, my solution did give that expected result above. My solution only fails one random test occasionally, and sometimes completely passes. Just rerunning the tests now 'til I get another failed test short enough to see the expected.
Here's another one:
My log:
[ 0, 2, 4, 6, 4, 3, 2, 1, 0 ]
Similar case, where the first floor takes on people.
This error is only reproducible in the older TypeScript translation, where the reference solution mistakenly calling the user solution in actual test line 47.
Closing, since reference solution is no longer affected by user solution in the current TypeScript translation.
This comment has been hidden.
@FastestCoderOnEarth. Can you please take a look at this?
Looking at the old TypeScript translation, the issue was caused by the reference solution mistakenly calling the user solution in actual test line 47.
Closing as this is no longer the case in the current TypeScript translation.
Hi Please could you check if my timeout is only inefficiency or an infinite loop in the attempt tests and if inefficiency, is it linked to the use of too many methods or just messy implementation ?
Thanks in advance
Hi, had the same problem. You might wanna try setting up a timer in the function and use an if statement to return your result vector after a given amout of time passes, so you would know where your algo gets stuck. You could also gain some insight by using the same timer to measure the average time your algorithm needs to solve one case
Thks I added a test case and I am in an infinite loop :-( So I added some prints in the code to understand , but not found for the moment
Now debugged with some prints .... My code needs to be cleaned a bit ... Thanks for the kata
After some tampering, my first solution that got all the test cases right, passed almost all the other cases, except some random ones and the "full up, full down" one. Was searching for almost an hour, and could not find where the algorithm was wrong. Only later did i discover that i mistakenly put an "is greater" instead of "is greater or equal" operator in a for loop with a lengthy continuation condition. Great Kata, had lots of fun :D
Hello everybody,
I believe I've found a test that might contradict the problem statement (see below).
highlander queue = [ [], [ 2 ], [ 3, 3, 3 ], [ 1 ], [], [], [] ] capacity = 1
To make sure I did not make a mistake in my code, I took a pen and a piece of paper, drew a simple diagram, and followed all the lift movements. Here's what I got:
[0, 1, 2, 3, 2, 1, 2, 3, 2, 3, 0]
That's exactly the result produced by my code that I'm trying to debug.
But, according to the test, the following result is expected:
[0, 1, 2, 3, 1, 2, 3, 2, 3, 0]
That is, the lift presumably must not stop on floor #2 after leaving floor #3 for the first time.
But here's what the problem statement specifically says:
"When called, the Lift will stop at a floor even if it is full, although unless somebody gets off nobody else can get on!"
Any comments are welcome. Of course, I might have misunderstood something. Actually the kata is great, and I've been happily debugging my clumsy solution. :)
Cheers.
Read Fchilling's post below, he asked the same question.
Thanks, I didn't know that.
That fire drill test is wrong. In the event of a fire, everyone is suppose to use the stairs.
Well spotted. You get a bonus point for that :-)
Fun Kata! Loved it
Hi, I have added a TypeScript translation!
Will someone please review it?
Thanks.
Aprroved. Thanks :-)
Cool kata!
Wonder why so few people are using a recursive function for this, seems like the obvious solution.
I like this solution. Very graceful. To answer your question: looks like most people chose an object oriented approach. In my case it was the only way to wrap my head around a solution. ;-)
obvious, but hard. Writing in in an object oriented approach is more accessible to most programmers I guess. For me too, I'd die trying to find a correct recursive algorithm for this :P
Elixir translation.
Approved :-)
Beautiful kata, but too high rating : my solution is a total mess but I managed to pass all tests :D (I feel bad to go away with a so poor solution, I will update it)
you're the only one responsible for the mess in your code. This has nothing to do with the quality or the rank of the kata.
yes , you're right I was just pointing out that usually if I mess up my program fails to pass in 3 kyu katas
messing the code has nothing to do either with messing the algo. Writing a mess doesn't mean it fails the logic...
Will the lift stop at a floor where all the people on that floor want to go in the opposite direction from the lift's current direction? For example, the lift has a passenger 6 and is heading up from floor 1, there is a person on floor 4 who wants to go to floor 0. I know the person on floor 4 can't get on the lift since the lift's current direction is up but he wants to go down, but will the lift stop at floor 4 or will it just continue going up to floor 6?
If The Lift is going UP (and has passengers) it will not stop for somebody who pressed the DOWN button
How annoying would that be?
Really excellent and challenging kata!
I am, however, stuck on what seems to me to be a conflict between 2 of the lift rules:
For example: lift is going up and discharges all passenger at floor 3. There are 2 people waiting at higher floors say, 1 at floor 6 wanting to go to floor 10 and 1 at floor 9 wanting to go to floor 5.
Does it
or
Option b) seems the more logical but appears to conflict with the empty lift rule. I'm probably just being dense, but would really appreciate some clarification.
Lift was going up...
Person at floor 6 wants to go up... keep going up.
IIRC the empty lift rule applies only when that rule can't.
Thanks, that's clear now - still got bugs but getting closer to solution!!
Excellent kata!
Thanks for this nice kata. But in C++ I am struggling with the 'Highlander' test. All the other tests are passing fine.
So with a capacity of '1' the queue is queues = { {}, {2}, {3,3,3}, {1}, {}, {}, {} };
Expected: equal to [ 0, 1, 2, 3, 1, 2, 3, 2, 3, 0 ] Actual: [ 0, 1, 2, 3, 2, 1, 2, 3, 2, 3, 0 ]
The rule is to stop on the way up/down when somebody is waiting, even when the max capacity is reached. So why is it expected to jump from floor 3 to 1, without stopping for the two people still waiting at floor 2 to go to floor 3?
Is there a bug or do i misread the rules? Thanks for a short answer!
This is 1st guess where you've gone wrong:
Lift has TWO call buttons (up and down).
The "when called" means only stop when person pressed call button to go in same direction the lift is already travelling.
~
If that's the problem, let me know, otherwise I'll spend more time later to work thru the scenario.
Thanks for the fast answer! solved my problem...
I have to add my praise for this interesting and enjoyable kata!
It was really good to see other -- more concise and clever -- solutions as well. Very instructional.
Thanks.
This comment has been hidden.
:-)
This comment has been hidden.
@pathbart I'm not good with JS but my guess is you have a
Lift
class competing somewhow with the reference implementation which has class of the same name. Try changing your class to something else (e.g.MyLift
) to see if that get you past this hurdle.@dinglemouse Thank you for pointing that out. Yes, you were right - I had the Lift class and after renaming it all the tests passed.
The elevator problem was one of my first school projects in CS. Seems easy at first but it's non-trivial. Good kata. After a few attempts I managed to come up with a simple solution. It's like a piston. Up and down, up and down. But where do you stop, that's the question.
The people in my work building sometimes go into the lift even when the lift is moving in a different direction than their destination is. Because they actually expect the lift will be full when it comes back to that floor. xD
Interesting. That's like walking down one flight of stairs to catch the lift going up so you are not stuck at back of the queue when lift arrives at the floor you started from! Maybe I should make a variation of this Kata where people get into the lift regardlesss of direction, just to see how much worse that makes it for everybody.
JS, the situation is: The solution have passed all the tests, but when i pressed submit, 1 of random test failed. i have repeated several times and found out : sometimes random tests pass, sometimes got 1-2 fails of ~30 tests. suppose there is some mistake in my approach, for now got no idea what is the mistake, maybe there is some specific conditions that random tests can not cover. All 'fixed' tests got pass anyway
Very funny kata,btw
175 other people pass the JS Kata and didn't have the same problems with the random tests, so chances are that your code is slightly wrong but only fails in rare cases
If it is the Kata is at fault then I want to fix it, but nothing can be fixed unless you give more information like:
If you can come back with supporting evidience of a real problem them please report it again as an issue.
worked some more on my solution, and have found the mistake -> fixed it. Random test now pass 100%.
The wrong i think was an overall situation when some (slighly) wrong solution could sometimes pass random tests and score as right solution. I suppose the situation could be avoided if add another 'fixed' testcase: total 4 floors, capacity = 1, queues: 3 : Empty 2 : 3,3,3 1 : 0 0 : Empty That was the case where my program had a little problem but still could pass all fixed tests.
p.s for now not sure to mark as an 'issue' whatever it could mean here.
Nice one. Worked on it for 3 hours, then threw all my code away and started again. Eventually it dawned on me that a simple solution is possible, if you take the time to think of it.
I don't understand this test case: queue = [[8, 8, 6], [8, 3, 4, 7], [], [2, 6, 8, 5], [], [0, 8, 8, 4, 1], [], [3, 2, 2, 3], []] capacity = 9 result should be: [0, 1, 3, 5, 8, 7, 5, 3, 2, 0, 1, 3, 5, 6, 8, 7, 5, 3, 2, 1, 3, 4, 5, 6, 8, 5, 4, 0, 1, 3, 5, 7, 8, 5, 1, 3, 5, 8, 0]
Why does element #3 (0-indexed) in the result equal 5? After stopping at floor 3, people in the lift are: [8, 8, 6, 8, 4, 7, 6, 8, 5] (because 3 exited here), so I assume we should stop at floor 4 next, not 5. What's going on here?
First impression is I agree with you.
Was this a random test?
Which language?
Sorry, forgot to mention that. The language is Python 3 and it was a random test.
@widelec9,
I've worked though this by hand and with my Java reference implementation.
For the input you gave the results should be as follows:
FINAL result should be
[0, 1, 3, 4, 5, 6, 7, 8, 7, 5, 4, 3, 2, 1, 0]
====
Only explanation I have is:
A) You are somehow mistaken. Numbers you wrote above are not same as what the test was running, or
B) You encountered some bug in Python random tests
This comment has been hidden.
This comment has been hidden.
Nope. https://github.com/Codewars/codewars.com/issues/540
(And I marked your question as spoiler in case it gives somebody else ideas...)
C# Translation added.Please review and approve~
Another good translation. Thanks! Approved :-)
This was a very good one, and made me realise some weaknesses in my planning of more complex tasks. Definitely need to refactor my mess..
There was also a fair bit of confusion from the queuing rules which I was mired in. Could I suggest: "Only people going the same direction as the Lift may enter it, and they do so according to their "queue" order" changed to -> "Only people going the same direction as the Lift may enter it" + "People enter accoriding to their "queue" order, but those not able to enter do not block those behind them that can enter" or "People enter accoriding to their "queue" order, but those those not able to board will move aside for those behind them that can"
Something to make it clearer that the single floor queue is effectively two queues: the upQueue and the downQueue.
Very satifsying overall, thank you.
OK. I changed wording similar to what you suggested. Thanks :-)
I can not undestand. Why the fire_drill test 6 {0,0,0,0} 5 {0,0,0,0} 4 {0,0,0,0} 3 {0,0,0,0} 2 {0,0,0,0} 1 {0,0,0,0} 0 {} has the result: [0, 6, 5, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 0, 4, 3, 2, 1, 0, 3, 2, 1, 0, 1, 0] with capacity 5. Why does the lift stop at 4, 3, 2, 1 floors after he took four people on the fifth floor and another one on the fourth? Shouldn't he then go down to the ground floor to drop them off? I use С++
This comment has been hidden.
I have the same question. From the instructions, it seems that it would start by going to 5th, 4th, then back to 0 to drop them off.
Unfortunately, the comment dinglemouse made has been hidden, so I apologize if you already answered this. (python)
Edit: nevermind. I was able to see that comment. I'll reiterate the rule dinglemouse quoted in case anybody else has the same confusion.
Had the same problem, as this behaiviour is not really usefull. Guess we have to read the instructions more carefully
Unavoidable. The lift doesn't know when it is full...
first of all i would like to thank dinglemouse for this really awesome kata, even though i haven't solved it yet, i have had fun pondering it so far. the simple tests have all been successful, but in the advanced tests, 2 fail: fire_drill and random. When I read your question, I wondered how you got the test case of fire_drill, for example?
For some reason lots of people have trouble with this one, although everything follows the same rules so failing just this clearly means you did not implement all the rules correctly yet. Notice for the fire drill everybody wants to go DOWN, and remember that the lift start on the ground so begins its journey going UP.
I don't know your problem but I can guess paying closer attention to the rules of the empty lift might help .... "When empty the Lift tries to be smart. For example, if it was going up then it may continue up to collect the highest floor person wanting to go down"...
Cheers.
Ha! Otis is getting it's firmeware fixed on codewars. For free! :-D
This is a great kata! The tests were very well written. Thank you!
Cheers :-)
C++ translation. Please, review and approve.
Did something just change? It appears something is broken in the tests. For example, for queue: ((3, 3, 3, 3, 3, 3), (), (), (), (), (4, 4, 4, 4, 4, 4), ()) , 5 It says, should equal [0, 1, 2, 3, 6, 5, 3, 2, 0]
Another example: ((3, 3, 3, 3, 3, 3), (), (), (), (), (4, 4, 4, 4, 4, 4), ()) , 5 Says, should equal [0, 3, 5, 4, 0, 3, 5, 4, 0]
I am using Python 3.6
It seems I saw that reported correctly 1 day ago.
Are you sure you printed the inputs correctly?
Here is a copy and paste: Up and down [0, 1, 2, 3, 2, 0] should equal [0, 1, 2, 3, 6, 5, 3, 2, 0] Log ((), (), (4, 4, 4, 4), (), (2, 2, 2, 2), (), ()) , 2
errrr.... and? The "log" is the input you printed out from inside your code. Nothing weird, here.
[0, 1, 2, 3, 2, 0] should equal [0, 1, 2, 3, 6, 5, 3, 2, 0]
<-- This is message about failed test #5((), (), (4, 4, 4, 4), (), (2, 2, 2, 2), (), ()) , 2
<-- These are inputs in test #6You mix up the messages, and wrongly assume what refers to which test. Anyway, everything is okay with the kata, the problem is in your solution.
I guess I am confused. Not sure if you are saying I am printing the wrong input to the expected output. I was saying that something changed as The tests were generating seeming correct expected output. Now you can see in the above example that the expected output shows that the elevator stops at floor 6. there are no passengers there.
FArekkusu is right, the log is matching the next test. The line announcing the test is just after the call to your function. Old translation of mine... I'll correct that (tho, the weirdest part in the story is that it bahaves the exact same way the last time you tried! ;) )
.
Yes, thank you. It seems correct now. Thanks!
Thanks again for fixing the log. I SOLVED this kata and it was immensely satisfying. Definitely was harder than I initially envisioned. You are right, I now respect elevators :)
I'm pretty confused here. Does the order of the people matter in the queue matter or not? In the testTrickyQueues case it doesnt but I have a random case where the only expalantion as to why I am getting it wrong is that it does.
4: [2] 3: [4, 1, 0, 4] 2: [3, 4, 4, 4] 1: [0, 4] 0: [4, 3]
Answer says it's 0, 1, 2, 3, 4, 3, 2, 1, 0, 2, 3, 4, 0 but I'm getting 0, 1, 2, 3, 4, 3, 2, 1, 0, 2, 4, 0
The only way that 3 can be at the end is if when the elevator stops at floor 3 in the beginning it doesn't get the 4 that's last in line, which means it picks up the first 4 and then order suddenly does matter.
Am I missing something?
Also, the capacity is 4 so there's no capacity issues encountered
Yes! From Kata description:
Are you sure about that?
This comment has been hidden.
This part is badly explained. The people are entering according to the queue, but you should consider only those who are going in the current direction, not every person in line. It stops once at the 6th floor in the first example because at the 5th floor everybody who's going up enters the lift, not only the first person in the queue.
@Farekkusu
I realize this but if this is true then the random case is not consistent. Can you see any error in my "if order doesn't matter" version of how that particular case should go?
Not sure if it means much but I am able to pass every case except for this random one
I don't know why you keep saying "if" order matters... It matters, believe me. Or if you don't want to believe me, then believe the other 193 people who already solved the Kata.
Just like in the real world, you wouldn't wait behind somebody wanting to go the other way...
You can imagine there being 2 lines of people (those going up and those going down) if that helps you.
@joshua082390 - Which languge you solving here? Perhaps there is a bug if you are working with a recent translation.
Java
@joshua082390
You told me
For following input (with lift capacity 4) I agree with your answer
But chances of Java tests being wrong are low. I wrote this Kata 1st in Java, and 59 others have solved OK already.
Then I tried with your queues reversed
but for this input it gave the expected result!
So I am suspicious about the input you said:
Are you sure you showed those input queues correctly?
Are you sure you are using queue iindex
[0]
as head of the queue as in description?You were right. I had swapped the order to see if it would fix a different error I was having but it ended up being something else and I forgot to switch it back. Thanks for your help
Well, that's a an hour of my life I will never get back... (but cheers for reporting back that all is ok now)
The moral is: If your solution does not work but you are convinced it should, first thing to check is how many people solved if for that language already. If there are many, then the problem (or misinterpretation) is almost always going to be at your end.
I'm having the exact same problem and I really can't understand why the lift stops at floor 3 at the end.
SO the case is:
Lift Stops at 0 - [4, 3] in the lift Lift Stops at 1 - [4, 3, 4] in the lift (queue at floor 1: [0]) Lift Stops at 2 - [4, 3, 4, 3] in the lift (queue at floor 2: [4, 4, 4]) Lift Stops at 3 - [4, 4, 4, 4] in the lift (queue at floor 3: [1, 0]) Lift Stops at 4 - [2] in the lift (queue at floor 4: []) Lift Stops at 3 - [2, 1, 0] in the lift (queue at floor 3: []) Lift Stops at 2 - [1, 0] in the lift (queue at floor 2: [4, 4, 4]) Lift Stops at 1 - [0, 0] in the lift (queue at floor 1: []) Lift Stops at 0 - [] in the lift (queue at floor 0: []) Lift Stops at 2 - [4, 4, 4] in the lift (queue at floor 2: []) ************* WHY IS IT SUPPOSE TO STOP AT FLOOR 3 HERE **************** Lift Stops at 4 - [] in the lift (queue at floor 4: []) Lift Stops at 0 - [] in the lift (queue at floor 4: [])
Can someone tell me why my logic above is wrong. I'm going crazy on this
Hello @Tonio31
Using the input you provided I think the result should be
[0, 1, 2, 3, 4, 3, 2, 1, 0, 2, 4, 0]
Maybe you found a problem.
What language are you using?
This is reminiscent of a project I was supposed to do in school but never did. And now I have!
Yep once had similiar assignment revolving around threads for each elevator. Didn't work for anyone and the prof cancelled the assignment :o
When I solved this 3Kyu I think I only got 2 points. I'm currently at 4Kyu, is that right? Other people at work getting like 20 points at a time from solutions higher than their level. I mean, I totally understand that the awful code I wrote might deserve only 2 points ... but I don't think that's how this works? Call it a refactoring in progress :).
xD, avt and cmt so deep =:")))
the count sometimes isn't up to date. Just complete another kata you'll enventually get the points.
Sometimes there is a lag between solving and getting the credit. Just wait.
Ruby translation. Please, review.
Approved. Thanks :-)
If the lift is going up and at floor 3 it finds [2,5,5], will the 2 climb in or will he stay out? Also if he doesnt climb in, can the 5s go in since the lift is going up?
First, it seems you didn't read the description (at least, you didn't do it correctly). Second, it's almost impossible to answer to your question since the context is lacking like half of the constraints of the problem. ;)
@wayne1512.
lift rule #6 "When called, the Lift will stop at a floor even if it is full, although unless somebody gets off nobody else can get on!"
I'm confused with this rule, what i understood is when the lift is full it will stop at floor even though no one can get in, although they could have get in if lift wasn't full. Am I correct or wrong, please help.
Your understanding is correct.
This comment has been hidden.
[0, 1, 2, 3, 7, 6, 5, 2, 1, 2, 3, 7, 6, 5, 3, 0, 2, 3, 4, 7, 6, 5, 2, 0, 5, 2, 0]
Pure OOP task. With elements of industrial automation. And very confusing explanation of the lift logic.
Thanks for your feedback. Do you have suggestions how it can be improved while remaining 100% accurate?
I think this place:
should be replaced by:
Your suggestion is not 100% accurate. e.g. The Lift may be empty and going up but there can be people below it still "wanting to get on in the direction the Lift is already travelling". This might happen if the Lift was full and they could not get on earlier...
It is a minefield of tricky cases like this which people might interpret wrongly, which is how I ended up with my current description. It might not be perfect, but I think at least it is correct for all cases.
But existing explanation of the empty lift logic is wrong because of:
Now I am confused because you used a quote block for something which is not written in the description. Please give details what you meant by above comment. Can you give examples what is wrong? I am happy to change this if it can be made better.
Sorry, from now I will use quote block appropriately. I just wanted to say that the behavior of the lift does not only depend on whether it is empty or not. According to the rule #3:
Thus rule #4 is redundant or mistaken. Example:
Lift path: 0 (lift is empty) -> 1 -> 2 (lift is empty again) -> 3 -> 4 (it is empty again) -> 9 -> 8 -> 7 -> 0
Lift was empty three times, but up to the fourth floor it stopped at each level to let the people get on/off (according to the rule #3). And only from the fourth floor it moved immediately to the ninth floor (according to the rule #4 ???). So despite the fact that all three times the lift was empty its behavior was different.
I apologize for not being word-perfect in English.
(Talking about case alexshalovsky provided.)
True, the
4th lift rule
is unneeded. According to the3rd lift rule
and the3rd people rule
it CAN'T act smart by going one more floor in the same direction to pick up an additional person, because it MUST go to the very top/bottom, leaving every one who wants to go down/up behind respectively, and only after that it can start picking up people who want to go in the opposite direction (so the opposite direction becomes the current diction and people who missed the lift also need to go in the current direction).This comment has been hidden.
As you can see, your message and the information you gave are not really clear, for now... :o
I am new to codewars, how can I see the unit tests used when I press "Attempt" button? (not the ones in the "Sample Tests")
EDIT: Figured it out, just need to print out the input array in the function and it will show up in the Log of failed tests.
Here is the "testUpAndDown":
capacity = 5; input: ({3}, {2}, {0}, {2}, {}, {}, {5}) expectedResult: {0, 1, 2, 3, 6, 5, 3, 2, 0}
.
What is the expected result for this test case?
capacity: 5 floor 0: floor 1: 0 0 0 6 floor 2: floor 3: floor 4: floor 5: 6 6 0 0 0 6 floor 6:
[0,1,5,6,5,1,0,1,0]
This one is REALLY well named... "Tricky queues". As long as you do not find the expected answer, that means you do not have the correct algo.
Thanks. I feel the word "queue" in problem description is kind of confusing, it turns out that the order doesn't matter at all...
actually, it does matter, but you have to considere the direction wanted by the different people too.
really a great kata to practice organization
I really liked this one, but I think the description could use a little more "redundancy". I mean it's precise (i.e. it describes the problem completely) but I had a hard time figuring out some details. Just a little explained example, e.g. for
testDownAndDown
ortestTrickyQueues
could really help. However, analyzing the test cases and figuring this out was kind of an interesting part too, so ...Confused myself for a while because my interpretation of this was "until there are no more people wanting to travel in the direction it is already travelling." Then I finally realized it actually means "until there are no more people wanting to get on or off of a floor that the lift is currently approaching." :P
Hi @dcsmith. Thanks for feedback. Yes, your interpretation is what it means, but I am not sure if yours is improvement or just different way of saying the same thing. Anyway, I plan to wait a while to see if there are common problems interpreting my rules before I start modifying the description.
good details!
The second kata in my collection.
I'm averaging 55 ms on the sample tests. How close am I?
great kata btw
This comment has been hidden.
This comment has been hidden.
mine does that too, with a lot of checks. Might be your code is really inefficient, then. Look for inefficient actions (pop in the middle or the beginning, containment checks in lists, ...)
This comment has been hidden.
This comment has been hidden.
Very nice Kata. The lesson I learned: even "smart" rules, when applied to tech can become trivial ones. Or vice versa: trivial tech rules look like "smart" ones, when descibed in human language. :smiling_imp:
This comment has been hidden.
Thanks for kind comments.
Coffeescript translation
Approved. Thanks.
Python: Actual and expected results are swapped in random tests.
woops... corrected! ;)
This comment has been hidden.
Instead of continuing down to ground floor
0
to pick up the last person wanting to go up (to 3), you changed direction to go back and get that last guy back on level5
.By changing direction you violated my rule:
RTFM to me, thanks @dinglemouse.
approved!!! ;)
Yay. Finally!! Thanks B4B.
(I hope there aren't too many tricky questions about this one - my brain has moved on)
I certainly appreciate the detailed instructions thus far, but I feel there are still questions left unanswered. And trying to apply the "what would happen in real life?" rule is not easy. Let's face it, this kata isn't very representative of the real world. For one major thing, the real world has a time dimension which this kata does not address. This kata assumes that everyone pushes their respective call button at the very beginning. I'm not suggesting we need to add a time argument or anything like that. But I do want to point out that, for some cases, it's difficult to compare this to the real world.
That being said, I eventually figured out the answers to all my questions. Perhaps that's half the challenge. But I felt like that's now how it was designed.
Overall, it's was quite a fun kata to do. Even before doing this I've always thought about elevator logic when waiting for or in one. Maybe, now that I've 'programmed' one, I won't so much.
Thanks for completing my Kata!
Sure. Actually my original intention was to also write a Version2 of this Kata did include such "timings". But considering details like button pres times + the speed of the lift in transit + delays of people entering/exiting + doors opening/closing .... all seems way too tricky. Probably the resulting Kata would just be too complex for anybody to be bothered with :-(
This Kata was supposed to be a just coding challenge; not a puzzle where it is a struggle to figure out the rules.
If you have any suggestions how the Kata description could be improved please let me know.
Cheers. DM
Just completed this in Python. A class idea, thank you. There appears to be some sort of codewars delay as I cannot see my solution but am allowed to see other solutions.
Hi,
I'm working on the translation and I would like to ask you for one thing : in python tests, I don't have an equivalent of
assertArrayEquals
. For now, I used a simpleassertEqual
but this way, the full moves solution is displayed in the "expected" part when a test fails. This will make the kata a bit easier (but just a bit). Are you ok with that or would you rather like that I create a behaviour equivalent toassertArrayEquals
? (that would not be a problem, it's just a little more work)Your opinion about that, please ?
Cheers,
B4B
This comment has been hidden.
Hi
@Blind4Basics
I'm happy with this as-is.
It wasn't supposed to be a "puzzle", so I don't think giving away the expected answer is a big problem.
Approved :-)
Thanks!
Thanks ! :)
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Hi
@Blind4Basics
I intended for everything in this Kata to behave just like in the real world.
It's not meant to be a PUZZLE. OTOH, it's not meant to be easy either...
Hint: If you find yourself writing code assuming psychic abilities of people or lifts or or anything else that would seem "wrong" in real-life then probably you are off course.
I don't think I need to spell out "or if it is empty" because when an empty lift arrives in answer to somebody pressing the call button they will know its direction. For example
~~
No. That will never happen:
People don't enter a lift unless it is going the direction they want to go and there is room to enter (just like in the real world)
People do know the direction of the lift when it stops on their floor because you can assume there is a little light outside the lift that shows that! (just like in the real world)
~~
Keep trying! Be the first to solve it :-)
Hi,
Very well ! I'll work on it... :)
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Ok, I was to hungry for it, I made the modifications in the test cases. ;-) Now it works plainly.
I didn't modify the description, of course. It's up to you.
Kata upvoted and ranked (maybe a bit too high. Others will say..). Are you ok if I do the python version ? (can I do it in the edition panel ?)
(EDIT : well, on second thought, I think I was a bit too much enthusiastic on the ranking... So, 3 kyu. But it might depend on the final version of the description... ;) )
Next time maybe try waiting 24hrs to give me chance to fix my own bugs! But thanks anyway.
Yes, I'll take your suggestions on-board and come up with something. Thanks.
Sure (I saw you haven't translated anything before, so you are starting with a hard one...)
No. Select the languge of this Kata and press
+ Add New
and go from there...That was my guess too
Hi
B4B
, Description is updated. Hopefully it's a bit clearer now. If you have any more suggestions please let me know.Cheers, DM
Ok, fine !!
(actually, I already translated 3 to 5 katas (6 kyu to 4 kyu ; I don't remember the exact number). I watched the code of this one, it should not cause troubles. If I encounter problems, I'll ask for your support here. :)
Regards,
B4B
Quick question,
It seems from your previous comments that this is intended to operate like elevators operate in the real world, which makes sense. However, I think, if I'm seeing this correctly, that there is a case where your instructions create a case where this does not occur. Your instructions say "The Lift never changes direction until there are no more people wanting to get on/off in the direction it is already travelling", but in the real world I would think that if an elevator is travelling up to a floor with passengers who need to get off on that floor, and that floor has people waiting to go up AND people waiting to go down it will determine the direction of it's next movement (if there are no more buttons pressed inside the elevator and thus it thinks it is empty) based on the order in which the elevator call button was pressed.
It's not a big deal and I'm coding based on the instructions, but it would seem to add a nice wrinkle (and mimic reality) if the order in which an elevator call button is pressed comes into play.
Thanks for the Kata.
Hi @ThomFabian - thanks for your feedback!
I expect there are many variation of lift software that are much more clever and do weird things like
And your "who pressed the button first" is maybe just another variable for different stratagies.
So when I say it is like a real world lift... it is... but it my slightly simplified world :-)