Sujet : Re: ( Substring function in Python, Lisp) -- [Hijack] contains [hijk]
De : no.email (at) *nospam* nospam.invalid (Paul Rubin)
Groupes : comp.lang.lispDate : 17. Feb 2025, 08:44:22
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <878qq5582x.fsf@nightsong.com>
References : 1 2 3 4 5 6
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Madhu <
enometh@meer.net> writes:
FWIW My code is pretty clunky, somehow TIME SBCL says it conses 0 bytes
when I run it on individual words without interning although when I'm
explicitly manipulating plists and arrays.
Yikes. I did a Python version that conses a lot but is fairly concise,
I thought. From the looks of things, your /usr/share/dict/words is
larger than mine. Mine has about 100k entries and this code took about
0.7 seconds on my laptop, not great, but obviously many optimizations
are possible with the same basic approach:
from collections import defaultdict
def runlen(word:str) -> int:
d = defaultdict(int)
def pred(c): return chr(ord(c)-1) # unicode predecessor
for c in word:
d[c] = d[pred(c)] + 1
return max((n,c) for c,n in d.items())
def main():
for w in open('/usr/share/dict/words'):
n,c = runlen(w.strip())
if n>=5: print(w.strip(),n,c)
main()