diff --git a/python/dailyprogrammer/#390 - Number of 1s.py b/python/dailyprogrammer/#390 - Number of 1s.py new file mode 100644 index 0000000..5d34954 --- /dev/null +++ b/python/dailyprogrammer/#390 - Number of 1s.py @@ -0,0 +1,16 @@ +def count1(n): + x = str(n) + count = 0 + for index, digit in enumerate(x[::-1]): + digit = int(digit) + if digit != 0: + if digit == 1: + numberAfter = x[len(x) - index:] or '0' + count += int(numberAfter) + 1 + else: + count += 10 ** index + count += int(10 ** (index - 1) * index * digit) + return count + + +print(count1(3**35)) diff --git a/python/dailyprogrammer/readme.md b/python/dailyprogrammer/readme.md index 7a7d6ed..02c8ee9 100644 --- a/python/dailyprogrammer/readme.md +++ b/python/dailyprogrammer/readme.md @@ -5,6 +5,9 @@ [The Subreddit](https://reddit.com/r/dailyprogrammer) +- #390 - Number of 1s + - [Solution](%23390%20-%20Number%20of%201s.py) + - [Reddit Post](https://www.reddit.com/r/dailyprogrammer/comments/neg49j/20210517_challenge_390_difficult_number_of_1s/) - #391 - ABACABA - [Solution](%23391%20-%20ABACABA.py) - [Reddit Post](https://www.reddit.com/r/dailyprogrammer/comments/njxq95/20210524_challenge_391_easy_the_abacaba_sequence/)