Finished
1def largest(min_factor, max_factor):
1def largest(*, max_factor, min_factor=0):
22 """Given a range of numbers, find the largest palindromes which
33 are products of two numbers within that range.
44
77 :return: tuple of (palindrome, iterable).
88 Iterable should contain both factors of the palindrome in an arbitrary order.
99 """
10 # Edge Case: min_factor greater than max_factor
11 if min_factor > max_factor:
12 raise ValueError("min must be <= max")
13
14 # Edge Case: negative factors
15 if max_factor < 0:
16 return (None, [])
17
18 largest_palindrome = None
19 factors_list = []
20
21 # Edge Case: single digit ranges
22 for i in range(max_factor, min_factor - 1, -1):
23 for j in range(i, min_factor - 1, -1): # Start from i to avoid duplicates
24 product = i * j
25
26 # If we already found a larger palindrome, we can break early
27 if largest_palindrome is not None and product < largest_palindrome:
28 break
29
30 # Check if product is palindrome
31 if str(product) == str(product)[::-1]:
32 if largest_palindrome is None or product > largest_palindrome:
33 largest_palindrome = product
34 factors_list = [(j, i)]
35 elif product == largest_palindrome:
36 factors_list.append((j, i))
37
38 # Edge Case: no palindromes exist in range
39 if largest_palindrome is None:
40 return (None, [])
41
42 return (largest_palindrome, factors_list)
43
44 # Handled Edge Cases: min_factor greater than max_factor, negative factors, single digit ranges, no palindromes exist in range
1045
11 pass
1246
13
14def smallest(min_factor, max_factor):
47def smallest(*, max_factor, min_factor=0):
1548 """Given a range of numbers, find the smallest palindromes which
1649 are products of two numbers within that range.
1750
2053 :return: tuple of (palindrome, iterable).
2154 Iterable should contain both factors of the palindrome in an arbitrary order.
2255 """
23
24 pass
56 # Edge Case: min_factor greater than max_factor
57 if min_factor > max_factor:
58 raise ValueError("min must be <= max")
59
60 # Edge Case: negative factors
61 if max_factor < 0:
62 return (None, [])
63
64 smallest_palindrome = None
65 factors_list = []
66
67 # Edge Case: single digit ranges
68 for i in range(min_factor, max_factor + 1):
69 for j in range(i, max_factor + 1): # Start from i to avoid duplicates
70 product = i * j
71
72 # If we already found a smaller palindrome and current product is larger, we can break
73 if smallest_palindrome is not None and product > smallest_palindrome:
74 break
75
76 # Check if product is palindrome
77 if str(product) == str(product)[::-1]:
78 if smallest_palindrome is None or product < smallest_palindrome:
79 smallest_palindrome = product
80 factors_list = [(i, j)]
81 elif product == smallest_palindrome:
82 factors_list.append((i, j))
83
84 # Edge Case: no palindromes exist in range
85 if smallest_palindrome is None:
86 return (None, [])
87
88 return (smallest_palindrome, factors_list)
89
90 # Handled Edge Cases: min_factor greater than max_factor, negative factors, single digit ranges, no palindromes exist in range
Test NameStatus
test_empty_result_for_largest_if_no_palindrome_in_the_range
Pass
test_empty_result_for_smallest_if_no_palindrome_in_the_range
Pass
test_error_result_for_largest_if_min_is_more_than_max
Pass
test_error_result_for_smallest_if_min_is_more_than_max
Pass
test_find_the_largest_palindrome_from_double_digit_factors
Pass
test_find_the_largest_palindrome_from_four_digit_factors
Pass
test_find_the_largest_palindrome_from_single_digit_factors
Pass
test_find_the_largest_palindrome_from_triple_digit_factors
Pass
test_find_the_smallest_palindrome_from_double_digit_factors
Pass
test_find_the_smallest_palindrome_from_four_digit_factors
Pass
test_find_the_smallest_palindrome_from_single_digit_factors
Pass
test_find_the_smallest_palindrome_from_triple_digit_factors
Pass
test_smallest_product_does_not_use_the_smallest_factor
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.