Created
March 22, 2021 05:20
-
-
Save neeyatl/20d74e46821cb3485c4f901417000a8d to your computer and use it in GitHub Desktop.
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
""" | |
This week's question: | |
Given a number n, find the sum of all n-digit palindromes. | |
Example: | |
> nPalindromes(2) | |
> 495 // 11 + 22 + 33 + 44 + 55 + 66 + 77 + 88 + 99 | |
""" | |
def nSum(nDigits): | |
numbers = [int(x) for x in getNums(nDigits)] | |
y = 0 | |
for x in numbers: | |
y += x | |
return y | |
def getNums(nDigits): | |
lst = [] | |
for i in range(1, 10): | |
string = str(i) | |
for n in range(1, nDigits): | |
string += string | |
lst.append(string) | |
return lst | |
nDigits = int(input("Enter the number of digits: ")) | |
print("nPalindrome is: " + str(nSum(nDigits))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment