Difference between revisions of "PGsort"

From WeBWorK_wiki
Jump to navigation Jump to search
 
Line 19: Line 19:
 
'''Examples'''
 
'''Examples'''
   
<blockquote> join(" ",PGsort( sub {$_[0] &lt; $_[1]} , (23,2,10,11,11,31) ); returns "2 10 11 11 23 31"</blockquote>
 
  +
<blockquote><ul>
<blockquote> PGsort (sub { ($_[0] % 2) &lt; ($_[1] % 2) }, @L ); will move the even numbers to the left of the odd numbers.</blockquote>
 
  +
<li> <code>join(" ",PGsort( sub {$_[0] &lt; $_[1]} , (23,2,10,11,11,31) );</code> returns "2 10 11 11 23 31"</li>
<blockquote> PGsort( <br />
 
  +
<br />
&nbsp; &nbsp; &nbsp; sub { my ($P1, $P2) = @_;
 
  +
<li> <code>PGsort (sub { ($_[0] % 2) &lt; ($_[1] % 2) }, @L );</code> will move the even numbers to the left of the odd numbers.</li>
my $P1x = $P1 ->{data}[0], $P2x = $P2 -> {data}[0];
 
  +
<br />
if ($P1x < $P2x) { return 1; }<br />
 
  +
<li> <code>sub pointLex { <br />&nbsp; &nbsp; &nbsp;my ($P1, $P2) = @_;<br />
&nbsp; &nbsp; &nbsp;
+
&nbsp; &nbsp; &nbsp;my $P1x = $P1 ->{data}[0], $P2x = $P2 -> {data}[0];<br />
if ($P1x > $P2x) { return 0; }
+
&nbsp; &nbsp; &nbsp;if ($P1x < $P2x) { return 1; }<br />
$P1 ->{data}[1] < $P2 -> {data}[1]; }
+
&nbsp; &nbsp; &nbsp;if ($P1x > $P2x) { return 0; }<br />
<br />,
+
&nbsp; &nbsp; &nbsp;$P1 ->{data}[1] < $P2 -> {data}[1]; <br />
( Point (2,2), Point (1,3), Point(2,1) ) ) <br />&nbsp; &nbsp; &nbsp; returns (Point (1,3), Point (2,1), Point (2,2)) (now in lexigraphical order)
+
&nbsp; &nbsp; &nbsp;} </code>
</blockquote>
+
<br /><br />
  +
tells whether two 2-D points are in Lexicographical order. ((0,0) < (0, 3) < (1, 0), etc.)<br />
  +
<code>PGsort( ~~&pointLex, ( Point (2,2), Point (1,3), Point(2,1) ) )</code> returns (Point (1,3), Point (2,1), Point (2,2))
  +
</li>
  +
</ul></blockquote>
   
 
'''Notes'''
 
'''Notes'''

Latest revision as of 20:40, 5 January 2014

PGSort

Description

General sorting macro

Syntax

PGsort( &sort_subroutine, @list);

Params

&sort_subroutine is a subroutine of two variables which defines order. Its output must be 0 or 1, with a 1 indicating the first argument is less than the second.

@list is the list to be sorted.

Returns

Returns list of elements in @list, ignoring duplicates. The order is unspecified.

Examples

  • join(" ",PGsort( sub {$_[0] < $_[1]} , (23,2,10,11,11,31) ); returns "2 10 11 11 23 31"

  • PGsort (sub { ($_[0] % 2) < ($_[1] % 2) }, @L ); will move the even numbers to the left of the odd numbers.

  • sub pointLex {
         my ($P1, $P2) = @_;
         my $P1x = $P1 ->{data}[0], $P2x = $P2 -> {data}[0];
         if ($P1x < $P2x) { return 1; }
         if ($P1x > $P2x) { return 0; }
         $P1 ->{data}[1] < $P2 -> {data}[1];
         }


    tells whether two 2-D points are in Lexicographical order. ((0,0) < (0, 3) < (1, 0), etc.)
    PGsort( ~~&pointLex, ( Point (2,2), Point (1,3), Point(2,1) ) ) returns (Point (1,3), Point (2,1), Point (2,2))

Notes

Using the perl sort directly in problems is not allowed for two reasons. The first is that the hardwired $a and $b variables used by sort can interfere with other values assigned to $a and $b. The second is that it is pretty easy to create closed loops with sort that hang the program. Currently nothing is done to prevent this, but later we may wish to modify num_sort and lex_sort to catch this problem.