#Python Palindrome Products

25 messages · Page 1 of 1 (latest)

thick agate
#

def largest(min_factor, max_factor):
    if max_factor < min_factor:
        raise ValueError("min must be <= max")
    factors = []
    result = -1
    for i in range(min_factor, max_factor + 1):
        for j in range(i, max_factor + 1):
            if i*j > result:
                if isPalindrome(i*j):
                    result = i*j
                    factors.clear()
                    factors.append(i)
                    factors.append(j)
    if result == -1:
        return None
    return result, factors


def smallest(min_factor, max_factor):
    if max_factor < min_factor:
        raise ValueError("min must be <= max")
    factors = []
    result = sys.maxsize
    for i in range(min_factor, max_factor + 1):
        for j in range(i, max_factor + 1):
            if i*j < result:
                if isPalindrome(i*j):
                    result = i*j
                    factors.clear()
                    factors.append(i)
                    factors.append(j)
    if result == sys.maxsize:
        return None
    return result, factors

def isPalindrome(number):
    string = str(number)
    if string == string[::-1]:
        return True
    return False
#

The only tests that pass are the ones where the TypeError gets thrown

#

P.S. This same error is thrown for all the other test cases

thick agate
#

UPDATE : I changed a bit how I return the values and now I also pass the tests where no palindrome exists

lament bough
#

What does the picture show/say? I can see it (that it is there) but not what it shows. Is it text that could benefit from everyone that is interested in being able to read it?

#

It seems too large to only say what you posted it says.

vast tree
thick agate
#

fewer tests failing

thick agate
vast tree
#

Since you modified your solution, can you post this new version?

lament bough
thick agate
# vast tree Since you modified your solution, can you post this new version?

def largest(min_factor, max_factor):
    if max_factor < min_factor:
        raise ValueError("min must be <= max")
    factors = []
    result = -1
    for i in range(min_factor, max_factor + 1):
        for j in range(i, max_factor + 1):
            if i*j > result:
                if isPalindrome(i*j):
                    result = i*j
                    factors = [i, j]
    if result == -1:
        return None, []
    return result, factors


def smallest(min_factor, max_factor):
    if max_factor < min_factor:
        raise ValueError("min must be <= max")
    factors = []
    result = sys.maxsize
    for i in range(min_factor, max_factor + 1):
        for j in range(i, max_factor + 1):
            if i*j < result:
                if isPalindrome(i*j):
                    result = i*j
                    factors = [i, j]
    if result == sys.maxsize:
        return None, []
    return result, factors

def isPalindrome(number):
    string = str(number)
    if string == string[::-1]:
        return True
    return False

#

this is the new solution, minimal changes to what is returned

lament bough
#

Any error messages?

thick agate
#

my guess is my issue is in the format of what I'm returning, since it runs just fine on my machine

#

        

          

        value, factors = smallest(min_factor=1, max_factor=9)

        

          

        self.assertEqual(value, 1)

        

          

        self.assertFactorsEqual(factors, [[1, 1]])```
#

and here is an example of a test case, to see what is expected from me. I feel like what I am returning and what they want me to return is the same

vast tree
#

This is one of the tests:

    def test_find_the_largest_palindrome_from_single_digit_factors(self):
        value, factors = largest(min_factor=1, max_factor=9)
        self.assertEqual(value, 9)
        self.assertFactorsEqual(factors, [[1, 9], [3, 3]])

The largest possible palindrome product from two factors in the range [1, 9] is 9.
There are two ways to get that product: 1 * 9 and 3 * 3.
Therefore you have to include them both in the factors

thick agate
#

YEA!!

#

I just noticed that too

#

lemme see if it improves it

#

it passed all tests!🥳

#

thanks for the support 🙂

lament bough
#

Thank you for the support. Providing information that we can read (and copy and paste as text) is definitely helpful!