Project Euler - Largest Palindrome #4 Python
<p>I'm new to python and decided it would be a good idea to improve my coding (in general) by doing some of the challenges on project Euler. I'm currently stuck on problem 4 and i'm not sure what is going wrong (for those not in the know, problem 4 is as follows):</p>

<blockquote>
  <p>A palindromic number reads the same both ways.</p>
  
  <p>The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.</p>
</blockquote>

<p>Find the largest palindrome made from the product of two 3-digit numbers.</p>

<pre><code>x, y = 999, 999
palindrome = []
while True:
    palindrome = [i for i in str(x * y)]
    r_palindrome = palindrome[::-1]
    if palindrome == r_palindrome:
        break
    else:
        y -= 1
        if y &lt; 100: 
            y = x
            x -= 1
print x, y, palindrome
</code></pre>

<p>I seem to get the answer <code>987 * 286 = 282282</code> which feels awfully low. 
Can someone explain the best way of doing this and what my current code is doing wrong rather than just a simple "here is the code" answer please. </p>

<p>Starting at <code>x = 999</code> and <code>y = 999</code> and then decrementing only <code>y</code> until you restart with a decremented <code>x</code> and a reset <code>y</code> does not guarantee you that you will hit the largest palindrome first.</p>

<p>Just think of the following example: Letâs imagine a different requirement for the product (to not spoil the palindrome result here). Imagine you starting with <code>x = 999</code> and then you reach to <code>y = 101</code> until you hit the first âvalidâ result.</p>

<p>In your case, you would accept <code>999 * 101 = 100899</code> as the largest result. But there may actually be a different solution <code>998 * 998 = 996004</code> which you never looked at but is obviously much larger.</p>

<p>So you need to change how you decide when to stop looking and know that you reached the largest number.</p>

<hr>

<p>Btw. as a general hint for Project Euler: Especially the first problems can be easily solved with brute force (i.e. trying out every possible solution). While this will probably not give you a satisfying feeling that you solved the problem in a smart way, it does give you an idea on how to get there. You can always work out a better solution then, but keep in mind that Project Euler has actually <em>a lot</em> problems which are completely impossible using brute force anyway, so you have enough to worry about later ;)</p>

<p>For this particular problem, you could write a list comprehension that would give you all palindromes and then get the maximum of it. Thatâs a one-liner; itâs very inefficient but for this particularly small input domain, itâs fast enough to still give you an answer instantly:</p>

<pre><code>max([(x * y, x, y) for x in range(100, 1000) for y in range(100, 1000) if str(x * y) == str(x * y)[::-1]])
</code></pre>

python