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

From Jon Ribbens <jon+usenet@unequivocal.co.uk>
Newsgroups comp.lang.python
Subject Re: Is my implementation of happy number OK
Date 2015-04-30 17:04 +0000
Organization A noiseless patient Spider
Message-ID <slrnmk4o7a.apd.jon+usenet@frosty.unequivocal.co.uk> (permalink)
References <87oam5vc8k.fsf@Equus.decebal.nl>

Show all headers | 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