Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21089
| 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 | 2012-03-01 14:39 +0000 |
| Message-ID | <87obsg1j36.fsf@sapphire.mobileactivedefense.com> (permalink) |
| References | <85fa5760-68c8-41a2-9116-2489165f7ca1@j5g2000yqm.googlegroups.com> |
Cross-posted to 4 groups.
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