First solution in Clear category for Count Inversions by bukebuer
def count_inversion(sequence): return sum(sum(m Sept. 3, 2014 Comments: nickie on Sept. 16, 2014, 9:43 p.m. <p>I wouldn't call this clear but I like it as a one-liner.</p> marshall.zheng on Dec. 25, 2014, 5:43 a.m. <p>good one-line solution</p> l2032 on Jan. 10, 2015, 1:37 p.m. <p>same logic as mine, elegant as a one liner, but not exactly clear.</p> Pyth0n2 on Jan. 10, 2015, 4:46 p.m. <p>That's a really smart way of solving this.</p> <p>I really wish the clear category actually had clear problems upvoted to the top. maybe we need a new category called "actually clear" or "clear to beginners"</p> Tabreau on Feb. 27, 2015, 6:54 a.m. <p>I believe there should be a category called "concise" where these one-line solutions could be posted. I tend to be impressed by these solutions, but many of them may not be considered "clear".</p> BootzenKatzen on June 13, 2023, 6:15 p.m. <p>I agree. There's a lot of "well written" code that I don't understand as a beginner. I can tell it's probably good, but it's not clear to me without a lot of digging. It might be clearer if people added at least some annotation (which is a good habit to get into anyway).</p> BootzenKatzen on June 13, 2023, 6:13 p.m. <p>I agree. There's a lot of really cool/short codes that I don't quite understand without a lot of digging. It might be made clearer if people made some annotations or notes.</p> Happiness on Feb. 8, 2015, 4:55 p.m. <p>Can you explain your solution?</p> bukebuer on Feb. 9, 2015, 1:04 a.m. <p>It equals to:</p> <pre class='brush: python'>def count_inversion(sequence): count = 0 for i,n in enumerate(sequence): # for every number n in sequence for m in sequence[i+1:]: # for every number m behind n if m < n: # if there is a inversion count = count + 1 # count 1 more return count </pre> <p>The trick is that the value of (m < n) would be True or False and in Python they can be treated like 1 and 0. Thus you can sum a list of Trues and Falses. Obviously the result is the amount which True occurs. </p> DJO3 on April 28, 2015, 1:54 a.m. <p>Nice one liner, but not Pythonic and not clear. </p> Chandrachud_Murali on June 5, 2015, 10:15 a.m. <p>Very well exploited!!</p> gnucian on June 21, 2015, 1:57 a.m. <p>OK. A smart solution using 'enumerate()'. But what about the comparison of the last value with a value from a 'null' list? I'm wondering how Python deals with that case and what Boolean value it returns. The above function works only if the result is 'False' for that comparison, and apparently, yes, it seems so.</p> lukehammer on June 21, 2015, 6:58 a.m. <p>not clear, Speedy but not clear. </p> iggymas on July 8, 2015, 12:43 p.m. <p>Good solution, nice!</p> alpha313 on July 28, 2015, 12:10 p.m. <p>nice one-liner but does not belong in the "clear" category imho</p> ictar on Sept. 5, 2015, 7:20 a.m. <p>Beautiful~</p> Sedfer on Oct. 29, 2015, 11:34 a.m. <p>This is not clear solution, but creative. Please, NEVER use such style in real projects</p> M157q on Nov. 5, 2015, 1:14 p.m. <p>good for using `enmerate()` here.</p> lyxy on Jan. 7, 2016, 7:18 a.m. <p>I do not think this one can be categorized as clear</p> lijsh on Jan. 10, 2016, 7:46 p.m. <p>one line using lambda</p> <pre class='brush: python'>count_inversion = lambda sequence: sum(sum(v > j for j in sequence[i+1:]) for i, v in enumerate(sequence)) </pre> volodymyr.smuk on Jan. 16, 2016, 10:08 p.m. <p>Why "i+1"?</p> <p>sum(sum(m<n for m in sequence[i:]) for i,n in enumerate(sequence)). </p> bukebuer on Jan. 20, 2016, 1:29 a.m. <p>Yeah, they're same cuz m >= m always holds.</p>
Sept. 3, 2014
Comments: