Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21080

Re: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.lisp, comp.emacs, comp.lang.python, comp.lang.perl.misc
Subject Re: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp
Followup-To comp.lang.lisp
Date 2012-03-01 06:37 +0000
Organization NewsGuy - Unlimited Usenet $19.95
Message-ID <jin5fp03os@enews1.newsguy.com> (permalink)
References <85fa5760-68c8-41a2-9116-2489165f7ca1@j5g2000yqm.googlegroups.com>

Cross-posted to 4 groups.

Followups directed to: comp.lang.lisp

Show all headers | View raw


Xah Lee wrote:

> fun example.
> 
> in-place algorithm for reversing a list in Perl, Python, Lisp
> http://xahlee.org/comp/in-place_algorithm.html
> 
> plain text follows
> ----------------------------------------
> 
> What's “In-place Algorithm”?
> 
> Xah Lee, 2012-02-29
> 
> This page tells you what's “In-place algorithm”, using {python, perl,
> emacs lisp} code to illustrate.
> 
> Here's Wikipedia In-place algorithm excerpt:
> 
> In computer science, an in-place algorithm (or in Latin in situ) is an
> algorithm which transforms input using a data structure with a small,
> constant amount of extra storage space. The input is usually
> overwritten by the output as the algorithm executes. An algorithm
> which is not in-place is sometimes called not-in-place or out-of-
> place.
> 
> Python
> 
> Here's a python code for reversing a list. Done by creating a new
> list, NOT using in-place:
> 
> # python
> # reverse a list
> 
> list_a = ["a", "b", "c", "d", "e", "f", "g"]
> 
> list_length = len(list_a)
> list_b = [0] * list_length
> 
> for i in range(list_length):
>     list_b[i] = list_a[list_length -1 - i]
> 
> print list_b
> Here's in-place algorithm for reversing a list:
> 
> # python
> # in-place algorithm for reversing a list
> 
> list_a = ["a", "b", "c", "d", "e", "f", "g"]
> 
> list_length = len(list_a)
> 
> for i in range(list_length/2):
>     x = list_a[i]
>     list_a[i] = list_a[ list_length -1 - i]
>     list_a[ list_length -1 - i] = x
> 
> print list_a
> Perl
> 
> Here's a perl code for reversing a list. Done by creating a new list,
> NOT using in-place:
> 
> # perl
> 
> use strict;
> use Data::Dumper;
> 
> my @listA = qw(a b c d e f g);
> 
> my $listLength = scalar @listA;
> my @listB = ();
> 
> for ( my $i = 0; $i < $listLength; $i++ ) {
>  $listB[$i] = $listA[ $listLength - 1 - $i];
> }
> 
> print Dumper(\@listB);
> 
> # perl
> # in-place algorithm for reversing a list.
> 
> use strict;
> use Data::Dumper;
> use POSIX; # for “floor”
> 
> my @listA = qw(a b c d e f g);
> 
> my $listLength = scalar @listA;
> 
> for ( my $i = 0; $i < floor($listLength/2); $i++ ) {
>   my $x = $listA[$i];
>   $listA[$i] = $listA[ $listLength - 1 - $i];
>   $listA[ $listLength - 1 - $i] = $x;
> }
> 
> print Dumper(\@listA);
> __END__
> 
> emacs lisp
> 
> ;; emacs lisp
> ;; reverse a array
> 
> (setq arrayA ["a" "b" "c" "d" "e" "f" "g"])
> 
> (setq arrayLength (length arrayA))
> 
> (setq arrayB (make-vector arrayLength 0))
> 
> (dotimes (i arrayLength )
>   (aset arrayB i (aref arrayA (- (1- arrayLength) i)) )
>   )
> 
> (print (format "%S" arrayB))
> ;; emacs lisp
> ;; in-place algorithm for reversing a array
> 
> (setq arrayA ["a" "b" "c" "d" "e" "f" "g"])
> 
> (setq arrayLength (length arrayA))
> 
> (dotimes (i (floor (/ arrayLength 2)))
>   (let (x)
>     (setq x (aref arrayA i))
>     (aset arrayA i (aref arrayA (- (1- arrayLength) i)))
>     (aset arrayA (- (1- arrayLength) i) x) ) )
> 
> (print (format "%S" arrayA))
> 

MatzLisp:

a = [2,3,5,8]
    ==>[2, 3, 5, 8]
a.reverse!
    ==>[8, 5, 3, 2]
a
    ==>[8, 5, 3, 2]

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

lang comparison: in-place algorithm for reversing a list in Perl, Python, Lisp Xah Lee <xahlee@gmail.com> - 2012-02-29 20:07 -0800
  Re: lang comparison: in-place algorithm for reversing a list in Perl, Python, Lisp Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-01 05:01 +0000
    Re: lang comparison: in-place algorithm for reversing a list in Perl, Python, Lisp Xah Lee <xahlee@gmail.com> - 2012-02-29 21:39 -0800
  Re: Re: lang comparison: in-place algorithm for reversing a list in Perl, Python, Lisp Evan Driscoll <driscoll@cs.wisc.edu> - 2012-03-01 00:07 -0600
  Re: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp "WJ" <w_a_x_man@yahoo.com> - 2012-03-01 06:37 +0000
    Re: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-01 22:14 +0000
    Re: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp Xah Lee <xahlee@gmail.com> - 2012-03-01 14:04 -0800
      Re: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp Chris Angelico <rosuav@gmail.com> - 2012-03-02 18:11 +1100
        Re: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp Xah Lee <xahlee@gmail.com> - 2012-03-02 03:30 -0800
  Re: lang comparison: in-place algorithm for reversing a list in Perl, Python, Lisp Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-01 14:39 +0000
  Re: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp "WJ" <w_a_x_man@yahoo.com> - 2012-03-02 07:17 +0000

csiph-web