Created
December 28, 2018 14:42
-
-
Save priyankvex/e402601c43e9e3b5b24d815605f09eec to your computer and use it in GitHub Desktop.
Scamming the coding interview : Problem 013 : Find the nth perfect number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Scamming the coding interview. | |
Subscribe to our newsletter at https://scammingthecodinginterview.com/ | |
to get a coding or design problem daily in your inbox and become exceptionally good at | |
coding interviews. | |
""" | |
def find_nth_perfect_number(n): | |
sum_of_digits = 0 | |
for char in str(n): | |
sum_of_digits += int(char) | |
return (10 * n) + (10 - sum_of_digits) | |
if __name__ == '__main__': | |
assert find_nth_perfect_number(1) == 19 | |
assert find_nth_perfect_number(2) == 28 | |
assert find_nth_perfect_number(9) == 91 | |
assert find_nth_perfect_number(19) == 190 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment