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


Groups > comp.lang.python > #21064 > unrolled thread

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

Started byXah Lee <xahlee@gmail.com>
First post2012-02-29 20:07 -0800
Last post2012-03-02 07:17 +0000
Articles 11 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  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

#21064 — lang comparison: in-place algorithm for reversing a list in Perl, Python, Lisp

FromXah Lee <xahlee@gmail.com>
Date2012-02-29 20:07 -0800
Subjectlang comparison: in-place algorithm for reversing a list in Perl, Python, Lisp
Message-ID<85fa5760-68c8-41a2-9116-2489165f7ca1@j5g2000yqm.googlegroups.com>
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))

 Xah

[toc] | [next] | [standalone]


#21067

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-03-01 05:01 +0000
Message-ID<4f4f02a9$0$1539$c3e8da3$76491128@news.astraweb.com>
In reply to#21064
On Wed, 29 Feb 2012 20:07:49 -0800, Xah Lee wrote:

> 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

This is a good example of code written by somebody not very familiar with 
Python idioms. You don't need a temporary variable to swap two values in 
Python. A better way to reverse a list using more Pythonic idioms is:

for i in range(len(list_a)//2):
    list_a[i], list_a[-i-1] = list_a[-i-1], list_a[i]


But the best way (even more idiomatic and much, much faster) is this:

list_a.reverse()



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#21073

FromXah Lee <xahlee@gmail.com>
Date2012-02-29 21:39 -0800
Message-ID<865b899b-8ec7-4650-88d7-153d4e26178d@i6g2000yqe.googlegroups.com>
In reply to#21067
On Feb 29, 9:01 pm, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> wrote:
> You don't need a temporary variable to swap two values in
> Python. A better way to reverse a list using more Pythonic idioms is:
>
> for i in range(len(list_a)//2):
>     list_a[i], list_a[-i-1] = list_a[-i-1], list_a[i]

forgive me sir, but i haven't been at python for a while. :)
i was, actually, refreshing myself of what little polyglot skills i
have.

 Xah

[toc] | [prev] | [next] | [standalone]


#21078

FromEvan Driscoll <driscoll@cs.wisc.edu>
Date2012-03-01 00:07 -0600
Message-ID<mailman.318.1330582525.3037.python-list@python.org>
In reply to#21064

[Multipart message — attachments visible in raw view] — view raw

On 2/29/2012 23:05, Dan Stromberg wrote:
> 
> On Wed, Feb 29, 2012 at 8:07 PM, Xah Lee <xahlee@gmail.com
> <mailto:xahlee@gmail.com>> wrote:
> 
>     This page tells you what's “In-place algorithm”, using {python, perl,
>     emacs lisp} code to illustrate.
> 
> Aren't in-place reversals rather non-functional?

There is one place where they're reasonably idiomatic in Lispy
languages, at least by my understanding. That occurs when you are
writing a function that returns a list and there is a natural recursive
way to build up the answer -- but backwards. The idiom then is to build
up a temporary list up backwards, then call an in-place reversal
function. (NREVERSE in Common Lisp. I thought there was a reverse! in
Scheme, but apparently not.)

This doesn't break the external view of a pure function because the list
that's being reversed is a fresh, temporary list, which is why this
idiom would even fit in pretty well in Scheme.

Evan

[toc] | [prev] | [next] | [standalone]


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

From"WJ" <w_a_x_man@yahoo.com>
Date2012-03-01 06:37 +0000
SubjectRe: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp
Message-ID<jin5fp03os@enews1.newsguy.com>
In reply to#21064
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]

[toc] | [prev] | [next] | [standalone]


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

FromRainer Weikusat <rweikusat@mssgmbh.com>
Date2012-03-01 22:14 +0000
SubjectRe: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp
Message-ID<87zkc0oto4.fsf@sapphire.mobileactivedefense.com>
In reply to#21080
Xah Lee <xahlee@gmail.com> writes:

[...]

> similarly, in perl, either one
> require POSIX; floor(x/y);
> the require POSIX instead of Math is a quirk. But even, floor should
> really be builtin.
> or
> using a perl hack
> int(x/y)
>
> all of the above are quirks. They rely on computer engineering by-
> products (such as int),

Integral numbers are not 'a computer engineering byproduct'.

> or rely on the lang's idiosyncrasy. One easy way to measure it is
> whether a programer can read and understand a program without having
> to delve into its idiosyncrasies. Problem with these lang idioms is
> that it's harder to understand, and whatever advantage/optimization
> they provide is microscopic and temporary.

It's hard to understand for someone who knows only mathematical
idiosyncrasies and who is also convinced that this should really be
more than enough for a lifetime. But that's not some kind of 'natural
knowledge' people just happen to have but systematically drilled into
pupils from a very early age, despite most of them won't ever have any
use for any of it insofar it goes beyond + - * /. 

[...]

> idiomatic programing, is a bad thing.

If you have to use something (like a particular programming language)
but you resent learning how to use it and rather make lofty excuses,
chances are that you are rather a lazy f*cker than a great philosopher
...

[toc] | [prev] | [next] | [standalone]


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

FromXah Lee <xahlee@gmail.com>
Date2012-03-01 14:04 -0800
SubjectRe: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp
Message-ID<8e769add-e63c-4a74-bb7e-ddd36c04a415@vs5g2000pbc.googlegroups.com>
In reply to#21080
On Mar 1, 7:04 am, Kaz Kylheku <k...@kylheku.com> wrote:
 lisp:
 (floor (/ x y)) --[rewrite]--> (floor x y)

Thanks for this interesting point.

I don't think it's a good lang design, more of a lang quirk.

similarly, in Python 2.x,
x/y
will work when both x and y are integers. Also,
x//y
works too, but that // is just perlish unreadable syntax quirk.

similarly, in perl, either one
require POSIX; floor(x/y);
the require POSIX instead of Math is a quirk. But even, floor should
really be builtin.
or
using a perl hack
int(x/y)

all of the above are quirks. They rely on computer engineering by-
products (such as int), or rely on the lang's idiosyncrasy. One easy
way to measure it is whether a programer can read and understand a
program without having to delve into its idiosyncrasies. Problem with
these lang idioms is that it's harder to understand, and whatever
advantage/optimization they provide is microscopic and temporary.

best is really floor(x/y).

idiomatic programing, is a bad thing. It was spread by perl, of
course, in the 1990s. Idiomatic lang, i.e. lang with huge number of
bizarre idioms, such as perl, is the worst.

 Xah

[toc] | [prev] | [next] | [standalone]


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

FromChris Angelico <rosuav@gmail.com>
Date2012-03-02 18:11 +1100
SubjectRe: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp
Message-ID<mailman.341.1330672293.3037.python-list@python.org>
In reply to#21105
On Fri, Mar 2, 2012 at 9:04 AM, Xah Lee <xahlee@gmail.com> wrote:
> One easy
> way to measure it is whether a programer can read and understand a
> program without having to delve into its idiosyncrasies.

Neither the behavior of ints nor the behavior of IEEE floating point
is a "quirk" or an "idiosyncracy". These are data types with
well-defined semantics, and you need to understand them to use them.
The fact that dividing two positive integers and producing (or casting
to) a third integer rounds the result down is just as much a part of
the definition as is two's complement negatives, which most people can
safely ignore because they "just work" the way you expect.

Learn what you're working with, if you expect to get decent results from it.

ChrisA

[toc] | [prev] | [next] | [standalone]


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

FromXah Lee <xahlee@gmail.com>
Date2012-03-02 03:30 -0800
SubjectRe: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp
Message-ID<a388925d-6b71-4588-a7e1-7078be3bd97e@qr9g2000pbc.googlegroups.com>
In reply to#21124
Xah Lee wrote:
«… One easy way to measure it is whether a programer can read and
understand a program without having to delve into its idiosyncrasies.
…»

Chris Angelico wrote:
«Neither the behavior of ints nor the behavior of IEEE floating point
is a "quirk" or an "idiosyncracy". …»

they are computer engineering by-products. Are quirks and
idiosyncracies. Check out a advanced lang such as Mathematica. There,
one can learn how the mathematical concept of integer or real number
are implemented in a computer language, without lots by-products of
comp engineering as in vast majority of langs (all those that chalks
up to some IEEEEEEE. (which, sadly, includes C, C++, java, perl,
python, lisp, and almost all. (lisp idiots speak of the jargon “number
tower” instead IEEEE.) (part of the reason almost all langs stick to
some IEEEEEEEE stuff is because it's kinda standard, and everyone
understand it, in the sense that unix RFC (aka really fucking common)
is wide-spread because its free yet technically worst. (in a sense,
when everybody's stupid, there arise a cost to not be stupid.)))).

 Xah

[toc] | [prev] | [next] | [standalone]


#21089

FromRainer Weikusat <rweikusat@mssgmbh.com>
Date2012-03-01 14:39 +0000
Message-ID<87obsg1j36.fsf@sapphire.mobileactivedefense.com>
In reply to#21064
Xah Lee <xahlee@gmail.com> writes:

[...]


> # 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);

Better algorithm for that (expects an array reference as first
argument)

sub rev
{
    my $a = $_[0];
    my ($n0, $n1, $x);
    
    $n0 = 0;
    $n1 = $#$a;
    while ($n0 < $n1) {
	$x = $a->[$n0];
	$a->[$n0] = $a->[$n1];
	$a->[$n1] = $x;
	
	++$n0;
	--$n1;
    }
}

NB: The fact that a sufficiently sophisticated compiler might be able
to fix this automatically emphasizes the deficiencies of the original
attempt, it doesn't excuse them.

[toc] | [prev] | [next] | [standalone]


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

From"WJ" <w_a_x_man@yahoo.com>
Date2012-03-02 07:17 +0000
SubjectRe: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp
Message-ID<jips6e0f14@enews2.newsguy.com>
In reply to#21064
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))
> 
>  Xah

NewLisp:

> (setq lst '(2 3 5 8))
(2 3 5 8)
> (reverse lst)
(8 5 3 2)
> lst
(8 5 3 2)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web