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


Groups > comp.lang.python > #29670

Re: Algorithms using Python?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:Python': 0.05; 'none,': 0.05; '21,': 0.07; 'python': 0.09; 'self.data': 0.09; 'sep': 0.09; 'subject:using': 0.09; 'def': 0.10; 'elements,': 0.16; 'node,': 0.16; 'remove(self,': 0.16; 'value:': 0.16; 'wrote:': 0.17; 'char': 0.17; 'element': 0.17; 'yield': 0.17; '>>>': 0.18; 'seems': 0.23; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'fine': 0.28; 'node': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'class': 0.29; "i'm": 0.29; 'fri,': 0.30; '(and': 0.32; 'implement': 0.32; 'print': 0.32; 'shift': 0.33; 'to:addr:python- list': 0.33; 'received:google.com': 0.34; 'data,': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'but': 0.36; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'skip:l 20': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'skip:n 10': 0.63; 'become': 0.65; '"remove"': 0.81; 'to:name:python': 0.84; 'comment.': 0.91; 'dennis': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=34oASaGo0prCJZqc5TAabVDRkSuBHjOL1PqQH3uUAzo=; b=nK3qIcrnIbDyJVQCR+2qjhUKAwg1JpsClFiwvnKSX38qHE76BUAvabscUi2eHv/OCz YBSKEyqBLlaM89qKKS3gwZMmrycodBz60v871x3FUSFbJQFPwZF/QgRY875MCjtf90KL kVTozidPZpoiA681S3Jib7DrB5LPXLToEsVu8H4oM4qr+j1/HBgvymLGp+O5MsdAGj7O ZT13ZViUL5v4Efy65ELZTXGpvYI8QrDbtGODVyf7hg0xWww5FIinCP1e5PwGArnvnyx0 YBfSnqZ5ppyN2wEeTYsmNw458TEzjLwcx1pXwZlEvZRyDomUR1Lqq5NvQJUw68rYEpch SvmQ==
MIME-Version 1.0
In-Reply-To <jngp58p2mq1nrov4l0tj29e891vrg8vo6b@invalid.netcom.com>
References <TYV6s.7304$Vh6.4862@newsfe20.iad> <jngp58p2mq1nrov4l0tj29e891vrg8vo6b@invalid.netcom.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 21 Sep 2012 14:07:01 -0600
Subject Re: Algorithms using Python?
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1032.1348258054.27098.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348258054 news.xs4all.nl 6912 [2001:888:2000:d::a6]:48118
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29670

Show key headers only | View raw


On Fri, Sep 21, 2012 at 1:45 PM, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:
>         You can probably implement them, but they're not going to be very
> efficient. (And never "remove" an element from the linked-list
> implementation because Python would shift all the other elements, hence
> your "links" become invalid).

I'm not sure what you mean by that last comment.

class Node(object):
    def __init__(self, data, next):
        self.data = data
        self.next = next

class LinkedList(object):
    def __init__(self):
        self._head = None
    def __iter__(self):
        node = self._head
        while node:
            yield node.data
            node = node.next
    def insert_front(self, value):
        self._head = Node(value, self._head)
    def remove(self, value):
        prior, node = None, self._head
        while node:
            if node.data == value:
                if prior:
                    prior.next = node.next
                else:
                    self._head = node.next
                break
            prior, node = node, node.next
        else:
            raise ValueError("value not found")

>>> li = LinkedList()
>>> for char in 'edcba':
...     li.insert_front(char)
...
>>> print ''.join(li)
abcde
>>> li.remove('c')
>>> print ''.join(li)
abde

It seems to work fine to me.

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


Thread

Algorithms using Python? Mayuresh Kathe <mayuresh@kathe.in> - 2012-09-21 14:26 +0530
  Re: Algorithms using Python? Joel Goldstick <joel.goldstick@gmail.com> - 2012-09-21 10:41 -0400
  Re: Algorithms using Python? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-21 15:45 -0400
  Re: Algorithms using Python? Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-21 14:07 -0600
  Re: Re: Algorithms using Python? Evan Driscoll <driscoll@cs.wisc.edu> - 2012-09-21 15:43 -0500
  Re: Algorithms using Python? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-21 17:14 -0400
    Re: Algorithms using Python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-22 01:28 +0000
      Re: Algorithms using Python? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-22 00:29 -0400
  Re: Algorithms using Python? Wayne Werner <wayne@waynewerner.com> - 2012-09-26 10:55 -0500
    Re: Algorithms using Python? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-27 04:15 -0700
    Re: Algorithms using Python? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-27 04:15 -0700

csiph-web