Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21089
| Path | csiph.com!aioe.org!news.mixmin.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Rainer Weikusat <rweikusat@mssgmbh.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 |
| Date | Thu, 01 Mar 2012 14:39:25 +0000 |
| Lines | 47 |
| Message-ID | <87obsg1j36.fsf@sapphire.mobileactivedefense.com> (permalink) |
| References | <85fa5760-68c8-41a2-9116-2489165f7ca1@j5g2000yqm.googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| X-Trace | individual.net ha//K37tnDedRQF2EO7nwALso5h/0RYG+tQ3sqJSYpRlQ5OKE= |
| Cancel-Lock | sha1:uss2uLKgM4ETuh7WwdcB7XIgJtc= sha1:y1nmgM+3TO4hKMAgLG39Cyf0Y5U= |
| User-Agent | Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) |
| Xref | csiph.com comp.lang.lisp:8141 comp.emacs:1174 comp.lang.python:21089 comp.lang.perl.misc:4550 |
Cross-posted to 4 groups.
Show key headers only | View raw
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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