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


Groups > comp.lang.python > #89645

Re: Is my implementation of happy number OK

Path csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Jon Ribbens <jon+usenet@unequivocal.co.uk>
Newsgroups comp.lang.python
Subject Re: Is my implementation of happy number OK
Date Thu, 30 Apr 2015 17:04:44 +0000 (UTC)
Organization A noiseless patient Spider
Lines 26
Message-ID <slrnmk4o7a.apd.jon+usenet@frosty.unequivocal.co.uk> (permalink)
References <87oam5vc8k.fsf@Equus.decebal.nl>
Injection-Date Thu, 30 Apr 2015 17:04:44 +0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="d7b793ff660f89c4945c470c81727cd8"; logging-data="32670"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+UuzMUaSiV5enz8cm2w/dhYJ0b2GX9qfs="
User-Agent slrn/1.0.1 (Linux)
Cancel-Lock sha1:biLq2mCa1Qu3LNrzcYjbBGyuk2w=
Xref csiph.com comp.lang.python:89645

Show key headers only | View raw


On 2015-04-30, Cecil Westerhof <Cecil@decebal.nl> wrote:
> Besides it need some documentation: is it a good implementation? Or
> are there things I should do differently?

Here's an alternative implementation which is a bit neater:

    def find_happy(maximum):
	"""Return set of happy numbers between 1 and `maximum`"""
	happy = {1}
	unhappy = set()
	for num in range(maximum + 1):
	    sequence = set()
	    while num not in sequence and num not in unhappy:
		sequence.add(num)
		if num in happy:
		    happy |= sequence
		    break
		nextnum = 0
		while num:
		    num, digit = divmod(num, 10)
		    nextnum += digit * digit
		num = nextnum
	    else:
		unhappy |= sequence
	return happy

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


Thread

Is my implementation of happy number OK Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 17:59 +0200
  Re: Is my implementation of happy number OK Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-04-30 17:04 +0000
  Re: Is my implementation of happy number OK Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-30 11:37 -0600
    Re: Is my implementation of happy number OK Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 21:05 +0200
  Re: Is my implementation of happy number OK Dave Angel <davea@davea.name> - 2015-04-30 14:53 -0400
    Re: Is my implementation of happy number OK Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 22:35 +0200
      Re: Is my implementation of happy number OK Dave Angel <davea@davea.name> - 2015-04-30 17:31 -0400
    Re: Is my implementation of happy number OK Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-04-30 23:31 +0000
      Re: Is my implementation of happy number OK Dave Angel <davea@davea.name> - 2015-04-30 19:52 -0400
        Re: Is my implementation of happy number OK Cecil Westerhof <Cecil@decebal.nl> - 2015-05-01 06:36 +0200
        Re: Is my implementation of happy number OK Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-05-01 07:23 +0000
          Re: Is my implementation of happy number OK Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-01 18:27 +1000
            Re: Is my implementation of happy number OK Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-01 09:03 -0600
            Re: Is my implementation of happy number OK Peter Otten <__peter__@web.de> - 2015-05-01 20:13 +0200
              Re: Is my implementation of happy number OK Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-05-02 21:23 +0000

csiph-web