Skip to content

Instantly share code, notes, and snippets.

@kashewnuts
Created July 22, 2016 02:04
Show Gist options
  • Save kashewnuts/5a9506a2ddb7892941381031911dc90d to your computer and use it in GitHub Desktop.
Save kashewnuts/5a9506a2ddb7892941381031911dc90d to your computer and use it in GitHub Desktop.
hamming2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from itertools import islice
def hamming2():
'''
A text docmenting this function (stripped)
'''
h = 1
_h = [h] # memoized
multipliers = (2, 3, 5)
multipdeces = [0 for i in multipliers] # index into _h for multipliers
multvalues = [x * _h[i] for x, i in zip(multipliers, multipdeces)]
yield h
while True:
h = min(multvalues)
_h.append(h)
for (n, (v, x, i)) in enumerate(zip(multvalues, multipliers,
multipdeces)):
if v == h:
i += 1
multipdeces[n] = i
multvalues[n] = x * _h[i]
# cap the memoization
mini = min(multipdeces)
if mini >= 1000:
del _h[:mini]
multipdeces = [i - mini for i in multipdeces]
#
yield h
def main():
hamming2()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment