Skip to content

Instantly share code, notes, and snippets.

@neeyatl
Created March 22, 2021 05:20
Show Gist options
  • Save neeyatl/20d74e46821cb3485c4f901417000a8d to your computer and use it in GitHub Desktop.
Save neeyatl/20d74e46821cb3485c4f901417000a8d to your computer and use it in GitHub Desktop.
"""
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