Set comprehension solution in Creative category for Find Sequence by veky
def checkio(data): n=len(data) def g(i,j): if 0<=i May 29, 2013 Comments: nickie on Nov. 17, 2013, 5:40 p.m. <p>Nice. I would avoid the encoding (p) for coordinates; it would cost a number of characters in the program but it would help understanding...</p> <p>The only drawback I can see (for all this family of solutions) is that it accesses 4x4xN^2 array elements (the first 4 being the number of directions, the second 4 being the number of identical elements in a sequence). It is possible to skip the second 4 by scanning complete rows, columns, and diagonal lines.</p> veky on Nov. 17, 2013, 6:06 p.m. <blockquote> <p>it would cost a number of characters in the program but it would help understanding</p> </blockquote> <p>... and therefore be lame. ;-)</p> <p>About microoptimizations... oh no, here you won't. There is a reason why N<=10 is specified in the task text. ;-PP</p> Cjkjvfnby on Jan. 25, 2014, 1:39 a.m. <p><strong>{g(p//n+d<em>k,p%n+e</em>k)</strong> I usualy produse such text by random typing instead of <strong>lorem ipsum</strong> :)</p> veky on Jan. 25, 2014, 4:57 a.m. <p>Now you have another use for my code. :-D</p> <p>Of course, I could have solved it with complex numbers, then it would be much more beautiful</p> <pre class='brush: python'>g(complex(*divmod(p,n))+k*round(1j**e)) </pre> <p>if only complex._ _ round _ _ existed (doing obvious thing). :-)</p> Checkos483 on Sept. 24, 2014, 2:34 p.m. <p>Very pythonic, I like it.</p> veky on Sept. 25, 2014, 2:50 p.m. <p>Thanks. Be sure to check other solutions of mine. ;-)</p> Winandfx on Jan. 26, 2015, 7:41 a.m. <p>wow, that's short. I took a long way of matrix rotation (as i understand it) to get diagonals <a href="https://pp.vk.me/c623930/v623930561/18ff7/wPgt0vP4oWY.jpg">https://pp.vk.me/c623930/v623930561/18ff7/wPgt0vP4oWY.jpg</a></p> StefanPochmann on April 23, 2015, 8:03 p.m. <p>Collecting None, sweet. Or is that just to confuse+educate? :-)</p> <p>`0<=i<n>j>=0` would have been nice as well. Or `n>i>-1<j<n`.</p> veky on April 23, 2015, 8:17 p.m. <blockquote> <p>Collecting None, sweet. Or is that just to confuse+educate? :-)</p> </blockquote> <p>Why "or"? It's "and". :-]</p> <p>Of course, there can't be all Nones since the starting position is always in the grid. And None is a hashable singleton, it would be a shame to neglect that. :-D</p> <blockquote> <p>0<=i<n>j>=0 would have been nice as well. Or n>i>-1<j<n. Of course. I used such constructs somewhere around here, I can't remember where exactly. :-) Or even \`0 <= min(i, j) <= max(i, j) < n\`. :-D Or \`{i, j} <= set(range(n))\`. Or just test \`i >~0< j` (eel operator;)) and use EAFP for another end. :-)</p> </blockquote> StefanPochmann on April 23, 2015, 8:36 p.m. <p>Ok now I want to confuse people with</p> <pre class='brush: python'>0 <= i,j < n </pre> <p>somehow...</p> <p>Btw, 0<=i is always True here, did you just test it for clarity?</p> veky on April 23, 2015, 8:46 p.m. <blockquote> <p>...confuse...</p> </blockquote> <p>Don't worry, they are already confused enough. :-)</p> <p>I think I got closest to that with min and max. Of course, you can write `both` so that</p> <pre class='brush: python'>0 <= both(i, j) < n </pre> <p>does what you want. :-D</p> <blockquote> <p>Btw, 0<=i is always True here, did you just test it for clarity?</p> </blockquote> <p>If by clarity you meant I managed to confuse even myself, you're correct. :-DD Yes, I didn't see that. Good news: then the eel operator is unary, and looks even nicer. :-D</p> StefanPochmann on April 23, 2015, 9:14 p.m. <p>That `both` looks nice. Here's a generalized version that already works (if you only have non-negative values):</p> <pre class='brush: python'># If all values are larger than zero... if all(values) > 0: </pre> <blockquote> <p>If by clarity you meant I managed to confuse even myself, you’re correct.</p> </blockquote> <p>:-D</p> veky on April 24, 2015, 4:06 a.m. <blockquote> <p>if all(values) > 0:</p> </blockquote> <p><a href="http://cdn.meme.am/instances/55581711.jpg">http://cdn.meme.am/instances/55581711.jpg</a> :-D</p> <p>(How can I be so sure it's wrong? <a href="http://search.cpan.org/~lembark/Quantum-Superpositions-2.02/lib/Quantum/Superpositions.pm">Perl does it.</a> :-P Perl6 even does it in the core. :-O)</p> <p>We already have a function that works perfectly well even if you don't only have non-negative values: `min`.</p> <p>And your example is just parsed wrongly... it's not "(all values) are larger than zero", but "all (values are larger than zero)":</p> <pre class='brush: python'>all(value > 0 for value in values) all(map(0 .__lt__, values)) </pre> StefanPochmann on April 25, 2015, 1:32 p.m. <p>Hey, stop trying to foil my plans of teaching people bad Python!</p> <p>That Perl thing sounds awesome.</p> veky on April 25, 2015, 11:08 p.m. <p>Hm, I guess you are too young. "Those Perl things" (prettymuch in general;) are some of the most dangerous hallucinogenic drugs in the programming universe. The false sense of power they give you is unmistakable. :-P Almost the whole (old enough) hacker community has gone through the phase of their recreational use. Most of us are clear now, but some are still addicted. These are powerful forces you're dealing with. :-O</p> StefanPochmann on April 25, 2015, 11:36 p.m. <p>I've been using Perl for a while as well, but not much anymore now that Python is my main language. Do you do Project Euler? I'm quite proud of my solution for <a href="https://projecteuler.net/problem=400">Problem 400</a>, my Perl solution for it is the shortest posted solution by far (my Python solution is longer, but still shorter and more elegant than anybody else's). I even thought about that problem/solution earlier today because of something else you posted here in a review :-)</p> veky on April 26, 2015, 12:10 p.m. <p>It's been a long time since I've been fascinated with Project Euclid, but that problem seems interesting in a mathematical way. If I catch few hours in the next week, I'll look into it. :-)</p> StefanPochmann on April 26, 2015, 3:50 p.m. <p>Yes, it's a nice problem. It's also Project Euler, not Project Euclid :-P</p> veky on April 26, 2015, 5:33 p.m. <p>Ah yes. You nitpicker, you. :-P :-D</p> StefanPochmann on April 26, 2015, 5:39 p.m. <p>I didn't know Project Euclid, btw. Are you using that?</p> veky on April 26, 2015, 6:02 p.m. <p>No, that way just a lapsus clavis. :-)</p> dannystaple on Nov. 4, 2016, 11:10 a.m. <p>Hmm - not come across "both" - is that an actual function somewhere in python? Can I get a link to the docs for it?</p> veky on Nov. 4, 2016, 2:09 p.m. <p>As I said, it is a homework. Write `both` such that `a < both(b, c) < d` has the intended semantics. Here is a start:</p> <pre class='brush: python'>@functools.total_ordering class both: def __init__(self, *args): self.items = args def __lt__(self, other): return all(...) </pre> dannystaple on Nov. 5, 2016, 8:06 p.m. <p>Ah missed that - I see what you meant there.</p> dannystaple on Nov. 4, 2016, 10:53 a.m. <p>Okay - let me see if I get this: * n - number of items per row. (assuming it's a square also the column). * g - will return a number if it's in range, and None if it's not. * the bit in the curly braces is making a set, of the 4 items, and should have only 1 item, since any combination that's not all the same would result in a set of more than 1. * d,e - these are the same compass directions I used. * p - looping through every point in the matrix. * So the coordinates: * `p//n` - integer division by n, the starting column. * `d<em>k` - direction times items in set (e</em>k in the next term is similar). * `p%n` - modulo of p by n - the starting row.</p> <p>With the multiple nested loops - presuming that any is working with a generator, where would it be able to terminate early. I think possibly only on d,e - which makes me wander if reversing those two loops would make it speedier (if that matters).</p> veky on Nov. 5, 2016, 4:18 p.m. <p>Mostly. :-)</p> <p>About the last remark... why don't you timeit? (Across all possible 4-sequences, if you want to be thorough.:)</p>
May 29, 2013
Comments: