Shortcut for chapter specific information

Showing posts with label insert sort perl one-liner. Show all posts
Showing posts with label insert sort perl one-liner. Show all posts

Thursday, April 28, 2011

Why I fail so easily in my perl one-liner insertion sort?

I guess part of it is that the program is complicated and takes simplification.

For instance, one can reduce the two assignments in the outer loop to
$k=$A[$j]; 
$i=$j-1;
to ($k,$i)=($A[$j],$j-1);
and
$A[$i+1]=$A[$i];
$i--;
to ($A[$i+1],$i)=($A[$i],$i-1);

Also the while loop should be gone because it is always the loop require more inspection adn error checking.
This should shorten the loop and reduce the chance of error.

In the case of perl, it also seems to be a good idea to have less $ in the program.  Got to think about it.

Wednesday, April 27, 2011

Debugged Insertion sort 47

What happened?  Turns out I used
"> =" instead of ">=". 

The whole thing dies.
The perfect one-liner game  is not for everyone......

Insertion sort 45 -47 (perl one-liner)

Ah, it got me.   45 and 46 both failed first time.  Debugging one-liner needs to make sure bracket matches mentally.  (It's difficult.) I even found that I can't debug insertion sort 47.  Since the machine is suddenly halt.

Bummer for the day, I guess this is skill.  You got to fail sometime to refine it.

Tuesday, April 26, 2011

Insertion sort 43 (perl one-liner)

:) Quite epic. 

perl -lane '{push @A,$_}END{for($j=1;$j<$#A+1;$j++){$k=$A[$j];$i=$j-1;while($i>=0&&$A[$i]>$k){$A[$i+1]=$A[$i];$i--}$A[$i+1]=$k;} printf("%s\n",join(" ",@A))}'

33_P