Skip to main content
added 18 characters in body
Source Link
GZ0
  • 2.4k
  • 8
  • 19

First of all, the question asks for the least number which the proportion is exactly 99%. Your code finds the first number that the proportion is more or equal to 99%. That is not quite right. Also, comparing floating-numbers is inaccurate and you should change it to integer comparision for exactness: bou * 100 == total * 99

Secondly, in the bouncy function, list(str(N)) is repeated three times unnecessarily. The twice sort calls are also unnecessary. It can be improved as follows

def bouncy(Nn):
    number = [*str(Nn)]
    n1sorted_n = sorted(number)
    return n1sorted_n != number and n1[sorted_n[::-1] != number

Thirdly, the overall algorithm is a naive one. If you need a better performance, a better approach is needed, e.g. by exploiting the fact that if \$n\$ is a bouncy number, then \$10n+b\$ for any \$b\in[0,9]\$ is also a bouncy number and does not need to be checked again.

First of all, the question asks for the least number which the proportion is exactly 99%. Your code finds the first number that the proportion is more or equal to 99%. That is not quite right. Also, comparing floating-numbers is inaccurate and you should change it to integer comparision for exactness: bou * 100 == total * 99

Secondly, in the bouncy function, list(str(N)) is repeated three times unnecessarily. The twice sort calls are also unnecessary. It can be improved as follows

def bouncy(N):
    number = [*str(N)]
    n1 = sorted(number)
    return n1 != number and n1[::-1] != number

Thirdly, the overall algorithm is a naive one. If you need a better performance, a better approach is needed, e.g. by exploiting the fact that if \$n\$ is a bouncy number, then \$10n+b\$ for any \$b\in[0,9]\$ is also a bouncy number and does not need to be checked again.

First of all, the question asks for the least number which the proportion is exactly 99%. Your code finds the first number that the proportion is more or equal to 99%. That is not quite right. Also, comparing floating-numbers is inaccurate and you should change it to integer comparision for exactness: bou * 100 == total * 99

Secondly, in the bouncy function, list(str(N)) is repeated three times unnecessarily. The twice sort calls are also unnecessary. It can be improved as follows

def bouncy(n):
    number = [*str(n)]
    sorted_n = sorted(number)
    return sorted_n != number and sorted_n[::-1] != number

Thirdly, the overall algorithm is a naive one. If you need a better performance, a better approach is needed, e.g. by exploiting the fact that if \$n\$ is a bouncy number, then \$10n+b\$ for any \$b\in[0,9]\$ is also a bouncy number and does not need to be checked again.

Source Link
GZ0
  • 2.4k
  • 8
  • 19

First of all, the question asks for the least number which the proportion is exactly 99%. Your code finds the first number that the proportion is more or equal to 99%. That is not quite right. Also, comparing floating-numbers is inaccurate and you should change it to integer comparision for exactness: bou * 100 == total * 99

Secondly, in the bouncy function, list(str(N)) is repeated three times unnecessarily. The twice sort calls are also unnecessary. It can be improved as follows

def bouncy(N):
    number = [*str(N)]
    n1 = sorted(number)
    return n1 != number and n1[::-1] != number

Thirdly, the overall algorithm is a naive one. If you need a better performance, a better approach is needed, e.g. by exploiting the fact that if \$n\$ is a bouncy number, then \$10n+b\$ for any \$b\in[0,9]\$ is also a bouncy number and does not need to be checked again.