Skip to content

Instantly share code, notes, and snippets.

@leighlars
Last active October 29, 2020 03:30
Show Gist options
  • Save leighlars/e7153146e6bb63eba19c1b8c367ae1b6 to your computer and use it in GitHub Desktop.
Save leighlars/e7153146e6bb63eba19c1b8c367ae1b6 to your computer and use it in GitHub Desktop.
Tech Challenge Gist
The number 47 has an interesting characteristic.
If you take the number, plus its reverse (47 => 74) and then add them together to a number (47+74=121) the resulting sum is a palindrome
Starting at 0, find the first 25 numbers that have this same characteristic as 47, but limit it to where the palindrome is greater than 1000.
Collect the results in an array. Be sure that you're collecting the interesting numbers like 47, not the palindromic sums.
Bonus points if you can do this without converting numbers to strings/arrays.
Rewrite the question in your own words:
Find an array of 25 positive integers that, when reversed and summed, produce a 4-digit palindrome number.
What assumptions will you make about this problem if you cannot ask any more clarifying questions? What are your reasons for making those assumptions?
Each integer must be at least a 3-digit number. Each number must be a whole #
What are your initial thoughts about this problem? (high level design, 2-3 sentences)
How would you identify the elements of this problem?
Math & Sorting of Data
Which data structure(s) do you think you'll use? What pros/cons do you see with that choice?
I'm going to use an array
Write out a few lines of initial pseudocode: (mid-level design, NOT REAL CODE)
I must first find the lowest 3-digit number that when it's reversed is summed, is greater than 1000 and each digit must have a
Declare an empty array.
Use a for Loop and stop the loop at 25 (array's length)
convert the index value to a string, split and reverse it and join it back to another and assign to variable
then sum the index value and reversed integer
then reverse the sum by splitting into an array splitting and joining again as we did the reversedInteger
if index value is equal to the reverseInteger and the sum of originalInteger and reverseInteger is greater than 1000, push the result to the empty array
return the result
Implementation Code
const findPalindromeSum = () => {
const result = []
for (var i = 0; i < result.length; i++) {
let reversedInteger = Number(i.toString().split('').reverse().join(''))
let sum = i + reversedInteger
let reverseSum = parseInt(sum.toString().split('').reverse().join(''))
}
if (sum > 1000 && reversedInteger === sum ) {
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment