Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89635
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Cecil Westerhof <Cecil@decebal.nl> |
| Newsgroups | comp.lang.python |
| Subject | Is my implementation of happy number OK |
| Date | Thu, 30 Apr 2015 17:59:07 +0200 |
| Organization | Decebal Computing |
| Lines | 46 |
| Message-ID | <87oam5vc8k.fsf@Equus.decebal.nl> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| Injection-Info | mx02.eternal-september.org; posting-host="1e75f8f7bcfe2945d0316cb03b0f6951"; logging-data="18980"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19k8rjxgIfgOA8M5lyU0XdpDd5r5VCeflY=" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
| X-Homepage | http://www.decebal.nl/ |
| X-Face | "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR<J%a^F2lh4[N~Yz4 nSp#c+aQo1b5=?HcNEkQ7QzF<])O3X4MDL/AYjys&*mt>,v+Pti8=Vi/Z"g^?b"E |
| Cancel-Lock | sha1:YuS0rlg00U63GbAoz7HRpCGTITY= sha1:RSjra7+YATxlCtjskSSULG1jyO8= |
| Xref | csiph.com comp.lang.python:89635 |
Show key headers only | View raw
I implemented happy_number function:
_happy_set = { '1' }
_unhappy_set = set()
def happy_number(n):
"""
Check if a number is a happy number
https://en.wikipedia.org/wiki/Happy_number
"""
def create_current(n):
current_array = sorted([value for value in str(n) if value != '0'])
return (current_array, ''.join(current_array))
global _happy_set
global _unhappy_set
current_run = set()
current_array, \
current_string = create_current(n)
if current_string in _happy_set:
return True
if current_string in _unhappy_set:
return False
while True:
current_run.add(current_string)
current_array, \
current_string = create_current(sum([int(value) ** 2
for value in current_string]))
if current_string in current_run | _unhappy_set:
_unhappy_set |= current_run
return False
if current_string in _happy_set:
_happy_set |= current_run
return True
Besides it need some documentation: is it a good implementation? Or
are there things I should do differently?
To decide for the values from 1 to 1E8 if it is happy or not, takes
280 seconds. Not to bad I think. Also not very good.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll 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