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


Groups > comp.lang.python > #64630

Re: generate De Bruijn sequence memory and string vs lists

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'string': 0.09; 'ascii': 0.09; 'lookup': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '23,': 0.16; 'b):': 0.16; 'bytearray': 0.16; 'nameerror:': 0.16; 'peak': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'thu,': 0.19; '>>>': 0.22; 'memory': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'forgot': 0.30; 'code': 0.31; 'factor': 0.31; 'noted': 0.31; 'produces': 0.31; 'extend': 0.32; 'run': 0.32; 'convert': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'really': 0.36; 'sequence': 0.36; 'method': 0.36; 'should': 0.36; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'new': 0.61; 'first': 0.61; 'name': 0.63; 'n):': 0.84; 'otten': 0.84; 'increases': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: generate De Bruijn sequence memory and string vs lists
Date Thu, 23 Jan 2014 21:10:06 +0100
Organization None
References <CALyJZZVU+j64Jn7fqqzLmwW+KcM=43UMKjo=HW7umnq8MSP2Uw@mail.gmail.com> <lbrjjq$96p$1@ger.gmane.org> <lbrlel$vpq$1@ger.gmane.org> <CALyJZZUn4XvTHSnsJhQwsM7CyXUJJis_BwwoZ8EYAQ209bvfig@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p508487ec.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5906.1390507780.18130.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1390507780 news.xs4all.nl 2963 [2001:888:2000:d::a6]:41727
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64630

Show key headers only | View raw


Vincent Davis wrote:

> On Thu, Jan 23, 2014 at 12:02 PM, Peter Otten <__peter__@web.de> wrote:
>>
>> I just noted that the first Python loop can be eliminated:


Oops, I forgot to paste

import string
def chars(a, b):
    return "".join(map(chr, range(a, b)))
_mapping = string.maketrans(chars(0, 10), chars(48, 58))


>> def debruijn(k, n):
>>     a = k * n * bytearray([0])
>>     sequence = bytearray()
>>     extend = sequence.extend # factor out method lookup
>>     def db(t, p):
>>         if t > n:
>>             if n % p == 0:
>>                 extend(a[1: p+1])
>>         else:
>>             a[t] = a[t - p]
>>             db(t + 1, p)
>>             for j in xrange(a[t - p] + 1, k):
>>                 a[t] = j
>>                 db(t + 1, t)
>>     db(1, 1)
>>     return sequence.translate(_mapping)
> 
> 
> I am not really sure what _mapping should be. The code above does not run
> because
> NameError: global name '_mapping' is not defined
> I tried to get the bytearray
> ​ ​
> sequence to convert to ascii but don't know how to.

It does the same as adding 48 to every byte in the first variant:

>>> import string
>>> def chars(a, b):
...     return "".join(map(chr, range(a, b)))
... 
>>> _mapping = string.maketrans(chars(0, 10), chars(48, 58))
>>> b = bytearray(range(10))
>>> b
bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t')
>>> b.translate(_mapping)
bytearray(b'0123456789')

The disadvantage of this approach is that it produces a new bytearray, i. e. 
it increases the peak amount of memory used by the function significantly.

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


Thread

Re: generate De Bruijn sequence memory and string vs lists Peter Otten <__peter__@web.de> - 2014-01-23 21:10 +0100

csiph-web