Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'static': 0.04; '"""': 0.07; 'intermediate': 0.07; 'modified': 0.07; 'python3': 0.07; 'string': 0.09; '"__main__":': 0.09; '[0]': 0.09; '__name__': 0.09; 'bytes,': 0.09; 'deprecated': 0.09; 'lawrence': 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; 'translate': 0.10; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'translation': 0.12; 'thread': 0.14; "'from": 0.16; '1):': 0.16; '23,': 0.16; 'alphabet': 0.16; 'bytearray': 0.16; 'mark,': 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; 'str,': 0.16; 'subsequences': 0.16; 'url:whatsnew': 0.16; 'comment:': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'thu,': 0.19; 'code,': 0.22; 'import': 0.22; 'previously': 0.22; 'header:User- Agent:1': 0.23; 'url:dev': 0.24; 'tables': 0.26; 'post': 0.26; 'supported': 0.26; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'function': 0.29; "skip:' 10": 0.31; 'assert': 0.31; 'sep': 0.31; 'extend': 0.32; 'url:python': 0.33; 'running': 0.33; 'updated': 0.34; '"the': 0.34; 'skip:_ 10': 0.34; 'subject:lists': 0.35; 'test': 0.35; 'but': 0.35; 'version': 0.36; 'module.': 0.36; 'sequence': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'gone': 0.61; 'length': 0.61; 'url:3': 0.61; 'first': 0.61; 'soon': 0.63; 'special': 0.74; 'n):': 0.84; 'suspicious': 0.91; '2013': 0.98 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 22:15:26 +0100 Organization: None References: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 99 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390511696 news.xs4all.nl 2843 [2001:888:2000:d::a6]:32787 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64633 Vincent Davis wrote: > On Thu, Jan 23, 2014 at 2:36 PM, Mark Lawrence > wrote: > >> FTR string.maketrans is gone from Python 3.2+. Quoting from >> http://docs.python.org/dev/whatsnew/3.2.html#porting-to-python-3-2 "The >> previously deprecated string.maketrans() function has been removed in >> favor of the static methods bytes.maketrans() and bytearray.maketrans(). >> This change solves the confusion around which types were supported by the >> string module. Now, str, bytes, and bytearray each have their own >> maketrans and translate methods with intermediate translation tables of >> the appropriate type." >> > > ​Thanks for pointing this out Mark, ​I will soon be running this on 3.3+ Well, my first post in this thread head this suspicious comment: > # Python 2 > def debruijn(k, n): In hindsight I have no idea what I was trying to say ;) Anyway, as a special service to Mark and Vincent here's an updated version that might work on both Python 2 and 3 (there's no test but the ad-hoc demo in the if __name__ == "__main__" block): [debruijn is Vincents original code, debruijn_bytes my modified version] $ cat debruijn_compat.py def debruijn(k, n): """ De Bruijn sequence for alphabet size k (0,1,2...k-1) and subsequences of length n. From wikipedia Sep 22 2013 """ a = [0] * k * n sequence = [] def db(t, p,): if t > n: if n % p == 0: for j in range(1, p + 1): sequence.append(a[j]) else: a[t] = a[t - p] db(t + 1, p) for j in range(int(a[t - p]) + 1, k): a[t] = j db(t + 1, t) db(1, 1) return ''.join(map(str, sequence)) _mapping = bytearray(b"?")*256 _mapping[:10] = b"0123456789" def debruijn_bytes(k, n): a = k * n * bytearray([0]) sequence = bytearray() extend = sequence.extend 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 range(a[t - p] + 1, k): a[t] = j db(t + 1, t) db(1, 1) return sequence.translate(_mapping).decode("ascii") if __name__ == "__main__": d1 = debruijn(4, 8) d2 = debruijn_bytes(4, 8) print(d1[:50]) print(d2[:50]) assert d1 == d2 $ python debruijn_compat.py 00000000100000002000000030000001100000012000000130 00000000100000002000000030000001100000012000000130 $ python3 debruijn_compat.py 00000000100000002000000030000001100000012000000130 00000000100000002000000030000001100000012000000130 $ python -m timeit -s 'from debruijn_compat import debruijn as d' 'd(4, 8)' 10 loops, best of 3: 53.5 msec per loop $ python -m timeit -s 'from debruijn_compat import debruijn_bytes as d' 'd(4, 8)' 10 loops, best of 3: 22.2 msec per loop $ python3 -m timeit -s 'from debruijn_compat import debruijn as d' 'd(4, 8)' 10 loops, best of 3: 68 msec per loop $ python3 -m timeit -s 'from debruijn_compat import debruijn_bytes as d' 'd(4, 8)' 10 loops, best of 3: 21.7 msec per loop