Re: [python-uk] A stack with better performance than using a list

2017-06-13 Thread Mark Lawrence via python-uk
On 13/06/2017 16:29, Jonathan Hartley wrote: On 06/13/2017 09:04 AM, Mark Lawrence via python-uk wrote: On 07/06/2017 18:50, Jonathan Hartley wrote: I recently submitted a solution to a coding challenge, in an employment context. One of the questions was to model a simple stack. I wrote a sol

Re: [python-uk] A stack with better performance than using a list

2017-06-13 Thread Jonathan Hartley
On 06/13/2017 09:04 AM, Mark Lawrence via python-uk wrote: On 07/06/2017 18:50, Jonathan Hartley wrote: I recently submitted a solution to a coding challenge, in an employment context. One of the questions was to model a simple stack. I wrote a solution which appended and popped from the end o

Re: [python-uk] A stack with better performance than using a list

2017-06-13 Thread Mark Lawrence via python-uk
On 07/06/2017 18:50, Jonathan Hartley wrote: I recently submitted a solution to a coding challenge, in an employment context. One of the questions was to model a simple stack. I wrote a solution which appended and popped from the end of a list. This worked, but failed with timeouts on their las

Re: [python-uk] A stack with better performance than using a list

2017-06-13 Thread Jonathan Hartley
Very interesting! Thanks for digging deeper and sharing. I was thinking about horrible complicated structures like storing the 'add_to_first_n' params in parallel to the stack, to apply them at 'pop' time, which doesn't work at all. As is so often the case with these things, your solution of

Re: [python-uk] A stack with better performance than using a list

2017-06-13 Thread Jonathan Hartley
You are right, when popping an empty stack I should probably raise. On 2017-06-08 13:06, Samuel F wrote: It may have failed for a different reason, (hard to say without the original question and answer). In the case where the stack is empty, you are returning None, was that the requirement? (

Re: [python-uk] A stack with better performance than using a list

2017-06-11 Thread Daniel Pope
I was able to get about a 20% speed up over Steve's solution, on some benchmark data I created, by: * converting LOAD_GLOBAL to LOAD_FAST for __builtins__ * eliminating the conditional in each loop in favour of a conditional on pop only * eliminating string comparison for the operation in favour o

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Stestagg
Apologies, In my previous email, I meant 'insert a marker', rather than 'push a marker' On Thu, Jun 8, 2017 at 7:17 PM Stestagg wrote: > I tracked down the challenge on the site, and have a working solution (I > won't share for obvious reasons). Basically the timeouts were being caused > by 'add

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Stestagg
I tracked down the challenge on the site, and have a working solution (I won't share for obvious reasons). Basically the timeouts were being caused by 'add_to_first_n' being called in horrible ways in the test cases. Because add_to_first_n alters the bottom of the stack, you can just push a marker

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Samuel F
It may have failed for a different reason, (hard to say without the original question and answer). In the case where the stack is empty, you are returning None, was that the requirement? (Likely to have been -1) Sam On Thu, 8 Jun 2017 at 17:27, Jonathan Hartley wrote: > Yep, that's a great el

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Jonathan Hartley
Yep, that's a great elimination of the suspicious small overheads. line_profiler is beautiful, I'll definitely be adding it to my toolbox, thanks for that! I tried a variant of accumulating the output and printing it all as a single string, but of course this didn't help, printing is already

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Stestagg
If it's who I think it is, then I'm not entirely surprised, this particular implementation is quite taxing for python in particular, and they don't do much in the way of catering to more modern languages in general (not a criticism, but most problems/samples are stated in a very 'traditional' way t

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Jonathan Hartley
Good point. FWIW, my submission was running Python 3. On 6/8/2017 04:33, Toby Dickenson wrote: In python 2, your use of range() without checking for a very large parameter n might cause either a MemoryError exception, or trigger a huge memory allocation just for the range list. Not a problem in

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Jonathan Hartley
I cannot be sure. It is certainly used by many people. They are competent in that it is a comprehensive online framework, allowing candidates to submit solutions using an online editor, in any one of about ten different languages. They are so large that there was no obvious way to talk to anyon

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Jonathan Hartley
I wondered about that too, but decided (without measuring) that it is no better. A deque allows us to append and pop elements from both ends, but the question didn't require that, it only needed from one end, which a list provides at O(1). On 6/8/2017 03:30, Simon Hayward wrote: Rather than

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Toby Dickenson
In python 2, your use of range() without checking for a very large parameter n might cause either a MemoryError exception, or trigger a huge memory allocation just for the range list. Not a problem in python 3 of course. On 8 June 2017 at 09:54, Stestagg wrote: > I honestly can't see a way to im

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Stestagg
I honestly can't see a way to improve this in python. My best solution is: def main(lines): stack = [] sa = stack.append sp = stack.pop si = stack.__getitem__ for line in lines: meth = line[:3] if meth == b'pus': sa(int(line[5:])) elif meth

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Andy Robinson
Are you sure that their test infrastructure was behaving correctly? Is it widely used, day in day out, by thousands, and known to be reliable? Did your colleagues all brag "no problem"? Or is it possible that the whole execution framework threw a momentary wobbly while trying to load up some larg

Re: [python-uk] A stack with better performance than using a list

2017-06-08 Thread Simon Hayward
Rather than using a list, aren't deques more appropriate as a data structure for stack like behaviour. https://docs.python.org/3.6/library/collections.html#collections.deque Regards Simon On Wed, 7 Jun 2017, at 19:33, Jonathan Hartley wrote: > Hey. > > Thanks for engaging, but I can't help wi

Re: [python-uk] A stack with better performance than using a list

2017-06-07 Thread Jonathan Hartley
Hey. Thanks for engaging, but I can't help with the most important of those questions - the large data sets on which my solution failed due to timeout are hidden from candidates. Not unreasonable to assume that they do exercise deep stacks, and large args to add_to_first_n, etc. Yes, the inp

Re: [python-uk] A stack with better performance than using a list

2017-06-07 Thread Tom Wright
Algorithms questions are always fun. Quick time to answer before other people! You might be hitting problems with the "amortized part" if their code didn't run for large enough n or used dumb special cases or bounds. They may have (inadvertantly?) meant "realtime constant" (python lists occasional

Re: [python-uk] A stack with better performance than using a list

2017-06-07 Thread Alex Willmer
On 7 June 2017 at 18:50, Jonathan Hartley wrote: > Ah. In writing this out, I have begun to suspect that my slicing of 'tokens' > to produce 'args' in the dispatch is needlessly wasting time. Not much, but > some. To put some numbers out there, eliminating the slice is not always a win. On Python

Re: [python-uk] A stack with better performance than using a list

2017-06-07 Thread Stestagg
Do you have any more context? For example, is the add_to_first_n likely to be called with very large numbers, or very often? Does the stack get very deep, or stay shallow? I'm assuming that lines look like this: push 1 push 2 add_to_first_n 2 10 pop pop with all arguments as integers, and the fi