[perl-python] sorting matrixes

Discussion in 'Python' started by Xah Lee, Mar 22, 2005.

  1. Xah Lee

    Xah Lee Guest

    Today we'll write a program that can sort a matrix in all possible
    ways.

    Here's the Perl documentation. I'll post a Perl and Python version in 2
    days.

    -----------

    sort_matrix( $matrix, [[$n1, $stringQ, $directionQ], [$n2, $stringQ,
    $directionQ], ...]) sorts a matrix by $n1 th column then $n2 th...and
    so on.

    $matrix must be a reference to references of arrays, having the form
    [[$e1, $e2,...], [...], ...]. $stringQ is a boolean indicating
    whether to treat corresponding columns as a strings instead of as
    number in the sorting process. True means string. $directionQ is a
    boolean indicating ascending sort or not for the correpsonding
    column. In the column spec $n1 $n2 ..., index counting starts at 0.

    Example:

    my $ref_matrix =
    [
    [3, 99, 'a'],
    [2, 77, 'a'],
    [1, 77, 'a']
    ];

    sort_matrix( $ref_matrix, [ [2,1,1], [1,0,1] ]);
    # this means sort by third column, regarding it as strings,
    # and in ascending order. If tie, sort by second column,
    # regarding it as number, in ascending order.

    # returns [[2,77,'a'],[1,77,'a'],[3,99,'a']];

    ------------------

    Note: in the above, ignore the "must be a reference to references of
    arrays". That's technical point, because Perl the language do nested
    lists thru workaround of "references".

    http://xahlee.org/perl-python/sort_matrix.html
    Xah Lee, Mar 22, 2005
    #1
    1. Advertising

  2. On 22 Mar 2005 09:02:51 -0800, rumours say that "Xah Lee" <> might
    have written:

    >Today we'll write a program that can sort a matrix in all possible
    >ways.
    >
    >Here's the Perl documentation. I'll post a Perl and Python version in 2
    >days.


    Don't bother writing a Python version... list.sort and its arguments are fine
    and send their greetings.
    --
    TZOTZIOY, I speak England very best.
    "Be strict when sending and tolerant when receiving." (from RFC1958)
    I really should keep that in mind when talking with people, actually...
    Christos TZOTZIOY Georgiou, Mar 22, 2005
    #2
    1. Advertising

  3. Xah Lee

    Xah Lee Guest

    Re: sorting matrixes

    Here's the solution to previous post.

    -------------------------------
    perl code:

    sub sort_matrix($$) {
    my $ref_matrix = $_[0];
    my @indexMatrix = @{$_[1]};

    my @indexes = map {$_->[0]} @indexMatrix;
    my @operators = map {$_->[1] ? ' cmp ' : ' <=> '} @indexMatrix;
    my @directions = map {$_->[2]} @indexMatrix;

    my $body_code = '';
    my @body_array;
    for (my $i = 0; $i <= $#indexes; $i++) {
    if ($directions[$i]) {
    push(@body_array, "(\$a->[$i]" . $operators[$i] .
    "\$b->[$i])");
    } else {
    push(@body_array, "(\$b->[$i]" . $operators[$i] .
    "\$a->[$i])");
    };
    };
    $body_code = join( ' or ', @body_array);

    my $array_code = '(map { [' . join(q(, ), map {"\$_->[$_]"}
    @indexes) . ', $_]} @$ref_matrix)';

    my $code = "map {\$_->[-1]} (sort { $body_code} $array_code)";
    my @result = eval $code;
    return [@result];
    };

    ------------------------------------------
    Python code

    # python v 2.4

    def sort_matrix(matrix, directives):
    result=matrix
    for dir in directives:
    if dir[1]:
    if dir[2]:
    result.sort(lambda x,y: cmp( str(x[dir[0]]),
    str(y[dir[0]])) )
    else:
    result.sort(lambda x,y: cmp( str(x[dir[0]]),
    str(y[dir[0]])), None, True)
    else:
    if dir[2]:
    result.sort(lambda x,y: cmp(float(x[dir[0]]),
    float(y[dir[0]])) )
    else:
    result.sort(lambda x,y: cmp(float(x[dir[0]]),
    float(y[dir[0]])), None, True )
    return result

    m = [
    [3, 99, 'a'],
    [2, 77, 'a'],
    [1, 77, 'a']
    ]

    print sort_matrix(m,[
    [2,True,True],
    [1,False,True]
    ])

    The Python code has not been tested much.

    http://xahlee.org/perl-python/sort_matrix.html

    Xah

    ∑ http://xahlee.org/PageTwo_dir/more.html

    ☄
    Xah Lee, Mar 28, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jose Garcia

    MATRIXES - Dinamic Memory

    Jose Garcia, Feb 10, 2005, in forum: C Programming
    Replies:
    3
    Views:
    354
    Barry Schwarz
    Feb 11, 2005
  2. LumisROB
    Replies:
    14
    Views:
    483
    LunisRob
    Sep 27, 2005
  3. Replies:
    2
    Views:
    346
    mlimber
    May 22, 2006
  4. KinGPIN
    Replies:
    1
    Views:
    313
    mlimber
    Jul 24, 2006
  5. Xah Lee

    [perl-python] sorting matrixes

    Xah Lee, Mar 22, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    95
    Xah Lee
    Mar 28, 2005
Loading...

Share This Page