8
The best solution I've found so far for a golf code puzzle I'm working on includes two rather fat-looking invocations of range
. I'm very new at code golf, especially in Python, so I could use a few tips.
The relevant fragment is this
[x for x in range(n+1,7**6)if`x`==`x`[::-1]*all(x%i for i in range(2,x))]
The upper limit of the first range
is not a sharp one. It should be at least 98690, and all else being equal (golf-wise, that is), the smaller the difference between this upper limit and 98690 the better, performance-wise1. I'm using 76 (=117649) because 7**6
is the shortest Python expression I can come up with that fits the bill.
In contrast, the lower limit in the first range
, as well as both limits in the second one, are firm. IOW, the program (in its current form) will produce incorrect results if those limits are changed.
Is there a way to shorten one or both of the expressions
range(n+1,7**6)
range(2,x)
?
BTW, in this case, aliasing range
to, say, r
gains nothing:
r=range;rr
rangerange
EDIT: FWIW, the full program is this:
p=lambda n:[x for x in range(n+1,7**6)if`x`==`x`[::-1]*all(x%i for i in range(2,x))][0]
p(n)
should be the smallest palindromic prime greater than n
. Also, p
should not be recursive. Warning: It is already obscenely slow!
1Yes, I know: performance is irrelevant in code golf, but that's why I wrote "all else being equal (golf-wise, that is)". For example, my choice of 7**6
, and not the more immediately obvious, but poorer-performing, "golf-equivalent" alternative 9**9
. I like to actually run my code golf attempts, which means not letting the performance degrade to the point that it would take years to run the code. If I can help it, of course.
1fwiw, you could make yours much faster for testing purposes by using generators; it's equivalent except for that:
p=lambda n:(x for x in xrange(n+1,7**6)if`x`==`x`[::-1]*all(x%i for i in xrange(2,x))).next()
. Of course, while your at that, might as well changexrange(2,x)
toxrange(2,int(x**.5+1))
and make your testing really fast. Clearly this code is equivalent to yours, just longer and faster. – Justin – 2014-11-23T20:55:29.3801It's impossible to come up with many good golf tricks with a short fragment isolated from its context. The best-golfed programs often make surprising connections between different part of the programs. For example, a seemingly useless discarded variable may prove key, or two unrelated loops combined into one. – feersum – 2014-11-23T21:09:14.740
@feersum: I posted the whole thing (in an EDIT) quite a while ago. It was before you posted your comment. Didn't you see it? – kjo – 2014-11-23T22:47:51.810
@FryAmTheEggman: yes, the statement of the puzzle is that it has to be a function, and, what's worse, the function can't be recursive. – kjo – 2014-11-23T22:50:18.080
I suspect you can save characters by directly finding the lowest prime rather than finding all of them in a range and taking the lowest one. Also, what exactly is the requirement that the function can't be recursive? Can you define
f
recursively, then sayp=f
? – xnor – 2014-11-24T00:17:25.180@xnor:there's an automated test for this that runs with a recursion limit set to ~100 – kjo – 2014-11-24T00:44:37.707
@kjo I didn't refresh the page at that time...and now it is indeed shown one of the
range
s was absolutely unnecessary for the actual program. – feersum – 2014-11-24T05:19:49.6471
You might want to look here
– Beta Decay – 2014-11-24T07:41:14.530Apparently this very coding challenge was already asked about on PPCG. You should look at isaacg's answer.
– xnor – 2014-11-25T05:41:12.817