Hi,
Below is my attempt at the palindrome_products exercise in the Python track.
All the test on my PC works perfectly fine, however, I get the following error on Exercism, when I run the tests:
Your tests timed out. This might mean that there was an issue in our infrastructure, but more likely it suggests that your code is running slowly. Is there an infinite loop or something similar?
Please check your code, and if nothing seems to be wrong, try running the tests again.
While running the tests locally on my PC, I realize that Tests 7 and Test 8 take really long to return values.
Is there a way to optimize my code? Using Numpy?
Test 7: smallest(min_factor=1000, max_factor=9999)
Test 8: largest(min_factor=1000, max_factor=9999)
NB: The issue is only when min_factor and max_factor are four (4) digit values, as shown above - three (3) digit values return values instantly.
def largest(min_factor, max_factor):
"""Given a range of numbers, find the largest palindromes which
are products of two numbers within that range.
:param min_factor: int with a default value of 0
:param max_factor: int
:return: tuple of (palindrome, iterable).
Iterable should contain both factors of the palindrome in an arbitrary order.
"""
factors_list = get_factors(min_factor, max_factor)
values = get_values(factors_list)
largest_value = max(values)
factor = [factor[:len(factor) - 1] for factor in factors_list if factor[-1] == largest_value]
return factors_list
def smallest(min_factor, max_factor):
"""Given a range of numbers, find the smallest palindromes which
are products of two numbers within that range.
:param min_factor: int with a default value of 0
:param max_factor: int
:return: tuple of (palindrome, iterable).
Iterable should contain both factors of the palindrome in an arbitrary order.
"""
factors_list = get_factors(min_factor, max_factor)
values = get_values(factors_list)
smallest_value = min(values)
factor = [factor[:len(factor) - 1] for factor in factors_list if factor[-1] == smallest_value]
return smallest_value, factor
def get_product_list(min_factor, max_factor):
product_set = set()
for num in range(min_factor, max_factor + 1):
for elem in range(min_factor, max_factor + 1):
product_set.add(num*elem)
return sorted(list(product_set))
def get_palindromes(nums_list):
palindromes = []
for num in nums_list:
list_to_reverse = [elem for elem in str(num)]
reversed_list = list(reversed(list_to_reverse))
if num == int(''.join(reversed_list)):
palindromes.append(num)
return palindromes
def get_factors(min_factor, max_factor):
divisible_nums = []
for palindrome in get_palindromes(get_product_list(min_factor, max_factor)):
factors_value = []
if palindrome == 1:
factors_value.append([1, 1])
else:
for num in range(min_factor + 1, max_factor + 1):
if palindrome % num == 0:
factors_value.append([num, palindrome//num])
factors_value.append(palindrome)
divisible_nums.append(factors_value)
return divisible_nums
def get_values(factors_list):
values = [item_list[-1] for item_list in factors_list]
return values